|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 4 | +# contributor license agreements. See the NOTICE file distributed with |
| 5 | +# this work for additional information regarding copyright ownership. |
| 6 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 7 | +# (the "License"); you may not use this file except in compliance with |
| 8 | +# the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | + |
| 18 | +set -e |
| 19 | + |
| 20 | +get_milestone_number() { |
| 21 | + local milestone_title="$1" |
| 22 | + local number=$(gh api "repos/$REPOSITORY/milestones" --jq ".[] | select(.title==\"$milestone_title\") | .number") |
| 23 | + |
| 24 | + if [ -z "$number" ]; then |
| 25 | + echo "Creating milestone: $milestone_title" |
| 26 | + number=$(gh api "repos/$REPOSITORY/milestones" -f title="$milestone_title" --jq '.number') |
| 27 | + fi |
| 28 | + |
| 29 | + echo "$number" |
| 30 | +} |
| 31 | + |
| 32 | +create_backport_issue() { |
| 33 | + local pr_number="$1" |
| 34 | + local pr_title="$2" |
| 35 | + local target_milestone="$3" |
| 36 | + |
| 37 | + local milestone_number=$(get_milestone_number "$target_milestone") |
| 38 | + local message="Backport #$pr_number to release branch for version $target_milestone" |
| 39 | + local note="Add the \`approved\` label to start the automatic backport process." |
| 40 | + local body=$(jq -n --arg source_pr "$pr_number" --arg target_version "$target_milestone" --arg message "$message" --arg note "$note" '$ARGS.named') |
| 41 | + |
| 42 | + gh api "repos/$REPOSITORY/issues" \ |
| 43 | + -f title="[$target_milestone] $pr_title" \ |
| 44 | + -f body="$body" \ |
| 45 | + -f milestone="$milestone_number" \ |
| 46 | + -f labels[]="backport" \ |
| 47 | + --jq '.number' |
| 48 | +} |
| 49 | + |
| 50 | +if [ "$#" -ne 2 ]; then |
| 51 | + echo "Usage: $0 <pr_number> <repository>" |
| 52 | + echo "Example: $0 12345 apache/druid" |
| 53 | + exit 1 |
| 54 | +fi |
| 55 | + |
| 56 | +PR_NUMBER="$1" |
| 57 | +REPOSITORY="$2" |
| 58 | + |
| 59 | +VERSION=$(xmllint --xpath "/*[local-name()='project']/*[local-name()='version']/text()" pom.xml | sed 's/-SNAPSHOT//') |
| 60 | + |
| 61 | +if [ -z "$VERSION" ]; then |
| 62 | + echo "Error: Could not extract version from pom.xml" |
| 63 | + exit 1 |
| 64 | +fi |
| 65 | + |
| 66 | +echo "Extracted version: $VERSION" |
| 67 | + |
| 68 | +EXISTING_MILESTONE=$(gh api "repos/$REPOSITORY/issues/$PR_NUMBER" --jq '.milestone.title // empty') |
| 69 | + |
| 70 | +if [ -n "$EXISTING_MILESTONE" ] && [ "$EXISTING_MILESTONE" != "$VERSION" ]; then |
| 71 | + echo "PR #$PR_NUMBER has milestone $EXISTING_MILESTONE, but should be $VERSION" |
| 72 | + |
| 73 | + PR_TITLE=$(gh api "repos/$REPOSITORY/issues/$PR_NUMBER" --jq '.title') |
| 74 | + BACKPORT_ISSUE=$(create_backport_issue "$PR_NUMBER" "$PR_TITLE" "$EXISTING_MILESTONE") |
| 75 | + |
| 76 | + echo "Created backport issue #$BACKPORT_ISSUE for milestone $EXISTING_MILESTONE" |
| 77 | +elif [ -n "$EXISTING_MILESTONE" ]; then |
| 78 | + echo "PR #$PR_NUMBER already has correct milestone: $EXISTING_MILESTONE" |
| 79 | + exit 0 |
| 80 | +fi |
| 81 | + |
| 82 | +MILESTONE_NUMBER=$(get_milestone_number "$VERSION") |
| 83 | + |
| 84 | +echo "Adding PR #$PR_NUMBER to milestone $VERSION" |
| 85 | +gh api "repos/$REPOSITORY/issues/$PR_NUMBER" -f milestone="$MILESTONE_NUMBER" -X PATCH |
| 86 | +echo "Successfully added PR #$PR_NUMBER to milestone $VERSION" |
0 commit comments