Skip to content

Commit

Permalink
Enhancement: Allow multiple task links in the PR description (#16)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
rafaeljusto and miguelbemartin authored Sep 19, 2020
1 parent 8a271ce commit bf14f3d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
39 changes: 21 additions & 18 deletions src/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,37 @@ main() {

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

if [ "$task_id" == "" ]; then
if [ "$task_ids_str" == "" ]; then
log::message "Task not found"
exit 0
fi

log::message "Task found with the id: $task_id"

export TEAMWORK_TASK_ID=$task_id

local -r event=$(github::get_event_name)
local -r action=$(github::get_action)

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

if [ "$event" == "pull_request" ] && [ "$action" == "opened" ]; then
teamwork::pull_request_opened
elif [ "$event" == "pull_request" ] && [ "$action" == "closed" ]; then
teamwork::pull_request_closed
elif [ "$event" == "pull_request_review" ] && [ "$action" == "submitted" ]; then
teamwork::pull_request_review_submitted
elif [ "$event" == "pull_request_review" ] && [ "$action" == "dismissed" ]; then
teamwork::pull_request_review_dismissed
else
log::message "Operation not allowed"
exit 0
fi
IFS=',' read -r -a task_ids <<< "$task_ids_str"
for task_id in "${task_ids[@]}"; do
log::message "Task found with the id: $task_id"

export TEAMWORK_TASK_ID=$task_id

if [ "$event" == "pull_request" ] && [ "$action" == "opened" ]; then
teamwork::pull_request_opened
elif [ "$event" == "pull_request" ] && [ "$action" == "closed" ]; then
teamwork::pull_request_closed
elif [ "$event" == "pull_request_review" ] && [ "$action" == "submitted" ]; then
teamwork::pull_request_review_submitted
elif [ "$event" == "pull_request_review" ] && [ "$action" == "dismissed" ]; then
teamwork::pull_request_review_dismissed
else
log::message "Operation not allowed"
exit 0
fi
done

exit $?
}
16 changes: 11 additions & 5 deletions src/teamwork.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
#!/usr/bin/env bash

teamwork::get_task_id_from_body() {
local -r body=$1
local body=$1
local task_ids=()

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

echo "$task_id"
local task_ids_str
task_ids_str=$(printf ",%s" "${task_ids[@]}")
task_ids_str=${task_ids_str:1} # remove initial comma
echo "$task_ids_str"
}

teamwork::add_comment() {
Expand Down

0 comments on commit bf14f3d

Please sign in to comment.