-
Notifications
You must be signed in to change notification settings - Fork 429
chore(ci): create pull request on changelog update #2224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
heitorlessa
merged 3 commits into
aws-powertools:develop
from
heitorlessa:chore/safer-changelog
May 8, 2023
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
#!/bin/bash | ||
set -uxo pipefail # enable debugging, prevent accessing unset env vars, prevent masking pipeline errors to the next command | ||
|
||
#docs | ||
#title :create_pr_for_staged_changes.sh | ||
#description :This script will create a PR for staged changes and detect and close duplicate PRs. | ||
#author :@heitorlessa | ||
#date :May 8th 2023 | ||
#version :0.1 | ||
#usage :bash create_pr_for_staged_changes.sh {git_staged_files_or_directories_separated_by_space} | ||
#notes :Meant to use in GitHub Actions only. Temporary branch will be named $TEMP_BRANCH_PREFIX-$GITHUB_RUN_ID | ||
#os_version :Ubuntu 22.04.2 LTS | ||
#required_env_vars :COMMIT_MSG, PR_TITLE, TEMP_BRANCH_PREFIX, GH_TOKEN, GITHUB_RUN_ID, GITHUB_SERVER_URL, GITHUB_REPOSITORY | ||
#============================================================================== | ||
|
||
PR_BODY="This is an automated PR created from the following workflow" | ||
FILENAME=".github/scripts/$(basename "$0")" | ||
readonly PR_BODY | ||
readonly FILENAME | ||
|
||
# Sets GitHub Action with error message to ease troubleshooting | ||
function raise_validation_error() { | ||
echo "::error file=${FILENAME}::$1" | ||
exit 1 | ||
} | ||
|
||
function debug() { | ||
echo "::debug::$1" | ||
} | ||
|
||
function notice() { | ||
echo "::notice file=${FILENAME}::$1" | ||
} | ||
|
||
function has_required_config() { | ||
# Default GitHub Actions Env Vars: https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables | ||
debug "Do we have required environment variables?" | ||
test -z "${TEMP_BRANCH_PREFIX}" && raise_validation_error "TEMP_BRANCH_PREFIX env must be set to create a PR" | ||
test -z "${GH_TOKEN}" && raise_validation_error "GH_TOKEN env must be set for GitHub CLI" | ||
test -z "${COMMIT_MSG}" && raise_validation_error "COMMIT_MSG env must be set" | ||
test -z "${PR_TITLE}" && raise_validation_error "PR_TITLE env must be set" | ||
test -z "${GITHUB_RUN_ID}" && raise_validation_error "GITHUB_RUN_ID env must be set to trace Workflow Run ID back to PR" | ||
test -z "${GITHUB_SERVER_URL}" && raise_validation_error "GITHUB_SERVER_URL env must be set to trace Workflow Run ID back to PR" | ||
test -z "${GITHUB_REPOSITORY}" && raise_validation_error "GITHUB_REPOSITORY env must be set to trace Workflow Run ID back to PR" | ||
|
||
set_environment_variables | ||
} | ||
|
||
function set_environment_variables() { | ||
WORKFLOW_URL="${GITHUB_SERVER_URL}"/"${GITHUB_REPOSITORY}"/actions/runs/"${GITHUB_RUN_ID}" # e.g., heitorlessa/aws-lambda-powertools-test/actions/runs/4913570678 | ||
TEMP_BRANCH="${TEMP_BRANCH_PREFIX}"-"${GITHUB_RUN_ID}" # e.g., ci-changelog-4894658712 | ||
|
||
export readonly WORKFLOW_URL | ||
export readonly TEMP_BRANCH | ||
} | ||
|
||
function has_anything_changed() { | ||
debug "Is there an update to the source code?" | ||
HAS_ANY_SOURCE_CODE_CHANGED="$(git status --porcelain)" | ||
|
||
test -z "${HAS_ANY_SOURCE_CODE_CHANGED}" && echo "Nothing to update" && exit 0 | ||
} | ||
|
||
function create_temporary_branch_with_changes() { | ||
debug "Creating branch ${TEMP_BRANCH}" | ||
git checkout -b "${TEMP_BRANCH}" | ||
|
||
debug "Committing staged files: $*" | ||
git add "$@" | ||
git commit -m "${COMMIT_MSG}" | ||
|
||
debug "Creating branch remotely" | ||
git push origin "${TEMP_BRANCH}" | ||
} | ||
|
||
function create_pr() { | ||
debug "Creating PR against ${BRANCH} branch" | ||
NEW_PR_URL=$(gh pr create --title "${PR_TITLE}" --body "${PR_BODY}: ${WORKFLOW_URL}" --base "${BRANCH}") # e.g, https://github.com/awslabs/aws-lambda-powertools/pull/13 | ||
|
||
# greedy remove any string until the last URL path, including the last '/'. https://opensource.com/article/17/6/bash-parameter-expansion | ||
NEW_PR_ID="${NEW_PR_URL##*/}" # 13 | ||
export NEW_PR_URL | ||
export NEW_PR_ID | ||
} | ||
|
||
function close_duplicate_prs() { | ||
debug "Do we have any duplicate PRs?" | ||
DUPLICATE_PRS=$(gh pr list --search "${PR_TITLE}" --json number --jq ".[] | select(.number != ${NEW_PR_ID}) | .number") # e.g, 13\n14 | ||
|
||
debug "Closing duplicated PRs if any" | ||
echo "${DUPLICATE_PRS}" | xargs -L1 gh pr close --delete-branch --comment "Superseded by #${NEW_PR_ID}" | ||
export readonly DUPLICATE_PRS | ||
} | ||
|
||
function report_summary() { | ||
debug "Creating job summary" | ||
echo "### Pull request created successfully :rocket: #${NEW_PR_URL} <br/><br/> Closed duplicated PRs (if any): ${DUPLICATE_PRS}" >>"$GITHUB_STEP_SUMMARY" | ||
|
||
notice "PR_URL is ${NEW_PR_URL}" | ||
notice "PR_BRANCH is ${TEMP_BRANCH}" | ||
notice "PR_DUPLICATES are ${DUPLICATE_PRS}" | ||
} | ||
|
||
function main() { | ||
# Sanity check | ||
has_anything_changed | ||
has_required_config | ||
|
||
create_temporary_branch_with_changes "$@" | ||
create_pr | ||
close_duplicate_prs | ||
|
||
report_summary | ||
} | ||
|
||
main "$@" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Super cool this check to avoid orphans branches in the repository and getting messy. :D