WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit bf14f3d

Browse files
Enhancement: Allow multiple task links in the PR description (#16)
* Enhancement: Allow multiple task links in the PR description Resolves #2 * Fix: Warnings detected by the linter * Fix: Modifying read-only local variable while extracting task IDs Issue detected by @miguelbemartin * Enhancement: Avoid repeated log messages while iterating task IDs Suggestion from @miguelbemartin Co-authored-by: Miguel Ángel Martín <[email protected]>
1 parent 8a271ce commit bf14f3d

File tree

2 files changed

+32
-23
lines changed

2 files changed

+32
-23
lines changed

src/main.sh

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,34 +22,37 @@ main() {
2222

2323
# Check if there is a task link in the PR
2424
local -r pr_body=$(github::get_pr_body)
25-
local -r task_id=$(teamwork::get_task_id_from_body "$pr_body" )
25+
local -r task_ids_str=$(teamwork::get_task_id_from_body "$pr_body" )
2626

27-
if [ "$task_id" == "" ]; then
27+
if [ "$task_ids_str" == "" ]; then
2828
log::message "Task not found"
2929
exit 0
3030
fi
3131

32-
log::message "Task found with the id: $task_id"
33-
34-
export TEAMWORK_TASK_ID=$task_id
35-
3632
local -r event=$(github::get_event_name)
3733
local -r action=$(github::get_action)
3834

3935
log::message "Event: $event - Action: $action"
4036

41-
if [ "$event" == "pull_request" ] && [ "$action" == "opened" ]; then
42-
teamwork::pull_request_opened
43-
elif [ "$event" == "pull_request" ] && [ "$action" == "closed" ]; then
44-
teamwork::pull_request_closed
45-
elif [ "$event" == "pull_request_review" ] && [ "$action" == "submitted" ]; then
46-
teamwork::pull_request_review_submitted
47-
elif [ "$event" == "pull_request_review" ] && [ "$action" == "dismissed" ]; then
48-
teamwork::pull_request_review_dismissed
49-
else
50-
log::message "Operation not allowed"
51-
exit 0
52-
fi
37+
IFS=',' read -r -a task_ids <<< "$task_ids_str"
38+
for task_id in "${task_ids[@]}"; do
39+
log::message "Task found with the id: $task_id"
40+
41+
export TEAMWORK_TASK_ID=$task_id
42+
43+
if [ "$event" == "pull_request" ] && [ "$action" == "opened" ]; then
44+
teamwork::pull_request_opened
45+
elif [ "$event" == "pull_request" ] && [ "$action" == "closed" ]; then
46+
teamwork::pull_request_closed
47+
elif [ "$event" == "pull_request_review" ] && [ "$action" == "submitted" ]; then
48+
teamwork::pull_request_review_submitted
49+
elif [ "$event" == "pull_request_review" ] && [ "$action" == "dismissed" ]; then
50+
teamwork::pull_request_review_dismissed
51+
else
52+
log::message "Operation not allowed"
53+
exit 0
54+
fi
55+
done
5356

5457
exit $?
5558
}

src/teamwork.sh

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
#!/usr/bin/env bash
22

33
teamwork::get_task_id_from_body() {
4-
local -r body=$1
4+
local body=$1
5+
local task_ids=()
56

6-
pat='tasks\/[0-9]{1,}'
7-
task=$(echo "$body" | grep -Eo "$pat")
8-
task_id=$(echo "$task" | tr -cd '[:digit:]')
7+
pat='tasks\/([0-9]{1,})'
8+
while [[ $body =~ $pat ]]; do
9+
task_ids+=( "${BASH_REMATCH[1]}" )
10+
body=${body#*"${BASH_REMATCH[1]}"}
11+
done
912

10-
echo "$task_id"
13+
local task_ids_str
14+
task_ids_str=$(printf ",%s" "${task_ids[@]}")
15+
task_ids_str=${task_ids_str:1} # remove initial comma
16+
echo "$task_ids_str"
1117
}
1218

1319
teamwork::add_comment() {

0 commit comments

Comments
 (0)