Skip to content

Commit

Permalink
tools: update create-release-proposal workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 committed Nov 27, 2024
1 parent 853b304 commit 3bf2272
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 22 deletions.
16 changes: 6 additions & 10 deletions .github/workflows/create-release-proposal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ env:
NODE_VERSION: lts/*

permissions:
contents: write
contents: read

jobs:
releasePrepare:
Expand All @@ -39,9 +39,6 @@ jobs:
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with:
ref: ${{ env.STAGING_BRANCH }}
# Needs the whole git history for ncu to work
# See https://github.com/nodejs/node-core-utils/pull/486
fetch-depth: 0

# Install dependencies
- name: Install Node.js
Expand All @@ -63,16 +60,15 @@ jobs:
ncu-config set owner "${GITHUB_REPOSITORY_OWNER}"
env:
USERNAME: ${{ secrets.JENKINS_USER }}
GH_TOKEN: ${{ secrets.GH_USER_TOKEN }}
GH_TOKEN: ${{ github.token }}
JENKINS_TOKEN: ${{ secrets.JENKINS_TOKEN }}

- name: Set up ghauth config (Ubuntu)
run: |
mkdir -p ~/.config/changelog-maker/
echo '{
"user": "'$(ncu-config get username)'",
"token": "'$(ncu-config get token)'"
}' > ~/.config/changelog-maker/config.json
jq '{user: env.USERNAME, token: env.TOKEN}' > "${XDG_CONFIG_HOME:-~/.config}/changelog-maker/config.json"
env:
USERNAME: ${{ secrets.JENKINS_USER }}
TOKEN: ${{ github.token }}

- name: Setup git author
run: |
Expand Down
76 changes: 64 additions & 12 deletions tools/actions/create-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,76 @@ if [ -z "$RELEASE_DATE" ] || [ -z "$RELEASE_LINE" ]; then
exit 1
fi

buildMutationQuery() {
cat - <<'EOF'
mutation ($repo: String! $branch: String!, $parent: GitObjectID!, $commit_title: String!, $commit_body: String) {
createCommitOnBranch(input: {
branch: {
repositoryNameWithOwner: $repo,
branchName: $branch
},
message: {
headline: $commit_title,
body: $commit_body
},
expectedHeadOid: $parent,
fileChanges: {
additions: [
EOF
git show HEAD --diff-filter=d --name-only --format= | while read -r FILE; do
echo "{ path: $(
node -p 'JSON.stringify(process.argv[1])' "$FILE"
), contents: $(
node -p 'JSON.stringify(fs.readFileSync(process.argv[1]).toString("base64"))' "$FILE"
) },"
done
echo '], deletions: ['
git show HEAD --diff-filter=D --name-only --format= | while read -r FILE; do
echo "$(node -p 'JSON.stringify(process.argv[1])' "$FILE"),"
done
cat - <<'EOF'
]
}}) {
commit {
url
}
}
}
EOF
}


git node release --prepare --skipBranchDiff --yes --releaseDate "$RELEASE_DATE"
# We use it to not specify the branch name as it changes based on
# the commit list (semver-minor/semver-patch)
git config push.default current
git push

HEAD="$(git rev-parse --abbrev-ref HEAD)"

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

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

PR_URL="$(gh pr create --title "$TITLE" --body "$TEMP_BODY" --base "v$RELEASE_LINE.x")"

# Amend commit message so it contains the correct PR-URL trailer.
AMENDED_COMMIT_MSG="$(git log -1 --pretty=%B | sed "s|PR-URL: TODO|PR-URL: $PR_URL|")"
# Create the proposal branch
gh api \
--method POST \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"/repos/${GITHUB_REPOSITORY}/git/refs" \
-f "ref=refs/heads/$HEAD" -f "sha=$(git rev-parse HEAD^)"

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

# Force-push the amended commit
git push --force
# Push the release commit to the proposal branch
gh api graphql \
-F repo="$GITHUB_REPOSITORY" \
-F branch="$HEAD" \
-F parent="$(git rev-parse HEAD^)" \
-F commit_title="$(git log -1 HEAD --format=%s)" \
-F commit_body="$(git log -1 HEAD --format=%b | sed "s|PR-URL: TODO|PR-URL: $PR_URL|")" \
-f query="$(buildMutationQuery)"

0 comments on commit 3bf2272

Please sign in to comment.