Skip to content
This repository was archived by the owner on Dec 2, 2020. It is now read-only.

Commit 758cc3b

Browse files
author
Christopher Mancini
committed
context improvements
- added base_context - added syntax highlighting to README - removed obsolete groundcrew section - added `put` context param to override the source param - created makefile for ease of development / testing - bumped `jq` version in makefile
1 parent bed72b0 commit 758cc3b

13 files changed

+169
-73
lines changed

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
FROM alpine
22
RUN apk --no-cache add curl ca-certificates gettext \
3-
&& curl -Ls https://github.com/stedolan/jq/releases/download/jq-1.5/jq-linux64 > /usr/bin/jq && chmod +x /usr/bin/jq
3+
&& curl -Ls https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64 > /usr/bin/jq && chmod +x /usr/bin/jq
44
ADD bin /opt/resource

Makefile

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
SHELL=/bin/bash
2+
3+
root_mkfile := $(abspath $(lastword $(MAKEFILE_LIST)))
4+
root_dir := $(dir $(root_mkfile))
5+
6+
.PHONY: resource-dev
7+
resource-dev:
8+
@docker run \
9+
-v $(root_dir):/resource \
10+
--rm -i -t dpb587/github-status-resource:master \
11+
/bin/ash
12+
13+
.PHONY: test
14+
test:
15+
@docker run \
16+
-v $(root_dir):/resource \
17+
--rm -i -t dpb587/github-status-resource:master \
18+
/resource/test/all.sh

README.md

+46-64
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,22 @@
22

33
A [Concourse](http://concourse.ci/) [resource](http://concourse.ci/resources.html) to interact with the [GitHub Status](https://developer.github.com/v3/repos/statuses/) type.
44

5-
65
## Configuration
76

87
* **`repository`** - the owner and repository name, slash delimited (e.g. `dpb587/github-status-resource`)
98
* **`access_token`** - GitHub API access token from a user with write access to the repository (minimum token scope of `repo:status`)
109
* `branch` - the branch currently being monitored (default: `master`)
10+
* `base_context` - prefix for the context label (default: `concourse-ci`)
1111
* `context` - a label to differentiate this status from the status of other systems (default: `default`)
1212
* `endpoint` - GitHub API endpoint (default: `https://api.github.com`)
1313
* `skip_ssl_verification` - Disable certificate validation for GitHub API calls (default: `false`)
1414

15-
1615
## Behavior
1716

18-
1917
### `check`
2018

2119
Triggers when the status of the branch for the configured context has been updated.
2220

23-
2421
### `in`
2522

2623
Lookup the state of a status.
@@ -31,7 +28,6 @@ Lookup the state of a status.
3128
* `/target_url` - the target URL associated with the status
3229
* `/updated_at` - when the status was last updated
3330

34-
3531
### `out`
3632

3733
Update the status of a commit. Optionally include a description and target URL which will be referenced from GitHub.
@@ -43,78 +39,65 @@ Parameters:
4339
* `description` - a short description of the status
4440
* `description_path` - path to an input file whose data is the value of `description`
4541
* `target_url` - the target URL to associate with the status (default: concourse build link)
46-
42+
* `context` - overrides the source context value (default: `""`)
4743

4844
## Example
4945

5046
A typical use case is to update the status of a commit as it traverses your pipeline. The following example marks the commit as pending before unit tests start. Once unit tests finish, the status is updated to either success or failure depending on how the task completes.
5147

52-
---
53-
jobs:
54-
- name: "unit-tests"
55-
plan:
56-
- get: "repo"
57-
trigger: true
58-
- put: "repo-status" # +
59-
params: { state: "pending", commit: "repo" } # +
60-
- task: "unit-tests"
61-
file: "repo/ci/unit-tests/task.yml"
62-
on_failure:
63-
- put: "repo-status" # +
64-
params: { state: "failure", commit: "repo" } # +
65-
- put: "repo-status" # +
66-
params: { state: "success", commit: "repo" } # +
67-
resources:
68-
- name: "repo"
69-
type: "git"
70-
source:
71-
uri: {{repo_uri}}
72-
branch: {{repo_branch}}
73-
- name: "repo-status" # +
74-
type: "github-status" # +
75-
source: # +
76-
repository: {{repo_github_path}} # +
77-
access_token: {{repo_github_token}} # +
48+
```yaml
49+
jobs:
50+
- name: "unit-tests"
51+
plan:
52+
- get: "repo"
53+
trigger: true
54+
- put: "repo-status" # +
55+
params: { state: "pending", commit: "repo" } # +
56+
- task: "unit-tests"
57+
file: "repo/ci/unit-tests/task.yml"
58+
on_failure:
59+
- put: "repo-status" # +
60+
params: { state: "failure", commit: "repo" } # +
61+
- put: "repo-status" # +
62+
params: { state: "success", commit: "repo" } # +
63+
resources:
64+
- name: "repo"
65+
type: "git"
66+
source:
67+
uri: {{repo_uri}}
68+
branch: {{repo_branch}}
69+
- name: "repo-status" # +
70+
type: "github-status" # +
71+
source: # +
72+
repository: {{repo_github_path}} # +
73+
access_token: {{repo_github_token}} # +
74+
```
7875
7976
When testing pull requests, use the PR ref as the `branch`. For example, if testing PR #12345 to your repository, your resource might look like...
8077

81-
name: "pr-status"
82-
type: "github-status"
83-
source:
84-
repository: {{repo_github_path}}
85-
access_token: {{repo_github_token}}
86-
branch: "pull/12345/head" # +
78+
```yaml
79+
name: "pr-status"
80+
type: "github-status"
81+
source:
82+
repository: {{repo_github_path}}
83+
access_token: {{repo_github_token}}
84+
branch: "pull/12345/head" # +
85+
```
8786

8887
For another pipeline example, see [`ci/pipelines/main.yml`](ci/pipelines/main.yml) which operates against this repository.
8988

90-
9189
## Installation
9290

93-
This resource is not included with the standard Concourse release. Use one of the following methods to make this resource available to your pipelines.
94-
95-
96-
### Deployment-wide
97-
98-
To install on all Concourse workers, update your deployment manifest properties to include a new `groundcrew.resource_types` entry...
99-
100-
properties:
101-
groundcrew:
102-
additional_resource_types:
103-
- image: "docker:///dpb587/github-status-resource#master" # +
104-
type: "github-status" # +
105-
106-
107-
### Pipeline-specific
108-
109-
To use on a single pipeline, update your pipeline to include a new `resource_types` entry...
110-
111-
resource_types:
112-
- name: "github-status" # +
113-
type: "docker-image" # +
114-
source: # +
115-
repository: "dpb587/github-status-resource" # +
116-
tag: "master" # +
91+
This resource is not included with the standard Concourse release. Add it to your pipeline's `resource_types` definition.
11792

93+
```yaml
94+
resource_types:
95+
- name: "github-status"
96+
type: "docker-image"
97+
source:
98+
repository: "dpb587/github-status-resource"
99+
tag: "master"
100+
```
118101

119102
## References
120103

@@ -123,7 +106,6 @@ To use on a single pipeline, update your pipeline to include a new `resource_typ
123106
* [Enabling required status checks (help.github.com)](https://help.github.com/articles/enabling-required-status-checks/)
124107
* [Personal Access Tokens (github.com)](https://github.com/settings/tokens)
125108

126-
127109
## License
128110

129111
[MIT License](./LICENSE)

bin/check

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ load_source
77

88
curlgh "$source_endpoint/repos/$source_repository/commits/$source_branch/status" \
99
| jq -c \
10-
--arg context "$source_context" \
10+
--arg context "$source_base_context/$source_context" \
1111
'.sha as $commit | .statuses | map(select( $context == .context )) | map({ "commit": $commit, "status": ( .id | tostring ) })' \
1212
>&3

bin/common.sh

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ load_source () {
2424
"source_repository": .source.repository,
2525
"source_access_token": .source.access_token,
2626
"source_branch": ( .source.branch // "master" ),
27+
"source_base_context": ( .source.base_context // "concourse-ci" ),
2728
"source_context": ( .source.context // "default" ),
2829
"source_endpoint": ( .source.endpoint // "https://api.github.com" ),
2930
"skip_ssl_verification": ( .source.skip_ssl_verification // "false" )

bin/out

+7-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ load_source
88
eval $( jq -r '{
99
"params_commit": .params.commit,
1010
"params_state": .params.state,
11+
"params_context": ( .params.context // "" ),
1112
"params_description": ( .params.description // "" ),
1213
"params_description_path": ( .params.description_path // "" ),
1314
"params_target_url": ( .params.target_url // ""),
@@ -73,11 +74,16 @@ fi
7374
# execute
7475
#
7576

77+
cntxt="$source_context"
78+
if [[ -n "$params_context" ]] ; then
79+
cntxt="$params_context"
80+
fi
81+
7682
jq -c -n \
7783
--arg state "$params_state" \
7884
--arg target_url "$( echo "$target_url" | buildtpl )" \
7985
--arg description "$( cat $description_path )" \
80-
--arg context "$source_context" \
86+
--arg context "$(echo "$source_base_context/$cntxt" | buildtpl)" \
8187
'{
8288
"context": $context,
8389
"description": $description,

test/all.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ cd $dir
77

88
export TMPDIR="${TMPDIR:-/tmp}"
99

10-
for test in $( find test -type f -perm +111 -print | grep -v 'test/all.sh' ) ; do
10+
for test in $( find test -type f -print | grep -v 'test/all.sh' ) ; do
1111
echo "==> $test"
1212
$test
1313
result=$?

test/check/simple.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ HTTP/1.0 200 OK
3030
"description": "Testing has completed successfully",
3131
"id": 2,
3232
"url": "https://api.github.com/repos/octocat/Hello-World/statuses/2",
33-
"context": "test-context"
33+
"context": "concourse-ci/test-context"
3434
}
3535
]
3636
}

test/out/default-target-url.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ if ! grep -q '^POST /repos/dpb587/test-repo/statuses/a1b2c3d4e5 ' $TMPDIR/http.r
6262
exit 1
6363
fi
6464

65-
if ! grep -q '^{"context":"test-context","description":"test-description","state":"success","target_url":"http://localhost/builds/123"}$' $TMPDIR/http.req-$$ ; then
65+
if ! grep -q '^{"context":"concourse-ci/test-context","description":"test-description","state":"success","target_url":"http://localhost/builds/123"}$' $TMPDIR/http.req-$$ ; then
6666
echo "FAILURE: Unexpected request body"
6767
cat $TMPDIR/http.req-$$
6868
exit 1

test/out/put_context.sh

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/bin/sh
2+
3+
set -eu
4+
5+
DIR=$( dirname "$0" )/../..
6+
7+
echo 'a1b2c3d4e5' > $TMPDIR/commit
8+
9+
cat <<EOF | nc -l -s 127.0.0.1 -p 9192 > $TMPDIR/http.req-$$ &
10+
HTTP/1.0 200 OK
11+
12+
{
13+
"created_at": "2012-07-20T01:19:13Z",
14+
"updated_at": "2012-07-20T02:19:13Z",
15+
"state": "success",
16+
"target_url": "https://ci.example.com/1000/output",
17+
"description": "Build has completed successfully",
18+
"id": 1,
19+
"url": "https://api.github.com/repos/octocat/Hello-World/statuses/1",
20+
"context": "continuous-integration/jenkins",
21+
"creator": {
22+
"login": "octocat",
23+
"id": 1,
24+
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
25+
"gravatar_id": "",
26+
"url": "https://api.github.com/users/octocat",
27+
"html_url": "https://github.com/octocat",
28+
"followers_url": "https://api.github.com/users/octocat/followers",
29+
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
30+
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
31+
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
32+
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
33+
"organizations_url": "https://api.github.com/users/octocat/orgs",
34+
"repos_url": "https://api.github.com/users/octocat/repos",
35+
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
36+
"received_events_url": "https://api.github.com/users/octocat/received_events",
37+
"type": "User",
38+
"site_admin": false
39+
}
40+
}
41+
EOF
42+
43+
BUILD_ID=123 BUILD_JOB_NAME=myjob $DIR/bin/out > $TMPDIR/resource-$$ <<EOF
44+
{
45+
"params": {
46+
"description": "test-description",
47+
"commit": "$TMPDIR/commit",
48+
"context": "override-context/\$BUILD_JOB_NAME",
49+
"state": "success",
50+
"target_url": "https://ci.example.com/\$BUILD_ID/output"
51+
},
52+
"source": {
53+
"access_token": "test-token",
54+
"context": "test-context",
55+
"endpoint": "http://127.0.0.1:9192",
56+
"repository": "dpb587/test-repo"
57+
}
58+
}
59+
EOF
60+
61+
if ! grep -q '^POST /repos/dpb587/test-repo/statuses/a1b2c3d4e5 ' $TMPDIR/http.req-$$ ; then
62+
echo "FAILURE: Invalid HTTP method or URI"
63+
cat $TMPDIR/http.req-$$
64+
exit 1
65+
fi
66+
67+
if ! grep -q '^{"context":"concourse-ci/override-context/myjob","description":"test-description","state":"success","target_url":"https://ci.example.com/123/output"}$' $TMPDIR/http.req-$$ ; then
68+
echo "FAILURE: Unexpected request body"
69+
cat $TMPDIR/http.req-$$
70+
exit 1
71+
fi
72+
73+
if ! grep -q '"version":{"commit":"a1b2c3d4e5","status":"1"}' $TMPDIR/resource-$$ ; then
74+
echo "FAILURE: Unexpected version output"
75+
cat $TMPDIR/resource-$$
76+
exit 1
77+
fi
78+
79+
if ! grep -q '{"name":"created_at","value":"2012-07-20T01:19:13Z"}' $TMPDIR/resource-$$ ; then
80+
echo "FAILURE: Unexpected created_at output"
81+
cat $TMPDIR/resource-$$
82+
exit 1
83+
fi
84+
85+
if ! grep -q '{"name":"created_by","value":"octocat"}' $TMPDIR/resource-$$ ; then
86+
echo "FAILURE: Unexpected creator output"
87+
cat $TMPDIR/resource-$$
88+
exit 1
89+
fi

test/out/simple.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ if ! grep -q '^POST /repos/dpb587/test-repo/statuses/a1b2c3d4e5 ' $TMPDIR/http.r
6363
exit 1
6464
fi
6565

66-
if ! grep -q '^{"context":"test-context","description":"test-description","state":"success","target_url":"https://ci.example.com/123/output"}$' $TMPDIR/http.req-$$ ; then
66+
if ! grep -q '^{"context":"concourse-ci/test-context","description":"test-description","state":"success","target_url":"https://ci.example.com/123/output"}$' $TMPDIR/http.req-$$ ; then
6767
echo "FAILURE: Unexpected request body"
6868
cat $TMPDIR/http.req-$$
6969
exit 1

test/out/target-url-path.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ if ! grep -q '^POST /repos/dpb587/test-repo/statuses/a1b2c3d4e5 ' $TMPDIR/http.r
6464
exit 1
6565
fi
6666

67-
if ! grep -q '^{"context":"test-context","description":"test-description","state":"success","target_url":"https://ci.example.com/123/output-path"}$' $TMPDIR/http.req-$$ ; then
67+
if ! grep -q '^{"context":"concourse-ci/test-context","description":"test-description","state":"success","target_url":"https://ci.example.com/123/output-path"}$' $TMPDIR/http.req-$$ ; then
6868
echo "FAILURE: Unexpected request body"
6969
cat $TMPDIR/http.req-$$
7070
exit 1

test/out/wait-for-status.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ if ! grep -q '^POST /repos/dpb587/test-repo/statuses/a1b2c3d4e5 ' $TMPDIR/http.r
138138
exit 1
139139
fi
140140

141-
if ! grep -q '^{"context":"test-context","description":"test-description","state":"success","target_url":"https://ci.example.com/123/output"}$' $TMPDIR/http.req-$$ ; then
141+
if ! grep -q '^{"context":"concourse-ci/test-context","description":"test-description","state":"success","target_url":"https://ci.example.com/123/output"}$' $TMPDIR/http.req-$$ ; then
142142
echo "FAILURE: Unexpected request body"
143143
cat $TMPDIR/http.req-$$
144144
exit 1

0 commit comments

Comments
 (0)