Skip to content

Commit 7806fdf

Browse files
authored
Merge pull request #45 from Teamwork/feature/board-columns
Adding support for Column changes on Tasks
2 parents 57a23b0 + e5c4a67 commit 7806fdf

File tree

5 files changed

+102
-0
lines changed

5 files changed

+102
-0
lines changed

.github/workflows/test.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ jobs:
1818
TEAMWORK_URI: "localhost"
1919
TEAMWORK_API_TOKEN: "test_api_token"
2020
AUTOMATIC_TAGGING: true
21+
BOARD_COLUMN_OPENED: "PR Open"
22+
BOARD_COLUMN_MERGED: "Ready to Test"
23+
BOARD_COLUMN_CLOSED: "Rejected"

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ jobs:
4848
TEAMWORK_URI: ${{ secrets.TEAMWORK_URI }}
4949
TEAMWORK_API_TOKEN: ${{ secrets.TEAMWORK_API_TOKEN }}
5050
AUTOMATIC_TAGGING: false
51+
BOARD_COLUMN_OPENED: 'PR Open'
52+
BOARD_COLUMN_MERGED: 'Ready to Test'
53+
BOARD_COLUMN_CLOSED: 'Rejected'
5154

5255
```
5356

@@ -66,6 +69,12 @@ Tags are added automatically on the task if you are have the option `AUTOMATIC_T
6669
- A PR is merged: tags `PR Open` and `PR Approved` removed, tag `PR merged` added
6770
- A PR is closed: tags `PR Open` and `PR Approved` removed
6871

72+
You may also specify columns you'd like the task to be moved to on every stage of the PR:
73+
- `BOARD_COLUMN_OPENED`: The case-sensitive column name of the column you'd like the task to be moved to once the PR has been opened
74+
- `BOARD_COLUMN_MERGED`: The case-sensitive column name of the column you'd like the task to be moved to once the PR has been merged
75+
- `BOARD_COLUMN_CLOSED`: The case-sensitive column name of the column you'd like the task to be moved to if the PR was closed without being merged
76+
77+
The column names will be checked against all board columns in the task's project, this will be using a `contains()` method so you may specify part of the name instead of the full name, however this `contains()` check is case-sensitive. The first matching column will be used.
6978

7079
## Contributing
7180
* Open a PR: https://github.com/Teamwork/github-sync/pulls

action.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ inputs:
1616
AUTOMATIC_TAGGING:
1717
description: 'Do you want to enable automatic tagging: true/false'
1818
required: false
19+
BOARD_COLUMN_OPENED:
20+
description: 'The case-sensitive column name of the column you would like the task to be moved to once the PR has been opened'
21+
required: false
22+
default: ''
23+
BOARD_COLUMN_MERGED:
24+
description: 'The case-sensitive column name of the column you would like the task to be moved to once the PR has been merged'
25+
required: false
26+
default: ''
27+
BOARD_COLUMN_CLOSED:
28+
description: 'The case-sensitive column name of the column you would like the task to be moved to if the PR was closed without being merged'
29+
required: false
30+
default: ''
1931
runs:
2032
using: 'docker'
2133
image: 'Dockerfile'
@@ -24,3 +36,6 @@ runs:
2436
- ${{ inputs.TEAMWORK_URI }}
2537
- ${{ inputs.TEAMWORK_API_TOKEN }}
2638
- ${{ inputs.AUTOMATIC_TAGGING }}
39+
- ${{ inputs.BOARD_COLUMN_OPENED }}
40+
- ${{ inputs.BOARD_COLUMN_MERGED }}
41+
- ${{ inputs.BOARD_COLUMN_CLOSED }}

src/main.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ main() {
1717
export TEAMWORK_URI="$2"
1818
export TEAMWORK_API_TOKEN="$3"
1919
export AUTOMATIC_TAGGING="$4"
20+
export BOARD_COLUMN_OPENED="$5"
21+
export BOARD_COLUMN_MERGED="$6"
22+
export BOARD_COLUMN_CLOSED="$7"
2023

2124
env::set_environment
2225

@@ -39,6 +42,8 @@ main() {
3942
log::message "Task found with the id: $task_id"
4043

4144
export TEAMWORK_TASK_ID=$task_id
45+
local -r project_id="$(teamwork::get_project_id_from_task "$task_id")"
46+
export TEAMWORK_PROJECT_ID=$project_id
4247

4348
if [ "$event" == "pull_request" ] && [ "$action" == "opened" ]; then
4449
teamwork::pull_request_opened

src/teamwork.sh

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,73 @@ teamwork::get_task_id_from_body() {
1616
echo "$task_ids_str"
1717
}
1818

19+
teamwork::get_project_id_from_task() {
20+
local -r task_id=$1
21+
22+
if [ "$ENV" == "test" ]; then
23+
echo "$task_id"
24+
return
25+
fi
26+
27+
response=$(
28+
curl "$TEAMWORK_URI/projects/api/v1/tasks/$task_id.json" -u "$TEAMWORK_API_TOKEN"':' |\
29+
jq -r '.["todo-item"]["project-id"]'
30+
)
31+
echo "$response"
32+
}
33+
34+
teamwork::get_matching_board_column_id() {
35+
local -r column_name=$1
36+
37+
if [ -z "$column_name" ]; then
38+
return
39+
fi
40+
41+
if [ "$ENV" == "test" ]; then
42+
echo "$TEAMWORK_PROJECT_ID"
43+
return
44+
fi
45+
46+
response=$(
47+
curl "$TEAMWORK_URI/projects/$TEAMWORK_PROJECT_ID/boards/columns.json" -u "$TEAMWORK_API_TOKEN"':' |\
48+
jq -r --arg column_name "$column_name" '[.columns[] | select(.name | contains($column_name))] | map(.id)[0]'
49+
)
50+
51+
if [ "$response" = "null" ]; then
52+
return
53+
fi
54+
55+
echo "$response"
56+
}
57+
58+
teamwork::move_task_to_column() {
59+
local -r task_id=$TEAMWORK_TASK_ID
60+
local -r column_name=$1
61+
62+
if [ -z "$column_name" ]; then
63+
log::message "No column name provided"
64+
return
65+
fi
66+
67+
local -r column_id=$(teamwork::get_matching_board_column_id "$column_name")
68+
if [ -z "$column_id" ]; then
69+
log::message "Failed to find a matching board column for '$column_name'"
70+
return
71+
fi
72+
73+
if [ "$ENV" == "test" ]; then
74+
log::message "Test - Simulate request. Task ID: $TEAMWORK_TASK_ID - Project ID: $TEAMWORK_PROJECT_ID - Column ID: $column_id"
75+
return
76+
fi
77+
78+
response=$(curl -X "PUT" "$TEAMWORK_URI/tasks/$TEAMWORK_TASK_ID.json" \
79+
-u "$TEAMWORK_API_TOKEN"':' \
80+
-H 'Content-Type: application/json; charset=utf-8' \
81+
-d "{ \"todo-item\": { \"columnId\": $column_id } }" )
82+
83+
log::message "$response"
84+
}
85+
1986
teamwork::add_comment() {
2087
local -r body=$1
2188

@@ -94,6 +161,7 @@ ${pr_body}
94161
"
95162

96163
teamwork::add_tag "PR Open"
164+
teamwork::move_task_to_column "$BOARD_COLUMN_OPENED"
97165
}
98166

99167
teamwork::pull_request_closed() {
@@ -110,13 +178,15 @@ teamwork::pull_request_closed() {
110178
teamwork::add_tag "PR Merged"
111179
teamwork::remove_tag "PR Open"
112180
teamwork::remove_tag "PR Approved"
181+
teamwork::move_task_to_column "$BOARD_COLUMN_MERGED"
113182
else
114183
teamwork::add_comment "
115184
**$user** closed a PR without merging: **$pr_title**
116185
[$pr_url]($pr_url)
117186
"
118187
teamwork::remove_tag "PR Open"
119188
teamwork::remove_tag "PR Approved"
189+
teamwork::move_task_to_column "$BOARD_COLUMN_CLOSED"
120190
fi
121191
}
122192

0 commit comments

Comments
 (0)