|
| 1 | +#!/bin/bash |
| 2 | +#/ |
| 3 | +# @license Apache-2.0 |
| 4 | +# |
| 5 | +# Copyright (c) 2024 The Stdlib Authors. |
| 6 | +# |
| 7 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +# you may not use this file except in compliance with the License. |
| 9 | +# You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +# See the License for the specific language governing permissions and |
| 17 | +# limitations under the License. |
| 18 | +#/ |
| 19 | + |
| 20 | +# Script to generate a commit message for a pull request. |
| 21 | +# |
| 22 | +# Usage: generate_pr_commit_message PR_NUMBER |
| 23 | +# |
| 24 | +# Arguments: |
| 25 | +# |
| 26 | +# PR_NUMBER Pull request number. |
| 27 | +# |
| 28 | +# Environment variables: |
| 29 | +# |
| 30 | +# GITHUB_TOKEN GitHub token for authentication. |
| 31 | + |
| 32 | +# Ensure that the exit status of pipelines is non-zero in the event that at least one of the commands in a pipeline fails: |
| 33 | +set -o pipefail |
| 34 | + |
| 35 | + |
| 36 | +# VARIABLES # |
| 37 | + |
| 38 | +# Get the pull request number: |
| 39 | +pr_number="$1" |
| 40 | + |
| 41 | +# GitHub API base URL |
| 42 | +GITHUB_API_URL="https://api.github.com" |
| 43 | + |
| 44 | +# Repository owner and name |
| 45 | +REPO_OWNER="stdlib-js" |
| 46 | +REPO_NAME="stdlib" |
| 47 | + |
| 48 | + |
| 49 | +# FUNCTIONS # |
| 50 | + |
| 51 | +# Error handler. |
| 52 | +# |
| 53 | +# $1 - error status |
| 54 | +on_error() { |
| 55 | + echo 'ERROR: An error was encountered during execution.' >&2 |
| 56 | + exit "$1" |
| 57 | +} |
| 58 | + |
| 59 | +# Function to resolve GitHub handle to name and email using .mailmap |
| 60 | +# |
| 61 | +# $1 - GitHub handle |
| 62 | +resolve_user() { |
| 63 | + local github_handle="$1" |
| 64 | + local mailmap_file=".mailmap" |
| 65 | + local name_email |
| 66 | + |
| 67 | + # Try to find a match for the GitHub handle: |
| 68 | + name_email=$(grep -i "$github_handle" "$mailmap_file" | head -n 1) |
| 69 | + |
| 70 | + if [ -n "$name_email" ]; then |
| 71 | + # Extract name and email from the matching line: |
| 72 | + echo "$name_email" | sed -E 's/^(.*)<(.*)>.*$/\1 <\2>/' | xargs |
| 73 | + else |
| 74 | + # If no match found, use the GitHub handle as is: |
| 75 | + echo "$github_handle <$github_handle@users.noreply.github.com>" |
| 76 | + fi |
| 77 | +} |
| 78 | + |
| 79 | +# Function to make authenticated GitHub API requests |
| 80 | +# |
| 81 | +# $1 - HTTP method (GET or POST) |
| 82 | +# $2 - API endpoint |
| 83 | +# $3 - Data for POST requests |
| 84 | +github_api() { |
| 85 | + local method="$1" |
| 86 | + local endpoint="$2" |
| 87 | + local data="$3" |
| 88 | + |
| 89 | + if [ "$method" == "GET" ]; then |
| 90 | + curl -s -H "Authorization: token $GITHUB_TOKEN" "$GITHUB_API_URL$endpoint" |
| 91 | + elif [ "$method" == "POST" ]; then |
| 92 | + curl -s -X POST -H "Authorization: token $GITHUB_TOKEN" -H "Content-Type: application/json" -d "$data" "$GITHUB_API_URL$endpoint" |
| 93 | + else |
| 94 | + echo "Invalid HTTP method: $method" |
| 95 | + on_error 1 |
| 96 | + fi |
| 97 | +} |
| 98 | + |
| 99 | +# Main execution sequence. |
| 100 | +main() { |
| 101 | + # Fetch pull request details: |
| 102 | + pr_details=$(github_api "GET" "/repos/$REPO_OWNER/$REPO_NAME/pulls/$pr_number") |
| 103 | + pr_title=$(echo "$pr_details" | jq -r '.title') |
| 104 | + pr_body=$(echo "$pr_details" | jq -r '.body // ""') |
| 105 | + pr_url=$(echo "$pr_details" | jq -r '.html_url') |
| 106 | + |
| 107 | + # Extract reviewers: |
| 108 | + pr_reviews=$(github_api "GET" "/repos/$REPO_OWNER/$REPO_NAME/pulls/$pr_number/reviews") |
| 109 | + reviewers=$(echo "$pr_reviews" | jq -r '.[] | select(.state == "APPROVED" ) | .user.login' | sort -u) |
| 110 | + |
| 111 | + # Fetch commits in the PR: |
| 112 | + pr_commits=$(github_api "GET" "/repos/$REPO_OWNER/$REPO_NAME/pulls/$pr_number/commits") |
| 113 | + |
| 114 | + # Extract co-authors from commits: |
| 115 | + co_authors=$(echo "$pr_commits" | jq -r '.[].commit.message' | grep -i "Co-authored-by:" | awk -F': ' '{print $2}' | sort | uniq | paste -sd '\n' -) |
| 116 | + |
| 117 | + # Extract linked issues from PR body (e.g., #123) |
| 118 | + issue_numbers=$(echo "$pr_body" | grep -oE '#[0-9]+' | grep -oE '[0-9]+' | sort | uniq) |
| 119 | + closes_issues="" |
| 120 | + ref_issues="" |
| 121 | + for issue in $issue_numbers; do |
| 122 | + if echo "$pr_body" | grep -qi "closes.*#$issue"; then |
| 123 | + closes_issues+="Closes: https://github.com/$REPO_OWNER/$REPO_NAME/issues/$issue\n" |
| 124 | + else |
| 125 | + ref_issues+="Ref: https://github.com/$REPO_OWNER/$REPO_NAME/issues/$issue\n" |
| 126 | + fi |
| 127 | + done |
| 128 | + closes_issues=$(echo -e "$closes_issues" | sed '$ s/\n$//') |
| 129 | + ref_issues=$(echo -e "$ref_issues" | sed '$ s/\n$//') |
| 130 | + |
| 131 | + # Assemble commit message components: |
| 132 | + commit_subject="$pr_title" |
| 133 | + commit_body="PR-URL: $pr_url" |
| 134 | + |
| 135 | + if [ -n "$closes_issues" ]; then |
| 136 | + commit_body+="\n$closes_issues" |
| 137 | + fi |
| 138 | + if [ -n "$ref_issues" ]; then |
| 139 | + commit_body+="\n$ref_issues" |
| 140 | + fi |
| 141 | + if [ -n "$co_authors" ]; then |
| 142 | + commit_body+="\n$co_authors" |
| 143 | + fi |
| 144 | + for reviewer in $reviewers; do |
| 145 | + resolved_reviewer=$(resolve_user "$reviewer") |
| 146 | + commit_body+="\nReviewed-by: $resolved_reviewer" |
| 147 | + done |
| 148 | + |
| 149 | + # Add Signed-off-by line: |
| 150 | + pr_author=$(echo "$pr_details" | jq -r '.user.login') |
| 151 | + signed_off_by=$(resolve_user "$pr_author") |
| 152 | + commit_body+="\nSigned-off-by: $signed_off_by" |
| 153 | + |
| 154 | + # Combine subject and body: |
| 155 | + commit_message="$commit_subject\n\n$commit_body" |
| 156 | + |
| 157 | + # Output the commit message: |
| 158 | + echo -e "$commit_message" |
| 159 | +} |
| 160 | + |
| 161 | +# Call main with all command-line arguments: |
| 162 | +main "$@" |
0 commit comments