From 6413eeeb36a7d669c2303a4fcaeafb57624c5e9b Mon Sep 17 00:00:00 2001 From: Rafael Dantas Justo Date: Wed, 11 Feb 2026 21:32:07 -0300 Subject: [PATCH 01/24] Feature: Lightweight comment New option to add a small comment to the task only with minimal information. This should help linking pull requests with Teamwork.com tasks without polluting task's discussions. --- README.md | 2 +- action.yml | 5 +++++ src/main.sh | 1 + src/teamwork.sh | 34 +++++++++++++++++++++++++--------- 4 files changed, 32 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 3c54c77b..847bb6a1 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ jobs: ``` ## Usage -When creating a new PR, write in the description of the PR the URL of the task. The action will automatically add a comment in the task. +When creating a new PR, write in the description of the PR the URL of the task. The action will automatically add a comment in the task. Optionally, you may choose for a lightweight comment that will only include the PR URL and the user that performed the action, to enable this option set the `LIGHTWEIGHT_COMMENT` input to `true`. Please note, the comment will be created in Teamwork under the account you have attached to this action. If the API key of the user you are using does not have permissions to access certain projects, the comment will not be created. diff --git a/action.yml b/action.yml index f704b08a..82cb7eb4 100644 --- a/action.yml +++ b/action.yml @@ -28,6 +28,10 @@ inputs: 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' required: false default: '' + LIGHTWEIGHT_COMMENT: + description: 'Generate tiny comments with minimal information' + required: false + default: 'false' runs: using: 'docker' image: 'Dockerfile' @@ -39,3 +43,4 @@ runs: - ${{ inputs.BOARD_COLUMN_OPENED }} - ${{ inputs.BOARD_COLUMN_MERGED }} - ${{ inputs.BOARD_COLUMN_CLOSED }} + - ${{ inputs.LIGHTWEIGHT_COMMENT }} \ No newline at end of file diff --git a/src/main.sh b/src/main.sh index cf553262..9646920d 100644 --- a/src/main.sh +++ b/src/main.sh @@ -20,6 +20,7 @@ main() { export BOARD_COLUMN_OPENED="$5" export BOARD_COLUMN_MERGED="$6" export BOARD_COLUMN_CLOSED="$7" + export LIGHTWEIGHT_COMMENT="$8" env::set_environment diff --git a/src/teamwork.sh b/src/teamwork.sh index b3c5e687..05433481 100644 --- a/src/teamwork.sh +++ b/src/teamwork.sh @@ -145,7 +145,10 @@ teamwork::pull_request_opened() { local -r pr_body=$(github::get_pr_body) IFS=" " read -r -a pr_stats_array <<< "$pr_stats" - teamwork::add_comment " + if [ "$LIGHTWEIGHT_COMMENT" == "true" ]; then + teamwork::add_comment "[PR]($pr_url) opened by $user" + else + teamwork::add_comment " **$user** opened a PR: **$pr_title** [$pr_url]($pr_url) \`$base_ref\` ⬅️ \`$head_ref\` @@ -158,7 +161,8 @@ ${pr_body} 🔢 ${pr_stats_array[0]} commits / 📝 ${pr_stats_array[1]} files updated / ➕ ${pr_stats_array[2]} additions / ➖ ${pr_stats_array[3]} deletions - " +" + fi teamwork::add_tag "PR Open" teamwork::move_task_to_column "$BOARD_COLUMN_OPENED" @@ -171,19 +175,27 @@ teamwork::pull_request_closed() { local -r pr_merged=$(github::get_pr_merged) if [ "$pr_merged" == "true" ]; then - teamwork::add_comment " + if [ "$LIGHTWEIGHT_COMMENT" == "true" ]; then + teamwork::add_comment "[PR]($pr_url) merged by $user" + else + teamwork::add_comment " **$user** merged a PR: **$pr_title** [$pr_url]($pr_url) " - teamwork::add_tag "PR Merged" - teamwork::remove_tag "PR Open" - teamwork::remove_tag "PR Approved" - teamwork::move_task_to_column "$BOARD_COLUMN_MERGED" + fi + 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 " + if [ "$LIGHTWEIGHT_COMMENT" == "true" ]; then + teamwork::add_comment "[PR]($pr_url) closed without merging by $user" + else + teamwork::add_comment " **$user** closed a PR without merging: **$pr_title** [$pr_url]($pr_url) " + fi teamwork::remove_tag "PR Open" teamwork::remove_tag "PR Approved" teamwork::move_task_to_column "$BOARD_COLUMN_CLOSED" @@ -199,7 +211,10 @@ teamwork::pull_request_review_submitted() { # Only add a message if the PR has been approved if [ "$review_state" == "approved" ]; then - teamwork::add_comment " + if [ "$LIGHTWEIGHT_COMMENT" == "true" ]; then + teamwork::add_comment "[PR]($pr_url) approved by $user" + else + teamwork::add_comment " **$user** submitted a review to the PR: **$pr_title** [$pr_url]($pr_url) @@ -208,6 +223,7 @@ teamwork::pull_request_review_submitted() { Review: **$review_state** $comment " + fi teamwork::add_tag "PR Approved" fi } From 617ee1e0ca3352280f9b8385d8e94965762eef56 Mon Sep 17 00:00:00 2001 From: Sean O'Grady <1761115+seanogdev@users.noreply.github.com> Date: Wed, 24 Jun 2026 20:58:33 +0100 Subject: [PATCH 02/24] Include PR title in lightweight comments When LIGHTWEIGHT_COMMENT is enabled, the Teamwork comments dropped the PR title entirely. Add the title as a bold, linked label after the PR link so brief comments still surface which PR they refer to while staying to a single line, e.g. [PR](url) [**"Fix login"**](url) opened by user --- README.md | 2 +- src/teamwork.sh | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 847bb6a1..972f4803 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ jobs: ``` ## Usage -When creating a new PR, write in the description of the PR the URL of the task. The action will automatically add a comment in the task. Optionally, you may choose for a lightweight comment that will only include the PR URL and the user that performed the action, to enable this option set the `LIGHTWEIGHT_COMMENT` input to `true`. +When creating a new PR, write in the description of the PR the URL of the task. The action will automatically add a comment in the task. Optionally, you may choose for a lightweight comment that will only include the PR title, URL and the user that performed the action, to enable this option set the `LIGHTWEIGHT_COMMENT` input to `true`. Please note, the comment will be created in Teamwork under the account you have attached to this action. If the API key of the user you are using does not have permissions to access certain projects, the comment will not be created. diff --git a/src/teamwork.sh b/src/teamwork.sh index 05433481..27005f03 100644 --- a/src/teamwork.sh +++ b/src/teamwork.sh @@ -146,7 +146,7 @@ teamwork::pull_request_opened() { IFS=" " read -r -a pr_stats_array <<< "$pr_stats" if [ "$LIGHTWEIGHT_COMMENT" == "true" ]; then - teamwork::add_comment "[PR]($pr_url) opened by $user" + teamwork::add_comment "[PR]($pr_url) [**\"$pr_title\"**]($pr_url) opened by $user" else teamwork::add_comment " **$user** opened a PR: **$pr_title** @@ -176,7 +176,7 @@ teamwork::pull_request_closed() { if [ "$pr_merged" == "true" ]; then if [ "$LIGHTWEIGHT_COMMENT" == "true" ]; then - teamwork::add_comment "[PR]($pr_url) merged by $user" + teamwork::add_comment "[PR]($pr_url) [**\"$pr_title\"**]($pr_url) merged by $user" else teamwork::add_comment " **$user** merged a PR: **$pr_title** @@ -189,7 +189,7 @@ teamwork::pull_request_closed() { teamwork::move_task_to_column "$BOARD_COLUMN_MERGED" else if [ "$LIGHTWEIGHT_COMMENT" == "true" ]; then - teamwork::add_comment "[PR]($pr_url) closed without merging by $user" + teamwork::add_comment "[PR]($pr_url) [**\"$pr_title\"**]($pr_url) closed without merging by $user" else teamwork::add_comment " **$user** closed a PR without merging: **$pr_title** @@ -212,7 +212,7 @@ teamwork::pull_request_review_submitted() { # Only add a message if the PR has been approved if [ "$review_state" == "approved" ]; then if [ "$LIGHTWEIGHT_COMMENT" == "true" ]; then - teamwork::add_comment "[PR]($pr_url) approved by $user" + teamwork::add_comment "[PR]($pr_url) [**\"$pr_title\"**]($pr_url) approved by $user" else teamwork::add_comment " **$user** submitted a review to the PR: **$pr_title** From e7f57cdcd3484ceabe6067f55a24099ede0f7be1 Mon Sep 17 00:00:00 2001 From: Sean O'Grady <1761115+seanogdev@users.noreply.github.com> Date: Wed, 24 Jun 2026 21:05:39 +0100 Subject: [PATCH 03/24] Don't link the 'PR' label in lightweight comments The title is already linked to the PR, so linking the 'PR' label to the same URL was redundant. Keep 'PR' as plain text and link only the title: PR [**"Fix login"**](url) opened by user --- src/teamwork.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/teamwork.sh b/src/teamwork.sh index 27005f03..6394c84b 100644 --- a/src/teamwork.sh +++ b/src/teamwork.sh @@ -146,7 +146,7 @@ teamwork::pull_request_opened() { IFS=" " read -r -a pr_stats_array <<< "$pr_stats" if [ "$LIGHTWEIGHT_COMMENT" == "true" ]; then - teamwork::add_comment "[PR]($pr_url) [**\"$pr_title\"**]($pr_url) opened by $user" + teamwork::add_comment "PR [**\"$pr_title\"**]($pr_url) opened by $user" else teamwork::add_comment " **$user** opened a PR: **$pr_title** @@ -176,7 +176,7 @@ teamwork::pull_request_closed() { if [ "$pr_merged" == "true" ]; then if [ "$LIGHTWEIGHT_COMMENT" == "true" ]; then - teamwork::add_comment "[PR]($pr_url) [**\"$pr_title\"**]($pr_url) merged by $user" + teamwork::add_comment "PR [**\"$pr_title\"**]($pr_url) merged by $user" else teamwork::add_comment " **$user** merged a PR: **$pr_title** @@ -189,7 +189,7 @@ teamwork::pull_request_closed() { teamwork::move_task_to_column "$BOARD_COLUMN_MERGED" else if [ "$LIGHTWEIGHT_COMMENT" == "true" ]; then - teamwork::add_comment "[PR]($pr_url) [**\"$pr_title\"**]($pr_url) closed without merging by $user" + teamwork::add_comment "PR [**\"$pr_title\"**]($pr_url) closed without merging by $user" else teamwork::add_comment " **$user** closed a PR without merging: **$pr_title** @@ -212,7 +212,7 @@ teamwork::pull_request_review_submitted() { # Only add a message if the PR has been approved if [ "$review_state" == "approved" ]; then if [ "$LIGHTWEIGHT_COMMENT" == "true" ]; then - teamwork::add_comment "[PR]($pr_url) [**\"$pr_title\"**]($pr_url) approved by $user" + teamwork::add_comment "PR [**\"$pr_title\"**]($pr_url) approved by $user" else teamwork::add_comment " **$user** submitted a review to the PR: **$pr_title** From e7a4dbde6e7bdc879e404ae26088193196f25545 Mon Sep 17 00:00:00 2001 From: Sean O'Grady <1761115+seanogdev@users.noreply.github.com> Date: Wed, 24 Jun 2026 21:10:22 +0100 Subject: [PATCH 04/24] Bold the user in lightweight comments Match the bold PR title by also bolding the actor's username, e.g. PR [**"Fix login"**](url) opened by **user** --- src/teamwork.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/teamwork.sh b/src/teamwork.sh index 6394c84b..a80abb0a 100644 --- a/src/teamwork.sh +++ b/src/teamwork.sh @@ -146,7 +146,7 @@ teamwork::pull_request_opened() { IFS=" " read -r -a pr_stats_array <<< "$pr_stats" if [ "$LIGHTWEIGHT_COMMENT" == "true" ]; then - teamwork::add_comment "PR [**\"$pr_title\"**]($pr_url) opened by $user" + teamwork::add_comment "PR [**\"$pr_title\"**]($pr_url) opened by **$user**" else teamwork::add_comment " **$user** opened a PR: **$pr_title** @@ -176,7 +176,7 @@ teamwork::pull_request_closed() { if [ "$pr_merged" == "true" ]; then if [ "$LIGHTWEIGHT_COMMENT" == "true" ]; then - teamwork::add_comment "PR [**\"$pr_title\"**]($pr_url) merged by $user" + teamwork::add_comment "PR [**\"$pr_title\"**]($pr_url) merged by **$user**" else teamwork::add_comment " **$user** merged a PR: **$pr_title** @@ -189,7 +189,7 @@ teamwork::pull_request_closed() { teamwork::move_task_to_column "$BOARD_COLUMN_MERGED" else if [ "$LIGHTWEIGHT_COMMENT" == "true" ]; then - teamwork::add_comment "PR [**\"$pr_title\"**]($pr_url) closed without merging by $user" + teamwork::add_comment "PR [**\"$pr_title\"**]($pr_url) closed without merging by **$user**" else teamwork::add_comment " **$user** closed a PR without merging: **$pr_title** @@ -212,7 +212,7 @@ teamwork::pull_request_review_submitted() { # Only add a message if the PR has been approved if [ "$review_state" == "approved" ]; then if [ "$LIGHTWEIGHT_COMMENT" == "true" ]; then - teamwork::add_comment "PR [**\"$pr_title\"**]($pr_url) approved by $user" + teamwork::add_comment "PR [**\"$pr_title\"**]($pr_url) approved by **$user**" else teamwork::add_comment " **$user** submitted a review to the PR: **$pr_title** From cc9e5699c8b6bb4fc5a2a94504eb6c1622cbc68f Mon Sep 17 00:00:00 2001 From: Sean O'Grady <1761115+seanogdev@users.noreply.github.com> Date: Wed, 24 Jun 2026 21:18:34 +0100 Subject: [PATCH 05/24] Migrate board columns to workflow stages Teamwork retired the Boards endpoints after migrating installations to Workflows, so the BOARD_COLUMN_* feature broke with "Boards endpoints are now unavailable, please use Workflows endpoints" (#68). Resolve the configured stage name to its workflow + stage IDs in a single v3 call (GET /projects/api/v3/workflows.json?projectIds=&include=stages), then move the task with PATCH /projects/api/v3/tasks/{id}/workflows/{wf}.json. The null-guarded jq also fixes the "Cannot iterate over null" crash. Rename the BOARD_COLUMN_* inputs to WORKFLOW_STAGE_*, keeping the old names as silent fallback aliases. --- .github/workflows/test.yml | 6 ++-- README.md | 16 +++++----- action.yml | 31 ++++++++++++++----- src/main.sh | 16 ++++++++-- src/teamwork.sh | 61 ++++++++++++++++++++------------------ 5 files changed, 79 insertions(+), 51 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 4bab5c6c..5109b634 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -17,6 +17,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" + WORKFLOW_STAGE_OPENED: "PR Open" + WORKFLOW_STAGE_MERGED: "Ready to Test" + WORKFLOW_STAGE_CLOSED: "Rejected" diff --git a/README.md b/README.md index 847bb6a1..c5da718d 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' + WORKFLOW_STAGE_OPENED: 'PR Open' + WORKFLOW_STAGE_MERGED: 'Ready to Test' + WORKFLOW_STAGE_CLOSED: 'Rejected' env: IGNORE_PROJECT_IDS: '1 2 3' @@ -71,12 +71,12 @@ 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-sensitive 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-sensitive 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-sensitive column name of the column you'd like the task to be moved to if the PR was closed without being merged +You may also specify [workflow](https://support.teamwork.com/projects/workflows/workflows-introduction) stages you'd like the task to be moved to on every stage of the PR: +- `WORKFLOW_STAGE_OPENED`: The case-sensitive name of the workflow stage you'd like the task to be moved to once the PR has been opened +- `WORKFLOW_STAGE_MERGED`: The case-sensitive name of the workflow stage you'd like the task to be moved to once the PR has been merged +- `WORKFLOW_STAGE_CLOSED`: The case-sensitive name of the workflow stage 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. +The stage names will be checked against the stages of every workflow 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 stage will be used. ## Contributing * Open a PR: https://github.com/Teamwork/github-sync/pulls diff --git a/action.yml b/action.yml index 82cb7eb4..014a25f7 100644 --- a/action.yml +++ b/action.yml @@ -16,22 +16,34 @@ inputs: AUTOMATIC_TAGGING: description: 'Do you want to enable automatic tagging: true/false' required: false - BOARD_COLUMN_OPENED: - description: 'The case-sensitive column name of the column you would like the task to be moved to once the PR has been opened' + WORKFLOW_STAGE_OPENED: + description: 'The case-sensitive name of the workflow stage you would like the task to be moved to once the PR has been opened' required: false default: '' - BOARD_COLUMN_MERGED: - description: 'The case-sensitive column name of the column you would like the task to be moved to once the PR has been merged' + WORKFLOW_STAGE_MERGED: + description: 'The case-sensitive name of the workflow stage you would like the task to be moved to once the PR has been merged' required: false default: '' - BOARD_COLUMN_CLOSED: - 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' + WORKFLOW_STAGE_CLOSED: + description: 'The case-sensitive name of the workflow stage you would like the task to be moved to if the PR was closed without being merged' required: false default: '' LIGHTWEIGHT_COMMENT: description: 'Generate tiny comments with minimal information' required: false default: 'false' + BOARD_COLUMN_OPENED: + description: 'Deprecated.' + required: false + default: '' + BOARD_COLUMN_MERGED: + description: 'Deprecated.' + required: false + default: '' + BOARD_COLUMN_CLOSED: + description: 'Deprecated.' + required: false + default: '' runs: using: 'docker' image: 'Dockerfile' @@ -40,7 +52,10 @@ runs: - ${{ inputs.TEAMWORK_URI }} - ${{ inputs.TEAMWORK_API_TOKEN }} - ${{ inputs.AUTOMATIC_TAGGING }} + - ${{ inputs.WORKFLOW_STAGE_OPENED }} + - ${{ inputs.WORKFLOW_STAGE_MERGED }} + - ${{ inputs.WORKFLOW_STAGE_CLOSED }} + - ${{ inputs.LIGHTWEIGHT_COMMENT }} - ${{ inputs.BOARD_COLUMN_OPENED }} - ${{ inputs.BOARD_COLUMN_MERGED }} - - ${{ inputs.BOARD_COLUMN_CLOSED }} - - ${{ inputs.LIGHTWEIGHT_COMMENT }} \ No newline at end of file + - ${{ inputs.BOARD_COLUMN_CLOSED }} \ No newline at end of file diff --git a/src/main.sh b/src/main.sh index 9646920d..62a3b37f 100644 --- a/src/main.sh +++ b/src/main.sh @@ -17,10 +17,20 @@ 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" + export WORKFLOW_STAGE_OPENED="$5" + export WORKFLOW_STAGE_MERGED="$6" + export WORKFLOW_STAGE_CLOSED="$7" export LIGHTWEIGHT_COMMENT="$8" + # BOARD_COLUMN_* are deprecated aliases kept for backwards compatibility. + export BOARD_COLUMN_OPENED="${9:-}" + export BOARD_COLUMN_MERGED="${10:-}" + export BOARD_COLUMN_CLOSED="${11:-}" + + # Fall back to the deprecated BOARD_COLUMN_* inputs when their WORKFLOW_STAGE_* + # replacements are not set. + : "${WORKFLOW_STAGE_OPENED:=$BOARD_COLUMN_OPENED}" + : "${WORKFLOW_STAGE_MERGED:=$BOARD_COLUMN_MERGED}" + : "${WORKFLOW_STAGE_CLOSED:=$BOARD_COLUMN_CLOSED}" env::set_environment diff --git a/src/teamwork.sh b/src/teamwork.sh index 05433481..033fb59c 100644 --- a/src/teamwork.sh +++ b/src/teamwork.sh @@ -31,54 +31,57 @@ teamwork::get_project_id_from_task() { echo "$response" } -teamwork::get_matching_board_column_id() { - local -r column_name=$1 - - if [ -z "$column_name" ]; then +# Resolves a workflow stage name to its " " pair within the +# task's project. Matching is a case-sensitive substring match against the stage +# name (mirroring the legacy board-column behaviour), and the first match wins. +# Echoes nothing when no stage name is provided or no stage matches. +teamwork::get_workflow_stage() { + local -r stage_name=$1 + + if [ -z "$stage_name" ]; then return fi if [ "$ENV" == "test" ]; then - echo "$TEAMWORK_PROJECT_ID" + echo "$TEAMWORK_PROJECT_ID $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" + # A single call scoped to the project sideloads every stage across the + # project's workflow(s); each stage carries its parent workflow id, which the + # move endpoint also needs. + curl -s "$TEAMWORK_URI/projects/api/v3/workflows.json?projectIds=$TEAMWORK_PROJECT_ID&include=stages" \ + -u "$TEAMWORK_API_TOKEN"':' |\ + jq -r --arg stage_name "$stage_name" \ + '[.included.stages[]? | select(.name | contains($stage_name)) | "\(.workflow.id) \(.id)"][0] // empty' } -teamwork::move_task_to_column() { - local -r task_id=$TEAMWORK_TASK_ID - local -r column_name=$1 +teamwork::move_task_to_stage() { + local -r stage_name=$1 - if [ -z "$column_name" ]; then - log::message "No column name provided" + if [ -z "$stage_name" ]; then + log::message "No workflow stage 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'" + local -r stage=$(teamwork::get_workflow_stage "$stage_name") + if [ -z "$stage" ]; then + log::message "Failed to find a matching workflow stage for '$stage_name'" return fi + local workflow_id stage_id + read -r workflow_id stage_id <<< "$stage" + if [ "$ENV" == "test" ]; then - log::message "Test - Simulate request. Task ID: $TEAMWORK_TASK_ID - Project ID: $TEAMWORK_PROJECT_ID - Column ID: $column_id" + log::message "Test - Simulate request. Task ID: $TEAMWORK_TASK_ID - Project ID: $TEAMWORK_PROJECT_ID - Workflow ID: $workflow_id - Stage ID: $stage_id" return fi - response=$(curl -X "PUT" "$TEAMWORK_URI/tasks/$TEAMWORK_TASK_ID.json" \ + response=$(curl -X "PATCH" "$TEAMWORK_URI/projects/api/v3/tasks/$TEAMWORK_TASK_ID/workflows/$workflow_id.json" \ -u "$TEAMWORK_API_TOKEN"':' \ -H 'Content-Type: application/json; charset=utf-8' \ - -d "{ \"todo-item\": { \"columnId\": $column_id } }" ) + -d "{ \"stageId\": $stage_id, \"positionAfterTask\": -1 }" ) log::message "$response" } @@ -165,7 +168,7 @@ ${pr_body} fi teamwork::add_tag "PR Open" - teamwork::move_task_to_column "$BOARD_COLUMN_OPENED" + teamwork::move_task_to_stage "$WORKFLOW_STAGE_OPENED" } teamwork::pull_request_closed() { @@ -186,7 +189,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" + teamwork::move_task_to_stage "$WORKFLOW_STAGE_MERGED" else if [ "$LIGHTWEIGHT_COMMENT" == "true" ]; then teamwork::add_comment "[PR]($pr_url) closed without merging by $user" @@ -198,7 +201,7 @@ teamwork::pull_request_closed() { fi teamwork::remove_tag "PR Open" teamwork::remove_tag "PR Approved" - teamwork::move_task_to_column "$BOARD_COLUMN_CLOSED" + teamwork::move_task_to_stage "$WORKFLOW_STAGE_CLOSED" fi } From 3e8447a13e021d18fadbf7e20dab4b8a70e658d7 Mon Sep 17 00:00:00 2001 From: GitOps CI Date: Mon, 13 Jul 2026 07:52:52 +0100 Subject: [PATCH 06/24] github-sync --- .github/workflows/linter.yml | 2 +- .github/workflows/test.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/linter.yml b/.github/workflows/linter.yml index b5ede0fc..91979671 100644 --- a/.github/workflows/linter.yml +++ b/.github/workflows/linter.yml @@ -7,6 +7,6 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout code - uses: actions/checkout@v1 + uses: actions/checkout@v5 - name: Run Shellcheck uses: azohra/shell-linter@latest diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5109b634..8f6ea6c9 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -10,7 +10,7 @@ jobs: runs-on: ubuntu-latest name: Ensure this project runs steps: - - uses: actions/checkout@v1 + - uses: actions/checkout@v5 - uses: ./ with: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} From 6f6bbf8a51adff18cd4518c6901295198bfbc1e5 Mon Sep 17 00:00:00 2001 From: Marc Izquierdo Date: Tue, 20 Oct 2026 18:20:39 +0200 Subject: [PATCH 07/24] Bump Alpine base image from 3.11 to 3.24 Alpine 3.11 reached end of support in November 2021. 3.24 is the current stable branch, supported until June 2028. Co-Authored-By: Claude Opus 5.5 --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 82925d22..6eaa2ae4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM alpine:3.11 +FROM alpine:3.24 RUN apk add --no-cache bash curl jq From f3bdbfba2d1d9585677df79d4b577b6f05534c7a Mon Sep 17 00:00:00 2001 From: Marc Izquierdo Date: Tue, 20 Oct 2026 18:29:53 +0200 Subject: [PATCH 08/24] Trim whitespace from PR titles A trailing space in a PR title rendered as **foo ** in Teamwork comments, which Markdown doesn't treat as bold, so the asterisks showed up literally. Requires jq 1.7.1+ for trim (Alpine 3.24 ships 1.8.2). Co-Authored-By: Claude Opus 5.5 --- src/github.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/github.sh b/src/github.sh index 726b906b..dbe93858 100644 --- a/src/github.sh +++ b/src/github.sh @@ -33,7 +33,7 @@ github::get_pr_url() { } github::get_pr_title() { - jq --raw-output .pull_request.title "$GITHUB_EVENT_PATH" + jq --raw-output '.pull_request.title | trim' "$GITHUB_EVENT_PATH" } github::get_pr_patch_stats() { From 2cff5e7a0ce6a057ce1401c16bd16b0c3a8a6bbd Mon Sep 17 00:00:00 2001 From: Marc Izquierdo Date: Tue, 20 Oct 2026 18:29:54 +0200 Subject: [PATCH 09/24] Omit review comment when the review has no body Bare approvals have a null review body, which jq printed as the string "null" at the end of the Teamwork comment. Co-Authored-By: Claude Opus 5.5 --- src/github.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/github.sh b/src/github.sh index dbe93858..23547aa7 100644 --- a/src/github.sh +++ b/src/github.sh @@ -53,7 +53,7 @@ github::get_review_state() { } github::get_review_comment() { - jq --raw-output .review.body "$GITHUB_EVENT_PATH" + jq --raw-output '.review.body // empty' "$GITHUB_EVENT_PATH" } github::print_all_data() { From 374b89d3d1d738a175d34453648c309ab4c4fa72 Mon Sep 17 00:00:00 2001 From: Marc Izquierdo Date: Tue, 20 Oct 2026 19:50:45 +0200 Subject: [PATCH 10/24] Send Teamwork requests through one curl helper Each API call assembled its own curl command line. Route them all through teamwork::request so the options live in one place. The helper also passes -sS. curl prints a progress meter when its output is captured, which filled the logs with timings that change on every run; -S still shows errors. Co-Authored-By: Claude Opus 5.5 --- src/teamwork.sh | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/src/teamwork.sh b/src/teamwork.sh index c7da9e07..4439a1df 100644 --- a/src/teamwork.sh +++ b/src/teamwork.sh @@ -16,6 +16,20 @@ teamwork::get_task_id_from_body() { echo "$task_ids_str" } +# Calls the Teamwork API and prints the response body. +teamwork::request() { + local -r method=$1 + local -r path=$2 + local -r payload=${3:-} + + local args=(-sS -X "$method" -u "$TEAMWORK_API_TOKEN"':') + if [ -n "$payload" ]; then + args+=(-H 'Content-Type: application/json; charset=utf-8' -d "$payload") + fi + + curl "${args[@]}" "$TEAMWORK_URI$path" +} + teamwork::get_project_id_from_task() { local -r task_id=$1 @@ -25,7 +39,7 @@ teamwork::get_project_id_from_task() { fi response=$( - curl "$TEAMWORK_URI/projects/api/v1/tasks/$task_id.json" -u "$TEAMWORK_API_TOKEN"':' |\ + teamwork::request GET "/projects/api/v1/tasks/$task_id.json" |\ jq -r '.["todo-item"]["project-id"]' ) echo "$response" @@ -50,8 +64,7 @@ teamwork::get_workflow_stage() { # A single call scoped to the project sideloads every stage across the # project's workflow(s); each stage carries its parent workflow id, which the # move endpoint also needs. - curl -s "$TEAMWORK_URI/projects/api/v3/workflows.json?projectIds=$TEAMWORK_PROJECT_ID&include=stages" \ - -u "$TEAMWORK_API_TOKEN"':' |\ + teamwork::request GET "/projects/api/v3/workflows.json?projectIds=$TEAMWORK_PROJECT_ID&include=stages" |\ jq -r --arg stage_name "$stage_name" \ '[.included.stages[]? | select(.name | contains($stage_name)) | "\(.workflow.id) \(.id)"][0] // empty' } @@ -78,10 +91,8 @@ teamwork::move_task_to_stage() { return fi - response=$(curl -X "PATCH" "$TEAMWORK_URI/projects/api/v3/tasks/$TEAMWORK_TASK_ID/workflows/$workflow_id.json" \ - -u "$TEAMWORK_API_TOKEN"':' \ - -H 'Content-Type: application/json; charset=utf-8' \ - -d "{ \"stageId\": $stage_id, \"positionAfterTask\": -1 }" ) + response=$(teamwork::request PATCH "/projects/api/v3/tasks/$TEAMWORK_TASK_ID/workflows/$workflow_id.json" \ + "{ \"stageId\": $stage_id, \"positionAfterTask\": -1 }") log::message "$response" } @@ -94,10 +105,8 @@ teamwork::add_comment() { return fi - response=$(curl -X "POST" "$TEAMWORK_URI/tasks/$TEAMWORK_TASK_ID/comments.json" \ - -u "$TEAMWORK_API_TOKEN"':' \ - -H 'Content-Type: application/json; charset=utf-8' \ - -d "{ \"comment\": { \"body\": \"${body//\"/}\", \"notify\": true, \"content-type\": \"text\", \"isprivate\": false } }" ) + response=$(teamwork::request POST "/tasks/$TEAMWORK_TASK_ID/comments.json" \ + "{ \"comment\": { \"body\": \"${body//\"/}\", \"notify\": true, \"content-type\": \"text\", \"isprivate\": false } }") log::message "$response" } @@ -111,10 +120,8 @@ teamwork::add_tag() { fi if [ "$AUTOMATIC_TAGGING" == true ]; then - response=$(curl -X "PUT" "$TEAMWORK_URI/tasks/$TEAMWORK_TASK_ID/tags.json" \ - -u "$TEAMWORK_API_TOKEN"':' \ - -H 'Content-Type: application/json; charset=utf-8' \ - -d "{ \"tags\": { \"content\": \"${tag_name//\"/}\" } }" ) + response=$(teamwork::request PUT "/tasks/$TEAMWORK_TASK_ID/tags.json" \ + "{ \"tags\": { \"content\": \"${tag_name//\"/}\" } }") log::message "$response" fi @@ -129,10 +136,8 @@ teamwork::remove_tag() { fi if [ "$AUTOMATIC_TAGGING" == true ]; then - response=$(curl -X "PUT" "$TEAMWORK_URI/tasks/$TEAMWORK_TASK_ID/tags.json" \ - -u "$TEAMWORK_API_TOKEN"':' \ - -H 'Content-Type: application/json; charset=utf-8' \ - -d "{ \"tags\": { \"content\": \"${tag_name//\"/}\" },\"removeProvidedTags\":\"true\" }" ) + response=$(teamwork::request PUT "/tasks/$TEAMWORK_TASK_ID/tags.json" \ + "{ \"tags\": { \"content\": \"${tag_name//\"/}\" },\"removeProvidedTags\":\"true\" }") log::message "$response" fi From 2cf73001ff5e6937fcb8c425019ac6d7fb349dba Mon Sep 17 00:00:00 2001 From: Marc Izquierdo Date: Tue, 20 Oct 2026 19:52:35 +0200 Subject: [PATCH 11/24] Test the action against a mock Teamwork API CI only ran the action in test mode, where every Teamwork request is skipped, so nothing checked what the action actually sends. tests/run.sh builds the image, starts a mock Teamwork API (tests/mock-teamwork.py) on a private Docker network, and runs each case in tests/cases the way the GitHub runner would: args built from action.yml, inputs as INPUT_* env vars. It compares the exit code, stderr and every request the mock received with the case's expected file. The expected files record the current behaviour, bugs included, so the fixes that follow show up as changes to them. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/test.yml | 2 + CONTRIBUTING.md | 18 ++ .../closed-merged-lightweight/event.json | 27 +++ .../cases/closed-merged-lightweight/expected | 10 ++ tests/cases/closed-merged-lightweight/inputs | 2 + tests/cases/closed-merged/event.json | 27 +++ tests/cases/closed-merged/expected | 27 +++ tests/cases/closed-merged/inputs | 5 + .../closed-unmerged-lightweight/event.json | 27 +++ .../closed-unmerged-lightweight/expected | 10 ++ .../cases/closed-unmerged-lightweight/inputs | 2 + tests/cases/closed-unmerged/event.json | 27 +++ tests/cases/closed-unmerged/expected | 23 +++ tests/cases/closed-unmerged/inputs | 5 + tests/cases/comment-fails/event.json | 27 +++ tests/cases/comment-fails/expected | 20 +++ tests/cases/comment-fails/inputs | 6 + .../deprecated-board-column-closed/event.json | 27 +++ .../deprecated-board-column-closed/expected | 16 ++ .../deprecated-board-column-closed/inputs | 3 + .../cases/deprecated-board-column/event.json | 27 +++ tests/cases/deprecated-board-column/expected | 16 ++ tests/cases/deprecated-board-column/inputs | 3 + tests/cases/ignored-project/event.json | 27 +++ tests/cases/ignored-project/expected | 3 + tests/cases/ignored-project/inputs | 3 + tests/cases/no-task-links/event.json | 27 +++ tests/cases/no-task-links/expected | 1 + tests/cases/no-task-links/inputs | 5 + tests/cases/opened-lightweight/event.json | 27 +++ tests/cases/opened-lightweight/expected | 20 +++ tests/cases/opened-lightweight/inputs | 6 + tests/cases/opened/event.json | 27 +++ tests/cases/opened/expected | 17 ++ tests/cases/opened/inputs | 5 + .../review-approved-lightweight/event.json | 31 ++++ .../review-approved-lightweight/expected | 14 ++ .../cases/review-approved-lightweight/inputs | 3 + .../cases/review-approved-no-body/event.json | 31 ++++ tests/cases/review-approved-no-body/expected | 11 ++ tests/cases/review-approved-no-body/inputs | 5 + tests/cases/review-approved/event.json | 31 ++++ tests/cases/review-approved/expected | 11 ++ tests/cases/review-approved/inputs | 5 + tests/cases/review-commented/event.json | 31 ++++ tests/cases/review-commented/expected | 3 + tests/cases/review-commented/inputs | 5 + tests/cases/review-dismissed/event.json | 31 ++++ tests/cases/review-dismissed/expected | 10 ++ tests/cases/review-dismissed/inputs | 5 + tests/cases/stage-not-found/event.json | 27 +++ tests/cases/stage-not-found/expected | 12 ++ tests/cases/stage-not-found/inputs | 3 + tests/cases/tagging-disabled/event.json | 27 +++ tests/cases/tagging-disabled/expected | 16 ++ tests/cases/tagging-disabled/inputs | 4 + tests/cases/task-links/event.json | 27 +++ tests/cases/task-links/expected | 73 ++++++++ tests/cases/task-links/inputs | 2 + tests/cases/task-not-found/event.json | 27 +++ tests/cases/task-not-found/expected | 19 +++ tests/cases/task-not-found/inputs | 2 + tests/cases/unsupported-event/event.json | 27 +++ tests/cases/unsupported-event/expected | 3 + tests/cases/unsupported-event/inputs | 5 + tests/cases/wrong-token/event.json | 27 +++ tests/cases/wrong-token/expected | 10 ++ tests/cases/wrong-token/inputs | 3 + tests/mock-teamwork.py | 105 ++++++++++++ tests/run.sh | 157 ++++++++++++++++++ 70 files changed, 1328 insertions(+) create mode 100644 tests/cases/closed-merged-lightweight/event.json create mode 100644 tests/cases/closed-merged-lightweight/expected create mode 100644 tests/cases/closed-merged-lightweight/inputs create mode 100644 tests/cases/closed-merged/event.json create mode 100644 tests/cases/closed-merged/expected create mode 100644 tests/cases/closed-merged/inputs create mode 100644 tests/cases/closed-unmerged-lightweight/event.json create mode 100644 tests/cases/closed-unmerged-lightweight/expected create mode 100644 tests/cases/closed-unmerged-lightweight/inputs create mode 100644 tests/cases/closed-unmerged/event.json create mode 100644 tests/cases/closed-unmerged/expected create mode 100644 tests/cases/closed-unmerged/inputs create mode 100644 tests/cases/comment-fails/event.json create mode 100644 tests/cases/comment-fails/expected create mode 100644 tests/cases/comment-fails/inputs create mode 100644 tests/cases/deprecated-board-column-closed/event.json create mode 100644 tests/cases/deprecated-board-column-closed/expected create mode 100644 tests/cases/deprecated-board-column-closed/inputs create mode 100644 tests/cases/deprecated-board-column/event.json create mode 100644 tests/cases/deprecated-board-column/expected create mode 100644 tests/cases/deprecated-board-column/inputs create mode 100644 tests/cases/ignored-project/event.json create mode 100644 tests/cases/ignored-project/expected create mode 100644 tests/cases/ignored-project/inputs create mode 100644 tests/cases/no-task-links/event.json create mode 100644 tests/cases/no-task-links/expected create mode 100644 tests/cases/no-task-links/inputs create mode 100644 tests/cases/opened-lightweight/event.json create mode 100644 tests/cases/opened-lightweight/expected create mode 100644 tests/cases/opened-lightweight/inputs create mode 100644 tests/cases/opened/event.json create mode 100644 tests/cases/opened/expected create mode 100644 tests/cases/opened/inputs create mode 100644 tests/cases/review-approved-lightweight/event.json create mode 100644 tests/cases/review-approved-lightweight/expected create mode 100644 tests/cases/review-approved-lightweight/inputs create mode 100644 tests/cases/review-approved-no-body/event.json create mode 100644 tests/cases/review-approved-no-body/expected create mode 100644 tests/cases/review-approved-no-body/inputs create mode 100644 tests/cases/review-approved/event.json create mode 100644 tests/cases/review-approved/expected create mode 100644 tests/cases/review-approved/inputs create mode 100644 tests/cases/review-commented/event.json create mode 100644 tests/cases/review-commented/expected create mode 100644 tests/cases/review-commented/inputs create mode 100644 tests/cases/review-dismissed/event.json create mode 100644 tests/cases/review-dismissed/expected create mode 100644 tests/cases/review-dismissed/inputs create mode 100644 tests/cases/stage-not-found/event.json create mode 100644 tests/cases/stage-not-found/expected create mode 100644 tests/cases/stage-not-found/inputs create mode 100644 tests/cases/tagging-disabled/event.json create mode 100644 tests/cases/tagging-disabled/expected create mode 100644 tests/cases/tagging-disabled/inputs create mode 100644 tests/cases/task-links/event.json create mode 100644 tests/cases/task-links/expected create mode 100644 tests/cases/task-links/inputs create mode 100644 tests/cases/task-not-found/event.json create mode 100644 tests/cases/task-not-found/expected create mode 100644 tests/cases/task-not-found/inputs create mode 100644 tests/cases/unsupported-event/event.json create mode 100644 tests/cases/unsupported-event/expected create mode 100644 tests/cases/unsupported-event/inputs create mode 100644 tests/cases/wrong-token/event.json create mode 100644 tests/cases/wrong-token/expected create mode 100644 tests/cases/wrong-token/inputs create mode 100644 tests/mock-teamwork.py create mode 100755 tests/run.sh diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 8f6ea6c9..f7037cdf 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -20,3 +20,5 @@ jobs: WORKFLOW_STAGE_OPENED: "PR Open" WORKFLOW_STAGE_MERGED: "Ready to Test" WORKFLOW_STAGE_CLOSED: "Rejected" + - name: Run the tests against a mock Teamwork API + run: ./tests/run.sh diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index e142fd0f..aca161b8 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -7,6 +7,24 @@ * [Install Docker](https://docs.docker.com/get-docker/) * [Install act tool](https://github.com/nektos/act) +## Running the tests + +The tests run the action's Docker image against a mock Teamwork API (`tests/mock-teamwork.py`) and check what it sends: + +``` +./tests/run.sh +``` + +Each directory in `tests/cases` is a test case: + +* `event.json`: the GitHub event the action receives +* `inputs`: the action's inputs as the `INPUT_*` environment variables GitHub creates for them, plus `GITHUB_EVENT_NAME` and any other environment variables such as `IGNORE_PROJECT_IDS` +* `expected`: the exit code, errors and requests the mock Teamwork API should end up with + +`./tests/run.sh ` only runs the cases whose name contains the filter. + +To add a case, create its `event.json` and `inputs`, run `./tests/run.sh --update ` to record its `expected` file, and check that file is right before committing it. The mock's data (which tasks exist, their projects and the workflow stages) is at the top of `tests/mock-teamwork.py`. + ## Usage Simulate a new pull request diff --git a/tests/cases/closed-merged-lightweight/event.json b/tests/cases/closed-merged-lightweight/event.json new file mode 100644 index 00000000..97732713 --- /dev/null +++ b/tests/cases/closed-merged-lightweight/event.json @@ -0,0 +1,27 @@ +{ + "action": "closed", + "number": 42, + "pull_request": { + "number": 42, + "title": "Add OAuth login", + "body": "Task: http://teamwork:8080/app/tasks/1001", + "html_url": "https://github.com/acme/webapp/pull/42", + "head": { + "ref": "feature/oauth", + "repo": { + "full_name": "acme/webapp" + } + }, + "base": { + "ref": "main" + }, + "commits": 3, + "changed_files": 5, + "additions": 120, + "deletions": 8, + "merged": true + }, + "sender": { + "login": "octocat" + } +} diff --git a/tests/cases/closed-merged-lightweight/expected b/tests/cases/closed-merged-lightweight/expected new file mode 100644 index 00000000..3c02d13c --- /dev/null +++ b/tests/cases/closed-merged-lightweight/expected @@ -0,0 +1,10 @@ +exit code: 0 + +GET /projects/api/v1/tasks/1001.json -> 200 + +POST /tasks/1001/comments.json -> 201 + comment: + body: "PR [**Add OAuth login**](https://github.com/acme/webapp/pull/42) merged by **octocat**" + content-type: "text" + isprivate: false + notify: true diff --git a/tests/cases/closed-merged-lightweight/inputs b/tests/cases/closed-merged-lightweight/inputs new file mode 100644 index 00000000..76857bf4 --- /dev/null +++ b/tests/cases/closed-merged-lightweight/inputs @@ -0,0 +1,2 @@ +GITHUB_EVENT_NAME=pull_request +INPUT_LIGHTWEIGHT_COMMENT=true diff --git a/tests/cases/closed-merged/event.json b/tests/cases/closed-merged/event.json new file mode 100644 index 00000000..97732713 --- /dev/null +++ b/tests/cases/closed-merged/event.json @@ -0,0 +1,27 @@ +{ + "action": "closed", + "number": 42, + "pull_request": { + "number": 42, + "title": "Add OAuth login", + "body": "Task: http://teamwork:8080/app/tasks/1001", + "html_url": "https://github.com/acme/webapp/pull/42", + "head": { + "ref": "feature/oauth", + "repo": { + "full_name": "acme/webapp" + } + }, + "base": { + "ref": "main" + }, + "commits": 3, + "changed_files": 5, + "additions": 120, + "deletions": 8, + "merged": true + }, + "sender": { + "login": "octocat" + } +} diff --git a/tests/cases/closed-merged/expected b/tests/cases/closed-merged/expected new file mode 100644 index 00000000..5fb0536e --- /dev/null +++ b/tests/cases/closed-merged/expected @@ -0,0 +1,27 @@ +exit code: 0 + +GET /projects/api/v1/tasks/1001.json -> 200 + +POST /tasks/1001/comments.json -> 400 + invalid JSON: Invalid control character at: line 1 column 25 (char 24) + "{ \"comment\": { \"body\": \"\n**octocat** merged a PR: **Add OAuth login**\n[https://github.com/acme/webapp/pull/42](https://github.com/acme/webapp/pull/42)\n\", \"notify\": true, \"content-type\": \"text\", \"isprivate\": false } }" + +PUT /tasks/1001/tags.json -> 200 + tags: + content: "PR Merged" + +PUT /tasks/1001/tags.json -> 200 + removeProvidedTags: "true" + tags: + content: "PR Open" + +PUT /tasks/1001/tags.json -> 200 + removeProvidedTags: "true" + tags: + content: "PR Approved" + +GET /projects/api/v3/workflows.json?projectIds=100&include=stages -> 200 + +PATCH /projects/api/v3/tasks/1001/workflows/7.json -> 204 + positionAfterTask: -1 + stageId: 73 diff --git a/tests/cases/closed-merged/inputs b/tests/cases/closed-merged/inputs new file mode 100644 index 00000000..9d43a813 --- /dev/null +++ b/tests/cases/closed-merged/inputs @@ -0,0 +1,5 @@ +GITHUB_EVENT_NAME=pull_request +INPUT_AUTOMATIC_TAGGING=true +INPUT_WORKFLOW_STAGE_OPENED=PR Open +INPUT_WORKFLOW_STAGE_MERGED=Ready to Test +INPUT_WORKFLOW_STAGE_CLOSED=Rejected diff --git a/tests/cases/closed-unmerged-lightweight/event.json b/tests/cases/closed-unmerged-lightweight/event.json new file mode 100644 index 00000000..09ccdcd9 --- /dev/null +++ b/tests/cases/closed-unmerged-lightweight/event.json @@ -0,0 +1,27 @@ +{ + "action": "closed", + "number": 42, + "pull_request": { + "number": 42, + "title": "Add OAuth login", + "body": "Task: http://teamwork:8080/app/tasks/1001", + "html_url": "https://github.com/acme/webapp/pull/42", + "head": { + "ref": "feature/oauth", + "repo": { + "full_name": "acme/webapp" + } + }, + "base": { + "ref": "main" + }, + "commits": 3, + "changed_files": 5, + "additions": 120, + "deletions": 8, + "merged": false + }, + "sender": { + "login": "octocat" + } +} diff --git a/tests/cases/closed-unmerged-lightweight/expected b/tests/cases/closed-unmerged-lightweight/expected new file mode 100644 index 00000000..e4430984 --- /dev/null +++ b/tests/cases/closed-unmerged-lightweight/expected @@ -0,0 +1,10 @@ +exit code: 0 + +GET /projects/api/v1/tasks/1001.json -> 200 + +POST /tasks/1001/comments.json -> 201 + comment: + body: "PR [**Add OAuth login**](https://github.com/acme/webapp/pull/42) closed without merging by **octocat**" + content-type: "text" + isprivate: false + notify: true diff --git a/tests/cases/closed-unmerged-lightweight/inputs b/tests/cases/closed-unmerged-lightweight/inputs new file mode 100644 index 00000000..76857bf4 --- /dev/null +++ b/tests/cases/closed-unmerged-lightweight/inputs @@ -0,0 +1,2 @@ +GITHUB_EVENT_NAME=pull_request +INPUT_LIGHTWEIGHT_COMMENT=true diff --git a/tests/cases/closed-unmerged/event.json b/tests/cases/closed-unmerged/event.json new file mode 100644 index 00000000..09ccdcd9 --- /dev/null +++ b/tests/cases/closed-unmerged/event.json @@ -0,0 +1,27 @@ +{ + "action": "closed", + "number": 42, + "pull_request": { + "number": 42, + "title": "Add OAuth login", + "body": "Task: http://teamwork:8080/app/tasks/1001", + "html_url": "https://github.com/acme/webapp/pull/42", + "head": { + "ref": "feature/oauth", + "repo": { + "full_name": "acme/webapp" + } + }, + "base": { + "ref": "main" + }, + "commits": 3, + "changed_files": 5, + "additions": 120, + "deletions": 8, + "merged": false + }, + "sender": { + "login": "octocat" + } +} diff --git a/tests/cases/closed-unmerged/expected b/tests/cases/closed-unmerged/expected new file mode 100644 index 00000000..ab0f25ce --- /dev/null +++ b/tests/cases/closed-unmerged/expected @@ -0,0 +1,23 @@ +exit code: 0 + +GET /projects/api/v1/tasks/1001.json -> 200 + +POST /tasks/1001/comments.json -> 400 + invalid JSON: Invalid control character at: line 1 column 25 (char 24) + "{ \"comment\": { \"body\": \"\n**octocat** closed a PR without merging: **Add OAuth login**\n[https://github.com/acme/webapp/pull/42](https://github.com/acme/webapp/pull/42)\n\", \"notify\": true, \"content-type\": \"text\", \"isprivate\": false } }" + +PUT /tasks/1001/tags.json -> 200 + removeProvidedTags: "true" + tags: + content: "PR Open" + +PUT /tasks/1001/tags.json -> 200 + removeProvidedTags: "true" + tags: + content: "PR Approved" + +GET /projects/api/v3/workflows.json?projectIds=100&include=stages -> 200 + +PATCH /projects/api/v3/tasks/1001/workflows/7.json -> 204 + positionAfterTask: -1 + stageId: 74 diff --git a/tests/cases/closed-unmerged/inputs b/tests/cases/closed-unmerged/inputs new file mode 100644 index 00000000..9d43a813 --- /dev/null +++ b/tests/cases/closed-unmerged/inputs @@ -0,0 +1,5 @@ +GITHUB_EVENT_NAME=pull_request +INPUT_AUTOMATIC_TAGGING=true +INPUT_WORKFLOW_STAGE_OPENED=PR Open +INPUT_WORKFLOW_STAGE_MERGED=Ready to Test +INPUT_WORKFLOW_STAGE_CLOSED=Rejected diff --git a/tests/cases/comment-fails/event.json b/tests/cases/comment-fails/event.json new file mode 100644 index 00000000..dd7b97e2 --- /dev/null +++ b/tests/cases/comment-fails/event.json @@ -0,0 +1,27 @@ +{ + "action": "opened", + "number": 42, + "pull_request": { + "number": 42, + "title": "Add OAuth login", + "body": "Task: http://teamwork:8080/app/tasks/1003", + "html_url": "https://github.com/acme/webapp/pull/42", + "head": { + "ref": "feature/oauth", + "repo": { + "full_name": "acme/webapp" + } + }, + "base": { + "ref": "main" + }, + "commits": 3, + "changed_files": 5, + "additions": 120, + "deletions": 8, + "merged": false + }, + "sender": { + "login": "octocat" + } +} diff --git a/tests/cases/comment-fails/expected b/tests/cases/comment-fails/expected new file mode 100644 index 00000000..faeb3aba --- /dev/null +++ b/tests/cases/comment-fails/expected @@ -0,0 +1,20 @@ +exit code: 0 + +GET /projects/api/v1/tasks/1003.json -> 200 + +POST /tasks/1003/comments.json -> 500 + comment: + body: "PR [**Add OAuth login**](https://github.com/acme/webapp/pull/42) opened by **octocat**" + content-type: "text" + isprivate: false + notify: true + +PUT /tasks/1003/tags.json -> 200 + tags: + content: "PR Open" + +GET /projects/api/v3/workflows.json?projectIds=300&include=stages -> 200 + +PATCH /projects/api/v3/tasks/1003/workflows/7.json -> 204 + positionAfterTask: -1 + stageId: 72 diff --git a/tests/cases/comment-fails/inputs b/tests/cases/comment-fails/inputs new file mode 100644 index 00000000..8a3fe736 --- /dev/null +++ b/tests/cases/comment-fails/inputs @@ -0,0 +1,6 @@ +GITHUB_EVENT_NAME=pull_request +INPUT_AUTOMATIC_TAGGING=true +INPUT_WORKFLOW_STAGE_OPENED=PR Open +INPUT_WORKFLOW_STAGE_MERGED=Ready to Test +INPUT_WORKFLOW_STAGE_CLOSED=Rejected +INPUT_LIGHTWEIGHT_COMMENT=true diff --git a/tests/cases/deprecated-board-column-closed/event.json b/tests/cases/deprecated-board-column-closed/event.json new file mode 100644 index 00000000..09ccdcd9 --- /dev/null +++ b/tests/cases/deprecated-board-column-closed/event.json @@ -0,0 +1,27 @@ +{ + "action": "closed", + "number": 42, + "pull_request": { + "number": 42, + "title": "Add OAuth login", + "body": "Task: http://teamwork:8080/app/tasks/1001", + "html_url": "https://github.com/acme/webapp/pull/42", + "head": { + "ref": "feature/oauth", + "repo": { + "full_name": "acme/webapp" + } + }, + "base": { + "ref": "main" + }, + "commits": 3, + "changed_files": 5, + "additions": 120, + "deletions": 8, + "merged": false + }, + "sender": { + "login": "octocat" + } +} diff --git a/tests/cases/deprecated-board-column-closed/expected b/tests/cases/deprecated-board-column-closed/expected new file mode 100644 index 00000000..3d2d9efc --- /dev/null +++ b/tests/cases/deprecated-board-column-closed/expected @@ -0,0 +1,16 @@ +exit code: 0 + +GET /projects/api/v1/tasks/1001.json -> 200 + +POST /tasks/1001/comments.json -> 201 + comment: + body: "PR [**Add OAuth login**](https://github.com/acme/webapp/pull/42) closed without merging by **octocat**" + content-type: "text" + isprivate: false + notify: true + +GET /projects/api/v3/workflows.json?projectIds=100&include=stages -> 200 + +PATCH /projects/api/v3/tasks/1001/workflows/7.json -> 204 + positionAfterTask: -1 + stageId: 74 diff --git a/tests/cases/deprecated-board-column-closed/inputs b/tests/cases/deprecated-board-column-closed/inputs new file mode 100644 index 00000000..3a803811 --- /dev/null +++ b/tests/cases/deprecated-board-column-closed/inputs @@ -0,0 +1,3 @@ +GITHUB_EVENT_NAME=pull_request +INPUT_LIGHTWEIGHT_COMMENT=true +INPUT_BOARD_COLUMN_CLOSED=Rejected diff --git a/tests/cases/deprecated-board-column/event.json b/tests/cases/deprecated-board-column/event.json new file mode 100644 index 00000000..5f80ac3b --- /dev/null +++ b/tests/cases/deprecated-board-column/event.json @@ -0,0 +1,27 @@ +{ + "action": "opened", + "number": 42, + "pull_request": { + "number": 42, + "title": "Add OAuth login", + "body": "Task: http://teamwork:8080/app/tasks/1001", + "html_url": "https://github.com/acme/webapp/pull/42", + "head": { + "ref": "feature/oauth", + "repo": { + "full_name": "acme/webapp" + } + }, + "base": { + "ref": "main" + }, + "commits": 3, + "changed_files": 5, + "additions": 120, + "deletions": 8, + "merged": false + }, + "sender": { + "login": "octocat" + } +} diff --git a/tests/cases/deprecated-board-column/expected b/tests/cases/deprecated-board-column/expected new file mode 100644 index 00000000..395ab4d1 --- /dev/null +++ b/tests/cases/deprecated-board-column/expected @@ -0,0 +1,16 @@ +exit code: 0 + +GET /projects/api/v1/tasks/1001.json -> 200 + +POST /tasks/1001/comments.json -> 201 + comment: + body: "PR [**Add OAuth login**](https://github.com/acme/webapp/pull/42) opened by **octocat**" + content-type: "text" + isprivate: false + notify: true + +GET /projects/api/v3/workflows.json?projectIds=100&include=stages -> 200 + +PATCH /projects/api/v3/tasks/1001/workflows/7.json -> 204 + positionAfterTask: -1 + stageId: 73 diff --git a/tests/cases/deprecated-board-column/inputs b/tests/cases/deprecated-board-column/inputs new file mode 100644 index 00000000..28f49173 --- /dev/null +++ b/tests/cases/deprecated-board-column/inputs @@ -0,0 +1,3 @@ +GITHUB_EVENT_NAME=pull_request +INPUT_LIGHTWEIGHT_COMMENT=true +INPUT_BOARD_COLUMN_OPENED=Ready to Test diff --git a/tests/cases/ignored-project/event.json b/tests/cases/ignored-project/event.json new file mode 100644 index 00000000..a68d412c --- /dev/null +++ b/tests/cases/ignored-project/event.json @@ -0,0 +1,27 @@ +{ + "action": "opened", + "number": 42, + "pull_request": { + "number": 42, + "title": "Add OAuth login", + "body": "Tasks: http://teamwork:8080/app/tasks/1001 and http://teamwork:8080/app/tasks/1002", + "html_url": "https://github.com/acme/webapp/pull/42", + "head": { + "ref": "feature/oauth", + "repo": { + "full_name": "acme/webapp" + } + }, + "base": { + "ref": "main" + }, + "commits": 3, + "changed_files": 5, + "additions": 120, + "deletions": 8, + "merged": false + }, + "sender": { + "login": "octocat" + } +} diff --git a/tests/cases/ignored-project/expected b/tests/cases/ignored-project/expected new file mode 100644 index 00000000..19f4cbd7 --- /dev/null +++ b/tests/cases/ignored-project/expected @@ -0,0 +1,3 @@ +exit code: 0 + +GET /projects/api/v1/tasks/1001.json -> 200 diff --git a/tests/cases/ignored-project/inputs b/tests/cases/ignored-project/inputs new file mode 100644 index 00000000..db11e710 --- /dev/null +++ b/tests/cases/ignored-project/inputs @@ -0,0 +1,3 @@ +GITHUB_EVENT_NAME=pull_request +INPUT_LIGHTWEIGHT_COMMENT=true +IGNORE_PROJECT_IDS=100 300 diff --git a/tests/cases/no-task-links/event.json b/tests/cases/no-task-links/event.json new file mode 100644 index 00000000..4517e6e7 --- /dev/null +++ b/tests/cases/no-task-links/event.json @@ -0,0 +1,27 @@ +{ + "action": "opened", + "number": 42, + "pull_request": { + "number": 42, + "title": "Add OAuth login", + "body": "Refactors the login form.", + "html_url": "https://github.com/acme/webapp/pull/42", + "head": { + "ref": "feature/oauth", + "repo": { + "full_name": "acme/webapp" + } + }, + "base": { + "ref": "main" + }, + "commits": 3, + "changed_files": 5, + "additions": 120, + "deletions": 8, + "merged": false + }, + "sender": { + "login": "octocat" + } +} diff --git a/tests/cases/no-task-links/expected b/tests/cases/no-task-links/expected new file mode 100644 index 00000000..01fe490f --- /dev/null +++ b/tests/cases/no-task-links/expected @@ -0,0 +1 @@ +exit code: 0 diff --git a/tests/cases/no-task-links/inputs b/tests/cases/no-task-links/inputs new file mode 100644 index 00000000..9d43a813 --- /dev/null +++ b/tests/cases/no-task-links/inputs @@ -0,0 +1,5 @@ +GITHUB_EVENT_NAME=pull_request +INPUT_AUTOMATIC_TAGGING=true +INPUT_WORKFLOW_STAGE_OPENED=PR Open +INPUT_WORKFLOW_STAGE_MERGED=Ready to Test +INPUT_WORKFLOW_STAGE_CLOSED=Rejected diff --git a/tests/cases/opened-lightweight/event.json b/tests/cases/opened-lightweight/event.json new file mode 100644 index 00000000..d524f93d --- /dev/null +++ b/tests/cases/opened-lightweight/event.json @@ -0,0 +1,27 @@ +{ + "action": "opened", + "number": 42, + "pull_request": { + "number": 42, + "title": "Handle \"null\" in C:\\temp", + "body": "Task: http://teamwork:8080/app/tasks/1001", + "html_url": "https://github.com/acme/webapp/pull/42", + "head": { + "ref": "feature/oauth", + "repo": { + "full_name": "acme/webapp" + } + }, + "base": { + "ref": "main" + }, + "commits": 3, + "changed_files": 5, + "additions": 120, + "deletions": 8, + "merged": false + }, + "sender": { + "login": "octocat" + } +} diff --git a/tests/cases/opened-lightweight/expected b/tests/cases/opened-lightweight/expected new file mode 100644 index 00000000..55e3f681 --- /dev/null +++ b/tests/cases/opened-lightweight/expected @@ -0,0 +1,20 @@ +exit code: 0 + +GET /projects/api/v1/tasks/1001.json -> 200 + +POST /tasks/1001/comments.json -> 201 + comment: + body: "PR [**Handle null in C:\temp**](https://github.com/acme/webapp/pull/42) opened by **octocat**" + content-type: "text" + isprivate: false + notify: true + +PUT /tasks/1001/tags.json -> 200 + tags: + content: "PR Open" + +GET /projects/api/v3/workflows.json?projectIds=100&include=stages -> 200 + +PATCH /projects/api/v3/tasks/1001/workflows/7.json -> 204 + positionAfterTask: -1 + stageId: 72 diff --git a/tests/cases/opened-lightweight/inputs b/tests/cases/opened-lightweight/inputs new file mode 100644 index 00000000..8a3fe736 --- /dev/null +++ b/tests/cases/opened-lightweight/inputs @@ -0,0 +1,6 @@ +GITHUB_EVENT_NAME=pull_request +INPUT_AUTOMATIC_TAGGING=true +INPUT_WORKFLOW_STAGE_OPENED=PR Open +INPUT_WORKFLOW_STAGE_MERGED=Ready to Test +INPUT_WORKFLOW_STAGE_CLOSED=Rejected +INPUT_LIGHTWEIGHT_COMMENT=true diff --git a/tests/cases/opened/event.json b/tests/cases/opened/event.json new file mode 100644 index 00000000..f3579555 --- /dev/null +++ b/tests/cases/opened/event.json @@ -0,0 +1,27 @@ +{ + "action": "opened", + "number": 42, + "pull_request": { + "number": 42, + "title": "Add \"OAuth\" login", + "body": "## Summary\r\nAdds OAuth login for http://teamwork:8080/app/tasks/1001\r\n\r\n```sh\r\n# configure the \"client\" secret\r\nexport CONFIG_DIR=C:\\Users\\dev\\config\r\n```", + "html_url": "https://github.com/acme/webapp/pull/42", + "head": { + "ref": "feature/oauth", + "repo": { + "full_name": "acme/webapp" + } + }, + "base": { + "ref": "main" + }, + "commits": 3, + "changed_files": 5, + "additions": 120, + "deletions": 8, + "merged": false + }, + "sender": { + "login": "octocat" + } +} diff --git a/tests/cases/opened/expected b/tests/cases/opened/expected new file mode 100644 index 00000000..e550bec5 --- /dev/null +++ b/tests/cases/opened/expected @@ -0,0 +1,17 @@ +exit code: 0 + +GET /projects/api/v1/tasks/1001.json -> 200 + +POST /tasks/1001/comments.json -> 400 + invalid JSON: Invalid control character at: line 1 column 25 (char 24) + "{ \"comment\": { \"body\": \"\n**octocat** opened a PR: **Add OAuth login**\n[https://github.com/acme/webapp/pull/42](https://github.com/acme/webapp/pull/42)\n`main` \u2b05\ufe0f `feature/oauth`\n\n---\n\n#### Summary\r\nAdds OAuth login for http://teamwork:8080/app/tasks/1001\r\n\r\n```sh\r\n#### configure the client secret\r\nexport CONFIG_DIR=C:\\Users\\dev\\config\r\n```\n\n---\n\n\ud83d\udd22 3 commits / \ud83d\udcdd 5 files updated / \u2795 120 additions / \u2796 8 deletions\n\n\", \"notify\": true, \"content-type\": \"text\", \"isprivate\": false } }" + +PUT /tasks/1001/tags.json -> 200 + tags: + content: "PR Open" + +GET /projects/api/v3/workflows.json?projectIds=100&include=stages -> 200 + +PATCH /projects/api/v3/tasks/1001/workflows/7.json -> 204 + positionAfterTask: -1 + stageId: 72 diff --git a/tests/cases/opened/inputs b/tests/cases/opened/inputs new file mode 100644 index 00000000..9d43a813 --- /dev/null +++ b/tests/cases/opened/inputs @@ -0,0 +1,5 @@ +GITHUB_EVENT_NAME=pull_request +INPUT_AUTOMATIC_TAGGING=true +INPUT_WORKFLOW_STAGE_OPENED=PR Open +INPUT_WORKFLOW_STAGE_MERGED=Ready to Test +INPUT_WORKFLOW_STAGE_CLOSED=Rejected diff --git a/tests/cases/review-approved-lightweight/event.json b/tests/cases/review-approved-lightweight/event.json new file mode 100644 index 00000000..48e8f8f7 --- /dev/null +++ b/tests/cases/review-approved-lightweight/event.json @@ -0,0 +1,31 @@ +{ + "action": "submitted", + "number": 42, + "pull_request": { + "number": 42, + "title": "Add OAuth login", + "body": "Task: http://teamwork:8080/app/tasks/1001", + "html_url": "https://github.com/acme/webapp/pull/42", + "head": { + "ref": "feature/oauth", + "repo": { + "full_name": "acme/webapp" + } + }, + "base": { + "ref": "main" + }, + "commits": 3, + "changed_files": 5, + "additions": 120, + "deletions": 8, + "merged": false + }, + "sender": { + "login": "octocat" + }, + "review": { + "state": "approved", + "body": "Looks good!" + } +} diff --git a/tests/cases/review-approved-lightweight/expected b/tests/cases/review-approved-lightweight/expected new file mode 100644 index 00000000..9d4207f1 --- /dev/null +++ b/tests/cases/review-approved-lightweight/expected @@ -0,0 +1,14 @@ +exit code: 0 + +GET /projects/api/v1/tasks/1001.json -> 200 + +POST /tasks/1001/comments.json -> 201 + comment: + body: "PR [**Add OAuth login**](https://github.com/acme/webapp/pull/42) approved by **octocat**" + content-type: "text" + isprivate: false + notify: true + +PUT /tasks/1001/tags.json -> 200 + tags: + content: "PR Approved" diff --git a/tests/cases/review-approved-lightweight/inputs b/tests/cases/review-approved-lightweight/inputs new file mode 100644 index 00000000..1f98d2ae --- /dev/null +++ b/tests/cases/review-approved-lightweight/inputs @@ -0,0 +1,3 @@ +GITHUB_EVENT_NAME=pull_request_review +INPUT_AUTOMATIC_TAGGING=true +INPUT_LIGHTWEIGHT_COMMENT=true diff --git a/tests/cases/review-approved-no-body/event.json b/tests/cases/review-approved-no-body/event.json new file mode 100644 index 00000000..ef9793c8 --- /dev/null +++ b/tests/cases/review-approved-no-body/event.json @@ -0,0 +1,31 @@ +{ + "action": "submitted", + "number": 42, + "pull_request": { + "number": 42, + "title": "Add OAuth login", + "body": "Task: http://teamwork:8080/app/tasks/1001", + "html_url": "https://github.com/acme/webapp/pull/42", + "head": { + "ref": "feature/oauth", + "repo": { + "full_name": "acme/webapp" + } + }, + "base": { + "ref": "main" + }, + "commits": 3, + "changed_files": 5, + "additions": 120, + "deletions": 8, + "merged": false + }, + "sender": { + "login": "octocat" + }, + "review": { + "state": "approved", + "body": null + } +} diff --git a/tests/cases/review-approved-no-body/expected b/tests/cases/review-approved-no-body/expected new file mode 100644 index 00000000..84ff709f --- /dev/null +++ b/tests/cases/review-approved-no-body/expected @@ -0,0 +1,11 @@ +exit code: 0 + +GET /projects/api/v1/tasks/1001.json -> 200 + +POST /tasks/1001/comments.json -> 400 + invalid JSON: Invalid control character at: line 1 column 25 (char 24) + "{ \"comment\": { \"body\": \"\n**octocat** submitted a review to the PR: **Add OAuth login**\n[https://github.com/acme/webapp/pull/42](https://github.com/acme/webapp/pull/42)\n\n---\n\nReview: **approved**\n\n\", \"notify\": true, \"content-type\": \"text\", \"isprivate\": false } }" + +PUT /tasks/1001/tags.json -> 200 + tags: + content: "PR Approved" diff --git a/tests/cases/review-approved-no-body/inputs b/tests/cases/review-approved-no-body/inputs new file mode 100644 index 00000000..bf4f6030 --- /dev/null +++ b/tests/cases/review-approved-no-body/inputs @@ -0,0 +1,5 @@ +GITHUB_EVENT_NAME=pull_request_review +INPUT_AUTOMATIC_TAGGING=true +INPUT_WORKFLOW_STAGE_OPENED=PR Open +INPUT_WORKFLOW_STAGE_MERGED=Ready to Test +INPUT_WORKFLOW_STAGE_CLOSED=Rejected diff --git a/tests/cases/review-approved/event.json b/tests/cases/review-approved/event.json new file mode 100644 index 00000000..3e77ae9d --- /dev/null +++ b/tests/cases/review-approved/event.json @@ -0,0 +1,31 @@ +{ + "action": "submitted", + "number": 42, + "pull_request": { + "number": 42, + "title": "Add OAuth login", + "body": "Task: http://teamwork:8080/app/tasks/1001", + "html_url": "https://github.com/acme/webapp/pull/42", + "head": { + "ref": "feature/oauth", + "repo": { + "full_name": "acme/webapp" + } + }, + "base": { + "ref": "main" + }, + "commits": 3, + "changed_files": 5, + "additions": 120, + "deletions": 8, + "merged": false + }, + "sender": { + "login": "octocat" + }, + "review": { + "state": "approved", + "body": "Looks good!\r\nOne nit: rename `cfg` to \"config\"." + } +} diff --git a/tests/cases/review-approved/expected b/tests/cases/review-approved/expected new file mode 100644 index 00000000..f24cf9cb --- /dev/null +++ b/tests/cases/review-approved/expected @@ -0,0 +1,11 @@ +exit code: 0 + +GET /projects/api/v1/tasks/1001.json -> 200 + +POST /tasks/1001/comments.json -> 400 + invalid JSON: Invalid control character at: line 1 column 25 (char 24) + "{ \"comment\": { \"body\": \"\n**octocat** submitted a review to the PR: **Add OAuth login**\n[https://github.com/acme/webapp/pull/42](https://github.com/acme/webapp/pull/42)\n\n---\n\nReview: **approved**\nLooks good!\r\nOne nit: rename `cfg` to config.\n\", \"notify\": true, \"content-type\": \"text\", \"isprivate\": false } }" + +PUT /tasks/1001/tags.json -> 200 + tags: + content: "PR Approved" diff --git a/tests/cases/review-approved/inputs b/tests/cases/review-approved/inputs new file mode 100644 index 00000000..bf4f6030 --- /dev/null +++ b/tests/cases/review-approved/inputs @@ -0,0 +1,5 @@ +GITHUB_EVENT_NAME=pull_request_review +INPUT_AUTOMATIC_TAGGING=true +INPUT_WORKFLOW_STAGE_OPENED=PR Open +INPUT_WORKFLOW_STAGE_MERGED=Ready to Test +INPUT_WORKFLOW_STAGE_CLOSED=Rejected diff --git a/tests/cases/review-commented/event.json b/tests/cases/review-commented/event.json new file mode 100644 index 00000000..aa67d482 --- /dev/null +++ b/tests/cases/review-commented/event.json @@ -0,0 +1,31 @@ +{ + "action": "submitted", + "number": 42, + "pull_request": { + "number": 42, + "title": "Add OAuth login", + "body": "Task: http://teamwork:8080/app/tasks/1001", + "html_url": "https://github.com/acme/webapp/pull/42", + "head": { + "ref": "feature/oauth", + "repo": { + "full_name": "acme/webapp" + } + }, + "base": { + "ref": "main" + }, + "commits": 3, + "changed_files": 5, + "additions": 120, + "deletions": 8, + "merged": false + }, + "sender": { + "login": "octocat" + }, + "review": { + "state": "commented", + "body": "Why this change?" + } +} diff --git a/tests/cases/review-commented/expected b/tests/cases/review-commented/expected new file mode 100644 index 00000000..19f4cbd7 --- /dev/null +++ b/tests/cases/review-commented/expected @@ -0,0 +1,3 @@ +exit code: 0 + +GET /projects/api/v1/tasks/1001.json -> 200 diff --git a/tests/cases/review-commented/inputs b/tests/cases/review-commented/inputs new file mode 100644 index 00000000..bf4f6030 --- /dev/null +++ b/tests/cases/review-commented/inputs @@ -0,0 +1,5 @@ +GITHUB_EVENT_NAME=pull_request_review +INPUT_AUTOMATIC_TAGGING=true +INPUT_WORKFLOW_STAGE_OPENED=PR Open +INPUT_WORKFLOW_STAGE_MERGED=Ready to Test +INPUT_WORKFLOW_STAGE_CLOSED=Rejected diff --git a/tests/cases/review-dismissed/event.json b/tests/cases/review-dismissed/event.json new file mode 100644 index 00000000..3ad66cf5 --- /dev/null +++ b/tests/cases/review-dismissed/event.json @@ -0,0 +1,31 @@ +{ + "action": "dismissed", + "number": 42, + "pull_request": { + "number": 42, + "title": "Add OAuth login", + "body": "Task: http://teamwork:8080/app/tasks/1001", + "html_url": "https://github.com/acme/webapp/pull/42", + "head": { + "ref": "feature/oauth", + "repo": { + "full_name": "acme/webapp" + } + }, + "base": { + "ref": "main" + }, + "commits": 3, + "changed_files": 5, + "additions": 120, + "deletions": 8, + "merged": false + }, + "sender": { + "login": "octocat" + }, + "review": { + "state": "dismissed", + "body": null + } +} diff --git a/tests/cases/review-dismissed/expected b/tests/cases/review-dismissed/expected new file mode 100644 index 00000000..73b63c87 --- /dev/null +++ b/tests/cases/review-dismissed/expected @@ -0,0 +1,10 @@ +exit code: 0 + +GET /projects/api/v1/tasks/1001.json -> 200 + +POST /tasks/1001/comments.json -> 201 + comment: + body: "Review dismissed by octocat" + content-type: "text" + isprivate: false + notify: true diff --git a/tests/cases/review-dismissed/inputs b/tests/cases/review-dismissed/inputs new file mode 100644 index 00000000..bf4f6030 --- /dev/null +++ b/tests/cases/review-dismissed/inputs @@ -0,0 +1,5 @@ +GITHUB_EVENT_NAME=pull_request_review +INPUT_AUTOMATIC_TAGGING=true +INPUT_WORKFLOW_STAGE_OPENED=PR Open +INPUT_WORKFLOW_STAGE_MERGED=Ready to Test +INPUT_WORKFLOW_STAGE_CLOSED=Rejected diff --git a/tests/cases/stage-not-found/event.json b/tests/cases/stage-not-found/event.json new file mode 100644 index 00000000..5f80ac3b --- /dev/null +++ b/tests/cases/stage-not-found/event.json @@ -0,0 +1,27 @@ +{ + "action": "opened", + "number": 42, + "pull_request": { + "number": 42, + "title": "Add OAuth login", + "body": "Task: http://teamwork:8080/app/tasks/1001", + "html_url": "https://github.com/acme/webapp/pull/42", + "head": { + "ref": "feature/oauth", + "repo": { + "full_name": "acme/webapp" + } + }, + "base": { + "ref": "main" + }, + "commits": 3, + "changed_files": 5, + "additions": 120, + "deletions": 8, + "merged": false + }, + "sender": { + "login": "octocat" + } +} diff --git a/tests/cases/stage-not-found/expected b/tests/cases/stage-not-found/expected new file mode 100644 index 00000000..b1796459 --- /dev/null +++ b/tests/cases/stage-not-found/expected @@ -0,0 +1,12 @@ +exit code: 0 + +GET /projects/api/v1/tasks/1001.json -> 200 + +POST /tasks/1001/comments.json -> 201 + comment: + body: "PR [**Add OAuth login**](https://github.com/acme/webapp/pull/42) opened by **octocat**" + content-type: "text" + isprivate: false + notify: true + +GET /projects/api/v3/workflows.json?projectIds=100&include=stages -> 200 diff --git a/tests/cases/stage-not-found/inputs b/tests/cases/stage-not-found/inputs new file mode 100644 index 00000000..c14aee62 --- /dev/null +++ b/tests/cases/stage-not-found/inputs @@ -0,0 +1,3 @@ +GITHUB_EVENT_NAME=pull_request +INPUT_LIGHTWEIGHT_COMMENT=true +INPUT_WORKFLOW_STAGE_OPENED=Code Review diff --git a/tests/cases/tagging-disabled/event.json b/tests/cases/tagging-disabled/event.json new file mode 100644 index 00000000..97732713 --- /dev/null +++ b/tests/cases/tagging-disabled/event.json @@ -0,0 +1,27 @@ +{ + "action": "closed", + "number": 42, + "pull_request": { + "number": 42, + "title": "Add OAuth login", + "body": "Task: http://teamwork:8080/app/tasks/1001", + "html_url": "https://github.com/acme/webapp/pull/42", + "head": { + "ref": "feature/oauth", + "repo": { + "full_name": "acme/webapp" + } + }, + "base": { + "ref": "main" + }, + "commits": 3, + "changed_files": 5, + "additions": 120, + "deletions": 8, + "merged": true + }, + "sender": { + "login": "octocat" + } +} diff --git a/tests/cases/tagging-disabled/expected b/tests/cases/tagging-disabled/expected new file mode 100644 index 00000000..c39de2a7 --- /dev/null +++ b/tests/cases/tagging-disabled/expected @@ -0,0 +1,16 @@ +exit code: 0 + +GET /projects/api/v1/tasks/1001.json -> 200 + +POST /tasks/1001/comments.json -> 201 + comment: + body: "PR [**Add OAuth login**](https://github.com/acme/webapp/pull/42) merged by **octocat**" + content-type: "text" + isprivate: false + notify: true + +GET /projects/api/v3/workflows.json?projectIds=100&include=stages -> 200 + +PATCH /projects/api/v3/tasks/1001/workflows/7.json -> 204 + positionAfterTask: -1 + stageId: 73 diff --git a/tests/cases/tagging-disabled/inputs b/tests/cases/tagging-disabled/inputs new file mode 100644 index 00000000..60904cd2 --- /dev/null +++ b/tests/cases/tagging-disabled/inputs @@ -0,0 +1,4 @@ +GITHUB_EVENT_NAME=pull_request +INPUT_AUTOMATIC_TAGGING=false +INPUT_WORKFLOW_STAGE_MERGED=Ready to Test +INPUT_LIGHTWEIGHT_COMMENT=true diff --git a/tests/cases/task-links/event.json b/tests/cases/task-links/event.json new file mode 100644 index 00000000..94875ab9 --- /dev/null +++ b/tests/cases/task-links/event.json @@ -0,0 +1,27 @@ +{ + "action": "opened", + "number": 42, + "pull_request": { + "number": 42, + "title": "Add OAuth login", + "body": "Linked twice: [http://teamwork:8080/app/tasks/1001](http://teamwork:8080/app/tasks/1001)\nUpper case, old route: HTTP://TEAMWORK:8080/#/tasks/1002\nAgain: http://teamwork:8080/#tasks/1001\nNot task links: subtasks/5, https://github.com/acme/webapp/blob/main/scripts/tasks/2024_cleanup.py\nAnother Teamwork site: https://acme.teamwork.com/app/tasks/1003\nLook-alike host: http://evil-teamwork:8080/app/tasks/1003", + "html_url": "https://github.com/acme/webapp/pull/42", + "head": { + "ref": "feature/oauth", + "repo": { + "full_name": "acme/webapp" + } + }, + "base": { + "ref": "main" + }, + "commits": 3, + "changed_files": 5, + "additions": 120, + "deletions": 8, + "merged": false + }, + "sender": { + "login": "octocat" + } +} diff --git a/tests/cases/task-links/expected b/tests/cases/task-links/expected new file mode 100644 index 00000000..8bc306ea --- /dev/null +++ b/tests/cases/task-links/expected @@ -0,0 +1,73 @@ +exit code: 0 + +GET /projects/api/v1/tasks/1001.json -> 200 + +POST /tasks/1001/comments.json -> 201 + comment: + body: "PR [**Add OAuth login**](https://github.com/acme/webapp/pull/42) opened by **octocat**" + content-type: "text" + isprivate: false + notify: true + +GET /projects/api/v1/tasks/1001.json -> 200 + +POST /tasks/1001/comments.json -> 201 + comment: + body: "PR [**Add OAuth login**](https://github.com/acme/webapp/pull/42) opened by **octocat**" + content-type: "text" + isprivate: false + notify: true + +GET /projects/api/v1/tasks/1002.json -> 200 + +POST /tasks/1002/comments.json -> 201 + comment: + body: "PR [**Add OAuth login**](https://github.com/acme/webapp/pull/42) opened by **octocat**" + content-type: "text" + isprivate: false + notify: true + +GET /projects/api/v1/tasks/1001.json -> 200 + +POST /tasks/1001/comments.json -> 201 + comment: + body: "PR [**Add OAuth login**](https://github.com/acme/webapp/pull/42) opened by **octocat**" + content-type: "text" + isprivate: false + notify: true + +GET /projects/api/v1/tasks/5.json -> 404 + +POST /tasks/5/comments.json -> 404 + comment: + body: "PR [**Add OAuth login**](https://github.com/acme/webapp/pull/42) opened by **octocat**" + content-type: "text" + isprivate: false + notify: true + +GET /projects/api/v1/tasks/2024.json -> 404 + +POST /tasks/2024/comments.json -> 404 + comment: + body: "PR [**Add OAuth login**](https://github.com/acme/webapp/pull/42) opened by **octocat**" + content-type: "text" + isprivate: false + notify: true + +GET /projects/api/v1/tasks/1003.json -> 200 + +POST /tasks/1003/comments.json -> 500 + comment: + body: "PR [**Add OAuth login**](https://github.com/acme/webapp/pull/42) opened by **octocat**" + content-type: "text" + isprivate: false + notify: true + +GET /projects/api/v1/tasks/1003.json -> 200 + +POST /tasks/1003/comments.json -> 500 + comment: + body: "PR [**Add OAuth login**](https://github.com/acme/webapp/pull/42) opened by **octocat**" + content-type: "text" + isprivate: false + notify: true diff --git a/tests/cases/task-links/inputs b/tests/cases/task-links/inputs new file mode 100644 index 00000000..76857bf4 --- /dev/null +++ b/tests/cases/task-links/inputs @@ -0,0 +1,2 @@ +GITHUB_EVENT_NAME=pull_request +INPUT_LIGHTWEIGHT_COMMENT=true diff --git a/tests/cases/task-not-found/event.json b/tests/cases/task-not-found/event.json new file mode 100644 index 00000000..c9ba3a23 --- /dev/null +++ b/tests/cases/task-not-found/event.json @@ -0,0 +1,27 @@ +{ + "action": "opened", + "number": 42, + "pull_request": { + "number": 42, + "title": "Add OAuth login", + "body": "Tasks: http://teamwork:8080/app/tasks/9999 and http://teamwork:8080/app/tasks/1001", + "html_url": "https://github.com/acme/webapp/pull/42", + "head": { + "ref": "feature/oauth", + "repo": { + "full_name": "acme/webapp" + } + }, + "base": { + "ref": "main" + }, + "commits": 3, + "changed_files": 5, + "additions": 120, + "deletions": 8, + "merged": false + }, + "sender": { + "login": "octocat" + } +} diff --git a/tests/cases/task-not-found/expected b/tests/cases/task-not-found/expected new file mode 100644 index 00000000..57c82da5 --- /dev/null +++ b/tests/cases/task-not-found/expected @@ -0,0 +1,19 @@ +exit code: 0 + +GET /projects/api/v1/tasks/9999.json -> 404 + +POST /tasks/9999/comments.json -> 404 + comment: + body: "PR [**Add OAuth login**](https://github.com/acme/webapp/pull/42) opened by **octocat**" + content-type: "text" + isprivate: false + notify: true + +GET /projects/api/v1/tasks/1001.json -> 200 + +POST /tasks/1001/comments.json -> 201 + comment: + body: "PR [**Add OAuth login**](https://github.com/acme/webapp/pull/42) opened by **octocat**" + content-type: "text" + isprivate: false + notify: true diff --git a/tests/cases/task-not-found/inputs b/tests/cases/task-not-found/inputs new file mode 100644 index 00000000..76857bf4 --- /dev/null +++ b/tests/cases/task-not-found/inputs @@ -0,0 +1,2 @@ +GITHUB_EVENT_NAME=pull_request +INPUT_LIGHTWEIGHT_COMMENT=true diff --git a/tests/cases/unsupported-event/event.json b/tests/cases/unsupported-event/event.json new file mode 100644 index 00000000..da136fb5 --- /dev/null +++ b/tests/cases/unsupported-event/event.json @@ -0,0 +1,27 @@ +{ + "action": "synchronize", + "number": 42, + "pull_request": { + "number": 42, + "title": "Add OAuth login", + "body": "Task: http://teamwork:8080/app/tasks/1001", + "html_url": "https://github.com/acme/webapp/pull/42", + "head": { + "ref": "feature/oauth", + "repo": { + "full_name": "acme/webapp" + } + }, + "base": { + "ref": "main" + }, + "commits": 3, + "changed_files": 5, + "additions": 120, + "deletions": 8, + "merged": false + }, + "sender": { + "login": "octocat" + } +} diff --git a/tests/cases/unsupported-event/expected b/tests/cases/unsupported-event/expected new file mode 100644 index 00000000..19f4cbd7 --- /dev/null +++ b/tests/cases/unsupported-event/expected @@ -0,0 +1,3 @@ +exit code: 0 + +GET /projects/api/v1/tasks/1001.json -> 200 diff --git a/tests/cases/unsupported-event/inputs b/tests/cases/unsupported-event/inputs new file mode 100644 index 00000000..9d43a813 --- /dev/null +++ b/tests/cases/unsupported-event/inputs @@ -0,0 +1,5 @@ +GITHUB_EVENT_NAME=pull_request +INPUT_AUTOMATIC_TAGGING=true +INPUT_WORKFLOW_STAGE_OPENED=PR Open +INPUT_WORKFLOW_STAGE_MERGED=Ready to Test +INPUT_WORKFLOW_STAGE_CLOSED=Rejected diff --git a/tests/cases/wrong-token/event.json b/tests/cases/wrong-token/event.json new file mode 100644 index 00000000..5f80ac3b --- /dev/null +++ b/tests/cases/wrong-token/event.json @@ -0,0 +1,27 @@ +{ + "action": "opened", + "number": 42, + "pull_request": { + "number": 42, + "title": "Add OAuth login", + "body": "Task: http://teamwork:8080/app/tasks/1001", + "html_url": "https://github.com/acme/webapp/pull/42", + "head": { + "ref": "feature/oauth", + "repo": { + "full_name": "acme/webapp" + } + }, + "base": { + "ref": "main" + }, + "commits": 3, + "changed_files": 5, + "additions": 120, + "deletions": 8, + "merged": false + }, + "sender": { + "login": "octocat" + } +} diff --git a/tests/cases/wrong-token/expected b/tests/cases/wrong-token/expected new file mode 100644 index 00000000..a86eb133 --- /dev/null +++ b/tests/cases/wrong-token/expected @@ -0,0 +1,10 @@ +exit code: 0 + +GET /projects/api/v1/tasks/1001.json -> 401 + +POST /tasks/1001/comments.json -> 401 + comment: + body: "PR [**Add OAuth login**](https://github.com/acme/webapp/pull/42) opened by **octocat**" + content-type: "text" + isprivate: false + notify: true diff --git a/tests/cases/wrong-token/inputs b/tests/cases/wrong-token/inputs new file mode 100644 index 00000000..1e001d12 --- /dev/null +++ b/tests/cases/wrong-token/inputs @@ -0,0 +1,3 @@ +GITHUB_EVENT_NAME=pull_request +INPUT_TEAMWORK_API_TOKEN=wrong-token +INPUT_LIGHTWEIGHT_COMMENT=true diff --git a/tests/mock-teamwork.py b/tests/mock-teamwork.py new file mode 100644 index 00000000..2058c2ba --- /dev/null +++ b/tests/mock-teamwork.py @@ -0,0 +1,105 @@ +"""Mock Teamwork API for tests/run.sh. + +Answers from the fixed data below and records every request it receives, in a +readable form, to the file given as its only argument. +""" +import base64 +import json +import re +import sys +from http.server import BaseHTTPRequestHandler, HTTPServer + +LOG_PATH = sys.argv[1] +AUTHORIZATION = "Basic " + base64.b64encode(b"test-token:").decode() + +# Task id -> project id. Requests about any other task fail with a 404. +TASKS = {"1001": 100, "1002": 200, "1003": 300} +# Adding a comment to these tasks fails with a server error. +FAILING_COMMENTS = {"1003"} +# Every project uses the same workflow (id 7), with these stages. +STAGES = { + str(stage_id): {"id": stage_id, "name": name, "workflow": {"id": 7, "type": "workflows"}} + for stage_id, name in [(71, "To Do"), (72, "PR Open"), (73, "Ready to Test"), (74, "Rejected")] +} + + +def render(value, indent=1): + """Renders a JSON body as indented "key: value" lines. Multi-line strings are + shown line by line after a "|" gutter, with control characters such as \\r + shown as their Unicode pictures (␍).""" + pad = " " * indent + if not isinstance(value, dict): + return [pad + json.dumps(value, ensure_ascii=False)] + lines = [] + for key in sorted(value): + item = value[key] + if isinstance(item, dict): + lines += [f"{pad}{key}:", *render(item, indent + 1)] + elif isinstance(item, str) and "\n" in item: + lines.append(f"{pad}{key}:") + for line in item.split("\n"): + line = "".join(chr(0x2400 + ord(c)) if ord(c) < 0x20 else c for c in line) + lines.append(f"{pad} | {line}".rstrip()) + else: + lines.append(f"{pad}{key}: {json.dumps(item, ensure_ascii=False)}") + return lines + + +class Handler(BaseHTTPRequestHandler): + def log_message(self, *args): + pass + + def handle_request(self): + raw = self.rfile.read(int(self.headers.get("Content-Length") or 0)).decode() + status, response, details = self.respond(raw) + # Record before replying, so the log is complete once the action exits + with open(LOG_PATH, "a") as log: + log.write("\n" + "\n".join([f"{self.command} {self.path} -> {status}", *details]) + "\n") + + body = json.dumps(response).encode() if response is not None else b"" + self.send_response(status) + if body: + self.send_header("Content-Type", "application/json") + self.send_header("Content-Length", str(len(body))) + self.end_headers() + self.wfile.write(body) + + do_GET = do_POST = do_PUT = do_PATCH = handle_request + + def respond(self, raw): + """Returns the status, response body and recorded details of a request.""" + details = [] + if raw: + if not self.headers.get("Content-Type", "").startswith("application/json"): + return 415, {"MESSAGE": "Expected a JSON body"}, [" " + json.dumps(raw)] + try: + details = render(json.loads(raw)) + except ValueError as error: + return 400, {"MESSAGE": "Invalid JSON"}, [f" invalid JSON: {error}", " " + json.dumps(raw)] + + if self.headers.get("Authorization") != AUTHORIZATION: + return 401, {"MESSAGE": "Unauthorized"}, details + + task = re.search(r"/tasks/(\d+)", self.path) + if task and task[1] not in TASKS: + return 404, {"MESSAGE": "Not found"}, details + + request = f"{self.command} {self.path}" + if task := re.fullmatch(r"GET /projects/api/v1/tasks/(\d+)\.json", request): + return 200, {"todo-item": {"id": int(task[1]), "project-id": TASKS[task[1]]}}, details + if re.fullmatch(r"GET /projects/api/v3/workflows\.json\?projectIds=\d+&include=stages", request): + return 200, {"workflows": [{"id": 7, "name": "Development"}], "included": {"stages": STAGES}}, details + if task := re.fullmatch(r"POST /tasks/(\d+)/comments\.json", request): + if task[1] in FAILING_COMMENTS: + return 500, {"MESSAGE": "Internal server error"}, details + return 201, {"STATUS": "OK", "commentId": "1"}, details + if re.fullmatch(r"PUT /tasks/\d+/tags\.json", request): + return 200, {"STATUS": "OK"}, details + if re.fullmatch(r"PATCH /projects/api/v3/tasks/\d+/workflows/7\.json", request): + return 204, None, details + return 404, {"MESSAGE": "Unknown route"}, details + + +server = HTTPServer(("", 8080), Handler) +print("listening", flush=True) +server.serve_forever() diff --git a/tests/run.sh b/tests/run.sh new file mode 100755 index 00000000..f2cd198c --- /dev/null +++ b/tests/run.sh @@ -0,0 +1,157 @@ +#!/usr/bin/env bash +# Runs the action's Docker image against a mock Teamwork API +# (tests/mock-teamwork.py) for every case in tests/cases, and compares the exit +# code, stderr and requests the mock received with the case's expected file. +# +# A case is a directory with: +# event.json the GitHub event payload +# inputs KEY=VALUE lines: GITHUB_EVENT_NAME, the INPUT_* env vars GitHub +# creates for the action's inputs, and any other env such as +# IGNORE_PROJECT_IDS. Inputs that aren't set are empty. +# expected what the run should produce +# +# Usage: tests/run.sh [--update] [filter] +# --update rewrites the expected files from the current output +# filter only runs the cases whose name contains it +set -euo pipefail + +ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd) +IMAGE=github-sync:test +NETWORK=github-sync-test +MOCK=github-sync-teamwork + +update=false +filter= +for arg in "$@"; do + if [ "$arg" == "--update" ]; then + update=true + else + filter=$arg + fi +done + +stop_mock() { + docker rm --force "$MOCK" >/dev/null 2>&1 || true + docker network rm "$NETWORK" >/dev/null 2>&1 || true +} + +work=$(mktemp -d) +trap 'stop_mock; rm -rf "$work"' EXIT + +# The action's args, in action.yml order, as the input each one comes from +# (empty for the '' placeholders). The GitHub runner builds them the same way. +arg_inputs=() +while IFS= read -r item || [ -n "$item" ]; do + case $item in + "''") arg_inputs+=("") ;; + "\${{ inputs."*" }}") + item=${item#"\${{ inputs."} + arg_inputs+=("${item%" }}"}") ;; + *) + echo "Unsupported item in action.yml's runs.args: $item" >&2 + exit 1 ;; + esac +done < <(sed -n '/^ args:/,/^[^ #]/ s/^ - //p' "$ROOT/action.yml") + +# Runs the action for the case in $1 the way GitHub does: inputs as INPUT_* env +# vars, passed by name only, and as positional args built from action.yml. +run_case() ( + local -r dir=$1 + + export INPUT_TEAMWORK_URI=http://teamwork:8080 + export INPUT_TEAMWORK_API_TOKEN=test-token + local env_names=(INPUT_TEAMWORK_URI INPUT_TEAMWORK_API_TOKEN) + local line + while IFS= read -r line || [ -n "$line" ]; do + case $line in ''|'#'*) continue ;; esac + export "${line%%=*}=${line#*=}" + env_names+=("${line%%=*}") + done < "$dir/inputs" + + local docker_args=(--rm --network "$NETWORK" --volume "$dir/event.json:/github/workflow/event.json:ro" + --env GITHUB_REPOSITORY=acme/webapp --env GITHUB_EVENT_PATH=/github/workflow/event.json) + local env_name + for env_name in "${env_names[@]}"; do + docker_args+=(--env "$env_name") + done + + local action_args=() input value + for input in "${arg_inputs[@]}"; do + value= + if [ -n "$input" ]; then + env_name="INPUT_$input" + value=${!env_name:-} + fi + action_args+=("$value") + done + + docker run "${docker_args[@]}" "$IMAGE" "${action_args[@]}" +) + +docker build --quiet --tag "$IMAGE" "$ROOT" >/dev/null + +stop_mock +docker network create "$NETWORK" >/dev/null +docker run --detach --name "$MOCK" --network "$NETWORK" --network-alias teamwork \ + --volume "$ROOT/tests/mock-teamwork.py:/mock-teamwork.py:ro" --volume "$work:/out" \ + python:3.13-alpine python /mock-teamwork.py /out/requests >/dev/null +for _ in $(seq 50); do + if docker logs "$MOCK" 2>&1 | grep -q listening; then + break + fi + sleep 0.1 +done +if ! docker logs "$MOCK" 2>&1 | grep -q listening; then + echo "The mock Teamwork API didn't start:" >&2 + docker logs "$MOCK" >&2 + exit 1 +fi + +ran=0 +failed=0 +for dir in "$ROOT"/tests/cases/*/; do + dir=${dir%/} + name=$(basename "$dir") + case $name in *"$filter"*) ;; *) continue ;; esac + ran=$((ran + 1)) + + : > "$work/requests" + code=0 + run_case "$dir" > "$work/stdout" 2> "$work/stderr" || code=$? + { + echo "exit code: $code" + if [ -s "$work/stderr" ]; then + echo + echo "stderr:" + sed 's/^/ /' "$work/stderr" + fi + cat "$work/requests" + } > "$work/actual" + + if $update; then + cp "$work/actual" "$dir/expected" + echo "updated $name" + elif diff -u "$dir/expected" "$work/actual" > "$work/diff" 2>&1; then + echo "ok $name" + else + failed=$((failed + 1)) + echo "FAIL $name" + sed 's/^/ /' "$work/diff" + echo " action stdout:" + sed 's/^/ /' "$work/stdout" + fi +done + +echo +if ((ran == 0)); then + echo "No cases match '$filter'" + exit 1 +fi +if $update; then + echo "Updated $ran expected file(s), review them before committing" +elif ((failed > 0)); then + echo "$failed of $ran case(s) failed" + exit 1 +else + echo "All $ran case(s) passed" +fi From 8f5ef70927c944d2e964bee2eea035b4352ac3e0 Mon Sep 17 00:00:00 2001 From: Marc Izquierdo Date: Tue, 20 Oct 2026 19:53:11 +0200 Subject: [PATCH 12/24] Build comment payloads with jq The comment JSON was assembled by string interpolation, with only double quotes stripped. Backslashes made the payload invalid (C:\Users, \d+, a trailing \), so the comment was rejected, and \n or \u0022 were reinterpreted. Stripping quotes also removed them from PR titles, bodies and review text, including the quotes the lightweight comments put around the title. Let jq escape the body instead. Co-Authored-By: Claude Opus 5.5 --- src/teamwork.sh | 9 ++++-- .../cases/closed-merged-lightweight/expected | 2 +- tests/cases/closed-merged/expected | 13 +++++++-- .../closed-unmerged-lightweight/expected | 2 +- tests/cases/closed-unmerged/expected | 13 +++++++-- tests/cases/comment-fails/expected | 2 +- .../deprecated-board-column-closed/expected | 2 +- tests/cases/deprecated-board-column/expected | 2 +- tests/cases/opened-lightweight/expected | 2 +- tests/cases/opened/expected | 29 +++++++++++++++++-- .../review-approved-lightweight/expected | 2 +- tests/cases/review-approved-no-body/expected | 18 ++++++++++-- tests/cases/review-approved/expected | 19 ++++++++++-- tests/cases/stage-not-found/expected | 2 +- tests/cases/tagging-disabled/expected | 2 +- tests/cases/task-links/expected | 16 +++++----- tests/cases/task-not-found/expected | 4 +-- tests/cases/wrong-token/expected | 2 +- 18 files changed, 103 insertions(+), 38 deletions(-) diff --git a/src/teamwork.sh b/src/teamwork.sh index 4439a1df..be7e18d3 100644 --- a/src/teamwork.sh +++ b/src/teamwork.sh @@ -101,12 +101,15 @@ teamwork::add_comment() { local -r body=$1 if [ "$ENV" == "test" ]; then - log::message "Test - Simulate request. Task ID: $TEAMWORK_TASK_ID - Comment: ${body//\"/}" + log::message "Test - Simulate request. Task ID: $TEAMWORK_TASK_ID - Comment: $body" return fi - response=$(teamwork::request POST "/tasks/$TEAMWORK_TASK_ID/comments.json" \ - "{ \"comment\": { \"body\": \"${body//\"/}\", \"notify\": true, \"content-type\": \"text\", \"isprivate\": false } }") + # jq escapes the body (quotes, backslashes, newlines...) into valid JSON + local -r payload=$(jq -n --arg body "$body" \ + '{ comment: { body: $body, notify: true, "content-type": "text", isprivate: false } }') + + response=$(teamwork::request POST "/tasks/$TEAMWORK_TASK_ID/comments.json" "$payload") log::message "$response" } diff --git a/tests/cases/closed-merged-lightweight/expected b/tests/cases/closed-merged-lightweight/expected index 3c02d13c..efc96a65 100644 --- a/tests/cases/closed-merged-lightweight/expected +++ b/tests/cases/closed-merged-lightweight/expected @@ -4,7 +4,7 @@ GET /projects/api/v1/tasks/1001.json -> 200 POST /tasks/1001/comments.json -> 201 comment: - body: "PR [**Add OAuth login**](https://github.com/acme/webapp/pull/42) merged by **octocat**" + body: "PR [**\"Add OAuth login\"**](https://github.com/acme/webapp/pull/42) merged by **octocat**" content-type: "text" isprivate: false notify: true diff --git a/tests/cases/closed-merged/expected b/tests/cases/closed-merged/expected index 5fb0536e..e7ec2753 100644 --- a/tests/cases/closed-merged/expected +++ b/tests/cases/closed-merged/expected @@ -2,9 +2,16 @@ exit code: 0 GET /projects/api/v1/tasks/1001.json -> 200 -POST /tasks/1001/comments.json -> 400 - invalid JSON: Invalid control character at: line 1 column 25 (char 24) - "{ \"comment\": { \"body\": \"\n**octocat** merged a PR: **Add OAuth login**\n[https://github.com/acme/webapp/pull/42](https://github.com/acme/webapp/pull/42)\n\", \"notify\": true, \"content-type\": \"text\", \"isprivate\": false } }" +POST /tasks/1001/comments.json -> 201 + comment: + body: + | + | **octocat** merged a PR: **Add OAuth login** + | [https://github.com/acme/webapp/pull/42](https://github.com/acme/webapp/pull/42) + | + content-type: "text" + isprivate: false + notify: true PUT /tasks/1001/tags.json -> 200 tags: diff --git a/tests/cases/closed-unmerged-lightweight/expected b/tests/cases/closed-unmerged-lightweight/expected index e4430984..e7628885 100644 --- a/tests/cases/closed-unmerged-lightweight/expected +++ b/tests/cases/closed-unmerged-lightweight/expected @@ -4,7 +4,7 @@ GET /projects/api/v1/tasks/1001.json -> 200 POST /tasks/1001/comments.json -> 201 comment: - body: "PR [**Add OAuth login**](https://github.com/acme/webapp/pull/42) closed without merging by **octocat**" + body: "PR [**\"Add OAuth login\"**](https://github.com/acme/webapp/pull/42) closed without merging by **octocat**" content-type: "text" isprivate: false notify: true diff --git a/tests/cases/closed-unmerged/expected b/tests/cases/closed-unmerged/expected index ab0f25ce..a6bd6e18 100644 --- a/tests/cases/closed-unmerged/expected +++ b/tests/cases/closed-unmerged/expected @@ -2,9 +2,16 @@ exit code: 0 GET /projects/api/v1/tasks/1001.json -> 200 -POST /tasks/1001/comments.json -> 400 - invalid JSON: Invalid control character at: line 1 column 25 (char 24) - "{ \"comment\": { \"body\": \"\n**octocat** closed a PR without merging: **Add OAuth login**\n[https://github.com/acme/webapp/pull/42](https://github.com/acme/webapp/pull/42)\n\", \"notify\": true, \"content-type\": \"text\", \"isprivate\": false } }" +POST /tasks/1001/comments.json -> 201 + comment: + body: + | + | **octocat** closed a PR without merging: **Add OAuth login** + | [https://github.com/acme/webapp/pull/42](https://github.com/acme/webapp/pull/42) + | + content-type: "text" + isprivate: false + notify: true PUT /tasks/1001/tags.json -> 200 removeProvidedTags: "true" diff --git a/tests/cases/comment-fails/expected b/tests/cases/comment-fails/expected index faeb3aba..de1fd952 100644 --- a/tests/cases/comment-fails/expected +++ b/tests/cases/comment-fails/expected @@ -4,7 +4,7 @@ GET /projects/api/v1/tasks/1003.json -> 200 POST /tasks/1003/comments.json -> 500 comment: - body: "PR [**Add OAuth login**](https://github.com/acme/webapp/pull/42) opened by **octocat**" + body: "PR [**\"Add OAuth login\"**](https://github.com/acme/webapp/pull/42) opened by **octocat**" content-type: "text" isprivate: false notify: true diff --git a/tests/cases/deprecated-board-column-closed/expected b/tests/cases/deprecated-board-column-closed/expected index 3d2d9efc..881198eb 100644 --- a/tests/cases/deprecated-board-column-closed/expected +++ b/tests/cases/deprecated-board-column-closed/expected @@ -4,7 +4,7 @@ GET /projects/api/v1/tasks/1001.json -> 200 POST /tasks/1001/comments.json -> 201 comment: - body: "PR [**Add OAuth login**](https://github.com/acme/webapp/pull/42) closed without merging by **octocat**" + body: "PR [**\"Add OAuth login\"**](https://github.com/acme/webapp/pull/42) closed without merging by **octocat**" content-type: "text" isprivate: false notify: true diff --git a/tests/cases/deprecated-board-column/expected b/tests/cases/deprecated-board-column/expected index 395ab4d1..0e1fefba 100644 --- a/tests/cases/deprecated-board-column/expected +++ b/tests/cases/deprecated-board-column/expected @@ -4,7 +4,7 @@ GET /projects/api/v1/tasks/1001.json -> 200 POST /tasks/1001/comments.json -> 201 comment: - body: "PR [**Add OAuth login**](https://github.com/acme/webapp/pull/42) opened by **octocat**" + body: "PR [**\"Add OAuth login\"**](https://github.com/acme/webapp/pull/42) opened by **octocat**" content-type: "text" isprivate: false notify: true diff --git a/tests/cases/opened-lightweight/expected b/tests/cases/opened-lightweight/expected index 55e3f681..0d73eac7 100644 --- a/tests/cases/opened-lightweight/expected +++ b/tests/cases/opened-lightweight/expected @@ -4,7 +4,7 @@ GET /projects/api/v1/tasks/1001.json -> 200 POST /tasks/1001/comments.json -> 201 comment: - body: "PR [**Handle null in C:\temp**](https://github.com/acme/webapp/pull/42) opened by **octocat**" + body: "PR [**\"Handle \"null\" in C:\\temp\"**](https://github.com/acme/webapp/pull/42) opened by **octocat**" content-type: "text" isprivate: false notify: true diff --git a/tests/cases/opened/expected b/tests/cases/opened/expected index e550bec5..6623d45e 100644 --- a/tests/cases/opened/expected +++ b/tests/cases/opened/expected @@ -2,9 +2,32 @@ exit code: 0 GET /projects/api/v1/tasks/1001.json -> 200 -POST /tasks/1001/comments.json -> 400 - invalid JSON: Invalid control character at: line 1 column 25 (char 24) - "{ \"comment\": { \"body\": \"\n**octocat** opened a PR: **Add OAuth login**\n[https://github.com/acme/webapp/pull/42](https://github.com/acme/webapp/pull/42)\n`main` \u2b05\ufe0f `feature/oauth`\n\n---\n\n#### Summary\r\nAdds OAuth login for http://teamwork:8080/app/tasks/1001\r\n\r\n```sh\r\n#### configure the client secret\r\nexport CONFIG_DIR=C:\\Users\\dev\\config\r\n```\n\n---\n\n\ud83d\udd22 3 commits / \ud83d\udcdd 5 files updated / \u2795 120 additions / \u2796 8 deletions\n\n\", \"notify\": true, \"content-type\": \"text\", \"isprivate\": false } }" +POST /tasks/1001/comments.json -> 201 + comment: + body: + | + | **octocat** opened a PR: **Add "OAuth" login** + | [https://github.com/acme/webapp/pull/42](https://github.com/acme/webapp/pull/42) + | `main` ⬅️ `feature/oauth` + | + | --- + | + | #### Summary␍ + | Adds OAuth login for http://teamwork:8080/app/tasks/1001␍ + | ␍ + | ```sh␍ + | #### configure the "client" secret␍ + | export CONFIG_DIR=C:\Users\dev\config␍ + | ``` + | + | --- + | + | 🔢 3 commits / 📝 5 files updated / ➕ 120 additions / ➖ 8 deletions + | + | + content-type: "text" + isprivate: false + notify: true PUT /tasks/1001/tags.json -> 200 tags: diff --git a/tests/cases/review-approved-lightweight/expected b/tests/cases/review-approved-lightweight/expected index 9d4207f1..3a097566 100644 --- a/tests/cases/review-approved-lightweight/expected +++ b/tests/cases/review-approved-lightweight/expected @@ -4,7 +4,7 @@ GET /projects/api/v1/tasks/1001.json -> 200 POST /tasks/1001/comments.json -> 201 comment: - body: "PR [**Add OAuth login**](https://github.com/acme/webapp/pull/42) approved by **octocat**" + body: "PR [**\"Add OAuth login\"**](https://github.com/acme/webapp/pull/42) approved by **octocat**" content-type: "text" isprivate: false notify: true diff --git a/tests/cases/review-approved-no-body/expected b/tests/cases/review-approved-no-body/expected index 84ff709f..4624ea25 100644 --- a/tests/cases/review-approved-no-body/expected +++ b/tests/cases/review-approved-no-body/expected @@ -2,9 +2,21 @@ exit code: 0 GET /projects/api/v1/tasks/1001.json -> 200 -POST /tasks/1001/comments.json -> 400 - invalid JSON: Invalid control character at: line 1 column 25 (char 24) - "{ \"comment\": { \"body\": \"\n**octocat** submitted a review to the PR: **Add OAuth login**\n[https://github.com/acme/webapp/pull/42](https://github.com/acme/webapp/pull/42)\n\n---\n\nReview: **approved**\n\n\", \"notify\": true, \"content-type\": \"text\", \"isprivate\": false } }" +POST /tasks/1001/comments.json -> 201 + comment: + body: + | + | **octocat** submitted a review to the PR: **Add OAuth login** + | [https://github.com/acme/webapp/pull/42](https://github.com/acme/webapp/pull/42) + | + | --- + | + | Review: **approved** + | + | + content-type: "text" + isprivate: false + notify: true PUT /tasks/1001/tags.json -> 200 tags: diff --git a/tests/cases/review-approved/expected b/tests/cases/review-approved/expected index f24cf9cb..5c558047 100644 --- a/tests/cases/review-approved/expected +++ b/tests/cases/review-approved/expected @@ -2,9 +2,22 @@ exit code: 0 GET /projects/api/v1/tasks/1001.json -> 200 -POST /tasks/1001/comments.json -> 400 - invalid JSON: Invalid control character at: line 1 column 25 (char 24) - "{ \"comment\": { \"body\": \"\n**octocat** submitted a review to the PR: **Add OAuth login**\n[https://github.com/acme/webapp/pull/42](https://github.com/acme/webapp/pull/42)\n\n---\n\nReview: **approved**\nLooks good!\r\nOne nit: rename `cfg` to config.\n\", \"notify\": true, \"content-type\": \"text\", \"isprivate\": false } }" +POST /tasks/1001/comments.json -> 201 + comment: + body: + | + | **octocat** submitted a review to the PR: **Add OAuth login** + | [https://github.com/acme/webapp/pull/42](https://github.com/acme/webapp/pull/42) + | + | --- + | + | Review: **approved** + | Looks good!␍ + | One nit: rename `cfg` to "config". + | + content-type: "text" + isprivate: false + notify: true PUT /tasks/1001/tags.json -> 200 tags: diff --git a/tests/cases/stage-not-found/expected b/tests/cases/stage-not-found/expected index b1796459..9bcdc992 100644 --- a/tests/cases/stage-not-found/expected +++ b/tests/cases/stage-not-found/expected @@ -4,7 +4,7 @@ GET /projects/api/v1/tasks/1001.json -> 200 POST /tasks/1001/comments.json -> 201 comment: - body: "PR [**Add OAuth login**](https://github.com/acme/webapp/pull/42) opened by **octocat**" + body: "PR [**\"Add OAuth login\"**](https://github.com/acme/webapp/pull/42) opened by **octocat**" content-type: "text" isprivate: false notify: true diff --git a/tests/cases/tagging-disabled/expected b/tests/cases/tagging-disabled/expected index c39de2a7..121253c7 100644 --- a/tests/cases/tagging-disabled/expected +++ b/tests/cases/tagging-disabled/expected @@ -4,7 +4,7 @@ GET /projects/api/v1/tasks/1001.json -> 200 POST /tasks/1001/comments.json -> 201 comment: - body: "PR [**Add OAuth login**](https://github.com/acme/webapp/pull/42) merged by **octocat**" + body: "PR [**\"Add OAuth login\"**](https://github.com/acme/webapp/pull/42) merged by **octocat**" content-type: "text" isprivate: false notify: true diff --git a/tests/cases/task-links/expected b/tests/cases/task-links/expected index 8bc306ea..10871ef8 100644 --- a/tests/cases/task-links/expected +++ b/tests/cases/task-links/expected @@ -4,7 +4,7 @@ GET /projects/api/v1/tasks/1001.json -> 200 POST /tasks/1001/comments.json -> 201 comment: - body: "PR [**Add OAuth login**](https://github.com/acme/webapp/pull/42) opened by **octocat**" + body: "PR [**\"Add OAuth login\"**](https://github.com/acme/webapp/pull/42) opened by **octocat**" content-type: "text" isprivate: false notify: true @@ -13,7 +13,7 @@ GET /projects/api/v1/tasks/1001.json -> 200 POST /tasks/1001/comments.json -> 201 comment: - body: "PR [**Add OAuth login**](https://github.com/acme/webapp/pull/42) opened by **octocat**" + body: "PR [**\"Add OAuth login\"**](https://github.com/acme/webapp/pull/42) opened by **octocat**" content-type: "text" isprivate: false notify: true @@ -22,7 +22,7 @@ GET /projects/api/v1/tasks/1002.json -> 200 POST /tasks/1002/comments.json -> 201 comment: - body: "PR [**Add OAuth login**](https://github.com/acme/webapp/pull/42) opened by **octocat**" + body: "PR [**\"Add OAuth login\"**](https://github.com/acme/webapp/pull/42) opened by **octocat**" content-type: "text" isprivate: false notify: true @@ -31,7 +31,7 @@ GET /projects/api/v1/tasks/1001.json -> 200 POST /tasks/1001/comments.json -> 201 comment: - body: "PR [**Add OAuth login**](https://github.com/acme/webapp/pull/42) opened by **octocat**" + body: "PR [**\"Add OAuth login\"**](https://github.com/acme/webapp/pull/42) opened by **octocat**" content-type: "text" isprivate: false notify: true @@ -40,7 +40,7 @@ GET /projects/api/v1/tasks/5.json -> 404 POST /tasks/5/comments.json -> 404 comment: - body: "PR [**Add OAuth login**](https://github.com/acme/webapp/pull/42) opened by **octocat**" + body: "PR [**\"Add OAuth login\"**](https://github.com/acme/webapp/pull/42) opened by **octocat**" content-type: "text" isprivate: false notify: true @@ -49,7 +49,7 @@ GET /projects/api/v1/tasks/2024.json -> 404 POST /tasks/2024/comments.json -> 404 comment: - body: "PR [**Add OAuth login**](https://github.com/acme/webapp/pull/42) opened by **octocat**" + body: "PR [**\"Add OAuth login\"**](https://github.com/acme/webapp/pull/42) opened by **octocat**" content-type: "text" isprivate: false notify: true @@ -58,7 +58,7 @@ GET /projects/api/v1/tasks/1003.json -> 200 POST /tasks/1003/comments.json -> 500 comment: - body: "PR [**Add OAuth login**](https://github.com/acme/webapp/pull/42) opened by **octocat**" + body: "PR [**\"Add OAuth login\"**](https://github.com/acme/webapp/pull/42) opened by **octocat**" content-type: "text" isprivate: false notify: true @@ -67,7 +67,7 @@ GET /projects/api/v1/tasks/1003.json -> 200 POST /tasks/1003/comments.json -> 500 comment: - body: "PR [**Add OAuth login**](https://github.com/acme/webapp/pull/42) opened by **octocat**" + body: "PR [**\"Add OAuth login\"**](https://github.com/acme/webapp/pull/42) opened by **octocat**" content-type: "text" isprivate: false notify: true diff --git a/tests/cases/task-not-found/expected b/tests/cases/task-not-found/expected index 57c82da5..48a71c91 100644 --- a/tests/cases/task-not-found/expected +++ b/tests/cases/task-not-found/expected @@ -4,7 +4,7 @@ GET /projects/api/v1/tasks/9999.json -> 404 POST /tasks/9999/comments.json -> 404 comment: - body: "PR [**Add OAuth login**](https://github.com/acme/webapp/pull/42) opened by **octocat**" + body: "PR [**\"Add OAuth login\"**](https://github.com/acme/webapp/pull/42) opened by **octocat**" content-type: "text" isprivate: false notify: true @@ -13,7 +13,7 @@ GET /projects/api/v1/tasks/1001.json -> 200 POST /tasks/1001/comments.json -> 201 comment: - body: "PR [**Add OAuth login**](https://github.com/acme/webapp/pull/42) opened by **octocat**" + body: "PR [**\"Add OAuth login\"**](https://github.com/acme/webapp/pull/42) opened by **octocat**" content-type: "text" isprivate: false notify: true diff --git a/tests/cases/wrong-token/expected b/tests/cases/wrong-token/expected index a86eb133..8ee23a7c 100644 --- a/tests/cases/wrong-token/expected +++ b/tests/cases/wrong-token/expected @@ -4,7 +4,7 @@ GET /projects/api/v1/tasks/1001.json -> 401 POST /tasks/1001/comments.json -> 401 comment: - body: "PR [**Add OAuth login**](https://github.com/acme/webapp/pull/42) opened by **octocat**" + body: "PR [**\"Add OAuth login\"**](https://github.com/acme/webapp/pull/42) opened by **octocat**" content-type: "text" isprivate: false notify: true From 510fa49deddf049205f99252f93e3165f2318996 Mon Sep 17 00:00:00 2001 From: Marc Izquierdo Date: Tue, 20 Oct 2026 19:53:35 +0200 Subject: [PATCH 13/24] Count each linked task once A task linked more than once, e.g. in a Markdown link whose text is also the URL, got one comment, tag and stage move per link. Co-Authored-By: Claude Opus 5.5 --- src/teamwork.sh | 4 +++- tests/cases/task-links/expected | 27 --------------------------- 2 files changed, 3 insertions(+), 28 deletions(-) diff --git a/src/teamwork.sh b/src/teamwork.sh index be7e18d3..e6ddf248 100644 --- a/src/teamwork.sh +++ b/src/teamwork.sh @@ -6,7 +6,9 @@ teamwork::get_task_id_from_body() { pat='tasks\/([0-9]{1,})' while [[ $body =~ $pat ]]; do - task_ids+=( "${BASH_REMATCH[1]}" ) + if ! utils::in_array "${BASH_REMATCH[1]}" "${task_ids[*]}"; then + task_ids+=( "${BASH_REMATCH[1]}" ) + fi body=${body#*"${BASH_REMATCH[0]}"} done diff --git a/tests/cases/task-links/expected b/tests/cases/task-links/expected index 10871ef8..97acb8f2 100644 --- a/tests/cases/task-links/expected +++ b/tests/cases/task-links/expected @@ -2,15 +2,6 @@ exit code: 0 GET /projects/api/v1/tasks/1001.json -> 200 -POST /tasks/1001/comments.json -> 201 - comment: - body: "PR [**\"Add OAuth login\"**](https://github.com/acme/webapp/pull/42) opened by **octocat**" - content-type: "text" - isprivate: false - notify: true - -GET /projects/api/v1/tasks/1001.json -> 200 - POST /tasks/1001/comments.json -> 201 comment: body: "PR [**\"Add OAuth login\"**](https://github.com/acme/webapp/pull/42) opened by **octocat**" @@ -27,15 +18,6 @@ POST /tasks/1002/comments.json -> 201 isprivate: false notify: true -GET /projects/api/v1/tasks/1001.json -> 200 - -POST /tasks/1001/comments.json -> 201 - comment: - body: "PR [**\"Add OAuth login\"**](https://github.com/acme/webapp/pull/42) opened by **octocat**" - content-type: "text" - isprivate: false - notify: true - GET /projects/api/v1/tasks/5.json -> 404 POST /tasks/5/comments.json -> 404 @@ -62,12 +44,3 @@ POST /tasks/1003/comments.json -> 500 content-type: "text" isprivate: false notify: true - -GET /projects/api/v1/tasks/1003.json -> 200 - -POST /tasks/1003/comments.json -> 500 - comment: - body: "PR [**\"Add OAuth login\"**](https://github.com/acme/webapp/pull/42) opened by **octocat**" - content-type: "text" - isprivate: false - notify: true From bd17b95463217f356d5f7a6f765c561a97b8559d Mon Sep 17 00:00:00 2001 From: Marc Izquierdo Date: Tue, 20 Oct 2026 19:53:56 +0200 Subject: [PATCH 14/24] Keep going after a task in an ignored project The IGNORE_PROJECT_IDS check exited the whole run, so tasks linked after one in an ignored project were never processed. Co-Authored-By: Claude Opus 5.5 --- src/main.sh | 2 +- tests/cases/ignored-project/expected | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/main.sh b/src/main.sh index 62a3b37f..fcb3bee2 100644 --- a/src/main.sh +++ b/src/main.sh @@ -61,7 +61,7 @@ main() { if utils::in_array "$project_id" "${ignored_project_ids[*]}" then log::message "ignored due to IGNORE_PROJECT_IDS" - exit 0 + continue fi if [ "$event" == "pull_request" ] && [ "$action" == "opened" ]; then diff --git a/tests/cases/ignored-project/expected b/tests/cases/ignored-project/expected index 19f4cbd7..cfd35fab 100644 --- a/tests/cases/ignored-project/expected +++ b/tests/cases/ignored-project/expected @@ -1,3 +1,12 @@ exit code: 0 GET /projects/api/v1/tasks/1001.json -> 200 + +GET /projects/api/v1/tasks/1002.json -> 200 + +POST /tasks/1002/comments.json -> 201 + comment: + body: "PR [**\"Add OAuth login\"**](https://github.com/acme/webapp/pull/42) opened by **octocat**" + content-type: "text" + isprivate: false + notify: true From d251f1c1d32f5672d85a794c171d2fad6d472133 Mon Sep 17 00:00:00 2001 From: Marc Izquierdo Date: Tue, 20 Oct 2026 19:54:19 +0200 Subject: [PATCH 15/24] Leave code blocks alone when demoting headings The H1-H3 rewrite in the PR body also turned comment lines in fenced code blocks (shell, Python, YAML...) into #### headings. Track fences with awk and only rewrite lines outside them. Co-Authored-By: Claude Opus 5.5 --- src/github.sh | 3 ++- tests/cases/opened/expected | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/github.sh b/src/github.sh index 23547aa7..dd59847d 100644 --- a/src/github.sh +++ b/src/github.sh @@ -13,7 +13,8 @@ github::get_pr_number() { } github::get_pr_body() { - jq --raw-output .pull_request.body "$GITHUB_EVENT_PATH" | sed -r 's/^#{1,3}\ /#### /g' + jq --raw-output .pull_request.body "$GITHUB_EVENT_PATH" |\ + awk '/^[[:space:]]*(```|~~~)/ { fenced = !fenced } !fenced { sub(/^#{1,3} /, "#### ") } { print }' } github::get_head_ref() { diff --git a/tests/cases/opened/expected b/tests/cases/opened/expected index 6623d45e..319ba31d 100644 --- a/tests/cases/opened/expected +++ b/tests/cases/opened/expected @@ -16,7 +16,7 @@ POST /tasks/1001/comments.json -> 201 | Adds OAuth login for http://teamwork:8080/app/tasks/1001␍ | ␍ | ```sh␍ - | #### configure the "client" secret␍ + | # configure the "client" secret␍ | export CONFIG_DIR=C:\Users\dev\config␍ | ``` | From ed5c4f1324dfa158ea46ab4fe8557f1dafc823c1 Mon Sep 17 00:00:00 2001 From: Marc Izquierdo Date: Tue, 20 Oct 2026 19:54:47 +0200 Subject: [PATCH 16/24] Remove unused functions Nothing calls ensure::total_args, github::get_pr_number, github::get_repository_full_name or github::print_all_data. Co-Authored-By: Claude Opus 5.5 --- src/ensure.sh | 10 ---------- src/github.sh | 12 ------------ 2 files changed, 22 deletions(-) diff --git a/src/ensure.sh b/src/ensure.sh index 05f12470..fe4dd489 100644 --- a/src/ensure.sh +++ b/src/ensure.sh @@ -6,13 +6,3 @@ ensure::env_variable_exist() { exit 1 fi } - -ensure::total_args() { - local -r received_args=$(( $# - 1 )) - local -r expected_args=$1 - - if ((received_args != expected_args)); then - echoerr "Illegal number of parameters, $expected_args expected but $received_args found" - exit 1 - fi -} diff --git a/src/github.sh b/src/github.sh index dd59847d..b3932a08 100644 --- a/src/github.sh +++ b/src/github.sh @@ -8,10 +8,6 @@ github::get_action() { jq --raw-output .action "$GITHUB_EVENT_PATH" } -github::get_pr_number() { - jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH" -} - github::get_pr_body() { jq --raw-output .pull_request.body "$GITHUB_EVENT_PATH" |\ awk '/^[[:space:]]*(```|~~~)/ { fenced = !fenced } !fenced { sub(/^#{1,3} /, "#### ") } { print }' @@ -25,10 +21,6 @@ github::get_base_ref() { jq --raw-output .pull_request.base.ref "$GITHUB_EVENT_PATH" } -github::get_repository_full_name() { - jq --raw-output .pull_request.head.repo.full_name "$GITHUB_EVENT_PATH" -} - github::get_pr_url() { jq --raw-output .pull_request.html_url "$GITHUB_EVENT_PATH" } @@ -56,7 +48,3 @@ github::get_review_state() { github::get_review_comment() { jq --raw-output '.review.body // empty' "$GITHUB_EVENT_PATH" } - -github::print_all_data() { - cat "$GITHUB_EVENT_PATH" -} From 38f3395317b28733489b8fc8bfe0826b92d4ab65 Mon Sep 17 00:00:00 2001 From: Marc Izquierdo Date: Tue, 20 Oct 2026 19:55:13 +0200 Subject: [PATCH 17/24] Fix the required environment variable check It called echoerr, which doesn't exist, so an empty variable failed with "command not found" (exit 127), and an unset one hit set -u ("unbound variable") before the check ran. Also check GITHUB_EVENT_NAME, which the action relies on. Co-Authored-By: Claude Opus 5.5 --- src/ensure.sh | 4 ++-- src/main.sh | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ensure.sh b/src/ensure.sh index fe4dd489..3bec16a0 100644 --- a/src/ensure.sh +++ b/src/ensure.sh @@ -1,8 +1,8 @@ #!/usr/bin/env bash ensure::env_variable_exist() { - if [[ -z "${!1}" ]]; then - echoerr "The env variable $1 is required." + if [[ -z "${!1:-}" ]]; then + log::error "The env variable $1 is required." exit 1 fi } diff --git a/src/main.sh b/src/main.sh index fcb3bee2..a2051e5a 100644 --- a/src/main.sh +++ b/src/main.sh @@ -12,6 +12,7 @@ main() { # Ensure env vars and args exist ensure::env_variable_exist "GITHUB_REPOSITORY" ensure::env_variable_exist "GITHUB_EVENT_PATH" + ensure::env_variable_exist "GITHUB_EVENT_NAME" export GITHUB_TOKEN="$1" export TEAMWORK_URI="$2" From 5d17ed3e7d3d76b2689331f30887d73404ae2470 Mon Sep 17 00:00:00 2001 From: Marc Izquierdo Date: Tue, 20 Oct 2026 19:55:42 +0200 Subject: [PATCH 18/24] Pick the event handler before looking tasks up Unsupported events looked up every linked task before giving up. Decide on the handler first, so they make no API calls. The next commit relies on this so that failed lookups can't fail runs for events the action ignores. Co-Authored-By: Claude Opus 5.5 --- src/main.sh | 33 +++++++++++++++----------- tests/cases/unsupported-event/expected | 2 -- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/main.sh b/src/main.sh index a2051e5a..bdd93d71 100644 --- a/src/main.sh +++ b/src/main.sh @@ -49,6 +49,24 @@ main() { log::message "Event: $event - Action: $action" + # Pick the handler before touching the API, so unsupported events make no + # requests at all + local handler + if [ "$event" == "pull_request" ] && [ "$action" == "opened" ]; then + handler=teamwork::pull_request_opened + elif [ "$event" == "pull_request" ] && [ "$action" == "closed" ]; then + handler=teamwork::pull_request_closed + elif [ "$event" == "pull_request_review" ] && [ "$action" == "submitted" ]; then + handler=teamwork::pull_request_review_submitted + elif [ "$event" == "pull_request_review" ] && [ "$action" == "dismissed" ]; then + handler=teamwork::pull_request_review_dismissed + elif [ "$ENV" == "test" ]; then # always run pull_request_opened at the very least when in test + handler=teamwork::pull_request_opened + else + log::message "Operation not allowed" + exit 0 + fi + local project_id IFS=',' read -r -a task_ids <<< "$task_ids_str" for task_id in "${task_ids[@]}"; do @@ -65,20 +83,7 @@ main() { continue fi - 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 - elif [ "$ENV" == "test" ]; then # always run pull_request_opened at the very least when in test - teamwork::pull_request_opened - else - log::message "Operation not allowed" - exit 0 - fi + "$handler" done exit $? diff --git a/tests/cases/unsupported-event/expected b/tests/cases/unsupported-event/expected index 19f4cbd7..01fe490f 100644 --- a/tests/cases/unsupported-event/expected +++ b/tests/cases/unsupported-event/expected @@ -1,3 +1 @@ exit code: 0 - -GET /projects/api/v1/tasks/1001.json -> 200 From 4312cadeeca20c49bf4e1ecb4c7805237d1c35b8 Mon Sep 17 00:00:00 2001 From: Marc Izquierdo Date: Tue, 20 Oct 2026 19:56:48 +0200 Subject: [PATCH 19/24] Fail the run when a Teamwork request fails curl ran without --fail, so HTTP errors (a revoked token, a task the token can't see, a Teamwork outage) were only logged and the run still passed. A failed task lookup also carried on with "null" as the project id. Network errors, on the other hand, aborted the run halfway, and a hung API held the runner until the job timed out. Pass --fail-with-body and --max-time 30, log what failed, carry on with the other steps and tasks, then exit 1 if anything failed. Co-Authored-By: Claude Opus 5.5 --- README.md | 4 +-- src/main.sh | 14 ++++++-- src/teamwork.sh | 54 ++++++++++++++++++++--------- tests/cases/comment-fails/expected | 7 +++- tests/cases/task-links/expected | 25 ++++++------- tests/cases/task-not-found/expected | 14 ++++---- tests/cases/wrong-token/expected | 14 ++++---- 7 files changed, 80 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index 5d8344ce..e124556e 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ Create the next environment vars in your repository: * `TEAMWORK_URI`: The URL of your installation (e.g.: https://yourcompany.teamwork.com) * `TEAMWORK_API_TOKEN`: The API token to authenticate the workflow. Follow [this guide](https://support.teamwork.com/projects/using-teamwork/locating-your-api-key) to find your URL and API key. -**Please Note:** The Teamwork account associated with this API key is the account which these comments will be created under. If this user does not have permission to access the project, this action will be ignored. +**Please Note:** The Teamwork account associated with this API key is the account which these comments will be created under. If this user does not have permission to access a task's project, that task will be skipped and the workflow run will be marked as failed. `GITHUB_TOKEN` doesn't need to be setup in the repository, this var is always available during the workflows execution. @@ -59,7 +59,7 @@ jobs: ## Usage When creating a new PR, write in the description of the PR the URL of the task. The action will automatically add a comment in the task. Optionally, you may choose for a lightweight comment that will only include the PR title, URL and the user that performed the action, to enable this option set the `LIGHTWEIGHT_COMMENT` input to `true`. -Please note, the comment will be created in Teamwork under the account you have attached to this action. If the API key of the user you are using does not have permissions to access certain projects, the comment will not be created. +Please note, the comment will be created in Teamwork under the account you have attached to this action. If the API key of the user you are using does not have permissions to access certain projects, the comment will not be created and the workflow run will report an error. ![GitHub pr comment](./.github/assets/github_pr_comment.png) diff --git a/src/main.sh b/src/main.sh index bdd93d71..f7312623 100644 --- a/src/main.sh +++ b/src/main.sh @@ -67,13 +67,18 @@ main() { exit 0 fi + TEAMWORK_FAILED_REQUESTS=0 + local project_id 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 - project_id="$(teamwork::get_project_id_from_task "$task_id")" + if ! project_id="$(teamwork::get_project_id_from_task "$task_id")" || [[ ! $project_id =~ ^[0-9]+$ ]]; then + teamwork::request_failed "Failed to get the project of task $task_id, check the task exists and the API token's user can access it" + continue + fi export TEAMWORK_PROJECT_ID=$project_id ignored_project_ids=("${IGNORE_PROJECT_IDS:-}") @@ -86,5 +91,10 @@ main() { "$handler" done - exit $? + if ((TEAMWORK_FAILED_REQUESTS > 0)); then + log::error "$TEAMWORK_FAILED_REQUESTS Teamwork request(s) failed, see the errors above" + exit 1 + fi + + exit 0 } diff --git a/src/teamwork.sh b/src/teamwork.sh index e6ddf248..b7ff7302 100644 --- a/src/teamwork.sh +++ b/src/teamwork.sh @@ -18,13 +18,14 @@ teamwork::get_task_id_from_body() { echo "$task_ids_str" } -# Calls the Teamwork API and prints the response body. +# Calls the Teamwork API and prints the response body. Fails on network errors, +# timeouts and HTTP error statuses, printing the error body in the latter case. teamwork::request() { local -r method=$1 local -r path=$2 local -r payload=${3:-} - local args=(-sS -X "$method" -u "$TEAMWORK_API_TOKEN"':') + local args=(-sS --fail-with-body --max-time 30 -X "$method" -u "$TEAMWORK_API_TOKEN"':') if [ -n "$payload" ]; then args+=(-H 'Content-Type: application/json; charset=utf-8' -d "$payload") fi @@ -32,6 +33,13 @@ teamwork::request() { curl "${args[@]}" "$TEAMWORK_URI$path" } +# Logs a failed request and records it, so the run fails once every task has +# been processed. +teamwork::request_failed() { + log::error "$1" + TEAMWORK_FAILED_REQUESTS=$((TEAMWORK_FAILED_REQUESTS + 1)) +} + teamwork::get_project_id_from_task() { local -r task_id=$1 @@ -40,17 +48,15 @@ teamwork::get_project_id_from_task() { return fi - response=$( - teamwork::request GET "/projects/api/v1/tasks/$task_id.json" |\ - jq -r '.["todo-item"]["project-id"]' - ) - echo "$response" + teamwork::request GET "/projects/api/v1/tasks/$task_id.json" |\ + jq -r '.["todo-item"]["project-id"]' } # Resolves a workflow stage name to its " " pair within the # task's project. Matching is a case-sensitive substring match against the stage # name (mirroring the legacy board-column behaviour), and the first match wins. -# Echoes nothing when no stage name is provided or no stage matches. +# Echoes nothing when no stage name is provided or no stage matches, and fails +# when the stages can't be fetched. teamwork::get_workflow_stage() { local -r stage_name=$1 @@ -79,7 +85,11 @@ teamwork::move_task_to_stage() { return fi - local -r stage=$(teamwork::get_workflow_stage "$stage_name") + local stage + if ! stage=$(teamwork::get_workflow_stage "$stage_name"); then + teamwork::request_failed "Failed to fetch the workflow stages of project $TEAMWORK_PROJECT_ID" + return + fi if [ -z "$stage" ]; then log::message "Failed to find a matching workflow stage for '$stage_name'" return @@ -93,8 +103,11 @@ teamwork::move_task_to_stage() { return fi - response=$(teamwork::request PATCH "/projects/api/v3/tasks/$TEAMWORK_TASK_ID/workflows/$workflow_id.json" \ - "{ \"stageId\": $stage_id, \"positionAfterTask\": -1 }") + if ! response=$(teamwork::request PATCH "/projects/api/v3/tasks/$TEAMWORK_TASK_ID/workflows/$workflow_id.json" \ + "{ \"stageId\": $stage_id, \"positionAfterTask\": -1 }"); then + teamwork::request_failed "Failed to move task $TEAMWORK_TASK_ID to the '$stage_name' workflow stage: $response" + return + fi log::message "$response" } @@ -111,7 +124,10 @@ teamwork::add_comment() { local -r payload=$(jq -n --arg body "$body" \ '{ comment: { body: $body, notify: true, "content-type": "text", isprivate: false } }') - response=$(teamwork::request POST "/tasks/$TEAMWORK_TASK_ID/comments.json" "$payload") + if ! response=$(teamwork::request POST "/tasks/$TEAMWORK_TASK_ID/comments.json" "$payload"); then + teamwork::request_failed "Failed to add a comment to task $TEAMWORK_TASK_ID: $response" + return + fi log::message "$response" } @@ -125,8 +141,11 @@ teamwork::add_tag() { fi if [ "$AUTOMATIC_TAGGING" == true ]; then - response=$(teamwork::request PUT "/tasks/$TEAMWORK_TASK_ID/tags.json" \ - "{ \"tags\": { \"content\": \"${tag_name//\"/}\" } }") + if ! response=$(teamwork::request PUT "/tasks/$TEAMWORK_TASK_ID/tags.json" \ + "{ \"tags\": { \"content\": \"${tag_name//\"/}\" } }"); then + teamwork::request_failed "Failed to add the '$tag_name' tag to task $TEAMWORK_TASK_ID: $response" + return + fi log::message "$response" fi @@ -141,8 +160,11 @@ teamwork::remove_tag() { fi if [ "$AUTOMATIC_TAGGING" == true ]; then - response=$(teamwork::request PUT "/tasks/$TEAMWORK_TASK_ID/tags.json" \ - "{ \"tags\": { \"content\": \"${tag_name//\"/}\" },\"removeProvidedTags\":\"true\" }") + if ! response=$(teamwork::request PUT "/tasks/$TEAMWORK_TASK_ID/tags.json" \ + "{ \"tags\": { \"content\": \"${tag_name//\"/}\" },\"removeProvidedTags\":\"true\" }"); then + teamwork::request_failed "Failed to remove the '$tag_name' tag from task $TEAMWORK_TASK_ID: $response" + return + fi log::message "$response" fi diff --git a/tests/cases/comment-fails/expected b/tests/cases/comment-fails/expected index de1fd952..f181ae8c 100644 --- a/tests/cases/comment-fails/expected +++ b/tests/cases/comment-fails/expected @@ -1,4 +1,9 @@ -exit code: 0 +exit code: 1 + +stderr: + curl: (22) The requested URL returned error: 500 + Failed to add a comment to task 1003: {"MESSAGE": "Internal server error"} + 1 Teamwork request(s) failed, see the errors above GET /projects/api/v1/tasks/1003.json -> 200 diff --git a/tests/cases/task-links/expected b/tests/cases/task-links/expected index 97acb8f2..148957e2 100644 --- a/tests/cases/task-links/expected +++ b/tests/cases/task-links/expected @@ -1,4 +1,13 @@ -exit code: 0 +exit code: 1 + +stderr: + curl: (22) The requested URL returned error: 404 + Failed to get the project of task 5, check the task exists and the API token's user can access it + curl: (22) The requested URL returned error: 404 + Failed to get the project of task 2024, check the task exists and the API token's user can access it + curl: (22) The requested URL returned error: 500 + Failed to add a comment to task 1003: {"MESSAGE": "Internal server error"} + 3 Teamwork request(s) failed, see the errors above GET /projects/api/v1/tasks/1001.json -> 200 @@ -20,22 +29,8 @@ POST /tasks/1002/comments.json -> 201 GET /projects/api/v1/tasks/5.json -> 404 -POST /tasks/5/comments.json -> 404 - comment: - body: "PR [**\"Add OAuth login\"**](https://github.com/acme/webapp/pull/42) opened by **octocat**" - content-type: "text" - isprivate: false - notify: true - GET /projects/api/v1/tasks/2024.json -> 404 -POST /tasks/2024/comments.json -> 404 - comment: - body: "PR [**\"Add OAuth login\"**](https://github.com/acme/webapp/pull/42) opened by **octocat**" - content-type: "text" - isprivate: false - notify: true - GET /projects/api/v1/tasks/1003.json -> 200 POST /tasks/1003/comments.json -> 500 diff --git a/tests/cases/task-not-found/expected b/tests/cases/task-not-found/expected index 48a71c91..c4a6fec0 100644 --- a/tests/cases/task-not-found/expected +++ b/tests/cases/task-not-found/expected @@ -1,13 +1,11 @@ -exit code: 0 +exit code: 1 -GET /projects/api/v1/tasks/9999.json -> 404 +stderr: + curl: (22) The requested URL returned error: 404 + Failed to get the project of task 9999, check the task exists and the API token's user can access it + 1 Teamwork request(s) failed, see the errors above -POST /tasks/9999/comments.json -> 404 - comment: - body: "PR [**\"Add OAuth login\"**](https://github.com/acme/webapp/pull/42) opened by **octocat**" - content-type: "text" - isprivate: false - notify: true +GET /projects/api/v1/tasks/9999.json -> 404 GET /projects/api/v1/tasks/1001.json -> 200 diff --git a/tests/cases/wrong-token/expected b/tests/cases/wrong-token/expected index 8ee23a7c..23f51418 100644 --- a/tests/cases/wrong-token/expected +++ b/tests/cases/wrong-token/expected @@ -1,10 +1,8 @@ -exit code: 0 +exit code: 1 -GET /projects/api/v1/tasks/1001.json -> 401 +stderr: + curl: (22) The requested URL returned error: 401 + Failed to get the project of task 1001, check the task exists and the API token's user can access it + 1 Teamwork request(s) failed, see the errors above -POST /tasks/1001/comments.json -> 401 - comment: - body: "PR [**\"Add OAuth login\"**](https://github.com/acme/webapp/pull/42) opened by **octocat**" - content-type: "text" - isprivate: false - notify: true +GET /projects/api/v1/tasks/1001.json -> 401 From 06e13a26605883f3a463aef05c9a74877e1c16fb Mon Sep 17 00:00:00 2001 From: Marc Izquierdo Date: Tue, 20 Oct 2026 19:57:31 +0200 Subject: [PATCH 20/24] Only match task links on the configured Teamwork site Any "tasks/" in the PR body counted as a task link, including "subtasks/5", paths like scripts/tasks/2024_cleanup.py on GitHub, and links to other Teamwork sites, so the action could comment on unrelated tasks. Only count links on the TEAMWORK_URI host whose path has a /tasks/ or #tasks/ part. Test mode keeps accepting any host, since its events link to placeholder sites. Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- src/teamwork.sh | 20 ++++++++++++++++---- tests/cases/task-links/expected | 24 +----------------------- 3 files changed, 18 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index e124556e..5971dcc4 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ jobs: ``` ## Usage -When creating a new PR, write in the description of the PR the URL of the task. The action will automatically add a comment in the task. Optionally, you may choose for a lightweight comment that will only include the PR title, URL and the user that performed the action, to enable this option set the `LIGHTWEIGHT_COMMENT` input to `true`. +When creating a new PR, write in the description of the PR the URL of the task (it must point to the same Teamwork site as `TEAMWORK_URI`, e.g. `https://yourcompany.teamwork.com/app/tasks/123`). The action will automatically add a comment in the task. Optionally, you may choose for a lightweight comment that will only include the PR title, URL and the user that performed the action, to enable this option set the `LIGHTWEIGHT_COMMENT` input to `true`. Please note, the comment will be created in Teamwork under the account you have attached to this action. If the API key of the user you are using does not have permissions to access certain projects, the comment will not be created and the workflow run will report an error. diff --git a/src/teamwork.sh b/src/teamwork.sh index b7ff7302..260c43f8 100644 --- a/src/teamwork.sh +++ b/src/teamwork.sh @@ -1,13 +1,25 @@ #!/usr/bin/env bash +# Extracts the ids of the tasks linked in the PR body, without duplicates. Only +# links to the Teamwork site in TEAMWORK_URI count, e.g. +# https://acme.teamwork.com/app/tasks/123 or https://acme.teamwork.com/#/tasks/123 teamwork::get_task_id_from_body() { - local body=$1 + # Hostnames are case-insensitive, so both the body and the host are lowercased + local body=${1,,} local task_ids=() - pat='tasks\/([0-9]{1,})' + local host=${TEAMWORK_URI,,} + host=${host#*://} + host=${host%%[/?#]*} + host=${host//./[.]} + if [ "$ENV" == "test" ]; then + host='[a-z0-9.-]+' # test events link to placeholder sites + fi + + local -r pat="(^|[^a-z0-9.-])${host}(/[^][()<>[:space:]]*)?[/#]tasks/([0-9]+)" while [[ $body =~ $pat ]]; do - if ! utils::in_array "${BASH_REMATCH[1]}" "${task_ids[*]}"; then - task_ids+=( "${BASH_REMATCH[1]}" ) + if ! utils::in_array "${BASH_REMATCH[3]}" "${task_ids[*]}"; then + task_ids+=( "${BASH_REMATCH[3]}" ) fi body=${body#*"${BASH_REMATCH[0]}"} done diff --git a/tests/cases/task-links/expected b/tests/cases/task-links/expected index 148957e2..7fc65fe7 100644 --- a/tests/cases/task-links/expected +++ b/tests/cases/task-links/expected @@ -1,13 +1,4 @@ -exit code: 1 - -stderr: - curl: (22) The requested URL returned error: 404 - Failed to get the project of task 5, check the task exists and the API token's user can access it - curl: (22) The requested URL returned error: 404 - Failed to get the project of task 2024, check the task exists and the API token's user can access it - curl: (22) The requested URL returned error: 500 - Failed to add a comment to task 1003: {"MESSAGE": "Internal server error"} - 3 Teamwork request(s) failed, see the errors above +exit code: 0 GET /projects/api/v1/tasks/1001.json -> 200 @@ -26,16 +17,3 @@ POST /tasks/1002/comments.json -> 201 content-type: "text" isprivate: false notify: true - -GET /projects/api/v1/tasks/5.json -> 404 - -GET /projects/api/v1/tasks/2024.json -> 404 - -GET /projects/api/v1/tasks/1003.json -> 200 - -POST /tasks/1003/comments.json -> 500 - comment: - body: "PR [**\"Add OAuth login\"**](https://github.com/acme/webapp/pull/42) opened by **octocat**" - content-type: "text" - isprivate: false - notify: true From 2e5ab1fe22bb25e36153375d6e575f3b81f23378 Mon Sep 17 00:00:00 2001 From: Marc Izquierdo Date: Tue, 20 Oct 2026 19:58:02 +0200 Subject: [PATCH 21/24] Stop requiring GITHUB_TOKEN The action never used it. Keep the input, now optional, so workflows that pass it don't get an "unexpected input" warning, and keep its args slot so the other args stay in place. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/test.yml | 1 - README.md | 3 --- action.yml | 5 +++-- src/main.sh | 2 +- 4 files changed, 4 insertions(+), 7 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f7037cdf..368dd45d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -13,7 +13,6 @@ jobs: - uses: actions/checkout@v5 - uses: ./ with: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} TEAMWORK_URI: "localhost" TEAMWORK_API_TOKEN: "test_api_token" AUTOMATIC_TAGGING: true diff --git a/README.md b/README.md index 5971dcc4..5f36515f 100644 --- a/README.md +++ b/README.md @@ -23,8 +23,6 @@ Create the next environment vars in your repository: **Please Note:** The Teamwork account associated with this API key is the account which these comments will be created under. If this user does not have permission to access a task's project, that task will be skipped and the workflow run will be marked as failed. -`GITHUB_TOKEN` doesn't need to be setup in the repository, this var is always available during the workflows execution. - ### Installation Create a new file `/.github/workflows/teamwork.yml` with the following: @@ -44,7 +42,6 @@ jobs: steps: - uses: teamwork/github-sync@master with: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} TEAMWORK_URI: ${{ secrets.TEAMWORK_URI }} TEAMWORK_API_TOKEN: ${{ secrets.TEAMWORK_API_TOKEN }} AUTOMATIC_TAGGING: false diff --git a/action.yml b/action.yml index 014a25f7..16713039 100644 --- a/action.yml +++ b/action.yml @@ -5,8 +5,9 @@ branding: color: 'gray-dark' inputs: GITHUB_TOKEN: - description: 'GitHub token' - required: true + description: 'Deprecated, no longer used.' + required: false + default: '' TEAMWORK_URI: description: 'Teamwork URI' required: true diff --git a/src/main.sh b/src/main.sh index f7312623..caa0a618 100644 --- a/src/main.sh +++ b/src/main.sh @@ -14,7 +14,7 @@ main() { ensure::env_variable_exist "GITHUB_EVENT_PATH" ensure::env_variable_exist "GITHUB_EVENT_NAME" - export GITHUB_TOKEN="$1" + # $1 used to be GITHUB_TOKEN, which was never needed. export TEAMWORK_URI="$2" export TEAMWORK_API_TOKEN="$3" export AUTOMATIC_TAGGING="$4" From aecde292fd78f076ee43242a4870308c37e08198 Mon Sep 17 00:00:00 2001 From: Marc Izquierdo Date: Tue, 20 Oct 2026 19:58:29 +0200 Subject: [PATCH 22/24] Read the Teamwork token from its INPUT_ env var Passing secrets in runs.args put them on the docker run command line, where other processes on the runner can read them; GitHub only masks them in the logs. The runner already gives the container every input as an INPUT_* env var, passed by name only, so read the token from there and leave the secrets' args slots empty. Co-Authored-By: Claude Opus 5.5 --- action.yml | 9 ++++++--- src/main.sh | 6 ++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/action.yml b/action.yml index 16713039..738df59e 100644 --- a/action.yml +++ b/action.yml @@ -48,10 +48,13 @@ inputs: runs: using: 'docker' image: 'Dockerfile' + # Secrets stay out of args, which show up in the runner's process list. The + # Teamwork token is read from its INPUT_TEAMWORK_API_TOKEN env var instead, + # and the empty slots keep the other args in place. args: - - ${{ inputs.GITHUB_TOKEN }} + - '' - ${{ inputs.TEAMWORK_URI }} - - ${{ inputs.TEAMWORK_API_TOKEN }} + - '' - ${{ inputs.AUTOMATIC_TAGGING }} - ${{ inputs.WORKFLOW_STAGE_OPENED }} - ${{ inputs.WORKFLOW_STAGE_MERGED }} @@ -59,4 +62,4 @@ runs: - ${{ inputs.LIGHTWEIGHT_COMMENT }} - ${{ inputs.BOARD_COLUMN_OPENED }} - ${{ inputs.BOARD_COLUMN_MERGED }} - - ${{ inputs.BOARD_COLUMN_CLOSED }} \ No newline at end of file + - ${{ inputs.BOARD_COLUMN_CLOSED }} diff --git a/src/main.sh b/src/main.sh index caa0a618..e1db5e0d 100644 --- a/src/main.sh +++ b/src/main.sh @@ -14,9 +14,11 @@ main() { ensure::env_variable_exist "GITHUB_EVENT_PATH" ensure::env_variable_exist "GITHUB_EVENT_NAME" - # $1 used to be GITHUB_TOKEN, which was never needed. + # $1 used to be GITHUB_TOKEN, which was never needed. The Teamwork token comes + # from the env var GitHub creates for the input, so it isn't on the command + # line; $3 is the fallback for running the image by hand. export TEAMWORK_URI="$2" - export TEAMWORK_API_TOKEN="$3" + export TEAMWORK_API_TOKEN="${INPUT_TEAMWORK_API_TOKEN:-$3}" export AUTOMATIC_TAGGING="$4" export WORKFLOW_STAGE_OPENED="$5" export WORKFLOW_STAGE_MERGED="$6" From 27efa5b41ad15743dc33d0079a52a7c5d3335e06 Mon Sep 17 00:00:00 2001 From: Marc Izquierdo Date: Tue, 20 Oct 2026 19:58:58 +0200 Subject: [PATCH 23/24] Remove the test mode With the tests running the real code against a mock Teamwork API, the "skip the request in test mode" branches only kept CI from exercising the code. Remove them, along with the uses: ./ CI step that relied on them, the old event fixtures and act instructions, and the dummy task link in the PR template. Co-Authored-By: Claude Opus 5.5 --- .github/PULL_REQUEST_TEMPLATE.md | 2 -- .github/workflows/test.yml | 12 ++------- CONTRIBUTING.md | 29 ---------------------- src/main.sh | 4 --- src/misc.sh | 8 ------ src/teamwork.sh | 33 ------------------------- tests/events/pull_request_approved.json | 21 ---------------- tests/events/pull_request_closed.json | 18 -------------- tests/events/pull_request_merged.json | 18 -------------- tests/events/pull_request_opened.json | 25 ------------------- 10 files changed, 2 insertions(+), 168 deletions(-) delete mode 100644 tests/events/pull_request_approved.json delete mode 100644 tests/events/pull_request_closed.json delete mode 100644 tests/events/pull_request_merged.json delete mode 100644 tests/events/pull_request_opened.json diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index 9ab77a04..b9d1bb8b 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -1,3 +1 @@ - -[Test Task](https://twtest.teamwork.com/#/tasks/20613952) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 368dd45d..0228a287 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,9 +1,9 @@ name: teamwork on: + push: + branches: [master] pull_request: - pull_request_review: - types: [submitted, dismissed] jobs: build: @@ -11,13 +11,5 @@ jobs: name: Ensure this project runs steps: - uses: actions/checkout@v5 - - uses: ./ - with: - TEAMWORK_URI: "localhost" - TEAMWORK_API_TOKEN: "test_api_token" - AUTOMATIC_TAGGING: true - WORKFLOW_STAGE_OPENED: "PR Open" - WORKFLOW_STAGE_MERGED: "Ready to Test" - WORKFLOW_STAGE_CLOSED: "Rejected" - name: Run the tests against a mock Teamwork API run: ./tests/run.sh diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index aca161b8..18c132e5 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,11 +1,8 @@ # Contributing -## Getting started - ## Setup local environment * [Install Docker](https://docs.docker.com/get-docker/) -* [Install act tool](https://github.com/nektos/act) ## Running the tests @@ -24,29 +21,3 @@ Each directory in `tests/cases` is a test case: `./tests/run.sh ` only runs the cases whose name contains the filter. To add a case, create its `event.json` and `inputs`, run `./tests/run.sh --update ` to record its `expected` file, and check that file is right before committing it. The mock's data (which tasks exist, their projects and the workflow stages) is at the top of `tests/mock-teamwork.py`. - -## Usage - -Simulate a new pull request - -``` -act pull_request -e tests/events/pull_request_opened.json -``` - -Simulate a pull request has been approved - -``` -act pull_request_review -e tests/events/pull_request_approved.json -``` - -Simulate a pull request has been merged - -``` -act pull_request -e tests/events/pull_request_merged.json -``` - -Simulate a pull request has been closed without merging - -``` -act pull_request -e tests/events/pull_request_closed.json -``` diff --git a/src/main.sh b/src/main.sh index e1db5e0d..0cc5af6f 100644 --- a/src/main.sh +++ b/src/main.sh @@ -35,8 +35,6 @@ main() { : "${WORKFLOW_STAGE_MERGED:=$BOARD_COLUMN_MERGED}" : "${WORKFLOW_STAGE_CLOSED:=$BOARD_COLUMN_CLOSED}" - env::set_environment - # Check if there is a task link in the PR local -r pr_body=$(github::get_pr_body) local -r task_ids_str=$(teamwork::get_task_id_from_body "$pr_body" ) @@ -62,8 +60,6 @@ main() { handler=teamwork::pull_request_review_submitted elif [ "$event" == "pull_request_review" ] && [ "$action" == "dismissed" ]; then handler=teamwork::pull_request_review_dismissed - elif [ "$ENV" == "test" ]; then # always run pull_request_opened at the very least when in test - handler=teamwork::pull_request_opened else log::message "Operation not allowed" exit 0 diff --git a/src/misc.sh b/src/misc.sh index fe935ea9..33ea2037 100644 --- a/src/misc.sh +++ b/src/misc.sh @@ -9,14 +9,6 @@ log::message() { echo "$@" } -env::set_environment() { - if [ "$TEAMWORK_URI" == "localhost" ] && [ "$TEAMWORK_API_TOKEN" == "test_api_token" ]; then - export ENV="test" - else - export ENV="prod" - fi -} - utils::in_array() { ARRAY=$2 for e in ${ARRAY[*]} diff --git a/src/teamwork.sh b/src/teamwork.sh index 260c43f8..9b421cd4 100644 --- a/src/teamwork.sh +++ b/src/teamwork.sh @@ -12,9 +12,6 @@ teamwork::get_task_id_from_body() { host=${host#*://} host=${host%%[/?#]*} host=${host//./[.]} - if [ "$ENV" == "test" ]; then - host='[a-z0-9.-]+' # test events link to placeholder sites - fi local -r pat="(^|[^a-z0-9.-])${host}(/[^][()<>[:space:]]*)?[/#]tasks/([0-9]+)" while [[ $body =~ $pat ]]; do @@ -55,11 +52,6 @@ teamwork::request_failed() { teamwork::get_project_id_from_task() { local -r task_id=$1 - if [ "$ENV" == "test" ]; then - echo "$task_id" - return - fi - teamwork::request GET "/projects/api/v1/tasks/$task_id.json" |\ jq -r '.["todo-item"]["project-id"]' } @@ -76,11 +68,6 @@ teamwork::get_workflow_stage() { return fi - if [ "$ENV" == "test" ]; then - echo "$TEAMWORK_PROJECT_ID $TEAMWORK_PROJECT_ID" - return - fi - # A single call scoped to the project sideloads every stage across the # project's workflow(s); each stage carries its parent workflow id, which the # move endpoint also needs. @@ -110,11 +97,6 @@ teamwork::move_task_to_stage() { local workflow_id stage_id read -r workflow_id stage_id <<< "$stage" - if [ "$ENV" == "test" ]; then - log::message "Test - Simulate request. Task ID: $TEAMWORK_TASK_ID - Project ID: $TEAMWORK_PROJECT_ID - Workflow ID: $workflow_id - Stage ID: $stage_id" - return - fi - if ! response=$(teamwork::request PATCH "/projects/api/v3/tasks/$TEAMWORK_TASK_ID/workflows/$workflow_id.json" \ "{ \"stageId\": $stage_id, \"positionAfterTask\": -1 }"); then teamwork::request_failed "Failed to move task $TEAMWORK_TASK_ID to the '$stage_name' workflow stage: $response" @@ -127,11 +109,6 @@ teamwork::move_task_to_stage() { teamwork::add_comment() { local -r body=$1 - if [ "$ENV" == "test" ]; then - log::message "Test - Simulate request. Task ID: $TEAMWORK_TASK_ID - Comment: $body" - return - fi - # jq escapes the body (quotes, backslashes, newlines...) into valid JSON local -r payload=$(jq -n --arg body "$body" \ '{ comment: { body: $body, notify: true, "content-type": "text", isprivate: false } }') @@ -147,11 +124,6 @@ teamwork::add_comment() { teamwork::add_tag() { local -r tag_name=$1 - if [ "$ENV" == "test" ]; then - log::message "Test - Simulate request. Task ID: $TEAMWORK_TASK_ID - Tag Added: ${tag_name//\"/}" - return - fi - if [ "$AUTOMATIC_TAGGING" == true ]; then if ! response=$(teamwork::request PUT "/tasks/$TEAMWORK_TASK_ID/tags.json" \ "{ \"tags\": { \"content\": \"${tag_name//\"/}\" } }"); then @@ -166,11 +138,6 @@ teamwork::add_tag() { teamwork::remove_tag() { local -r tag_name=$1 - if [ "$ENV" == "test" ]; then - log::message "Test - Simulate request. Task ID: $TEAMWORK_TASK_ID - Tag Removed: ${tag_name//\"/}" - return - fi - if [ "$AUTOMATIC_TAGGING" == true ]; then if ! response=$(teamwork::request PUT "/tasks/$TEAMWORK_TASK_ID/tags.json" \ "{ \"tags\": { \"content\": \"${tag_name//\"/}\" },\"removeProvidedTags\":\"true\" }"); then diff --git a/tests/events/pull_request_approved.json b/tests/events/pull_request_approved.json deleted file mode 100644 index 2ea240ce..00000000 --- a/tests/events/pull_request_approved.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "action": "submitted", - "number": 1, - "pull_request": { - "body": "This is the body of the PR. https://xxxx.teamwork.com/#/tasks/123 https://xxx.teamwork.com/#/tasks/456", - "head": { - "repo": { - "full_name": "teamwork/github-sync" - } - }, - "html_url": "https://github.com/teamwork/github-sync/pull/2", - "title": "This is an example of a title for the PR." - }, - "review": { - "state": "approved", - "body": "This is the comment of the review." - }, - "sender": { - "login": "username" - } -} \ No newline at end of file diff --git a/tests/events/pull_request_closed.json b/tests/events/pull_request_closed.json deleted file mode 100644 index 4e6ee486..00000000 --- a/tests/events/pull_request_closed.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "action": "closed", - "number": 1, - "pull_request": { - "body": "This is the body of the PR. https://xxxx.teamwork.com/#/tasks/123 https://xxx.teamwork.com/#/tasks/456", - "head": { - "repo": { - "full_name": "teamwork/github-sync" - } - }, - "html_url": "https://github.com/teamwork/github-sync/pull/1", - "title": "This is an example of a title for the PR.", - "merged": false - }, - "sender": { - "login": "username" - } -} \ No newline at end of file diff --git a/tests/events/pull_request_merged.json b/tests/events/pull_request_merged.json deleted file mode 100644 index d3b3998b..00000000 --- a/tests/events/pull_request_merged.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "action": "closed", - "number": 1, - "pull_request": { - "body": "This is the body of the PR. https://xxxx.teamwork.com/#/tasks/123 https://xxx.teamwork.com/#/tasks/456", - "head": { - "repo": { - "full_name": "teamwork/github-sync" - } - }, - "html_url": "https://github.com/teamwork/github-sync/pull/1", - "title": "This is an example of a title for the PR.", - "merged": true - }, - "sender": { - "login": "username" - } -} \ No newline at end of file diff --git a/tests/events/pull_request_opened.json b/tests/events/pull_request_opened.json deleted file mode 100644 index 88e64a11..00000000 --- a/tests/events/pull_request_opened.json +++ /dev/null @@ -1,25 +0,0 @@ -{ - "action": "opened", - "number": 1, - "pull_request": { - "body": "# Test heading \n This is the body of the PR. https://xxxx.teamwork.com/#/tasks/123 https://xxx.teamwork.com/#/tasks/456", - "head": { - "ref": "pr-branch", - "repo": { - "full_name": "teamwork/github-sync" - } - }, - "base": { - "ref": "main" - }, - "html_url": "https://github.com/teamwork/github-sync/pull/1", - "title": "This is an example of a title for the PR.", - "commits": 1, - "additions": 2, - "deletions": 3, - "changed_files": 4 - }, - "sender": { - "login": "username" - } -} From e0ac159d44d2b89859ba8b608568bc4513bd2a9d Mon Sep 17 00:00:00 2001 From: Rafael Dantas Justo Date: Wed, 18 Nov 2026 13:15:13 -0300 Subject: [PATCH 24/24] Accept bearer tokens in TEAMWORK_API_TOKEN A token that starts with "tkn." goes in a Bearer header, read by curl from stdin so it stays out of the process list. Other values still use basic auth. Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- action.yml | 2 +- src/teamwork.sh | 10 +++++++-- tests/cases/bearer-token/event.json | 27 +++++++++++++++++++++++ tests/cases/bearer-token/expected | 10 +++++++++ tests/cases/bearer-token/inputs | 3 +++ tests/cases/wrong-bearer-token/event.json | 27 +++++++++++++++++++++++ tests/cases/wrong-bearer-token/expected | 8 +++++++ tests/cases/wrong-bearer-token/inputs | 3 +++ tests/mock-teamwork.py | 4 ++-- 10 files changed, 90 insertions(+), 6 deletions(-) create mode 100644 tests/cases/bearer-token/event.json create mode 100644 tests/cases/bearer-token/expected create mode 100644 tests/cases/bearer-token/inputs create mode 100644 tests/cases/wrong-bearer-token/event.json create mode 100644 tests/cases/wrong-bearer-token/expected create mode 100644 tests/cases/wrong-bearer-token/inputs diff --git a/README.md b/README.md index 5f36515f..e7e3553e 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ ### Prerequisites Create the next environment vars in your repository: * `TEAMWORK_URI`: The URL of your installation (e.g.: https://yourcompany.teamwork.com) -* `TEAMWORK_API_TOKEN`: The API token to authenticate the workflow. Follow [this guide](https://support.teamwork.com/projects/using-teamwork/locating-your-api-key) to find your URL and API key. +* `TEAMWORK_API_TOKEN`: The API token to authenticate the workflow. Follow [this guide](https://support.teamwork.com/projects/using-teamwork/locating-your-api-key) to find your URL and API key. A bearer token (it starts with `tkn.`) also works, follow [this guide](https://apidocs.teamwork.com/guides/teamwork/authentication) to generate one. **Please Note:** The Teamwork account associated with this API key is the account which these comments will be created under. If this user does not have permission to access a task's project, that task will be skipped and the workflow run will be marked as failed. diff --git a/action.yml b/action.yml index 738df59e..0c27db6c 100644 --- a/action.yml +++ b/action.yml @@ -12,7 +12,7 @@ inputs: description: 'Teamwork URI' required: true TEAMWORK_API_TOKEN: - description: 'Teamwork API token' + description: 'Teamwork API key, or a bearer token (starts with "tkn.")' required: true AUTOMATIC_TAGGING: description: 'Do you want to enable automatic tagging: true/false' diff --git a/src/teamwork.sh b/src/teamwork.sh index 9b421cd4..f68a359f 100644 --- a/src/teamwork.sh +++ b/src/teamwork.sh @@ -34,12 +34,18 @@ teamwork::request() { local -r path=$2 local -r payload=${3:-} - local args=(-sS --fail-with-body --max-time 30 -X "$method" -u "$TEAMWORK_API_TOKEN"':') + local args=(-sS --fail-with-body --max-time 30 -X "$method") if [ -n "$payload" ]; then args+=(-H 'Content-Type: application/json; charset=utf-8' -d "$payload") fi - curl "${args[@]}" "$TEAMWORK_URI$path" + # A "tkn." token is a bearer token, anything else is an API key. The bearer + # header goes in on stdin to keep the token out of the process list. + if [[ $TEAMWORK_API_TOKEN == tkn.* ]]; then + curl "${args[@]}" -H @- "$TEAMWORK_URI$path" <<< "Authorization: Bearer $TEAMWORK_API_TOKEN" + else + curl "${args[@]}" -u "$TEAMWORK_API_TOKEN"':' "$TEAMWORK_URI$path" + fi } # Logs a failed request and records it, so the run fails once every task has diff --git a/tests/cases/bearer-token/event.json b/tests/cases/bearer-token/event.json new file mode 100644 index 00000000..5f80ac3b --- /dev/null +++ b/tests/cases/bearer-token/event.json @@ -0,0 +1,27 @@ +{ + "action": "opened", + "number": 42, + "pull_request": { + "number": 42, + "title": "Add OAuth login", + "body": "Task: http://teamwork:8080/app/tasks/1001", + "html_url": "https://github.com/acme/webapp/pull/42", + "head": { + "ref": "feature/oauth", + "repo": { + "full_name": "acme/webapp" + } + }, + "base": { + "ref": "main" + }, + "commits": 3, + "changed_files": 5, + "additions": 120, + "deletions": 8, + "merged": false + }, + "sender": { + "login": "octocat" + } +} diff --git a/tests/cases/bearer-token/expected b/tests/cases/bearer-token/expected new file mode 100644 index 00000000..61b2b98a --- /dev/null +++ b/tests/cases/bearer-token/expected @@ -0,0 +1,10 @@ +exit code: 0 + +GET /projects/api/v1/tasks/1001.json -> 200 + +POST /tasks/1001/comments.json -> 201 + comment: + body: "PR [**\"Add OAuth login\"**](https://github.com/acme/webapp/pull/42) opened by **octocat**" + content-type: "text" + isprivate: false + notify: true diff --git a/tests/cases/bearer-token/inputs b/tests/cases/bearer-token/inputs new file mode 100644 index 00000000..18f518c4 --- /dev/null +++ b/tests/cases/bearer-token/inputs @@ -0,0 +1,3 @@ +GITHUB_EVENT_NAME=pull_request +INPUT_TEAMWORK_API_TOKEN=tkn.test-token +INPUT_LIGHTWEIGHT_COMMENT=true diff --git a/tests/cases/wrong-bearer-token/event.json b/tests/cases/wrong-bearer-token/event.json new file mode 100644 index 00000000..5f80ac3b --- /dev/null +++ b/tests/cases/wrong-bearer-token/event.json @@ -0,0 +1,27 @@ +{ + "action": "opened", + "number": 42, + "pull_request": { + "number": 42, + "title": "Add OAuth login", + "body": "Task: http://teamwork:8080/app/tasks/1001", + "html_url": "https://github.com/acme/webapp/pull/42", + "head": { + "ref": "feature/oauth", + "repo": { + "full_name": "acme/webapp" + } + }, + "base": { + "ref": "main" + }, + "commits": 3, + "changed_files": 5, + "additions": 120, + "deletions": 8, + "merged": false + }, + "sender": { + "login": "octocat" + } +} diff --git a/tests/cases/wrong-bearer-token/expected b/tests/cases/wrong-bearer-token/expected new file mode 100644 index 00000000..23f51418 --- /dev/null +++ b/tests/cases/wrong-bearer-token/expected @@ -0,0 +1,8 @@ +exit code: 1 + +stderr: + curl: (22) The requested URL returned error: 401 + Failed to get the project of task 1001, check the task exists and the API token's user can access it + 1 Teamwork request(s) failed, see the errors above + +GET /projects/api/v1/tasks/1001.json -> 401 diff --git a/tests/cases/wrong-bearer-token/inputs b/tests/cases/wrong-bearer-token/inputs new file mode 100644 index 00000000..8690570f --- /dev/null +++ b/tests/cases/wrong-bearer-token/inputs @@ -0,0 +1,3 @@ +GITHUB_EVENT_NAME=pull_request +INPUT_TEAMWORK_API_TOKEN=tkn.wrong-token +INPUT_LIGHTWEIGHT_COMMENT=true diff --git a/tests/mock-teamwork.py b/tests/mock-teamwork.py index 2058c2ba..35829887 100644 --- a/tests/mock-teamwork.py +++ b/tests/mock-teamwork.py @@ -10,7 +10,7 @@ from http.server import BaseHTTPRequestHandler, HTTPServer LOG_PATH = sys.argv[1] -AUTHORIZATION = "Basic " + base64.b64encode(b"test-token:").decode() +AUTHORIZATIONS = {"Basic " + base64.b64encode(b"test-token:").decode(), "Bearer tkn.test-token"} # Task id -> project id. Requests about any other task fail with a 404. TASKS = {"1001": 100, "1002": 200, "1003": 300} @@ -77,7 +77,7 @@ def respond(self, raw): except ValueError as error: return 400, {"MESSAGE": "Invalid JSON"}, [f" invalid JSON: {error}", " " + json.dumps(raw)] - if self.headers.get("Authorization") != AUTHORIZATION: + if self.headers.get("Authorization") not in AUTHORIZATIONS: return 401, {"MESSAGE": "Unauthorized"}, details task = re.search(r"/tasks/(\d+)", self.path)