Skip to content

Commit 12baefb

Browse files
aduh95ruyadorno
authored andcommitted
tools: update create-release-proposal workflow
PR-URL: #56054 Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
1 parent d2007ae commit 12baefb

File tree

2 files changed

+118
-24
lines changed

2 files changed

+118
-24
lines changed

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

Lines changed: 14 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,32 @@ 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 "github-bot@iojs.org"
7874
git config --local user.name "Node.js GitHub Bot"
7975
8076
- name: Start git node release prepare
77+
# The curl command is to make sure we run the version of the script corresponding to the current workflow.
8178
run: |
79+
git update-index --assume-unchanged tools/actions/create-release.sh
80+
curl -fsSLo tools/actions/create-release.sh https://github.com/${GITHUB_REPOSITORY}/raw/${GITHUB_SHA}/tools/actions/create-release.sh
8281
./tools/actions/create-release.sh "${RELEASE_DATE}" "${RELEASE_LINE}"
8382
env:
84-
GH_TOKEN: ${{ secrets.GH_USER_TOKEN }}
83+
GH_TOKEN: ${{ github.token }}
84+
# We want the bot to push the push the release commit so CI runs on it.
85+
BOT_TOKEN: ${{ secrets.GH_USER_TOKEN }}

tools/actions/create-release.sh

Lines changed: 104 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
set -xe
44

5+
GITHUB_REPOSITORY=${GITHUB_REPOSITORY:-nodejs/node}
6+
BOT_TOKEN=${BOT_TOKEN:-}
7+
58
RELEASE_DATE=$1
69
RELEASE_LINE=$2
710

@@ -10,24 +13,114 @@ if [ -z "$RELEASE_DATE" ] || [ -z "$RELEASE_LINE" ]; then
1013
exit 1
1114
fi
1215

16+
if [ -z "$GITHUB_REPOSITORY" ] || [ -z "$BOT_TOKEN" ]; then
17+
echo "Invalid value in env for GITHUB_REPOSITORY and BOT_TOKEN"
18+
exit 1
19+
fi
20+
21+
if ! command -v node || ! command -v gh || ! command -v git || ! command -v awk; then
22+
echo "Missing required dependencies"
23+
exit 1
24+
fi
25+
1326
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
27+
28+
HEAD_BRANCH="$(git rev-parse --abbrev-ref HEAD)"
29+
HEAD_SHA="$(git rev-parse HEAD^)"
1830

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

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

24-
PR_URL="$(gh pr create --title "$TITLE" --body "$TEMP_BODY" --base "v$RELEASE_LINE.x")"
36+
# Create the proposal branch
37+
gh api \
38+
--method POST \
39+
-H "Accept: application/vnd.github+json" \
40+
-H "X-GitHub-Api-Version: 2022-11-28" \
41+
"/repos/${GITHUB_REPOSITORY}/git/refs" \
42+
-f "ref=refs/heads/$HEAD_BRANCH" -f "sha=$HEAD_SHA"
43+
44+
# Create the proposal PR
45+
PR_URL="$(gh api \
46+
--method POST \
47+
--jq .html_url \
48+
-H "Accept: application/vnd.github+json" \
49+
-H "X-GitHub-Api-Version: 2022-11-28" \
50+
"/repos/${GITHUB_REPOSITORY}/pulls" \
51+
-f "title=$TITLE" -f "body=$TEMP_BODY" -f "head=$HEAD_BRANCH" -f "base=v$RELEASE_LINE.x")"
2552

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|")"
53+
# Push the release commit to the proposal branch using `BOT_TOKEN` from the env
54+
node --input-type=module - \
55+
"$GITHUB_REPOSITORY" \
56+
"$HEAD_BRANCH" \
57+
"$HEAD_SHA" \
58+
"$(git log -1 HEAD --format=%s || true)" \
59+
"$(git log -1 HEAD --format=%b | awk -v PR_URL="$PR_URL" '{sub(/^PR-URL: TODO$/, "PR-URL: " PR_URL)} 1' || true)" \
60+
"$(git show HEAD --diff-filter=d --name-only --format= || true)" \
61+
"$(git show HEAD --diff-filter=D --name-only --format= || true)" \
62+
<<'EOF'
63+
const [,,
64+
repo,
65+
branch,
66+
parentCommitSha,
67+
commit_title,
68+
commit_body,
69+
modifiedOrAddedFiles,
70+
deletedFiles,
71+
] = process.argv;
2872
29-
# Replace "TODO" with the PR URL in the last commit
30-
git commit --amend --no-edit -m "$AMENDED_COMMIT_MSG" || true
73+
import { readFileSync } from 'node:fs';
74+
import util from 'node:util';
3175
32-
# Force-push the amended commit
33-
git push --force
76+
const query = `
77+
mutation ($repo: String! $branch: String!, $parentCommitSha: GitObjectID!, $changes: FileChanges!, $commit_title: String!, $commit_body: String) {
78+
createCommitOnBranch(input: {
79+
branch: {
80+
repositoryNameWithOwner: $repo,
81+
branchName: $branch
82+
},
83+
message: {
84+
headline: $commit_title,
85+
body: $commit_body
86+
},
87+
expectedHeadOid: $parentCommitSha,
88+
fileChanges: $changes
89+
}) {
90+
commit {
91+
url
92+
}
93+
}
94+
}
95+
`;
96+
const response = await fetch('https://api.github.com/graphql', {
97+
method: 'POST',
98+
headers: {
99+
'Authorization': `bearer ${process.env.BOT_TOKEN}`,
100+
},
101+
body: JSON.stringify({
102+
query,
103+
variables: {
104+
repo,
105+
branch,
106+
parentCommitSha,
107+
commit_title,
108+
commit_body,
109+
changes: {
110+
additions: modifiedOrAddedFiles.split('\n').filter(Boolean)
111+
.map(path => ({ path, contents: readFileSync(path).toString('base64') })),
112+
deletions: deletedFiles.split('\n').filter(Boolean),
113+
}
114+
},
115+
})
116+
});
117+
if (!response.ok) {
118+
console.log({statusCode: response.status, statusText: response.statusText});
119+
process.exitCode ||= 1;
120+
}
121+
const data = await response.json();
122+
if (data.errors?.length) {
123+
throw new Error('Endpoint returned an error', { cause: data });
124+
}
125+
console.log(util.inspect(data, { depth: Infinity }));
126+
EOF

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy