forked from longhorn/longhorn
-
Notifications
You must be signed in to change notification settings - Fork 0
62 lines (53 loc) · 2.36 KB
/
backport-pr.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
name: Link-Backport-PR-Issue
on:
pull_request:
types: [ opened ]
branches:
- master
- "v*"
jobs:
check-backport:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Check if PR is a backport
run: |
if [[ "$(echo "${{ github.event.pull_request.title }}" | sed 's/"/\\"/g')" =~ "backport #" ]]; then
echo "BACKPORT=true" >> $GITHUB_ENV
else
echo "BACKPORT=false" >> $GITHUB_ENV
fi
- name: Extract backport branch and issue number
if: env.BACKPORT == 'true'
run: |
# Extract branch from the target branch of the PR
BRANCH=$(echo "${{ github.event.pull_request.base.ref }}")
BRANCH=${BRANCH%.x} # Remove the '.x' suffix
BRANCH=$(echo "${BRANCH}" | sed 's/\./\\./g') # Escape periods
echo "BRANCH=$BRANCH" >> $GITHUB_ENV
# Extract issue number from the PR description
ORIGINAL_ISSUE_NUMBER=$(echo '${{ github.event.pull_request.body }}'' | grep -oE 'longhorn/longhorn#[0-9]+' | cut -d'#' -f2)
echo "ORIGINAL_ISSUE_NUMBER=$ORIGINAL_ISSUE_NUMBER" >> $GITHUB_ENV
- name: Link the PR with the backport issue
if: ${{ env.BACKPORT == 'true' && env.ORIGINAL_ISSUE_NUMBER != '' }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
for issue_number in "${{ env.ORIGINAL_ISSUE_NUMBER }}"; do
echo "Found source issue longhorn/longhorn#${issue_number}"
issue_title=$(gh issue view "$issue_number" --repo longhorn/longhorn --json 'title' -q '.title')
if [[ $? -eq 0 && -n "$issue_number" ]]; then
backport_issue_number=$(
curl -s "https://api.github.com/search/issues?q=repo:longhorn/longhorn+is:open+is:issue+in:title+${{ env.BRANCH }}+\"${issue_title}\"" \
| jq .items[0].number
)
if [[ -n "$backport_issue_number" ]]; then
echo "Found backport issue longhorn/longhorn#${backport_issue_number}"
echo "Linking backport issue longhorn/longhorn#${backport_issue_number}"
gh issue comment --repo longhorn/longhorn "${backport_issue_number}" --body "Backport PR: ${{ github.event.pull_request.html_url }}"
continue
fi
fi
echo "No issue title found"
done