Skip to content

Commit 54b66d9

Browse files
committed
tools: update create-release-proposal workflow
1 parent 4e44322 commit 54b66d9

File tree

2 files changed

+93
-25
lines changed

2 files changed

+93
-25
lines changed

.github/workflows/create-release-proposal.yml

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# This action requires the following secrets to be set on the repository:
22
# GH_USER_NAME: GitHub user whose Jenkins and GitHub token are defined below
33
# GH_USER_TOKEN: GitHub user token, to be used by ncu and to push changes
4-
# JENKINS_TOKEN: Jenkins token, to be used to check CI status
54

65
name: Create Release Proposal
76

@@ -24,6 +23,7 @@ env:
2423

2524
permissions:
2625
contents: write
26+
pull-requests: write
2727

2828
jobs:
2929
releasePrepare:
@@ -37,9 +37,7 @@ jobs:
3737
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
3838
with:
3939
ref: ${{ env.STAGING_BRANCH }}
40-
# Needs the whole git history for ncu to work
41-
# See https://github.com/nodejs/node-core-utils/pull/486
42-
fetch-depth: 0
40+
persist-credentials: false
4341

4442
# Install dependencies
4543
- name: Install Node.js
@@ -56,29 +54,36 @@ jobs:
5654
ncu-config set upstream origin
5755
ncu-config set username "$USERNAME"
5856
ncu-config set token "$GH_TOKEN"
59-
ncu-config set jenkins_token "$JENKINS_TOKEN"
6057
ncu-config set repo "$(echo "$GITHUB_REPOSITORY" | cut -d/ -f2)"
6158
ncu-config set owner "${GITHUB_REPOSITORY_OWNER}"
6259
env:
6360
USERNAME: ${{ secrets.JENKINS_USER }}
64-
GH_TOKEN: ${{ secrets.GH_USER_TOKEN }}
65-
JENKINS_TOKEN: ${{ secrets.JENKINS_TOKEN }}
61+
GH_TOKEN: ${{ github.token }}
6662

6763
- name: Set up ghauth config (Ubuntu)
6864
run: |
69-
mkdir -p ~/.config/changelog-maker/
70-
echo '{
71-
"user": "'$(ncu-config get username)'",
72-
"token": "'$(ncu-config get token)'"
73-
}' > ~/.config/changelog-maker/config.json
65+
mkdir -p "${XDG_CONFIG_HOME:-~/.config}/changelog-maker"
66+
echo '{}' | jq '{user: env.USERNAME, token: env.TOKEN}' > "${XDG_CONFIG_HOME:-~/.config}/changelog-maker/config.json"
67+
env:
68+
USERNAME: ${{ secrets.JENKINS_USER }}
69+
TOKEN: ${{ github.token }}
7470

7571
- name: Setup git author
7672
run: |
7773
git config --local user.email "[email protected]"
7874
git config --local user.name "Node.js GitHub Bot"
7975
76+
# Workaround, can be removed after https://github.com/nodejs/node-core-utils/pull/876 is released.
77+
- name: Deepen the shallow clone
78+
run: |
79+
git fetch "${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}.git" --shallow-exclude "v${RELEASE_LINE}.0.0" "$STAGING_BRANCH"
80+
8081
- name: Start git node release prepare
82+
# The curl command is to make sure we run the version of the script corresponding to the current workflow.
8183
run: |
84+
curl -L https://github.com/${GITHUB_REPOSITORY}/raw/${GITHUB_SHA}/tools/actions/create-release.sh > tools/actions/create-release.sh
8285
./tools/actions/create-release.sh "${RELEASE_DATE}" "${RELEASE_LINE}"
8386
env:
84-
GH_TOKEN: ${{ secrets.GH_USER_TOKEN }}
87+
GH_TOKEN: ${{ github.token }}
88+
# We want the bot to push the push the release commit so Ci runs on it.
89+
BOT_TOKEN: ${{ secrets.GH_USER_TOKEN }}

tools/actions/create-release.sh

Lines changed: 75 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,87 @@ if [ -z "$RELEASE_DATE" ] || [ -z "$RELEASE_LINE" ]; then
1010
exit 1
1111
fi
1212

13+
createCommitAPICall() {
14+
commit="${1:-HEAD}"
15+
cat - <<'EOF'
16+
mutation ($repo: String! $branch: String!, $parent: GitObjectID!, $commit_title: String!, $commit_body: String) {
17+
createCommitOnBranch(input: {
18+
branch: {
19+
repositoryNameWithOwner: $repo,
20+
branchName: $branch
21+
},
22+
message: {
23+
headline: $commit_title,
24+
body: $commit_body
25+
},
26+
expectedHeadOid: $parent,
27+
fileChanges: {
28+
additions: [
29+
EOF
30+
git show "$commit" --diff-filter=d --name-only --format= | while read -r FILE; do
31+
printf " { path: "
32+
node -p 'JSON.stringify(process.argv[1])' "$FILE"
33+
printf " , contents: \""
34+
base64 -w 0 -i "$FILE"
35+
echo "\"},"
36+
done
37+
echo ' ], deletions: ['
38+
git show "$commit" --diff-filter=D --name-only --format= | while read -r FILE; do
39+
echo " $(node -p 'JSON.stringify(process.argv[1])' "$FILE"),"
40+
done
41+
cat - <<'EOF'
42+
]
43+
}
44+
}) {
45+
commit {
46+
url
47+
}
48+
}
49+
}
50+
EOF
51+
}
52+
1353
git node release --prepare --skipBranchDiff --yes --releaseDate "$RELEASE_DATE"
14-
# We use it to not specify the branch name as it changes based on
15-
# the commit list (semver-minor/semver-patch)
16-
git config push.default current
17-
git push
54+
55+
HEAD_BRANCH="$(git rev-parse --abbrev-ref HEAD)"
56+
HEAD_SHA="$(git rev-parse HEAD^)"
1857

1958
TITLE=$(awk "/^## ${RELEASE_DATE}/ { print substr(\$0, 4) }" "doc/changelogs/CHANGELOG_V${RELEASE_LINE}.md")
2059

2160
# Use a temporary file for the PR body
2261
TEMP_BODY="$(awk "/## ${RELEASE_DATE}/,/^<a id=/{ if (!/^<a id=/) print }" "doc/changelogs/CHANGELOG_V${RELEASE_LINE}.md")"
2362

24-
PR_URL="$(gh pr create --title "$TITLE" --body "$TEMP_BODY" --base "v$RELEASE_LINE.x")"
25-
26-
# Amend commit message so it contains the correct PR-URL trailer.
27-
AMENDED_COMMIT_MSG="$(git log -1 --pretty=%B | sed "s|PR-URL: TODO|PR-URL: $PR_URL|")"
63+
# Create the proposal branch
64+
gh api \
65+
--method POST \
66+
-H "Accept: application/vnd.github+json" \
67+
-H "X-GitHub-Api-Version: 2022-11-28" \
68+
"/repos/${GITHUB_REPOSITORY}/git/refs" \
69+
-f "ref=refs/heads/$HEAD_BRANCH" -f "sha=$HEAD_SHA"
2870

29-
# Replace "TODO" with the PR URL in the last commit
30-
git commit --amend --no-edit -m "$AMENDED_COMMIT_MSG" || true
71+
# Create the proposal PR
72+
PR_URL="$(gh api \
73+
--method POST \
74+
--jq .html_url \
75+
-H "Accept: application/vnd.github+json" \
76+
-H "X-GitHub-Api-Version: 2022-11-28" \
77+
"/repos/${GITHUB_REPOSITORY}/pulls" \
78+
-f "title=$TITLE" -f "body=$TEMP_BODY" -f "head=$HEAD_BRANCH" -f "base=v$RELEASE_LINE.x")"
3179

32-
# Force-push the amended commit
33-
git push --force
80+
# Push the release commit to the proposal branch
81+
createCommitAPICall | node --input-type=module -e 'console.log(JSON.stringify({
82+
query: Buffer.concat(await process.stdin.toArray()).toString(),
83+
variables: {
84+
repo: process.argv[1],
85+
branch: process.argv[2],
86+
parent: process.argv[3],
87+
commit_title: process.argv[4],
88+
commit_body: process.argv[5]
89+
}
90+
}))' \
91+
"$GITHUB_REPOSITORY" \
92+
"$HEAD_BRANCH" \
93+
"$HEAD_SHA" \
94+
"$(git log -1 HEAD --format=%s)" \
95+
"$(git log -1 HEAD --format=%b | sed "s|PR-URL: TODO|PR-URL: $PR_URL|")" \
96+
| curl -fS -H "Authorization: bearer ${BOT_TOKEN}" -X POST --data @- https://api.github.com/graphql

0 commit comments

Comments
 (0)