-
Notifications
You must be signed in to change notification settings - Fork 7
/
on_pull_request.sh
executable file
·91 lines (72 loc) · 2.41 KB
/
on_pull_request.sh
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
#!/bin/bash
on_pull_request_close_del_branch_if_need() {
PR_ACTION=$(jq -r .action < "${GITHUB_EVENT_PATH}")
if [[ "${PR_ACTION}" != "closed" ]];then
echo "Action is not closed, pass"
return 0
fi
PR_HEAD_REF=$(jq -r .pull_request.head.ref < "${GITHUB_EVENT_PATH}")
if [[ ! "${PR_HEAD_REF}" =~ 'pr_' ]];then
echo "Not a valid pull request branch, pass"
return 0
fi
PR_HEAD_BRANCH_URL=$(jq -r .pull_request.head.repo.git_refs_url < "${GITHUB_EVENT_PATH}" |sed "s@{.*}@/heads/$PR_HEAD_REF@g")
curl \
--fail \
-X DELETE \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
"${PR_HEAD_BRANCH_URL}" || echo "May be has deleted"
}
on_pull_request_open_edit_auto_label_it() {
PR_ACTION=$(jq -r .action < "${GITHUB_EVENT_PATH}")
if [[ "${PR_ACTION}" != "edited" && "${PR_ACTION}" != "opened" ]];then
echo "Is nether edited nor opened action, pass"
return 0
fi
PR_TITLE=$(jq -r .pull_request.title < "${GITHUB_EVENT_PATH}")
PR_ISSUE_URL=$(jq -r .pull_request.issue_url < "${GITHUB_EVENT_PATH}")
label=""
if [[ "${PR_TITLE}" =~ "fix" ]];then
label="🐛 Bug"
elif [[ "${PR_TITLE}" =~ "feat" ]];then
label="⭐️ Feature Request"
elif [[ "${PR_TITLE}" =~ "perf" || ${PR_TITLE} =~ "refactor" ]];then
label="🚀 Performance"
fi
if [[ -z "${label}" ]];then
return 0
fi
data="{\"labels\":[\"${label}\"]}"
curl \
--fail \
-X POST \
--data "$data" \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
"${PR_ISSUE_URL}" > /dev/null
}
on_pull_request_open_add_reviewer() {
PR_ACTION=$(jq -r .action < "${GITHUB_EVENT_PATH}")
if [[ "${PR_ACTION}" != "opened" ]];then
echo "不是新建issue, 跳过设置reviewer"
return 0
fi
PR_URL=$(jq -r .pull_request.url < "${GITHUB_EVENT_PATH}")
PR_REVIEWER_URL="${PR_URL}/requested_reviewers"
data='{"team_reviewers":["developers"]}'
curl \
--fail \
-X POST \
--data ${data} \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
"${PR_REVIEWER_URL}" > /dev/null
}
if [[ "${GITHUB_EVENT_NAME}" != "pull_request" ]];then
echo "Is not pull request event, exit"
exit 0
fi
on_pull_request_close_del_branch_if_need
on_pull_request_open_edit_auto_label_it
on_pull_request_open_add_reviewer