-
-
Notifications
You must be signed in to change notification settings - Fork 34
235 lines (190 loc) · 8.42 KB
/
release-pr.yml
File metadata and controls
235 lines (190 loc) · 8.42 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
name: Release PR
on:
workflow_dispatch:
jobs:
release-pr:
# Prevent this action from running on forks.
if: github.repository == 'stylelint/vscode-stylelint'
runs-on: ubuntu-latest
permissions:
actions: write
contents: write
pull-requests: write
timeout-minutes: 10
steps:
- name: Checkout
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
fetch-depth: 0
- name: Set up Node.js
uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # v6.3.0
with:
node-version: 'lts/*'
cache: npm
- name: Install dependencies
run: npm ci
- name: Save previous versions
id: prev
run: |
echo "extension-version=$(jq -r '.version' package.json)" >> "${GITHUB_OUTPUT}"
echo "server-version=$(jq -r '.version' packages/language-server/package.json)" >> "${GITHUB_OUTPUT}"
- name: Version packages
env:
GITHUB_TOKEN: ${{ github.token }}
run: npm run changeset
- name: Check for changes
id: changes
run: |
EXT_CHANGED=false
SERVER_CHANGED=false
if ! git diff --quiet HEAD -- package.json CHANGELOG.md; then
EXT_CHANGED=true
fi
if ! git diff --quiet HEAD -- packages/language-server/package.json packages/language-server/CHANGELOG.md; then
SERVER_CHANGED=true
fi
if [[ "${EXT_CHANGED}" == "false" && "${SERVER_CHANGED}" == "false" ]]; then
echo "::error::No packages to release. Add changesets first."
exit 1
fi
echo "extension-changed=${EXT_CHANGED}" >> "${GITHUB_OUTPUT}"
echo "server-changed=${SERVER_CHANGED}" >> "${GITHUB_OUTPUT}"
if [[ "${EXT_CHANGED}" == "true" ]]; then
echo "extension-version=$(jq -r '.version' package.json)" >> "${GITHUB_OUTPUT}"
fi
if [[ "${SERVER_CHANGED}" == "true" ]]; then
echo "server-version=$(jq -r '.version' packages/language-server/package.json)" >> "${GITHUB_OUTPUT}"
fi
- name: Build release title
id: title
run: |
PARTS=()
if [[ "${{ steps.changes.outputs.extension-changed }}" == "true" ]]; then
PARTS+=("vscode-stylelint ${{ steps.changes.outputs.extension-version }}")
fi
if [[ "${{ steps.changes.outputs.server-changed }}" == "true" ]]; then
PARTS+=("@stylelint/language-server ${{ steps.changes.outputs.server-version }}")
fi
TITLE=$(IFS=', '; echo "${PARTS[*]}")
echo "title=${TITLE}" >> "${GITHUB_OUTPUT}"
- name: Create release commit
id: commit
env:
GH_TOKEN: ${{ github.token }}
run: |
BRANCH="release/next"
REPO="${{ github.repository }}"
HEAD_SHA=$(git rev-parse HEAD)
# Ensure the release branch exists and points to HEAD.
if gh api "repos/${REPO}/git/refs/heads/${BRANCH}" --silent 2>/dev/null; then
gh api "repos/${REPO}/git/refs/heads/${BRANCH}" \
--method PATCH --field sha="${HEAD_SHA}" --field force=true --silent
else
gh api "repos/${REPO}/git/refs" \
--method POST --field ref="refs/heads/${BRANCH}" --field sha="${HEAD_SHA}" --silent
fi
# Build file additions and deletions from the working tree.
ADDITIONS_FILE=$(mktemp)
DELETIONS_FILE=$(mktemp)
echo '[]' > "$ADDITIONS_FILE"
echo '[]' > "$DELETIONS_FILE"
while IFS= read -r file; do
if [ -f "${file}" ]; then
CONTENT_FILE=$(mktemp)
base64 -w 0 "${file}" > "$CONTENT_FILE"
jq --arg path "${file}" --rawfile contents "$CONTENT_FILE" \
'. + [{path: $path, contents: $contents}]' "$ADDITIONS_FILE" > "${ADDITIONS_FILE}.tmp"
mv "${ADDITIONS_FILE}.tmp" "$ADDITIONS_FILE"
rm "$CONTENT_FILE"
else
jq --arg path "${file}" \
'. + [{path: $path}]' "$DELETIONS_FILE" > "${DELETIONS_FILE}.tmp"
mv "${DELETIONS_FILE}.tmp" "$DELETIONS_FILE"
fi
done < <(git diff --name-only HEAD)
# Create a signed commit on the release branch via the GraphQL API.
jq -n \
--arg repo "${REPO}" \
--arg branch "${BRANCH}" \
--arg oid "${HEAD_SHA}" \
--arg headline "Release: ${{ steps.title.outputs.title }}" \
--slurpfile additions "$ADDITIONS_FILE" \
--slurpfile deletions "$DELETIONS_FILE" \
'{
query: "mutation($input: CreateCommitOnBranchInput!) { createCommitOnBranch(input: $input) { commit { oid } } }",
variables: {
input: {
branch: { repositoryNameWithOwner: $repo, branchName: $branch },
expectedHeadOid: $oid,
message: { headline: $headline },
fileChanges: (
{}
| if ($additions[0] | length) > 0 then .additions = $additions[0] else . end
| if ($deletions[0] | length) > 0 then .deletions = $deletions[0] else . end
)
}
}
}' | gh api graphql --input -
rm "$ADDITIONS_FILE" "$DELETIONS_FILE"
echo "branch=${BRANCH}" >> "${GITHUB_OUTPUT}"
- name: Create or update release PR
env:
GH_TOKEN: ${{ github.token }}
run: |
BRANCH="${{ steps.commit.outputs.branch }}"
REPO="${{ github.repository }}"
RUN_ID="${{ github.run_id }}"
EXISTING_PR=$(gh pr list --head "${BRANCH}" --state open --json number --jq '.[0].number')
BODY=""
if [[ "${{ steps.changes.outputs.extension-changed }}" == "true" ]]; then
EXT_VERSION="${{ steps.changes.outputs.extension-version }}"
EXT_PREV="${{ steps.prev.outputs.extension-version }}"
BODY+="This PR prepares the release of **vscode-stylelint ${EXT_VERSION}**.
- Changes: <https://github.com/${REPO}/compare/${EXT_PREV}...${BRANCH}>
- Published URL: https://marketplace.visualstudio.com/items?itemName=stylelint.vscode-stylelint
"
fi
if [[ "${{ steps.changes.outputs.server-changed }}" == "true" ]]; then
SERVER_VERSION="${{ steps.changes.outputs.server-version }}"
SERVER_PREV="${{ steps.prev.outputs.server-version }}"
SERVER_TAG="@stylelint/language-server@${SERVER_PREV}"
if [ -n "${BODY}" ]; then
BODY+="
---
"
fi
BODY+="This PR prepares the release of **@stylelint/language-server ${SERVER_VERSION}**."
# Only include the compare link if the previous tag exists.
if git tag --list "${SERVER_TAG}" | grep -q .; then
BODY+="
- Changes: <https://github.com/${REPO}/compare/${SERVER_TAG}...${BRANCH}>"
fi
BODY+="
- Published URL: https://www.npmjs.com/package/@stylelint/language-server
"
fi
BODY+="
Checklist:
- [ ] Quickly review the changes on the URL above.
- [ ] The new versions are expected.
- [ ] The CHANGELOG.md files are as expected. If necessary, update them.
- [ ] Request a review from the owners team.
If the PR is approved and all checks have passed, you can merge it. Then, the release job will be automatically started.
Visit [**Actions**](https://github.com/${REPO}/actions) and watch the job.
*Generated by the [action run (id=${RUN_ID})](https://github.com/${REPO}/actions/runs/${RUN_ID})*"
if [ -n "${EXISTING_PR}" ]; then
gh pr edit "${EXISTING_PR}" \
--title "Release: ${{ steps.title.outputs.title }}" \
--body "${BODY}"
echo "Updated existing PR #${EXISTING_PR}."
else
gh pr create \
--title "Release: ${{ steps.title.outputs.title }}" \
--body "${BODY}" \
--base main \
--head "${BRANCH}"
fi
- name: Trigger tests on release branch
env:
GH_TOKEN: ${{ github.token }}
run: gh workflow run testing.yml --ref "${{ steps.commit.outputs.branch }}"