-
Notifications
You must be signed in to change notification settings - Fork 2
/
action.yml
160 lines (129 loc) · 5.96 KB
/
action.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
name: 'action-package-version-bump'
description: 'Github action to upgrade the package.json version and create a changelog based on PR description. Only works with pull_request and pull_request_target events'
inputs:
user_name:
description: 'Name of the git user to push the changes'
required: true
user_email:
description: 'Email of the git user to push the changes'
required: true
ref_branch:
description: 'Branch to use as a base reference for the version bump. If not specified, it will use the pull_request one when available'
required: false
version_args:
description: 'npm version command arguments. '
required: false
default: 'patch'
node_version:
description: 'node version used for the npm commands'
required: false
default: '20'
access_token:
description: 'Personal access token to clone the repository. Default: github.token'
required: false
skip_push:
description: 'Skip pushing committed changes to the repository. Default: false'
required: true
type: boolean
default: false
skip_changelog:
description: 'Skip changelog update'
required: false
type: boolean
default: false
action:
description: 'Action to perform: "bump" or "restore"'
required: true
default: 'bump'
use_rebase:
description: 'Use rebase strategy for pull'
required: false
default: false
runs:
using: 'composite'
steps:
- name: Define target_branch
shell: bash
run: |
ref_branch_input=${{ inputs.ref_branch }}
target_branch=$ref_branch_input || $(gh pr view ${{ github.event.pull_request.number }} --json baseRefName -q '.baseRefName')
echo "TARGET_BRANCH=$target_branch" >> $GITHUB_ENV
- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ inputs.node_version }}
- uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.ref }}
repository: ${{ github.event.pull_request.head.repo.full_name }}
fetch-depth: 0
token: ${{ inputs.access_token || github.token }}
- name: Restore files
shell: bash
run: |
git config remote.main-repo.url >&- || git remote add main-repo https://github.com/${{ github.event.pull_request.head.repo.full_name }}
git config --global user.name ${{ inputs.user_name }}
git config --global user.email ${{ inputs.user_email }}
git config --global pull.rebase ${{ inputs.use_rebase }}
# Use the TARGET_BRANCH environment variable
target_branch=${TARGET_BRANCH}
# Fetch the target branch
git fetch main-repo $target_branch:$target_branch
# Get the last common commit of the current PR branch and the target branch
common_commit=$(git merge-base HEAD $target_branch)
# Get the version of the package from the common commit
target_version=$(git show $common_commit:package.json | jq -r '.version')
# Restore the version of the package in the current branch
jq ".version = \"$target_version\"" package.json > temp.json && mv temp.json package.json
# Replace the CHANGELOG.md content with the content of the CHANGELOG.md from the common commit
git show $common_commit:CHANGELOG.md > CHANGELOG.md
npm install --package-lock-only --ignore-scripts
echo "TARGET_VERSION=$target_version" >> $GITHUB_ENV
- name: Create Restore Commit
env:
GH_TOKEN: ${{ inputs.access_token || github.token }}
shell: bash
run: |
# We always need to restore to make sure we can pull after without conflicts on the changed files
git add -A
git diff-index --quiet HEAD || git commit -m "chore: restore package version to ${TARGET_VERSION} and restored changelog"
- name: Bump version
shell: bash
if: inputs.action == 'bump'
run: |
# Use the TARGET_BRANCH environment variable
target_branch=${TARGET_BRANCH}
# We need to pull to avoid conflicts
git pull main-repo $target_branch
# Get the version of the package from the ref_branch
target_version=$(git show main-repo/$target_branch:package.json | jq -r '.version')
# Use the latest version of the package.json from the ref_branch
jq ".version = \"$target_version\"" package.json > temp.json && mv temp.json package.json
echo "Updating version"
npm version --no-git-tag-version ${{ inputs.version_args }}
- name: Update changelog
shell: bash
if: inputs.action == 'bump' && inputs.skip_changelog != 'true'
run: |
# Use the TARGET_BRANCH environment variable
target_branch=${TARGET_BRANCH}
PACKAGE_VERSION=`cat package.json | jq -r '.version'`
echo "Package version: $PACKAGE_VERSION. Getting previous changelog"
PREV_CHANGELOG=`git show main-repo/$target_branch:CHANGELOG.md || true`
echo "Composing new changelog"
echo -e "Rev: $PACKAGE_VERSION\n=============\n ${{ github.event.pull_request.body }}\n\n\n" > CHANGELOG.md
echo -e "$PREV_CHANGELOG" >> CHANGELOG.md
echo "PACKAGE_VERSION=$PACKAGE_VERSION" >> $GITHUB_ENV
- name: Create Bump Commit
env:
GH_TOKEN: ${{ inputs.access_token || github.token }}
if: inputs.action == 'bump'
shell: bash
run: |
git add -A
git commit -m "chore: bump package version to ${PACKAGE_VERSION} and update changelog"
- name: Push
if: ${{ inputs.skip_push != 'true' }}
shell: bash
run: |
git push --force https://${{ inputs.access_token || github.token }}@github.com/${{ github.event.pull_request.head.repo.full_name }}