From 4c1ed225c6dfeaae819e1417ebe5b460b1f739ef Mon Sep 17 00:00:00 2001 From: Rodrigo Kreutz <8869678+rkreutz@users.noreply.github.com> Date: Tue, 20 Apr 2021 11:58:07 +0100 Subject: [PATCH 1/3] Adding support for setting board columns on tasks --- .github/workflows/test.yml | 3 ++ README.md | 10 ++++++ action.yml | 15 ++++++++ src/main.sh | 4 +++ src/teamwork.sh | 71 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 103 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f4ac80ca..1fecafe5 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -18,3 +18,6 @@ jobs: TEAMWORK_URI: "localhost" TEAMWORK_API_TOKEN: "test_api_token" AUTOMATIC_TAGGING: true + BOARD_COLUMN_OPENED: "PR Open" + BOARD_COLUMN_MERGED: "Ready to Test" + BOARD_COLUMN_CLOSED: "Rejected" diff --git a/README.md b/README.md index 504b85bb..79d3e512 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,9 @@ jobs: TEAMWORK_URI: ${{ secrets.TEAMWORK_URI }} TEAMWORK_API_TOKEN: ${{ secrets.TEAMWORK_API_TOKEN }} AUTOMATIC_TAGGING: false + BOARD_COLUMN_OPENED: 'PR Open' + BOARD_COLUMN_MERGED: 'Ready to Test' + BOARD_COLUMN_CLOSED: 'Rejected' ``` @@ -66,6 +69,13 @@ Tags are added automatically on the task if you are have the option `AUTOMATIC_T - A PR is merged: tags `PR Open` and `PR Approved` removed, tag `PR merged` added - A PR is closed: tags `PR Open` and `PR Approved` removed +You may also specify columns you'd like the task to be moved to on every stage of the PR: +- `BOARD_COLUMN_OPENED`: The case-sensiteve column name of the column you'd like the task to be moved to once the PR has been opened +- `BOARD_COLUMN_MERGED`: The case-sensiteve column name of the column you'd like the task to be moved to once the PR has been merged +- `BOARD_COLUMN_CLOSED`: The case-sensiteve column name of the column you'd like the task to be moved to if the PR was closed without being merged + +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. + ## Contributing * Open a PR: https://github.com/Teamwork/github-sync/pulls diff --git a/action.yml b/action.yml index c9889170..df2980b8 100644 --- a/action.yml +++ b/action.yml @@ -16,6 +16,18 @@ inputs: AUTOMATIC_TAGGING: description: 'Do you want to enable automatic tagging: true/false' required: false + BOARD_COLUMN_OPENED: + description: 'The case-sensiteve column name of the column you'd like the task to be moved to once the PR has been opened' + required: false + default: '' + BOARD_COLUMN_MERGED: + description: 'The case-sensiteve column name of the column you'd like the task to be moved to once the PR has been merged' + required: false + default: '' + BOARD_COLUMN_CLOSED: + description: 'The case-sensiteve column name of the column you'd like the task to be moved to if the PR was closed without being merged' + required: false + default: '' runs: using: 'docker' image: 'Dockerfile' @@ -24,3 +36,6 @@ runs: - ${{ inputs.TEAMWORK_URI }} - ${{ inputs.TEAMWORK_API_TOKEN }} - ${{ inputs.AUTOMATIC_TAGGING }} + - ${{ inputs.BOARD_COLUMN_OPENED }} + - ${{ inputs.BOARD_COLUMN_MERGED }} + - ${{ inputs.BOARD_COLUMN_CLOSED }} diff --git a/src/main.sh b/src/main.sh index e05b8b0f..c943f3ca 100644 --- a/src/main.sh +++ b/src/main.sh @@ -17,6 +17,9 @@ main() { export TEAMWORK_URI="$2" export TEAMWORK_API_TOKEN="$3" export AUTOMATIC_TAGGING="$4" + export BOARD_COLUMN_OPENED="$5" + export BOARD_COLUMN_MERGED="$6" + export BOARD_COLUMN_CLOSED="$7" env::set_environment @@ -39,6 +42,7 @@ main() { log::message "Task found with the id: $task_id" export TEAMWORK_TASK_ID=$task_id + export TEAMWORK_PROJECT_ID="$(teamwork::get_project_id_from_task "$task_id")" if [ "$event" == "pull_request" ] && [ "$action" == "opened" ]; then teamwork::pull_request_opened diff --git a/src/teamwork.sh b/src/teamwork.sh index d35d1a2e..f0635f5d 100644 --- a/src/teamwork.sh +++ b/src/teamwork.sh @@ -16,6 +16,74 @@ teamwork::get_task_id_from_body() { echo "$task_ids_str" } +teamwork::get_project_id_from_task() { + local -r task_id=$1 + + if [ "$ENV" == "test" ]; then + echo "$task_id" + return + fi + + response=$( + curl "$TEAMWORK_URI/projects/api/v1/tasks/$task_id.json" -u "$TEAMWORK_API_TOKEN"':' |\ + jq -r '.["todo-item"]["project-id"]' + ) + echo "$response" +} + +teamwork::get_matching_board_column_id() { + local -r column_name=$1 + + if [ -z "$column_name" ]; then + return + fi + + if [ "$ENV" == "test" ]; then + echo "$TEAMWORK_PROJECT_ID" + return + fi + + response=$( + curl "$TEAMWORK_URI/projects/$TEAMWORK_PROJECT_ID/boards/columns.json" -u "$TEAMWORK_API_TOKEN"':' |\ + jq -r --arg column_name "$column_name" '[.columns[] | select(.name | contains($column_name))] | map(.id)[0]' + ) + + if [ "$response" = "null" ]; then + return + fi + + echo "$response" +} + +teamwork::move_task_to_column() { + local -r task_id=$TEAMWORK_TASK_ID + local -r column_name=$1 + + if [ -z "$column_name" ]; then + log::message "No column name provided" + return + fi + + local -r column_id=$(teamwork::get_matching_board_column_id $column_name) + + if [ -z "$column_id" ]; then + log::message "Failed to find a matching board column for '$column_name'" + return + fi + + if [ "$ENV" == "test" ]; then + log::message "Test - Simulate request. Task ID: $TEAMWORK_TASK_ID - Project ID: $TEAMWORK_PROJECT_ID - Column ID: $column_id" + return + fi + + response=$(curl -X "PUT" "$TEAMWORK_URI/tasks/$TEAMWORK_TASK_ID.json" \ + -u "$TEAMWORK_API_TOKEN"':' \ + -H 'Content-Type: application/json; charset=utf-8' \ + -d "{ \"todo-item\": { \"columnId\": $column_id } }" ) + + log::message "$response" +} + teamwork::add_comment() { local -r body=$1 @@ -90,6 +158,7 @@ ${pr_body//###/####} " teamwork::add_tag "PR Open" + teamwork::move_task_to_column "$BOARD_COLUMN_OPENED" } teamwork::pull_request_closed() { @@ -106,6 +175,7 @@ teamwork::pull_request_closed() { teamwork::add_tag "PR Merged" teamwork::remove_tag "PR Open" teamwork::remove_tag "PR Approved" + teamwork::move_task_to_column "$BOARD_COLUMN_MERGED" else teamwork::add_comment " **$user** closed a PR without merging: **$pr_title** @@ -113,6 +183,7 @@ teamwork::pull_request_closed() { " teamwork::remove_tag "PR Open" teamwork::remove_tag "PR Approved" + teamwork::move_task_to_column "$BOARD_COLUMN_CLOSED" fi } From c0640d0f7bfde0c4036411f8e947e0869b80fc8f Mon Sep 17 00:00:00 2001 From: Rodrigo Kreutz <8869678+rkreutz@users.noreply.github.com> Date: Tue, 20 Apr 2021 12:08:02 +0100 Subject: [PATCH 2/3] Removing default values --- README.md | 6 +++--- action.yml | 3 --- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 79d3e512..e5bcba70 100644 --- a/README.md +++ b/README.md @@ -48,9 +48,9 @@ jobs: TEAMWORK_URI: ${{ secrets.TEAMWORK_URI }} TEAMWORK_API_TOKEN: ${{ secrets.TEAMWORK_API_TOKEN }} AUTOMATIC_TAGGING: false - BOARD_COLUMN_OPENED: 'PR Open' - BOARD_COLUMN_MERGED: 'Ready to Test' - BOARD_COLUMN_CLOSED: 'Rejected' + BOARD_COLUMN_OPENED: "PR Open" + BOARD_COLUMN_MERGED: "Ready to Test" + BOARD_COLUMN_CLOSED: "Rejected" ``` diff --git a/action.yml b/action.yml index df2980b8..e5cd1374 100644 --- a/action.yml +++ b/action.yml @@ -19,15 +19,12 @@ inputs: BOARD_COLUMN_OPENED: description: 'The case-sensiteve column name of the column you'd like the task to be moved to once the PR has been opened' required: false - default: '' BOARD_COLUMN_MERGED: description: 'The case-sensiteve column name of the column you'd like the task to be moved to once the PR has been merged' required: false - default: '' BOARD_COLUMN_CLOSED: description: 'The case-sensiteve column name of the column you'd like the task to be moved to if the PR was closed without being merged' required: false - default: '' runs: using: 'docker' image: 'Dockerfile' From d38754ccf896e42e180ce749d596e3c764aff60c Mon Sep 17 00:00:00 2001 From: Rodrigo Kreutz <8869678+rkreutz@users.noreply.github.com> Date: Tue, 20 Apr 2021 12:15:21 +0100 Subject: [PATCH 3/3] Fixing action.yml --- README.md | 6 +++--- action.yml | 9 ++++++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index e5bcba70..79d3e512 100644 --- a/README.md +++ b/README.md @@ -48,9 +48,9 @@ jobs: TEAMWORK_URI: ${{ secrets.TEAMWORK_URI }} TEAMWORK_API_TOKEN: ${{ secrets.TEAMWORK_API_TOKEN }} AUTOMATIC_TAGGING: false - BOARD_COLUMN_OPENED: "PR Open" - BOARD_COLUMN_MERGED: "Ready to Test" - BOARD_COLUMN_CLOSED: "Rejected" + BOARD_COLUMN_OPENED: 'PR Open' + BOARD_COLUMN_MERGED: 'Ready to Test' + BOARD_COLUMN_CLOSED: 'Rejected' ``` diff --git a/action.yml b/action.yml index e5cd1374..6a581366 100644 --- a/action.yml +++ b/action.yml @@ -17,14 +17,17 @@ inputs: description: 'Do you want to enable automatic tagging: true/false' required: false BOARD_COLUMN_OPENED: - description: 'The case-sensiteve column name of the column you'd like the task to be moved to once the PR has been opened' + description: 'The case-sensiteve column name of the column you would like the task to be moved to once the PR has been opened' required: false + default: '' BOARD_COLUMN_MERGED: - description: 'The case-sensiteve column name of the column you'd like the task to be moved to once the PR has been merged' + description: 'The case-sensiteve column name of the column you would like the task to be moved to once the PR has been merged' required: false + default: '' BOARD_COLUMN_CLOSED: - description: 'The case-sensiteve column name of the column you'd like the task to be moved to if the PR was closed without being merged' + description: 'The case-sensiteve column name of the column you would like the task to be moved to if the PR was closed without being merged' required: false + default: '' runs: using: 'docker' image: 'Dockerfile'