Skip to content

Commit defe5e7

Browse files
committed
chore: convert create-pr steps into composite action
Signed-off-by: heitorlessa <lessa@amazon.co.uk>
1 parent e826661 commit defe5e7

File tree

2 files changed

+201
-0
lines changed

2 files changed

+201
-0
lines changed

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
name: "Create PR custom action"
2+
description: "Create a PR and a temporary branch, close duplicates"
3+
4+
# This custom action
5+
6+
inputs:
7+
files:
8+
description: "Files to add"
9+
required: true
10+
temp_branch_prefix:
11+
description: "Prefix for temporary git branch to be created, e.g, ci-docs"
12+
required: true
13+
pull_request_title:
14+
description: "Pull Request title to use"
15+
required: true
16+
github_token:
17+
description: "GitHub token for GitHub CLI"
18+
required: true
19+
outputs:
20+
pull_request_id:
21+
description: "Pull request ID created"
22+
value: ${{ steps.create-pr.outputs.pull_request_id }}
23+
temp_branch:
24+
description: "Temporary branch created with staged changed"
25+
value: ${{ steps.create-pr.outputs.temp_branch }}
26+
runs:
27+
using: "composite"
28+
steps:
29+
- id: adjust-path
30+
run: echo "${{ github.action_path }}" >> $GITHUB_PATH
31+
shell: bash
32+
- id: setup-git
33+
name: Git client setup and refresh tip
34+
run: |
35+
git config user.name "Powertools bot"
36+
git config user.email "aws-lambda-powertools-feedback@amazon.com"
37+
git config pull.rebase true
38+
git config remote.origin.url >&-
39+
shell: bash
40+
- id: create-pr
41+
working-directory: ${{ env.GITHUB_WORKSPACE }}
42+
run: create_pr_for_staged_changes.sh "${FILES}"
43+
env:
44+
FILES: ${{ inputs.files }}
45+
TEMP_BRANCH_PREFIX: ${{ inputs.temp_branch_prefix }}
46+
GH_TOKEN: ${{ inputs.github_token }}
47+
PR_TITLE: ${{ inputs.pull_request_title }}
48+
shell: bash
49+
- id: cleanup
50+
name: Cleanup orphaned branch
51+
if: failure()
52+
run: git push origin --delete "${TEMP_BRANCH_PREFIX}-${GITHUB_RUN_ID}" || echo "Must have failed before creating temporary branch; no cleanup needed."
53+
env:
54+
TEMP_BRANCH_PREFIX: ${{ inputs.temp_branch_prefix }}
55+
GITHUB_RUN_ID: ${{ github.run_id }}
56+
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 "$@"

0 commit comments

Comments
 (0)