From 56a51c9780ebdb68808dc119017c2a1008fe48c9 Mon Sep 17 00:00:00 2001 From: Vincent Massaro Date: Thu, 4 Jan 2024 16:49:30 -0500 Subject: [PATCH] feat: add action workflow for release pull request --- .ci/github/create_release_pull_request | 93 ++++++++++++++++++++++++++ .github/workflows/release_pr.yml | 14 ++++ 2 files changed, 107 insertions(+) create mode 100755 .ci/github/create_release_pull_request create mode 100644 .github/workflows/release_pr.yml diff --git a/.ci/github/create_release_pull_request b/.ci/github/create_release_pull_request new file mode 100755 index 0000000000..f9c69f4f54 --- /dev/null +++ b/.ci/github/create_release_pull_request @@ -0,0 +1,93 @@ +#!/bin/bash + +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 [ -n "$(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 +fi + +# Extract the pull request number from the response. +pr_number=$(echo "$pull_request_response" | jq -r '.number') + +# Page through the /pulls/#/commits endpoint. +page=1 +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") + + current_commit_shas=$(echo "$commits_response" | jq -r '.[].sha') + + # Append current commit SHAs to the overall list. + commit_shas+="$current_commit_shas" + + # Check if there are more pages, otherwise finish. + if [[ $(echo "$commits_response" | jq -r '. | length') -lt "$per_page" ]]; then + break + fi + + # Increment page number for the next API call. + ((page++)) +done + +# Create list of unique pull requests based on commit SHAs, and another for changes without PRs. +pull_requests=() +changes_without_pr=() + +for sha in $commit_shas; do + pr_url=$(curl -s -H "Authorization: token $ACCESS_TOKEN" \ + "https://api.github.com/repos/$REPO/commits/$sha/pulls" \ + | jq -r '.[].html_url') + + if [ -n "$pr_url" ]; then + pull_requests+=("$pr_url") + else + changes_without_pr+=("$sha") + fi +done + +# Dedupe pull request URLs. +pull_requests=($(printf '%s\n' "${pull_requests[@]}" | sort -u)) + +# Begin outputting description if there are PRs. +if [[ -n "${pull_requests[*]}" ]]; then + description+="## Pull requests\n" + + # Create pull request description with list of pull requests as a markdown list. + for pr in "${pull_requests[@]}"; do + description+="- $pr\n" + done + +fi + +# Output changes that are not in a pull request. +if [[ -n "${changes_without_pr[*]}" ]]; then + description+="\n## Changes without a pull rqeuest:\n" + + for sha in "${changes_without_pr[@]}"; do + description+="- $sha\n" + done +fi + +# Update pull request with new description. +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") + +if [[ -n "$pull_request_response" || -n "$update_pull_request" ]]; then + echo "Pull request created" +fi diff --git a/.github/workflows/release_pr.yml b/.github/workflows/release_pr.yml new file mode 100644 index 0000000000..1826849c47 --- /dev/null +++ b/.github/workflows/release_pr.yml @@ -0,0 +1,14 @@ +name: Create release pull request +on: workflow_dispatch +jobs: + create_pull_request: + runs-on: ubuntu-latest + env: + ACCESS_TOKEN: ${{ secrets.YALESITES_BUILD_TOKEN }} + REPO: ${{ github.repository }} + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Create pull request + run: ./.ci/github/create_release_pull_request