Skip to content

Commit a2e9e5d

Browse files
authored
Merge pull request #3 from sgowroji/master
This is a feature request adds option for issues and also will remove label which are not required
2 parents e258aeb + 2f844bb commit a2e9e5d

File tree

4 files changed

+103
-2
lines changed

4 files changed

+103
-2
lines changed

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
FROM alpine:3.13.4
22

3-
LABEL "com.github.actions.name"="Add Label when a pull request require attention."
4-
LABEL "com.github.actions.description"="A GitHub action to add an attention label on an open pull-request after certain days."
3+
LABEL "com.github.actions.name"="Add Label when a pull request or issue require attention."
4+
LABEL "com.github.actions.description"="A GitHub action to add an attention label on an open pull-request or issue after certain days."
55
LABEL "com.github.actions.icon"="tag"
66
LABEL "com.github.actions.color"="blue"
77

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ jobs:
3131
ADD_LABEL: "need-review"
3232
AFTER_DAYS: 4
3333
SKIP_LABELS: "approved,wip"
34+
REMOVE_LABELS: "helpwanted,untriaged"
3435
```
3536

3637
#### GitHub action workflow execution

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ inputs:
1212
SKIP_LABELS:
1313
description: 'The comma separated labels string. If an open pull-request has one of those label then this action will skip adding the attention label.'
1414
default: 'work-in-progress,wip'
15+
REMOVE_LABEL:
16+
description: 'Remove the previous dependent label'
1517
SKIP_DRAFTS:
1618
description: 'Wheter to skip draft PRs or not. Defaults to include them'
1719
default: true
20+
1821
runs:
1922
using: 'docker'
2023
image: 'Dockerfile'

entrypoint.sh

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ if [[ -z "$SKIP_LABELS" ]]; then
1616
SKIP_LABELS="work-in-progress,wip"
1717
fi
1818

19+
if [[ -z "$REMOVE_LABEL" ]]; then
20+
echo "Setting the default REMOVE_LABEL variable value."
21+
REMOVE_LABEL="untriaged"
22+
fi
23+
1924
if [[ -z "$AFTER_DAYS" ]]; then
2025
echo "Setting the default AFTER_DAYS variable value."
2126
AFTER_DAYS=3
@@ -72,11 +77,25 @@ for PULL_REQUEST in $PULL_REQUESTS; do
7277
continue
7378
fi
7479

80+
if [[ -n "$REMOVE_LABEL" ]]; then
81+
REMOVE_LABEL_NAME_EXIST=$(echo "$PULL_REQUEST_INFO" | jq --raw-output --arg REMOVE_LABEL "$REMOVE_LABEL" '.labels | .[] | select(.name==$REMOVE_LABEL)')
82+
83+
if [[ -n "$REMOVE_LABEL_NAME_EXIST" ]]; then
84+
echo "Removing, Pull request label: $REMOVE_LABEL, Pull request NUMBER: $PULL_REQUEST_NUMBER"
85+
curl -sSL \
86+
-H "$AUTH_HEADER" \
87+
-H "$API_HEADER" \
88+
-X DELETE \
89+
"$URI/repos/$GITHUB_REPOSITORY/issues/$PULL_REQUEST_NUMBER/labels/$REMOVE_LABEL"
90+
else
91+
echo "Proceeding, This pull request doesn't have the label to remove: $REMOVE_LABEL"
92+
7593
if [[ $SKIP_DRAFTS != "false" ]]; then
7694
IS_A_DRAFT=$(echo "$PULL_REQUEST_INFO" | jq --raw-output '.draft')
7795
if [[ $IS_A_DRAFT == "true" ]]; then
7896
echo "Ignoring, this pull request because it's a DRAFT"
7997
continue
98+
8099
fi
81100
fi
82101

@@ -103,4 +122,82 @@ for PULL_REQUEST in $PULL_REQUESTS; do
103122
-d "{\"labels\":[\"$ADD_LABEL\"]}" \
104123
"$URI/repos/$GITHUB_REPOSITORY/issues/$PULL_REQUEST_NUMBER/labels"
105124

125+
echo "Fetching, Open issues"
126+
OPEN_ISSUES=$(
127+
curl -XGET -fsSL \
128+
-H "$AUTH_HEADER" \
129+
-H "$API_HEADER" \
130+
"$URI/repos/$GITHUB_REPOSITORY/issues?state=open"
131+
)
132+
133+
ISSUES=$(echo "$OPEN_ISSUES" | jq --raw-output '.[] | {number: .number, created_at: .created_at, labels: .labels} | @base64')
134+
135+
for ISSUE in $ISSUES; do
136+
ISSUE_INFO="$(echo "$ISSUE" | base64 -d)"
137+
138+
echo "Validating issue INFO: $ISSUE_INFO"
139+
140+
ADD_LABEL_NAME_EXIST=$(echo "$ISSUE_INFO" | jq --raw-output --arg ADD_LABEL "$ADD_LABEL" '.labels | .[] | select(.name==$ADD_LABEL)')
141+
142+
if [[ -z "$ADD_LABEL_NAME_EXIST" ]]; then
143+
echo "Proceeding, This issue doesn't have review attention LABEL: $ADD_LABEL"
144+
else
145+
echo "Ignoring, This issue already have review attention LABEL: $ADD_LABEL"
146+
continue
147+
fi
148+
149+
IS_SKIP_LABEL_NAME_EXIST=false
150+
151+
for SKIP_LABEL in $(echo $SKIP_LABELS | sed "s/,/ /g"); do
152+
SKIP_LABEL_NAME_EXIST=$(echo "$ISSUE_INFO" | jq --raw-output --arg SKIP_LABEL "$SKIP_LABEL" '.labels | .[] | select(.name==$SKIP_LABEL)')
153+
154+
if [[ -z "$SKIP_LABEL_NAME_EXIST" ]]; then
155+
echo "Proceeding, This issue doesn't have an skip LABEL: $SKIP_LABEL"
156+
else
157+
echo "Ignoring, This issue have an skip LABEL: $SKIP_LABEL"
158+
IS_SKIP_LABEL_NAME_EXIST=true
159+
break
160+
fi
161+
done
162+
163+
if [[ "$IS_SKIP_LABEL_NAME_EXIST" == "true" ]]; then
164+
continue
165+
fi
166+
167+
if [[ -n "$REMOVE_LABEL" ]]; then
168+
REMOVE_LABEL_NAME_EXIST=$(echo "$ISSUE_INFO" | jq --raw-output --arg REMOVE_LABEL "$REMOVE_LABEL" '.labels | .[] | select(.name==$REMOVE_LABEL)')
169+
170+
if [[ -n "$REMOVE_LABEL_NAME_EXIST" ]]; then
171+
echo "Removing, Issue label: $REMOVE_LABEL, Issue NUMBER: "
172+
curl -sSL \
173+
-H "$AUTH_HEADER" \
174+
-H "$API_HEADER" \
175+
-X DELETE \
176+
"$URI/repos/$GITHUB_REPOSITORY/issues/$ISSUE_NUMBER/labels/$REMOVE_LABEL"
177+
else
178+
echo "Proceeding, This Issue doesn't have the label to remove: $REMOVE_LABEL"
179+
fi
180+
fi
181+
182+
CREATED_AT=$(echo "$ISSUE_INFO" | jq --raw-output '.created_at')
183+
CREATED_AT_EPOCH=$(date -d $CREATED_AT +%s)
184+
CURRENT_EPOCH=$(date +%s)
185+
STALE_TIME_INTERVAL=$(($AFTER_DAYS * 24 * 60 * 60))
186+
187+
if [[ $(($CURRENT_EPOCH - $CREATED_AT_EPOCH)) -ge $STALE_TIME_INTERVAL ]]; then
188+
echo "Proceeding, This issue is CREATED_AT: $CREATED_AT, is greater than $AFTER_DAYS DAYS."
189+
else
190+
echo "Ignoring, This issue is CREATED_AT: $CREATED_AT, is less than $
191+
192+
ISSUE_NUMBER=$(echo "$ISSUE_INFO" | jq --raw-output '.number')
193+
echo "Adding, Issue review attention LABEL: $ADD_LABEL, issue NUMBER: $ISSUE_NUMBER"
194+
195+
curl -sSL \
196+
-H "$AUTH_HEADER" \
197+
-H "$API_HEADER" \
198+
-X POST \
199+
-H "Content-Type: application/json" \
200+
-d "{\"labels\":[\"$ADD_LABEL\"]}" \
201+
"$URI/repos/$GITHUB_REPOSITORY/issues/$ISSUE_NUMBER/labels"
202+
106203
done

0 commit comments

Comments
 (0)