Skip to content

Commit 8cb7b83

Browse files
Add first version
1 parent 47d24c0 commit 8cb7b83

8 files changed

Lines changed: 254 additions & 0 deletions

File tree

Dockerfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM alpine:3.11
2+
3+
RUN apk add --no-cache bash curl jq
4+
5+
ADD entrypoint.sh /entrypoint.sh
6+
ADD src /src
7+
8+
ENTRYPOINT ["/entrypoint.sh"]

action.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: 'Teamwork Github'
2+
description: 'This action helps you to keep in sync your PRs and your Teamwork tasks.'
3+
branding:
4+
icon: 'alert-circle'
5+
color: 'gray-dark'
6+
inputs:
7+
GITHUB_TOKEN:
8+
description: 'GitHub token'
9+
required: true
10+
TEAMWORK_URI:
11+
description: 'Teamwork URI'
12+
required: true
13+
TEAMWORK_API_TOKEN:
14+
description: 'Teamwork API token'
15+
required: true
16+
runs:
17+
using: 'docker'
18+
image: 'Dockerfile'
19+
args:
20+
- ${{ inputs.GITHUB_TOKEN }}
21+
- ${{ inputs.TEAMWORK_URI }}
22+
- ${{ inputs.TEAMWORK_API_TOKEN }}

entrypoint.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
PROJECT_HOME="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
5+
6+
if [ "$PROJECT_HOME" == "/" ]; then
7+
PROJECT_HOME=""
8+
fi
9+
10+
export PROJECT_HOME
11+
12+
source "$PROJECT_HOME/src/main.sh"
13+
14+
main "$@"
15+
16+
exit $?

src/ensure.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env bash
2+
3+
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"
16+
exit 1
17+
fi
18+
}

src/github.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env bash
2+
3+
github::get_event_name() {
4+
echo "$GITHUB_EVENT_NAME"
5+
}
6+
7+
github::get_action() {
8+
jq --raw-output .action "$GITHUB_EVENT_PATH"
9+
}
10+
11+
github::get_pr_number() {
12+
jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH"
13+
}
14+
15+
github::get_pr_body() {
16+
jq --raw-output .pull_request.body "$GITHUB_EVENT_PATH"
17+
}
18+
19+
github::get_repository_full_name() {
20+
jq --raw-output .pull_request.head.repo.full_name "$GITHUB_EVENT_PATH"
21+
}
22+
23+
github::get_pr_url() {
24+
jq --raw-output .pull_request.html_url "$GITHUB_EVENT_PATH"
25+
}
26+
27+
github::get_pr_title() {
28+
jq --raw-output .pull_request.title "$GITHUB_EVENT_PATH"
29+
}
30+
31+
github::get_sender_user() {
32+
jq --raw-output .sender.login "$GITHUB_EVENT_PATH"
33+
}
34+
35+
github::print_all_data() {
36+
cat "$GITHUB_EVENT_PATH"
37+
}

src/main.sh

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env bash
2+
3+
source "$PROJECT_HOME/src/ensure.sh"
4+
source "$PROJECT_HOME/src/github.sh"
5+
source "$PROJECT_HOME/src/misc.sh"
6+
source "$PROJECT_HOME/src/teamwork.sh"
7+
8+
main() {
9+
log::message "Running the process..."
10+
11+
# Ensure env vars and args exist
12+
ensure::env_variable_exist "GITHUB_REPOSITORY"
13+
ensure::env_variable_exist "GITHUB_EVENT_PATH"
14+
ensure::total_args 3 "$@"
15+
16+
export GITHUB_TOKEN="$1"
17+
export TEAMWORK_URI="$2"
18+
export TEAMWORK_API_TOKEN="$3"
19+
20+
# Check if there is a task link in the PR
21+
local -r pr_body=$(github::get_pr_body)
22+
local -r task_id=$(teamwork::get_task_id_from_body "$pr_body" )
23+
24+
if [ "$task_id" == "" ]; then
25+
log::message "Task not found"
26+
exit 0
27+
fi
28+
29+
log::message "Task found with the id: "$task_id
30+
31+
export TEAMWORK_TASK_ID=$task_id
32+
33+
local -r event=$(github::get_event_name)
34+
local -r action=$(github::get_action)
35+
36+
log::message "Event: " $event " - Action: " $action
37+
38+
if [ "$event" == "pull_request" ] && [ "$action" == "opened" ]; then
39+
teamwork::pull_request_opened
40+
elif [ "$event" == "pull_request" ] && [ "$action" == "synchronize" ]; then
41+
teamwork::pull_request_synchronize
42+
elif [ "$event" == "pull_request" ] && [ "$action" == "closed" ]; then
43+
teamwork::pull_request_closed
44+
elif [ "$event" == "pull_request_review" ] && [ "$action" == "submitted" ]; then
45+
teamwork::pull_request_review_submitted
46+
elif [ "$event" == "pull_request_review" ] && [ "$action" == "edited" ]; then
47+
teamwork::pull_request_review_edited
48+
elif [ "$event" == "pull_request_review" ] && [ "$action" == "dismissed" ]; then
49+
teamwork::pull_request_review_dismissed
50+
elif [ "$event" == "pull_request_review_comment" ] && [ "$action" == "created" ]; then
51+
teamwork::pull_request_review_comment_created
52+
elif [ "$event" == "pull_request_review_comment" ] && [ "$action" == "deleted" ]; then
53+
teamwork::pull_request_review_comment_deleted
54+
elif [ "$event" == "issue_comment" ] && [ "$action" == "created" ]; then
55+
teamwork::issue_comment_created
56+
else
57+
log::message "Operation not allowed"
58+
exit 0
59+
fi
60+
61+
exit $?
62+
}

src/misc.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bash
2+
3+
log::error() {
4+
echo "$@" 1>&2
5+
}
6+
7+
log::message() {
8+
echo "--------------"
9+
echo "$@"
10+
}

src/teamwork.sh

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env bash
2+
3+
teamwork::get_task_id_from_body() {
4+
local -r body=$1
5+
6+
pat='tasks\/[0-9]{1,}'
7+
task=$(echo $body | grep -Eo $pat)
8+
task_id=$(echo $task | tr -cd '[[:digit:]]')
9+
10+
echo $task_id
11+
}
12+
13+
teamwork::add_comment() {
14+
local -r body=$1
15+
16+
response=$(curl -X "POST" "$TEAMWORK_URI/projects/api/v1/tasks/$TEAMWORK_TASK_ID/comments.json" \
17+
-H 'Authorization: Bearer '$TEAMWORK_API_TOKEN \
18+
-H 'Content-Type: application/json; charset=utf-8' \
19+
-d "{ \"comment\": { \"body\": \"$body\", \"notify\": \"\", \"content-type\": \"text\", \"isprivate\": false } }" )
20+
21+
echo $response
22+
}
23+
24+
teamwork::pull_request_opened() {
25+
local -r pr_url=$(github::get_pr_url)
26+
local -r pr_title=$(github::get_pr_title)
27+
local -r user=$(github::get_sender_user)
28+
29+
teamwork::add_comment "
30+
**$user** opened a PR: **$pr_title**
31+
[$pr_url]($pr_url)
32+
"
33+
}
34+
35+
teamwork::pull_request_synchronize() {
36+
local -r pr_url=$(github::get_pr_url)
37+
local -r pr_title=$(github::get_pr_title)
38+
local -r user=$(github::get_sender_user)
39+
40+
teamwork::add_comment "
41+
**$user** updated a PR: **$pr_title**
42+
[$pr_url]($pr_url)
43+
"
44+
}
45+
46+
teamwork::pull_request_closed() {
47+
local -r user=$(github::get_sender_user)
48+
teamwork::add_comment "
49+
**$user** closed a PR: **$pr_title**
50+
[$pr_url]($pr_url)
51+
"
52+
}
53+
54+
teamwork::pull_request_review_submitted() {
55+
local -r user=$(github::get_sender_user)
56+
teamwork::add_comment "
57+
**$user** submited a review to the PR: **$pr_title**
58+
[$pr_url]($pr_url)
59+
"
60+
}
61+
62+
teamwork::pull_request_review_edited() {
63+
teamwork::add_comment "Review edited"
64+
}
65+
66+
teamwork::pull_request_review_dismissed() {
67+
teamwork::add_comment "Review dismissed"
68+
}
69+
70+
teamwork::pull_request_review_comment_created() {
71+
teamwork::add_comment "Added a new comment to the review"
72+
}
73+
74+
teamwork::pull_request_review_comment_deleted() {
75+
teamwork::add_comment "Review deleted"
76+
}
77+
78+
teamwork::issue_comment_created() {
79+
local -r user=$(github::get_sender_user)
80+
teamwork::add_comment "Comment added by $user"
81+
}

0 commit comments

Comments
 (0)