From 06e13a26605883f3a463aef05c9a74877e1c16fb Mon Sep 17 00:00:00 2001 From: Marc Izquierdo Date: Tue, 20 Oct 2026 19:57:31 +0200 Subject: [PATCH] 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