-
Notifications
You must be signed in to change notification settings - Fork 37
120 lines (102 loc) · 4.69 KB
/
backport.yaml
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
name: Backport
on:
pull_request_target:
types: ["labeled", "closed"]
env:
GITHUB_BRANCH: ${{ github.ref_name }}
jobs:
backport:
name: Backport PR
runs-on: ubuntu-latest
if: |
github.event.pull_request.merged == true
&& contains(github.event.pull_request.labels.*.name, 'auto-backport')
&& (
(github.event.action == 'labeled' && github.event.label.name == 'auto-backport')
|| (github.event.action == 'closed')
)
outputs:
BEFORE_BACKPORTS: ${{ steps.count_prs.outputs.pr_count }}
steps:
- name: Retrieve Credentials
id: import-secrets
uses: hashicorp/[email protected]
with:
url: https://vault.prism.spectrocloud.com
method: approle
roleId: ${{ secrets.VAULT_ROLE_ID }}
secretId: ${{ secrets.VAULT_SECRET_ID }}
secrets: /providers/github/organizations/spectrocloud/token?org_name=spectrocloud token | VAULT_GITHUB_TOKEN
- name: Count PRs created by vault-token-factory-spectrocloud
id: count_prs
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
PR_COUNT=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/repos/${{ github.repository }}/pulls?state=open&per_page=100" | \
jq '[.[] | select(.user.login == "vault-token-factory-spectrocloud[bot]")] | length')
echo "Number of PRs created by vault-token-factory-spectrocloud: $PR_COUNT"
echo "pr_count=$PR_COUNT" >> $GITHUB_OUTPUT
- name: Determine branch name
id: extract_branch
run: |
if [ "${{ github.event_name }}" = "pull_request_target" ]; then
echo "GITHUB_BRANCH=${{ github.head_ref }}" >> $GITHUB_ENV
else
echo "GITHUB_BRANCH=${{ github.ref_name }}" >> $GITHUB_ENV
fi
- name: Check for backport labels
id: check_labels
run: |-
labels='${{ toJSON(github.event.pull_request.labels.*.name) }}'
matched=$(echo "${labels}" | jq '. | map(select(startswith("backport-"))) | length')
echo "matched=$matched"
echo "matched=$matched" >> $GITHUB_OUTPUT
- name: Backport Action
uses: sqren/[email protected]
with:
# We are using a PAT token through our Vault Operator to address the issue of PRs workflows not being triggered
# Refer to issue https://github.com/sqren/backport-github-action/issues/79 for more details.
github_token: ${{ steps.import-secrets.outputs.VAULT_GITHUB_TOKEN }}
auto_backport_label_prefix: backport-
add_original_reviewers: true
- name: Info log
if: ${{ success() }}
run: cat ~/.backport/backport.info.log
- name: Debug log
id: debug-log
if: ${{ failure() }}
run: cat ~/.backport/backport.debug.log
- name: Verify PR count increment
id: verify_increment
env:
GITHUB_TOKEN: ${{ steps.import-secrets.outputs.VAULT_GITHUB_TOKEN }}
run: |-
BEFORE_BACKPORTS=${{ steps.count_prs.outputs.pr_count }}
NEW_COUNT=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/repos/${{ github.repository }}/pulls?state=open&per_page=100" | \
jq '[.[] | select(.user.login == "vault-token-factory-spectrocloud[bot]")] | length')
MATCHED=${{ steps.check_labels.outputs.matched }}
echo "Before Backports Count: $BEFORE_BACKPORTS"
echo "New PR Count: $NEW_COUNT"
echo "Matched Backport Labels: $MATCHED"
if [ $((NEW_COUNT - BEFORE_BACKPORTS)) -eq $MATCHED ]; then
echo "PR count increased by the expected amount."
echo "missing_prs=0" >> $GITHUB_OUTPUT
else
MISSING_PR_COUNT=$((MATCHED - (NEW_COUNT - BEFORE_BACKPORTS)))
echo "PR count did not increase by the expected amount."
echo "missing_prs=$MISSING_PR_COUNT" >> $GITHUB_OUTPUT
exit 1
fi
- name: Slack Notification
if: ${{ failure() }}
uses: rtCamp/[email protected]
env:
SLACK_WEBHOOK: ${{ secrets.SLACK_PRIVATE_TEAM_WEBHOOK }}
SLACK_USERNAME: "spectromate"
SLACK_ICON_EMOJI: ":robot_panic:"
SLACK_COLOR: ${{ job.status }}
SLACKIFY_MARKDOWN: true
ENABLE_ESCAPES: true
SLACK_MESSAGE: 'The backports for branch `${{env.GITHUB_BRANCH}}` in PR [#${{ github.event.pull_request.number }}](${{ github.event.pull_request.html_url }}) failed. Number of missing PRs: `${{ steps.verify_increment.outputs.missing_prs }}`. Review the PR for more details.'