Skip to content

Commit a802565

Browse files
authored
Merge branch 'awslabs:develop' into develop
2 parents 3602445 + b3679c3 commit a802565

File tree

75 files changed

+1781
-855
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+1781
-855
lines changed

.chglog/CHANGELOG.tpl.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
## {{ .Title }}
1111

1212
{{ range .Commits -}}
13+
{{ if and (not (hasPrefix .Subject "changelog rebuild")) (not (hasPrefix .Subject "layer docs update")) (not (hasPrefix .Subject "bump version to")) -}}
1314
* {{ if .Scope }}**{{ .Scope }}:** {{ end }}{{ .Subject }}
15+
{{ end -}}
1416
{{ end }}
1517
{{ end -}}
1618
{{ end -}}
@@ -24,7 +26,9 @@
2426
## {{ .Title }}
2527

2628
{{ range .Commits -}}
29+
{{ if and (not (hasPrefix .Subject "changelog rebuild")) (not (hasPrefix .Subject "layer docs update")) (not (hasPrefix .Subject "bump version to")) -}}
2730
* {{ if .Scope }}**{{ .Scope }}:** {{ end }}{{ .Subject }}
31+
{{ end -}}
2832
{{ end }}
2933
{{ end -}}
3034

.github/ISSUE_TEMPLATE/bug_report.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ body:
5858
attributes:
5959
label: AWS Lambda function runtime
6060
options:
61-
- 3.7
62-
- 3.8
63-
- 3.9
64-
- 3.10
61+
- "3.7"
62+
- "3.8"
63+
- "3.9"
64+
- "3.10"
6565
validations:
6666
required: true
6767
- type: dropdown

.github/ISSUE_TEMPLATE/static_typing.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ body:
2525
attributes:
2626
label: AWS Lambda function runtime
2727
options:
28-
- 3.7
29-
- 3.8
30-
- 3.9
31-
- 3.10
28+
- "3.7"
29+
- "3.8"
30+
- "3.9"
31+
- "3.10"
3232
validations:
3333
required: true
3434
- type: input

.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: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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. All PRs will be omitted from Release Notes and Changelogs
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+
export readonly SKIP_LABEL="skip-changelog"
65+
66+
end_span
67+
}
68+
69+
function has_anything_changed() {
70+
start_span "Validating git staged files"
71+
HAS_ANY_SOURCE_CODE_CHANGED="$(git status --porcelain)"
72+
73+
test -z "${HAS_ANY_SOURCE_CODE_CHANGED}" && debug "Nothing to update; exitting early" && exit 0
74+
end_span
75+
}
76+
77+
function create_temporary_branch_with_changes() {
78+
start_span "Creating temporary branch: "${TEMP_BRANCH}""
79+
git checkout -b "${TEMP_BRANCH}"
80+
81+
debug "Committing staged files: $*"
82+
echo "$@" | xargs -n1 git add || error "Failed to add staged changes: "$@""
83+
git commit -m "${PR_TITLE}"
84+
85+
git push origin "${TEMP_BRANCH}" || error "Failed to create new temporary branch"
86+
end_span
87+
}
88+
89+
function create_pr() {
90+
start_span "Creating PR against ${TEMP_BRANCH} branch"
91+
# TODO: create label
92+
NEW_PR_URL=$(gh pr create --title "${PR_TITLE}" --body "${PR_BODY}: ${WORKFLOW_URL}" --base "${BASE_BRANCH}" --label "${SKIP_LABEL}" || error "Failed to create PR") # e.g, https://github.com/awslabs/aws-lambda-powertools/pull/13
93+
94+
# greedy remove any string until the last URL path, including the last '/'. https://opensource.com/article/17/6/bash-parameter-expansion
95+
debug "Extracing PR Number from PR URL: "${NEW_PR_URL}""
96+
NEW_PR_ID="${NEW_PR_URL##*/}" # 13
97+
export NEW_PR_URL
98+
export NEW_PR_ID
99+
end_span
100+
}
101+
102+
function close_duplicate_prs() {
103+
start_span "Searching for duplicate PRs"
104+
DUPLICATE_PRS=$(gh pr list --search "${PR_TITLE}" --json number --jq ".[] | select(.number != ${NEW_PR_ID}) | .number") # e.g, 13\n14
105+
106+
if [ -z "${DUPLICATE_PRS}" ]; then
107+
debug "No duplicate PRs found"
108+
DUPLICATE_PRS="${NO_DUPLICATES_MESSAGE}"
109+
else
110+
debug "Closing duplicated PRs: "${DUPLICATE_PRS}""
111+
echo "${DUPLICATE_PRS}" | xargs -L1 gh pr close --delete-branch --comment "Superseded by #${NEW_PR_ID}"
112+
fi
113+
114+
export readonly DUPLICATE_PRS
115+
end_span
116+
}
117+
118+
function report_job_output() {
119+
start_span "Updating job outputs"
120+
echo pull_request_id="${NEW_PR_ID}" >>"$GITHUB_OUTPUT"
121+
echo temp_branch="${TEMP_BRANCH}" >>"$GITHUB_OUTPUT"
122+
end_span
123+
}
124+
125+
function report_summary() {
126+
start_span "Creating job summary"
127+
echo "### Pull request created successfully :rocket: ${NEW_PR_URL} <br/><br/> Closed duplicated PRs: ${DUPLICATE_PRS}" >>"$GITHUB_STEP_SUMMARY"
128+
129+
notice "PR_URL is: ${NEW_PR_URL}"
130+
notice "PR_BRANCH is: ${TEMP_BRANCH}"
131+
notice "PR_DUPLICATES are: ${DUPLICATE_PRS}"
132+
end_span
133+
}
134+
135+
function main() {
136+
# Sanity check
137+
has_anything_changed
138+
has_required_config
139+
140+
create_temporary_branch_with_changes "$@"
141+
create_pr
142+
close_duplicate_prs
143+
144+
report_job_output
145+
report_summary
146+
}
147+
148+
main "$@"

0 commit comments

Comments
 (0)