-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
action.yml
100 lines (84 loc) · 3.72 KB
/
action.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
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
name: Check conflict branch in PR
description: Ensure PRs with conflict branch getting help started to syncing branch latest.
branding:
icon: check
color: green
inputs:
token:
description: GitHub token to comment on the PR
default: ${{ github.token }}
required: true
label:
description: GitHub label conflict to labeling on the PR
default: conflict
required: true
limit:
description: Limitable Pull Request List
default: 100
comment:
description: The comment to place in the PR
default: |
:wave: Hi, @authorTarget!
We detected conflicts in your PR against the base branch :speak_no_evil:
You may want to sync :arrows_counterclockwise: your branch with upstream!
Ref: https://github.com/codeigniter4/CodeIgniter4/blob/develop/contributing/workflow.md#pushing-your-branch
runs:
using: composite
steps:
- shell: bash
env:
GITHUB_TOKEN: ${{ inputs.token }}
LABEL_GIT: ${{ inputs.label }}
LIMIT_GIT: ${{ inputs.limit }}
COMMENT_TEXT: ${{ inputs.comment }}
run: |
PR_LIST=$(gh pr list -L $LIMIT_GIT --json mergeable,url,labels,number,author)
# Escape double quotes and newlines
COMMENT_TEXT="$(echo "$COMMENT_TEXT" | sed 's/"/\\"/g' | awk '{printf "%s\\n", $0}')"
IFS=$'\n' # Set Internal Field Separator to newline to handle array elements
# Iterate through the PRs in PR_LIST
for pr in $(echo "$PR_LIST" | jq -c '.[]'); do
mergeable=$(echo "$pr" | jq -r '.mergeable')
authorVar=$(echo "$pr" | jq -r '.author.login')
labels=$(echo "$pr" | jq -c '.labels[].name' | tr -d '[]"')
PR_NUMBER=$(echo "$pr" | jq -r '.number')
url=$(echo "$pr" | jq -r '.url')
echo "$pr"
echo "[+] Repository on $GITHUB_REPOSITORY"
echo "[+] PR Number on $PR_NUMBER"
echo "[+] Status mergeable : $mergeable"
echo "[+] Status label : $labels"
echo "[+] Labeling Target on $LABEL_GIT"
echo "[+] Author Target on $authorVar"
echo ""
# MERGEABLE with label
if [ "$mergeable" == "MERGEABLE" ] && [[ "$labels" == *"$LABEL_GIT"* ]]; then
# Remove label
responseRemoveWithLabel=$(curl -X DELETE -H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/repos/$GITHUB_REPOSITORY/issues/$PR_NUMBER/labels/$LABEL_GIT")
echo "$responseRemoveWithLabel"
fi
# CONFLICTING with no label
if [ "$mergeable" == "CONFLICTING" ] && [[ ! "$labels" == *"$LABEL_GIT"* ]]; then
# Replace author
COMMENT_TEXT="$(echo $COMMENT_TEXT | sed -e "s/authorTarget/${authorVar}/g")"
echo -e "$COMMENT_TEXT"
# Add label
responseAddNoLabel=$(curl -X POST -H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-d "{\"labels\":[\"$LABEL_GIT\"]}" \
"https://api.github.com/repos/$GITHUB_REPOSITORY/issues/$PR_NUMBER/labels")
echo "$responseAddNoLabel"
# Add a comment
responseCommentAddNoLabel=$(curl -X POST -H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-d "{\"body\":\"$COMMENT_TEXT\"}" \
"https://api.github.com/repos/$GITHUB_REPOSITORY/issues/$PR_NUMBER/comments")
echo "$responseCommentAddNoLabel"
fi
done
echo "Keep running 🎉"