Skip to content

Commit dba4eb4

Browse files
authored
Merge pull request #79 from Teamwork/remove-test-mode
Remove the test mode
2 parents 3514c60 + 27efa5b commit dba4eb4

82 files changed

Lines changed: 1453 additions & 234 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.github/PULL_REQUEST_TEMPLATE.md‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
11
<!-- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change. -->
2-
3-
[Test Task](https://twtest.teamwork.com/#/tasks/20613952) <!-- Dummy test task to make sure the test job runs - replace with real link where necessary. -->

‎.github/workflows/test.yml‎

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
11
name: teamwork
22

33
on:
4+
push:
5+
branches: [master]
46
pull_request:
5-
pull_request_review:
6-
types: [submitted, dismissed]
77

88
jobs:
99
build:
1010
runs-on: ubuntu-latest
1111
name: Ensure this project runs
1212
steps:
1313
- uses: actions/checkout@v5
14-
- uses: ./
15-
with:
16-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
17-
TEAMWORK_URI: "localhost"
18-
TEAMWORK_API_TOKEN: "test_api_token"
19-
AUTOMATIC_TAGGING: true
20-
WORKFLOW_STAGE_OPENED: "PR Open"
21-
WORKFLOW_STAGE_MERGED: "Ready to Test"
22-
WORKFLOW_STAGE_CLOSED: "Rejected"
14+
- name: Run the tests against a mock Teamwork API
15+
run: ./tests/run.sh

‎CONTRIBUTING.md‎

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,23 @@
11
# Contributing
22

3-
## Getting started
4-
53
## Setup local environment
64

75
* [Install Docker](https://docs.docker.com/get-docker/)
8-
* [Install act tool](https://github.com/nektos/act)
96

10-
## Usage
7+
## Running the tests
118

12-
Simulate a new pull request
9+
The tests run the action's Docker image against a mock Teamwork API (`tests/mock-teamwork.py`) and check what it sends:
1310

1411
```
15-
act pull_request -e tests/events/pull_request_opened.json
12+
./tests/run.sh
1613
```
1714

18-
Simulate a pull request has been approved
15+
Each directory in `tests/cases` is a test case:
1916

20-
```
21-
act pull_request_review -e tests/events/pull_request_approved.json
22-
```
17+
* `event.json`: the GitHub event the action receives
18+
* `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`
19+
* `expected`: the exit code, errors and requests the mock Teamwork API should end up with
2320

24-
Simulate a pull request has been merged
21+
`./tests/run.sh <filter>` only runs the cases whose name contains the filter.
2522

26-
```
27-
act pull_request -e tests/events/pull_request_merged.json
28-
```
29-
30-
Simulate a pull request has been closed without merging
31-
32-
```
33-
act pull_request -e tests/events/pull_request_closed.json
34-
```
23+
To add a case, create its `event.json` and `inputs`, run `./tests/run.sh --update <case name>` 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`.

‎README.md‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ Create the next environment vars in your repository:
2121
* `TEAMWORK_URI`: The URL of your installation (e.g.: https://yourcompany.teamwork.com)
2222
* `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.
2323

24-
**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.
25-
26-
`GITHUB_TOKEN` doesn't need to be setup in the repository, this var is always available during the workflows execution.
24+
**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.
2725

2826
### Installation
2927
Create a new file `/.github/workflows/teamwork.yml` with the following:
@@ -44,7 +42,6 @@ jobs:
4442
steps:
4543
- uses: teamwork/github-sync@master
4644
with:
47-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4845
TEAMWORK_URI: ${{ secrets.TEAMWORK_URI }}
4946
TEAMWORK_API_TOKEN: ${{ secrets.TEAMWORK_API_TOKEN }}
5047
AUTOMATIC_TAGGING: false
@@ -57,9 +54,9 @@ jobs:
5754
```
5855

5956
## Usage
60-
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`.
57+
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`.
6158

62-
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.
59+
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.
6360

6461
![GitHub pr comment](./.github/assets/github_pr_comment.png)
6562

‎action.yml‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ branding:
55
color: 'gray-dark'
66
inputs:
77
GITHUB_TOKEN:
8-
description: 'GitHub token'
9-
required: true
8+
description: 'Deprecated, no longer used.'
9+
required: false
10+
default: ''
1011
TEAMWORK_URI:
1112
description: 'Teamwork URI'
1213
required: true
@@ -47,15 +48,18 @@ inputs:
4748
runs:
4849
using: 'docker'
4950
image: 'Dockerfile'
51+
# Secrets stay out of args, which show up in the runner's process list. The
52+
# Teamwork token is read from its INPUT_TEAMWORK_API_TOKEN env var instead,
53+
# and the empty slots keep the other args in place.
5054
args:
51-
- ${{ inputs.GITHUB_TOKEN }}
55+
- ''
5256
- ${{ inputs.TEAMWORK_URI }}
53-
- ${{ inputs.TEAMWORK_API_TOKEN }}
57+
- ''
5458
- ${{ inputs.AUTOMATIC_TAGGING }}
5559
- ${{ inputs.WORKFLOW_STAGE_OPENED }}
5660
- ${{ inputs.WORKFLOW_STAGE_MERGED }}
5761
- ${{ inputs.WORKFLOW_STAGE_CLOSED }}
5862
- ${{ inputs.LIGHTWEIGHT_COMMENT }}
5963
- ${{ inputs.BOARD_COLUMN_OPENED }}
6064
- ${{ inputs.BOARD_COLUMN_MERGED }}
61-
- ${{ inputs.BOARD_COLUMN_CLOSED }}
65+
- ${{ inputs.BOARD_COLUMN_CLOSED }}

‎src/ensure.sh‎

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
11
#!/usr/bin/env bash
22

33
ensure::env_variable_exist() {
4-
if [[ -z "${!1}" ]]; then
5-
echoerr "The env variable $1 is required."
6-
exit 1
7-
fi
8-
}
9-
10-
ensure::total_args() {
11-
local -r received_args=$(( $# - 1 ))
12-
local -r expected_args=$1
13-
14-
if ((received_args != expected_args)); then
15-
echoerr "Illegal number of parameters, $expected_args expected but $received_args found"
4+
if [[ -z "${!1:-}" ]]; then
5+
log::error "The env variable $1 is required."
166
exit 1
177
fi
188
}

‎src/github.sh‎

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,9 @@ github::get_action() {
88
jq --raw-output .action "$GITHUB_EVENT_PATH"
99
}
1010

11-
github::get_pr_number() {
12-
jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH"
13-
}
14-
1511
github::get_pr_body() {
16-
jq --raw-output .pull_request.body "$GITHUB_EVENT_PATH" | sed -r 's/^#{1,3}\ /#### /g'
12+
jq --raw-output .pull_request.body "$GITHUB_EVENT_PATH" |\
13+
awk '/^[[:space:]]*(```|~~~)/ { fenced = !fenced } !fenced { sub(/^#{1,3} /, "#### ") } { print }'
1714
}
1815

1916
github::get_head_ref() {
@@ -24,10 +21,6 @@ github::get_base_ref() {
2421
jq --raw-output .pull_request.base.ref "$GITHUB_EVENT_PATH"
2522
}
2623

27-
github::get_repository_full_name() {
28-
jq --raw-output .pull_request.head.repo.full_name "$GITHUB_EVENT_PATH"
29-
}
30-
3124
github::get_pr_url() {
3225
jq --raw-output .pull_request.html_url "$GITHUB_EVENT_PATH"
3326
}
@@ -55,7 +48,3 @@ github::get_review_state() {
5548
github::get_review_comment() {
5649
jq --raw-output '.review.body // empty' "$GITHUB_EVENT_PATH"
5750
}
58-
59-
github::print_all_data() {
60-
cat "$GITHUB_EVENT_PATH"
61-
}

‎src/main.sh‎

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ main() {
1212
# Ensure env vars and args exist
1313
ensure::env_variable_exist "GITHUB_REPOSITORY"
1414
ensure::env_variable_exist "GITHUB_EVENT_PATH"
15+
ensure::env_variable_exist "GITHUB_EVENT_NAME"
1516

16-
export GITHUB_TOKEN="$1"
17+
# $1 used to be GITHUB_TOKEN, which was never needed. The Teamwork token comes
18+
# from the env var GitHub creates for the input, so it isn't on the command
19+
# line; $3 is the fallback for running the image by hand.
1720
export TEAMWORK_URI="$2"
18-
export TEAMWORK_API_TOKEN="$3"
21+
export TEAMWORK_API_TOKEN="${INPUT_TEAMWORK_API_TOKEN:-$3}"
1922
export AUTOMATIC_TAGGING="$4"
2023
export WORKFLOW_STAGE_OPENED="$5"
2124
export WORKFLOW_STAGE_MERGED="$6"
@@ -32,8 +35,6 @@ main() {
3235
: "${WORKFLOW_STAGE_MERGED:=$BOARD_COLUMN_MERGED}"
3336
: "${WORKFLOW_STAGE_CLOSED:=$BOARD_COLUMN_CLOSED}"
3437

35-
env::set_environment
36-
3738
# Check if there is a task link in the PR
3839
local -r pr_body=$(github::get_pr_body)
3940
local -r task_ids_str=$(teamwork::get_task_id_from_body "$pr_body" )
@@ -48,37 +49,50 @@ main() {
4849

4950
log::message "Event: $event - Action: $action"
5051

52+
# Pick the handler before touching the API, so unsupported events make no
53+
# requests at all
54+
local handler
55+
if [ "$event" == "pull_request" ] && [ "$action" == "opened" ]; then
56+
handler=teamwork::pull_request_opened
57+
elif [ "$event" == "pull_request" ] && [ "$action" == "closed" ]; then
58+
handler=teamwork::pull_request_closed
59+
elif [ "$event" == "pull_request_review" ] && [ "$action" == "submitted" ]; then
60+
handler=teamwork::pull_request_review_submitted
61+
elif [ "$event" == "pull_request_review" ] && [ "$action" == "dismissed" ]; then
62+
handler=teamwork::pull_request_review_dismissed
63+
else
64+
log::message "Operation not allowed"
65+
exit 0
66+
fi
67+
68+
TEAMWORK_FAILED_REQUESTS=0
69+
5170
local project_id
5271
IFS=',' read -r -a task_ids <<< "$task_ids_str"
5372
for task_id in "${task_ids[@]}"; do
5473
log::message "Task found with the id: $task_id"
5574

5675
export TEAMWORK_TASK_ID=$task_id
57-
project_id="$(teamwork::get_project_id_from_task "$task_id")"
76+
if ! project_id="$(teamwork::get_project_id_from_task "$task_id")" || [[ ! $project_id =~ ^[0-9]+$ ]]; then
77+
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"
78+
continue
79+
fi
5880
export TEAMWORK_PROJECT_ID=$project_id
5981

6082
ignored_project_ids=("${IGNORE_PROJECT_IDS:-}")
6183
if utils::in_array "$project_id" "${ignored_project_ids[*]}"
6284
then
6385
log::message "ignored due to IGNORE_PROJECT_IDS"
64-
exit 0
86+
continue
6587
fi
6688

67-
if [ "$event" == "pull_request" ] && [ "$action" == "opened" ]; then
68-
teamwork::pull_request_opened
69-
elif [ "$event" == "pull_request" ] && [ "$action" == "closed" ]; then
70-
teamwork::pull_request_closed
71-
elif [ "$event" == "pull_request_review" ] && [ "$action" == "submitted" ]; then
72-
teamwork::pull_request_review_submitted
73-
elif [ "$event" == "pull_request_review" ] && [ "$action" == "dismissed" ]; then
74-
teamwork::pull_request_review_dismissed
75-
elif [ "$ENV" == "test" ]; then # always run pull_request_opened at the very least when in test
76-
teamwork::pull_request_opened
77-
else
78-
log::message "Operation not allowed"
79-
exit 0
80-
fi
89+
"$handler"
8190
done
8291

83-
exit $?
92+
if ((TEAMWORK_FAILED_REQUESTS > 0)); then
93+
log::error "$TEAMWORK_FAILED_REQUESTS Teamwork request(s) failed, see the errors above"
94+
exit 1
95+
fi
96+
97+
exit 0
8498
}

‎src/misc.sh‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,6 @@ log::message() {
99
echo "$@"
1010
}
1111

12-
env::set_environment() {
13-
if [ "$TEAMWORK_URI" == "localhost" ] && [ "$TEAMWORK_API_TOKEN" == "test_api_token" ]; then
14-
export ENV="test"
15-
else
16-
export ENV="prod"
17-
fi
18-
}
19-
2012
utils::in_array() {
2113
ARRAY=$2
2214
for e in ${ARRAY[*]}

0 commit comments

Comments
 (0)