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 @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
10 changes: 8 additions & 2 deletions src/teamwork.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions tests/cases/bearer-token/event.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
10 changes: 10 additions & 0 deletions tests/cases/bearer-token/expected
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions tests/cases/bearer-token/inputs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
GITHUB_EVENT_NAME=pull_request
INPUT_TEAMWORK_API_TOKEN=tkn.test-token
INPUT_LIGHTWEIGHT_COMMENT=true
27 changes: 27 additions & 0 deletions tests/cases/wrong-bearer-token/event.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
8 changes: 8 additions & 0 deletions tests/cases/wrong-bearer-token/expected
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions tests/cases/wrong-bearer-token/inputs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
GITHUB_EVENT_NAME=pull_request
INPUT_TEAMWORK_API_TOKEN=tkn.wrong-token
INPUT_LIGHTWEIGHT_COMMENT=true
4 changes: 2 additions & 2 deletions tests/mock-teamwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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)
Expand Down
Loading