-
Notifications
You must be signed in to change notification settings - Fork 9
141 lines (123 loc) Β· 4.8 KB
/
kube-actions.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
name: PR Actions
on:
issue_comment:
types: [created]
pull_request:
types: [labeled]
push:
branches:
- '**'
env:
GH_TOKEN: ${{ secrets.AGENT_TOKEN }}
jobs:
handle-comments:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Remove lgtm label on new push
if: github.event_name == 'push'
run: |
PR_NUMBER=$(jq -r '.issue.number' $GITHUB_EVENT_PATH)
if [ -z "$PR_NUMBER" ]; then
echo "No PR number found in push event context."
exit 0
fi
if gh pr view $PR_NUMBER --json labels --jq '.labels[].name' | grep -q "lgtm"; then
gh pr edit $pr --remove-label lgtm
echo "Removed 'lgtm' label due to new push."
fi
exit 0
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.x'
- name: Install yq
run: pip install yq
- name: Handle PR Comments
run: |
COMMENT_BODY=$(jq -r '.comment.body' $GITHUB_EVENT_PATH)
PR_NUMBER=$(jq -r '.issue.number' $GITHUB_EVENT_PATH)
COMMENT_USER=$(jq -r '.comment.user.login' $GITHUB_EVENT_PATH)
while IFS= read -r COMMENT_LINE; do
COMMENT_LINE=$(echo "$COMMENT_LINE" | sed 's/^[ \t]*//;s/[ \t]*$//' | tr -d '\n' | awk '{$1=$1};1')
LABEL=$(echo "$COMMENT_LINE" | awk '{print $2}')
LABEL=$(echo "$LABEL" | sed 's/^[ \t]*//;s/[ \t]*$//' | tr -d '\n' | awk '{$1=$1};1')
# Handle lgtm command
if [[ "$COMMENT_LINE" =~ ^/lgtm ]]; then
if [[ "$LABEL" =~ ^cancel ]]; then
gh pr edit $PR_NUMBER --remove-label lgtm
echo "Remove label: lgtm"
else
gh pr edit $PR_NUMBER --add-label lgtm
echo "Add label: lgtm"
fi
fi
# Handle approved command
if [[ "$COMMENT_LINE" =~ ^/approve ]]; then
if yq ".approvers[] | select(. == \"$COMMENT_USER\") " OWNERS | grep -q "$COMMENT_USER"; then
if [[ "$LABEL" =~ ^cancel ]]; then
gh pr edit $PR_NUMBER --remove-label approved
echo "Remove label: approved"
else
gh pr edit $PR_NUMBER --add-label approved
echo "Add label: approved"
fi
else
gh pr comment $PR_NUMBER --body "Sorry, @$COMMENT_USER is not authorized to approve/unapprove this PR."
echo "User $COMMENT_USER is not authorized to approve/unapprove this PR"
fi
fi
# Handle hold command
if [[ "$COMMENT_LINE" =~ ^/hold ]]; then
if [[ "$LABEL" =~ ^cancel ]]; then
gh pr edit $PR_NUMBER --remove-label do-not-merge/hold
echo "Remove label: do-not-merge/hold"
else
gh pr edit $PR_NUMBER --add-label do-not-merge/hold
echo "Add label: do-not-merge/hold"
fi
# Handle kind command
elif [[ "$COMMENT_LINE" =~ ^/kind ]]; then
if [[ "$LABEL" =~ ^lgtm || "$LABEL" =~ ^approve ]]; then
echo "The label '$LABEL' cannot be added using /kind."
else
gh pr edit $PR_NUMBER --add-label $LABEL
echo "Add label: '$LABEL'"
fi
elif [[ "$COMMENT_LINE" =~ ^/remove-kind ]]; then
if [[ "$LABEL" =~ ^lgtm || "$LABEL" =~ ^approve ]]; then
echo "The label '$LABEL' cannot be removed using /remove-kind."
else
gh pr edit $PR_NUMBER --remove-label $LABEL
echo "Remove label: '$LABEL'"
fi
# Handle assignment
elif [[ "$COMMENT_LINE" =~ ^/assign ]]; then
USERNAME=$(echo "$LABEL" | sed 's/@//')
if [[ -n "$USERNAME" ]]; then
gh pr edit $PR_NUMBER --add-assignee "$USERNAME"
echo "Assign to $USERNAME"
else
echo "No username provided for assignment."
fi
elif [[ "$COMMENT_LINE" =~ ^/unassign ]]; then
USERNAME=$(echo "$LABEL" | sed 's/@//')
if [[ -n "$USERNAME" ]]; then
gh pr edit $PR_NUMBER --remove-assignee "$USERNAME"
echo "Unassign to $USERNAME"
else
echo "No username provided for unassignment."
fi
else
echo "$COMMENT_LINE is not supported"
fi
done <<< "$COMMENT_BODY"
# Merge PR if LGTMed and approved and do not hold.
LABELS=$(gh pr view $PR_NUMBER --json labels --jq '.labels[].name')
if [[ "$LABELS" == *"lgtm"* && "$LABELS" == *"approved"* && "$LABELS" != *"do-not-merge/hold"* ]]; then
gh pr merge $PR_NUMBER --merge
echo "Merge!"
else
echo "Not ready to merge"
fi