Skip to content

Commit b4b0137

Browse files
authored
chore(ci): convert create-pr steps into composite action (#2238)
* chore: convert create-pr steps into composite action Signed-off-by: heitorlessa <[email protected]> * chore(ci): changelog to use new create-pr action Signed-off-by: heitorlessa <[email protected]> * chore(ci): revert changelog to trigger on push Signed-off-by: heitorlessa <[email protected]> * chore: document custom action Signed-off-by: heitorlessa <[email protected]> * chore: add support for any target branch Signed-off-by: heitorlessa <[email protected]> --------- Signed-off-by: heitorlessa <[email protected]>
1 parent 0a4cf7a commit b4b0137

File tree

5 files changed

+246
-147
lines changed

5 files changed

+246
-147
lines changed

.github/actions/create-pr/action.yml

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
name: "Create PR custom action"
2+
description: "Create a PR and a temporary branch, close duplicates"
3+
4+
# PROCESS
5+
#
6+
# 1. Setup git client using Powertools bot username
7+
# 2. Pushes staged files to a temporary branch
8+
# 3. Creates a PR from temporary branch against a target branch (typically trunk: develop, main, etc.)
9+
# 4. Searches for duplicate PRs with the same title
10+
# 5. If duplicates are found, link to the most recent one, close and delete their branches so we keep a single PR
11+
# 6. In the event of failure, we delete the now orphaned branch (if any), and propagate the failure
12+
13+
# USAGE
14+
#
15+
# - name: Create PR
16+
# id: create-pr
17+
# uses: ./.github/actions/create-pr
18+
# with:
19+
# files: "CHANGELOG.md"
20+
# temp_branch_prefix: "ci-changelog"
21+
# pull_request_title: "chore(ci): changelog rebuild"
22+
# github_token: ${{ secrets.GITHUB_TOKEN }}
23+
# - name: Step to demonstrate how to access outputs (no need for this)
24+
# run: |
25+
# echo "PR number: ${PR_ID}"
26+
# echo "Branch: ${BRANCH}"
27+
# env:
28+
# PR_ID: ${{ steps.create-pr.outputs.pull_request_id}}
29+
# BRANCH: ${{ steps.create-pr.outputs.temp_branch}}
30+
31+
inputs:
32+
files:
33+
description: "Files to add separated by space"
34+
required: true
35+
temp_branch_prefix:
36+
description: "Prefix for temporary git branch to be created, e.g, ci-docs"
37+
required: true
38+
pull_request_title:
39+
description: "Pull Request title to use"
40+
required: true
41+
github_token:
42+
description: "GitHub token for GitHub CLI"
43+
required: true
44+
target_branch:
45+
description: "Branch to target when creating a PR against (develop, by default)"
46+
required: false
47+
default: develop
48+
49+
outputs:
50+
pull_request_id:
51+
description: "Pull request ID created"
52+
value: ${{ steps.create-pr.outputs.pull_request_id }}
53+
temp_branch:
54+
description: "Temporary branch created with staged changed"
55+
value: ${{ steps.create-pr.outputs.temp_branch }}
56+
57+
runs:
58+
using: "composite"
59+
steps:
60+
- id: adjust-path
61+
run: echo "${{ github.action_path }}" >> $GITHUB_PATH
62+
shell: bash
63+
- id: setup-git
64+
name: Git client setup and refresh tip
65+
run: |
66+
git config user.name "Powertools bot"
67+
git config user.email "[email protected]"
68+
git config pull.rebase true
69+
git config remote.origin.url >&-
70+
shell: bash
71+
- id: create-pr
72+
working-directory: ${{ env.GITHUB_WORKSPACE }}
73+
run: create_pr_for_staged_changes.sh "${FILES}"
74+
env:
75+
FILES: ${{ inputs.files }}
76+
TEMP_BRANCH_PREFIX: ${{ inputs.temp_branch_prefix }}
77+
PR_TITLE: ${{ inputs.pull_request_title }}
78+
BASE_BRANCH: ${{ inputs.target_branch }}
79+
GH_TOKEN: ${{ inputs.github_token }}
80+
shell: bash
81+
- id: cleanup
82+
name: Cleanup orphaned branch
83+
if: failure()
84+
run: git push origin --delete "${TEMP_BRANCH_PREFIX}-${GITHUB_RUN_ID}" || echo "Must have failed before creating temporary branch; no cleanup needed."
85+
env:
86+
TEMP_BRANCH_PREFIX: ${{ inputs.temp_branch_prefix }}
87+
GITHUB_RUN_ID: ${{ github.run_id }}
88+
shell: bash
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
#!/bin/bash
2+
set -uo pipefail # prevent accessing unset env vars, prevent masking pipeline errors to the next command
3+
4+
#docs
5+
#title :create_pr_for_staged_changes.sh
6+
#description :This script will create a PR for staged changes, detect and close duplicate PRs.
7+
#author :@heitorlessa
8+
#date :May 8th 2023
9+
#version :0.1
10+
#usage :bash create_pr_for_staged_changes.sh {git_staged_files_or_directories_separated_by_space}
11+
#notes :Meant to use in GitHub Actions only. Temporary branch will be named $TEMP_BRANCH_PREFIX-$GITHUB_RUN_ID
12+
#os_version :Ubuntu 22.04.2 LTS
13+
#required_env_vars :PR_TITLE, TEMP_BRANCH_PREFIX, GH_TOKEN
14+
#==============================================================================
15+
16+
# Sets GitHub Action with error message to ease troubleshooting
17+
function error() {
18+
echo "::error file=${FILENAME}::$1"
19+
exit 1
20+
}
21+
22+
function debug() {
23+
TIMESTAMP=$(date -u "+%FT%TZ") # 2023-05-10T07:53:59Z
24+
echo ""${TIMESTAMP}" - $1"
25+
}
26+
27+
function notice() {
28+
echo "::notice file=${FILENAME}::$1"
29+
}
30+
31+
function start_span() {
32+
echo "::group::$1"
33+
}
34+
35+
function end_span() {
36+
echo "::endgroup::"
37+
}
38+
39+
function has_required_config() {
40+
start_span "Validating required config"
41+
test -z "${TEMP_BRANCH_PREFIX}" && error "TEMP_BRANCH_PREFIX env must be set to create a PR"
42+
test -z "${PR_TITLE}" && error "PR_TITLE env must be set"
43+
test -z "${GH_TOKEN}" && error "GH_TOKEN env must be set for GitHub CLI"
44+
45+
# Default GitHub Actions Env Vars: https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables
46+
debug "Are we running in GitHub Action environment?"
47+
test -z "${GITHUB_RUN_ID}" && error "GITHUB_RUN_ID env must be set to trace Workflow Run ID back to PR"
48+
test -z "${GITHUB_SERVER_URL}" && error "GITHUB_SERVER_URL env must be set to trace Workflow Run ID back to PR"
49+
test -z "${GITHUB_REPOSITORY}" && error "GITHUB_REPOSITORY env must be set to trace Workflow Run ID back to PR"
50+
51+
debug "Config validated successfully!"
52+
set_environment_variables
53+
end_span
54+
}
55+
56+
function set_environment_variables() {
57+
start_span "Setting environment variables"
58+
export readonly WORKFLOW_URL="${GITHUB_SERVER_URL}"/"${GITHUB_REPOSITORY}"/actions/runs/"${GITHUB_RUN_ID}" # e.g., heitorlessa/aws-lambda-powertools-test/actions/runs/4913570678
59+
export readonly TEMP_BRANCH="${TEMP_BRANCH_PREFIX}"-"${GITHUB_RUN_ID}" # e.g., ci-changelog-4894658712
60+
export readonly BASE_BRANCH="${BASE_BRANCH:-develop}" # e.g., main, defaults to develop if missing
61+
export readonly PR_BODY="This is an automated PR created from the following workflow"
62+
export readonly FILENAME=".github/scripts/$(basename "$0")"
63+
export readonly NO_DUPLICATES_MESSAGE="No duplicated PRs found"
64+
end_span
65+
}
66+
67+
function has_anything_changed() {
68+
start_span "Validating git staged files"
69+
HAS_ANY_SOURCE_CODE_CHANGED="$(git status --porcelain)"
70+
71+
test -z "${HAS_ANY_SOURCE_CODE_CHANGED}" && debug "Nothing to update; exitting early" && exit 0
72+
end_span
73+
}
74+
75+
function create_temporary_branch_with_changes() {
76+
start_span "Creating temporary branch: "${TEMP_BRANCH}""
77+
git checkout -b "${TEMP_BRANCH}"
78+
79+
debug "Committing staged files: $*"
80+
echo "$@" | xargs -n1 git add || error "Failed to add staged changes: "$@""
81+
git commit -m "${PR_TITLE}"
82+
83+
git push origin "${TEMP_BRANCH}"
84+
end_span
85+
}
86+
87+
function create_pr() {
88+
start_span "Creating PR against ${TEMP_BRANCH} branch"
89+
NEW_PR_URL=$(gh pr create --title "${PR_TITLE}" --body "${PR_BODY}: ${WORKFLOW_URL}" --base "${BASE_BRANCH}" || error "Failed to create PR") # e.g, https://github.com/awslabs/aws-lambda-powertools/pull/13
90+
91+
# greedy remove any string until the last URL path, including the last '/'. https://opensource.com/article/17/6/bash-parameter-expansion
92+
debug "Extracing PR Number from PR URL: "${NEW_PR_URL}""
93+
NEW_PR_ID="${NEW_PR_URL##*/}" # 13
94+
export NEW_PR_URL
95+
export NEW_PR_ID
96+
end_span
97+
}
98+
99+
function close_duplicate_prs() {
100+
start_span "Searching for duplicate PRs"
101+
DUPLICATE_PRS=$(gh pr list --search "${PR_TITLE}" --json number --jq ".[] | select(.number != ${NEW_PR_ID}) | .number") # e.g, 13\n14
102+
103+
if [ -z "${DUPLICATE_PRS}" ]; then
104+
debug "No duplicate PRs found"
105+
DUPLICATE_PRS="${NO_DUPLICATES_MESSAGE}"
106+
else
107+
debug "Closing duplicated PRs: "${DUPLICATE_PRS}""
108+
echo "${DUPLICATE_PRS}" | xargs -L1 gh pr close --delete-branch --comment "Superseded by #${NEW_PR_ID}"
109+
fi
110+
111+
export readonly DUPLICATE_PRS
112+
end_span
113+
}
114+
115+
function report_job_output() {
116+
start_span "Updating job outputs"
117+
echo pull_request_id="${NEW_PR_ID}" >>"$GITHUB_OUTPUT"
118+
echo temp_branch="${TEMP_BRANCH}" >>"$GITHUB_OUTPUT"
119+
end_span
120+
}
121+
122+
function report_summary() {
123+
start_span "Creating job summary"
124+
echo "### Pull request created successfully :rocket: ${NEW_PR_URL} <br/><br/> Closed duplicated PRs: ${DUPLICATE_PRS}" >>"$GITHUB_STEP_SUMMARY"
125+
126+
notice "PR_URL is: ${NEW_PR_URL}"
127+
notice "PR_BRANCH is: ${TEMP_BRANCH}"
128+
notice "PR_DUPLICATES are: ${DUPLICATE_PRS}"
129+
end_span
130+
}
131+
132+
function main() {
133+
# Sanity check
134+
has_anything_changed
135+
has_required_config
136+
137+
create_temporary_branch_with_changes "$@"
138+
create_pr
139+
close_duplicate_prs
140+
141+
report_job_output
142+
report_summary
143+
}
144+
145+
main "$@"

.github/scripts/create_pr_for_staged_changes.sh

Lines changed: 0 additions & 116 deletions
This file was deleted.

.github/workflows/build_changelog.yml

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,9 @@ name: Build changelog
33

44
on:
55
workflow_dispatch:
6-
schedule:
7-
# ┌───────────── minute (0 - 59)
8-
# │ ┌───────────── hour (0 - 23)
9-
# │ │ ┌───────────── day of the month (1 - 31)
10-
# │ │ │ ┌───────────── month (1 - 12 or JAN-DEC)
11-
# │ │ │ │ ┌───────────── day of the week (0 - 6 or SUN-SAT)
12-
# │ │ │ │ │
13-
# │ │ │ │ │
14-
# │ │ │ │ │
15-
# * * * * *
16-
- cron: '0 8 * * *'
6+
push:
7+
branches:
8+
- develop
179

1810
jobs:
1911
changelog:

0 commit comments

Comments
 (0)