-
Notifications
You must be signed in to change notification settings - Fork 0
118 lines (109 loc) · 3.85 KB
/
synchronise-pr-version-npm.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
name: Synchronise PR Version
on:
workflow_call:
inputs:
pr-number:
required: true
type: number
trunk-branch:
required: true
type: string
default: main
secrets:
bot-id:
required: true
bot-key:
required: true
permissions:
contents: write
pull-requests: write
concurrency:
group: ${{ github.workflow }}-${{ inputs.pr-number }}
cancel-in-progress: true
jobs:
synchronise-version:
runs-on: ubuntu-latest
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ inputs.pr-number }}
TRUNK_BRANCH: ${{ inputs.trunk-branch }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ inputs.trunk-branch }}
- name: Retrieve PR details
id: pr-details
run: |
VERSION_LABEL=$(gh pr view $PR_NUMBER --json labels --jq '.labels | map(select(.name == "v:major" or .name == "v:minor" or .name == "v:patch")) | sort_by(.name) | .[0].name')
PR_BRANCH=$(gh pr view $PR_NUMBER --json headRefName --jq .headRefName)
echo "Pull request branch: $PR_BRANCH"
echo "Pull Request Version label: $VERSION_LABEL"
echo "pr_branch=$PR_BRANCH" >> $GITHUB_OUTPUT
echo "version_label=$VERSION_LABEL" >> $GITHUB_OUTPUT
- name: Retrieve ${{ inputs.trunk-branch }} branch version
id: trunk-details
run: |
TRUNK_VERSION=$(jq -r .version package.json)
echo "Version on `$TRUNK_BRANCH`: $TRUNK_VERSION"
echo "trunk_version=$TRUNK_VERSION" >> $GITHUB_OUTPUT
- name: Checkout PR
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ steps.pr-details.outputs.pr_branch }}
- name: Update version
id: update
if: ${{ steps.pr-details.outputs.version_label }}
env:
TRUNK_VERSION: ${{ steps.trunk-details.outputs.trunk_version }}
VERSION_LABEL: ${{ steps.pr-details.outputs.version_label }}
run: |
get_next_version() {
local base="$1"
local increment_type="$2"
local RE='[^0-9]*\([0-9]*\)[.]\([0-9]*\)[.]\([0-9]*\)\([0-9A-Za-z-]*\)'
local MAJOR=$(echo $base | sed -e "s#$RE#\1#")
local MINOR=$(echo $base | sed -e "s#$RE#\2#")
local PATCH=$(echo $base | sed -e "s#$RE#\3#")
case "$increment_type" in
major)
((MAJOR += 1))
((MINOR = 0))
((PATCH = 0))
;;
minor)
((MINOR += 1))
((PATCH = 0))
;;
patch)
((PATCH += 1))
;;
esac
local NEXT_VERSION="$MAJOR.$MINOR.$PATCH"
echo "$NEXT_VERSION"
}
next_version=$(get_next_version $TRUNK_VERSION ${VERSION_LABEL:2})
current_version=$(jq -r .version package.json)
if [ "$next_version" != "$current_version" ]; then
npm version $next_version --allow-same-version=true --git-tag-version=false
echo "new_version=$(jq -r .version package.json)" >> $GITHUB_OUTPUT
else
echo "Version does not need updating"
fi
- name: Generate a token
id: generate-token
uses: actions/create-github-app-token@v1
with:
app-id: ${{ secrets.bot-id }}
private-key: ${{ secrets.bot-key }}
- uses: planetscale/ghcommit-action@v0.2.0
with:
commit_message: "Updating version to ${{ steps.update.outputs.new_version }}"
repo: ${{ github.repository }}
branch: ${{ steps.pr-details.outputs.pr_branch }}
env:
GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }}
- name: Remove stale
run: |
gh pr edit $PR_NUMBER --remove-label v:stale || true