-
Notifications
You must be signed in to change notification settings - Fork 1.1k
75 lines (60 loc) · 3.07 KB
/
add-lockdown-label.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
63
64
65
66
67
68
69
70
71
72
73
74
75
name: Add Branch Lockdown Label to PRs
on:
pull_request_target:
workflow_dispatch: # Allows manual triggering of the workflow
permissions:
actions: write # For managing the operation state cache
issues: write
jobs:
add-label:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Install jq
run: sudo apt-get install -y jq
- name: Add label to PRs
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# Determine the third Tuesday of the current month
third_tuesday=$(date -d "$(date +%Y-%m-01) +14 days" +%Y-%m-%d)
while [ $(date -d "$third_tuesday" +%u) -ne 2 ]; do
third_tuesday=$(date -d "$third_tuesday + 1 day" +%Y-%m-%d)
done
# Determine the first Tuesday of the current month
first_tuesday=$(date -d "$(date +%Y-%m-01)" +%Y-%m-%d)
while [ $(date -d "$first_tuesday" +%u) -ne 2 ]; do
first_tuesday=$(date -d "$first_tuesday + 1 day" +%Y-%m-%d)
done
# Get current date
current_date=$(date +%Y-%m-%d)
echo "Current Date: $current_date"
echo "Third Tuesday of the month: $third_tuesday"
echo "First Tuesday of the month: $first_tuesday"
# Check if the current date is after the third Tuesday of this month or before the first Tuesday of this month
if [[ "$current_date" > "$third_tuesday" || "$current_date" < "$first_tuesday" ]]; then
echo "Within the label period. Checking if the branch matches..."
# Get all open PRs targeting branches release/8* and release/9* excluding release/9.0.3xx
echo "Running gh pr list query..."
prs=$(gh pr list --state open --limit 200 --json number,baseRefName,author,labels)
echo "Total PRs Count: $(echo "$prs" | jq length)"
echo "PRs JSON: $prs"
echo "Filtering PRs targeting release/8* and release/9* excluding release/9.0.3xx..."
prs_targeting_branches=$(echo "$prs" | jq '[.[] | select(.baseRefName | test("^release/[89].*")) | select(.baseRefName | test("^release/9.0.3xx") | not)]')
echo "Count of PRs targeting specific branches: $(echo "$prs_targeting_branches" | jq length)"
echo "Filtering PRs without Branch Lockdown label..."
filtered_prs=$(echo "$prs_targeting_branches" | jq '[.[] | select(.labels | map(.name) | index("Branch Lockdown") | not) | .number]')
echo "Filtered PRs without Branch Lockdown label JSON: $filtered_prs"
echo "Count of Filtered PRs without Branch Lockdown label: $(echo "$filtered_prs" | jq length)"
# Loop through filtered PRs and add label
for pr in $(echo "$filtered_prs" | jq -r '.[]'); do
echo "Adding label to PR #$pr"
gh pr edit $pr --add-label "Branch Lockdown"
done
else
echo "Outside the label period. No labels added."
fi