Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
20 changes: 16 additions & 4 deletions src/teamwork.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down
24 changes: 1 addition & 23 deletions tests/cases/task-links/expected
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Loading