Skip to content

Commit

Permalink
[infinispan#2209] Add release labels to PRs
Browse files Browse the repository at this point in the history
Co-authored-by: Tristan Tarrant <[email protected]>
Copied and adapted from infinispan/infinispan#13518
  • Loading branch information
fax4ever committed Jan 27, 2025
1 parent 2f3ed84 commit 963e868
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/label.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Labeller

on:
pull_request_target:
types: [closed]

jobs:
label:
runs-on: ubuntu-latest
permissions:
contents: read
issues: write

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Add release labels on merge
run: ./scripts/ci/pr-label-issues.sh "${{ github.event.pull_request.number }}" "$GITHUB_REPOSITORY" "$GITHUB_BASE_REF" >> "$GITHUB_STEP_SUMMARY"
if: github.repository == 'infinispan/infinispan-operator' && github.event.pull_request.merged == true
env:
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
30 changes: 30 additions & 0 deletions scripts/ci/pr-find-issues.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash -e

PR="$1"
REPO="$2"

if [ "$REPO" == "" ]; then
REPO="infinispan/infinispan-operator"
fi

function parse_issues() {
echo "$1" | grep -i -P -o "(close|closes|closed|resolve|resolves|resolved|fixes|fixed):? #[[:digit:]]*" | cut -d '#' -f 2 | sort -n
}

if [ "$PR" != "" ]; then
PR_JSON=$(gh api "/repos/$REPO/pulls/$PR")
PR_BODY=$(echo "$PR_JSON" | jq -r .body)
PR_MERGE_COMMIT_SHA=$(echo "$PR_JSON" | jq -r .merge_commit_sha)

ISSUES=$(parse_issues "$PR_BODY")
if [ "$ISSUES" == "" ]; then
COMMIT_JSON=$(gh api "/repos/$REPO/commits/$PR_MERGE_COMMIT_SHA")
COMMIT_MESSAGE=$(echo "$COMMIT_JSON" | jq -r .commit.message)

ISSUES=$(parse_issues "$COMMIT_MESSAGE")
fi

for i in $ISSUES; do
echo "$i"
done
fi
37 changes: 37 additions & 0 deletions scripts/ci/pr-label-issues.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/bash -e

PR="$1"
REPO="$2"
REF="$3"

echo "**Branch:** [$REF](https://github.com/$REPO/tree/$REF)"
echo "**PR:** [$PR](https://github.com/$REPO/pull/$PR)"

if [[ $REF == main ]]; then
LAST_RELEASE="$(gh api /repos/$REPO/branches --paginate --jq .[].name | grep -E '^[0-9]+\.[0-9]+\.x' | sort -n -r | head -n 1)"
LAST_MAJOR=$(echo "$LAST_RELEASE" | cut -d '.' -f 1)
LAST_MINOR=$(echo "$LAST_RELEASE" | cut -d '.' -f 2)

NEXT_MAJOR=$LAST_MAJOR
NEXT_MINOR="$(($LAST_MINOR + 1))"
LABEL="release/$NEXT_MAJOR.$NEXT_MINOR.0"
EXTRA_EDIT=""
elif [[ $REF =~ ^[0-9]+\.[0-9]+\.x$ ]]; then
MAJOR_MINOR="$(echo $REF | cut -d . -f 1,2)"
LAST_MICRO="$(gh api /repos/$REPO/tags --jq .[].name | sort -V -r | grep $MAJOR_MINOR | head -n 1 | cut -d . -f 3)"
NEXT_MICRO="$(($LAST_MICRO + 1))"
LABEL="release/$MAJOR_MINOR.$NEXT_MICRO"
EXTRA_EDIT="--remove-label backport/$MAJOR_MINOR"
fi

echo "**Label:** [$LABEL](https://github.com/$REPO/labels/$LABEL)"

gh api "/repos/$REPO/labels/$LABEL" --silent 2>/dev/null || gh label create -R "$REPO" "$LABEL" -c "0E8A16"

echo "**Updating issues:**"

ISSUES=$(./scripts/ci/pr-find-issues.sh "$PR" "$REPO")
for ISSUE in $ISSUES; do
echo "* [$ISSUE](https://github.com/$REPO/issues/$ISSUE)"
gh issue edit "$ISSUE" -R "$REPO" --add-label "$LABEL" $EXTRA_EDIT
done

0 comments on commit 963e868

Please sign in to comment.