Skip to content

Commit 37a78df

Browse files
committed
tools: update create-release-proposal workflow
1 parent 853b304 commit 37a78df

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

@@ -26,6 +25,7 @@ env:
2625

2726
permissions:
2827
contents: write
28+
pull-requests: write
2929

3030
jobs:
3131
releasePrepare:
@@ -39,9 +39,7 @@ jobs:
3939
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
4040
with:
4141
ref: ${{ env.STAGING_BRANCH }}
42-
# Needs the whole git history for ncu to work
43-
# See https://github.com/nodejs/node-core-utils/pull/486
44-
fetch-depth: 0
42+
persist-credentials: false
4543

4644
# Install dependencies
4745
- name: Install Node.js
@@ -58,29 +56,36 @@ jobs:
5856
ncu-config set upstream origin
5957
ncu-config set username "$USERNAME"
6058
ncu-config set token "$GH_TOKEN"
61-
ncu-config set jenkins_token "$JENKINS_TOKEN"
6259
ncu-config set repo "$(echo "$GITHUB_REPOSITORY" | cut -d/ -f2)"
6360
ncu-config set owner "${GITHUB_REPOSITORY_OWNER}"
6461
env:
6562
USERNAME: ${{ secrets.JENKINS_USER }}
66-
GH_TOKEN: ${{ secrets.GH_USER_TOKEN }}
67-
JENKINS_TOKEN: ${{ secrets.JENKINS_TOKEN }}
63+
GH_TOKEN: ${{ github.token }}
6864

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

7773
- name: Setup git author
7874
run: |
7975
git config --local user.email "[email protected]"
8076
git config --local user.name "Node.js GitHub Bot"
8177
78+
# Workaround, can be removed after https://github.com/nodejs/node-core-utils/pull/876 is released.
79+
- name: Deepen the shallow clone
80+
run: |
81+
git fetch "${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}.git" --shallow-exclude "v${RELEASE_LINE}.0.0" "$STAGING_BRANCH"
82+
8283
- name: Start git node release prepare
84+
# The curl command is to make sure we run the version of the script corresponding to the current workflow.
8385
run: |
86+
curl -L https://github.com/${GITHUB_REPOSITORY}/raw/${GITHUB_SHA}/tools/actions/create-release.sh > tools/actions/create-release.sh
8487
./tools/actions/create-release.sh "${RELEASE_DATE}" "${RELEASE_LINE}"
8588
env:
86-
GH_TOKEN: ${{ secrets.GH_USER_TOKEN }}
89+
GH_TOKEN: ${{ github.token }}
90+
# We want the bot to push the push the release commit so Ci runs on it.
91+
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)