Skip to content

Commit

Permalink
feat: support updating existing release PRs
Browse files Browse the repository at this point in the history
  • Loading branch information
vinmassaro committed Jan 10, 2024
1 parent 2082a93 commit bdcbcd4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
42 changes: 22 additions & 20 deletions .ci/github/create_release_pull_request
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,26 @@

set -eo pipefail

# Create a pull request to merge develop into master.
pull_request_response=$(curl -s -H "Accept: application/vnd.github+json" \
-H "Authorization: token $ACCESS_TOKEN" \
-X POST -d '{"title": "Release", "head": "develop", "base": "master"}' \
"https://api.github.com/repos/$REPO/pulls")

# Check if the pull request creation was successful.
if [ -z "$(echo "$pull_request_response" | jq -r '.html_url')" ]; then
message=$(echo "$pull_request_response" | jq -r '.errors[].message')
echo "Failed to create pull request."
echo "Error: $message"
exit 1
else
opened_pr_url=$(echo "$pull_request_response" | jq -r '.html_url')
fi
# If we aren't updating an existing pull request, create one to merge develop into master.
if [ -z "$PR_NUMBER" ]; then
pull_request_response=$(curl -s -H "Accept: application/vnd.github+json" \
-H "Authorization: token $ACCESS_TOKEN" \
-X POST -d '{"title": "Release", "head": "develop", "base": "master"}' \
"https://api.github.com/repos/$REPO/pulls")

# Check if the pull request creation was successful.
if [ -z "$(echo "$pull_request_response" | jq -r '.html_url')" ]; then
message=$(echo "$pull_request_response" | jq -r '.errors[].message')
echo "Failed to create pull request."
echo "Error: $message"
exit 1
else
opened_pr_url=$(echo "$pull_request_response" | jq -r '.html_url')
fi

# Extract the pull request number from the response.
pr_number=$(echo "$pull_request_response" | jq -r '.number')
# Extract the pull request number from the response.
PR_NUMBER=$(echo "$pull_request_response" | jq -r '.number')
fi

# Page through the /pulls/#/commits endpoint.
page=1
Expand All @@ -28,7 +30,7 @@ per_page=100
while true; do
# Get the list of commits from the pull request and extract commit SHAs.
commits_response=$(curl --silent -H "Authorization: token $ACCESS_TOKEN" \
"https://api.github.com/repos/$REPO/pulls/$pr_number/commits?per_page=$per_page&page=$page")
"https://api.github.com/repos/$REPO/pulls/$PR_NUMBER/commits?per_page=$per_page&page=$page")

current_commit_shas=$(echo "$commits_response" | jq -r '.[].sha')

Expand Down Expand Up @@ -76,7 +78,7 @@ fi

# Output changes that are not in a pull request.
if [[ -n "${changes_without_pr[*]}" ]]; then
description+="\n## Changes without a pull rqeuest:\n"
description+="\n## Changes without a pull request:\n"

for sha in "${changes_without_pr[@]}"; do
description+="- $sha\n"
Expand All @@ -88,7 +90,7 @@ update_pull_request=$(curl -s -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: token $ACCESS_TOKEN" \
-X PATCH -d "{\"body\": \"$description\"}" \
"https://api.github.com/repos/$REPO/pulls/$pr_number")
"https://api.github.com/repos/$REPO/pulls/$PR_NUMBER")

if [[ -n "$pull_request_response" || -n "$update_pull_request" ]]; then
echo "Pull request created: $opened_pr_url"
Expand Down
7 changes: 6 additions & 1 deletion .github/workflows/release_pr.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
name: Create release pull request
on: workflow_dispatch
on:
pull_request:
branches:
- master
workflow_dispatch:
jobs:
create_pull_request:
runs-on: ubuntu-latest
env:
ACCESS_TOKEN: ${{ secrets.YALESITES_BUILD_TOKEN }}
REPO: ${{ github.repository }}
PR_NUMBER: ${{ github.event.number }}
steps:
- name: Checkout
uses: actions/checkout@v4
Expand Down

0 comments on commit bdcbcd4

Please sign in to comment.