Add release labels to issues on merge

Signed-off-by: stianst <stianst@gmail.com>
This commit is contained in:
stianst 2023-11-28 08:47:35 +01:00 committed by Stian Thorgersen
parent 7e0cbcafae
commit 2ce7d318a6
2 changed files with 79 additions and 0 deletions

29
.github/scripts/pr-find-issues.sh vendored Executable file
View file

@ -0,0 +1,29 @@
#!/bin/bash -e
PR="$1"
REPO="$2"
if [ "$REPO" == "" ]; then
REPO="keycloak/keycloak"
fi
function parse_issues() {
echo "$1" | grep -i -P -o "(close|closes|closed|resolve|resolves|resolved) #[[:digit:]]*" | cut -d '#' -f 2 | sort -n
}
PR_JSON=$(gh api "/repos/$REPO/pulls/$PR")
PR_BODY=$(echo "$PR_JSON" | jq .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 .commit.message)
ISSUES=$(parse_issues "$COMMIT_MESSAGE")
fi
for i in $ISSUES; do
echo "$i"
done

50
.github/workflows/label.yml vendored Normal file
View file

@ -0,0 +1,50 @@
name: Labeler
on:
pull_request_target:
types: closed
jobs:
label:
runs-on: ubuntu-latest
permissions:
contents: read
issues: write
steps:
- uses: actions/checkout@v4
with:
sparse-checkout: .github/scripts
- name: Add release labels on merge
run: |
PR_NUMBER="${{ github.event.pull_request.number }}"
echo "Base REF: $GITHUB_BASE_REF"
echo "PR: https://github.com/$GITHUB_REPOSITORY/pull/$PR_NUMBER"
if [ "$GITHUB_BASE_REF" == "main" ]; then
LAST_MAJOR="$(gh api /repos/$GITHUB_REPOSITORY/branches --paginate --jq .[].name | grep '^release/' | cut -d '/' -f 2 | cut -d '.' -f 1 | sort -n -r | head -n 1)"
NEXT_MAJOR="$(($LAST_MAJOR + 1))"
LABEL="release/$NEXT_MAJOR.0.0"
BACKPORT_LABEL="backport/main"
elif [[ "$GITHUB_BASE_REF" = release/* ]]; then
MAJOR_MINOR="$(echo $GITHUB_BASE_REF | cut -d '/' -f 2)"
LAST_MICRO="$(gh api /repos/$GITHUB_REPOSITORY/tags --jq .[].name | sort -n -r | grep $MAJOR_MINOR | head -n 1 | cut -d '.' -f 3)"
NEXT_MICRO="$(($LAST_MICRO + 1))"
LABEL="release/$MAJOR_MINOR.$NEXT_MICRO"
BACKPORT_LABEL="backport/$MAJOR_MINOR"
fi
echo "Label: $LABEL"
gh api "repos/$GITHUB_REPOSITORY/labels/$(echo $LABEL | sed 's|/|%2F|g')" --silent 2>/dev/null || gh label create -R "$GITHUB_REPOSITORY" "$LABEL" -c "0E8A16"
echo ""
echo "Updating issues:"
ISSUES=$(.github/scripts/pr-find-issues.sh "$PR_NUMBER" "$GITHUB_REPOSITORY")
for ISSUE in $ISSUES; do
gh issue edit "$ISSUE" -R "$GITHUB_REPOSITORY" --add-label "$LABEL" --remove-label "$BACKPORT_LABEL"
done
if: github.repository == 'keycloak/keycloak' && github.event_name == 'pull_request_target' && github.event.action == 'closed' && github.event.pull_request.merged == true
env:
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"