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

Commit

Permalink
context improvements
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
Christopher Mancini committed Sep 19, 2019
1 parent bed72b0 commit 758cc3b
Show file tree
Hide file tree
Showing 13 changed files with 169 additions and 73 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM alpine
RUN apk --no-cache add curl ca-certificates gettext \
&& curl -Ls https://github.com/stedolan/jq/releases/download/jq-1.5/jq-linux64 > /usr/bin/jq && chmod +x /usr/bin/jq
&& curl -Ls https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64 > /usr/bin/jq && chmod +x /usr/bin/jq
ADD bin /opt/resource
18 changes: 18 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
SHELL=/bin/bash

root_mkfile := $(abspath $(lastword $(MAKEFILE_LIST)))
root_dir := $(dir $(root_mkfile))

.PHONY: resource-dev
resource-dev:
@docker run \
-v $(root_dir):/resource \
--rm -i -t dpb587/github-status-resource:master \
/bin/ash

.PHONY: test
test:
@docker run \
-v $(root_dir):/resource \
--rm -i -t dpb587/github-status-resource:master \
/resource/test/all.sh
110 changes: 46 additions & 64 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,22 @@

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.


## Configuration

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


## Behavior


### `check`

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


### `in`

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


### `out`

Update the status of a commit. Optionally include a description and target URL which will be referenced from GitHub.
Expand All @@ -43,78 +39,65 @@ Parameters:
* `description` - a short description of the status
* `description_path` - path to an input file whose data is the value of `description`
* `target_url` - the target URL to associate with the status (default: concourse build link)

* `context` - overrides the source context value (default: `""`)

## Example

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.

---
jobs:
- name: "unit-tests"
plan:
- get: "repo"
trigger: true
- put: "repo-status" # +
params: { state: "pending", commit: "repo" } # +
- task: "unit-tests"
file: "repo/ci/unit-tests/task.yml"
on_failure:
- put: "repo-status" # +
params: { state: "failure", commit: "repo" } # +
- put: "repo-status" # +
params: { state: "success", commit: "repo" } # +
resources:
- name: "repo"
type: "git"
source:
uri: {{repo_uri}}
branch: {{repo_branch}}
- name: "repo-status" # +
type: "github-status" # +
source: # +
repository: {{repo_github_path}} # +
access_token: {{repo_github_token}} # +
```yaml
jobs:
- name: "unit-tests"
plan:
- get: "repo"
trigger: true
- put: "repo-status" # +
params: { state: "pending", commit: "repo" } # +
- task: "unit-tests"
file: "repo/ci/unit-tests/task.yml"
on_failure:
- put: "repo-status" # +
params: { state: "failure", commit: "repo" } # +
- put: "repo-status" # +
params: { state: "success", commit: "repo" } # +
resources:
- name: "repo"
type: "git"
source:
uri: {{repo_uri}}
branch: {{repo_branch}}
- name: "repo-status" # +
type: "github-status" # +
source: # +
repository: {{repo_github_path}} # +
access_token: {{repo_github_token}} # +
```
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...

name: "pr-status"
type: "github-status"
source:
repository: {{repo_github_path}}
access_token: {{repo_github_token}}
branch: "pull/12345/head" # +
```yaml
name: "pr-status"
type: "github-status"
source:
repository: {{repo_github_path}}
access_token: {{repo_github_token}}
branch: "pull/12345/head" # +
```

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


## Installation

This resource is not included with the standard Concourse release. Use one of the following methods to make this resource available to your pipelines.


### Deployment-wide

To install on all Concourse workers, update your deployment manifest properties to include a new `groundcrew.resource_types` entry...

properties:
groundcrew:
additional_resource_types:
- image: "docker:///dpb587/github-status-resource#master" # +
type: "github-status" # +


### Pipeline-specific

To use on a single pipeline, update your pipeline to include a new `resource_types` entry...

resource_types:
- name: "github-status" # +
type: "docker-image" # +
source: # +
repository: "dpb587/github-status-resource" # +
tag: "master" # +
This resource is not included with the standard Concourse release. Add it to your pipeline's `resource_types` definition.

```yaml
resource_types:
- name: "github-status"
type: "docker-image"
source:
repository: "dpb587/github-status-resource"
tag: "master"
```

## References

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


## License

[MIT License](./LICENSE)
2 changes: 1 addition & 1 deletion bin/check
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ load_source

curlgh "$source_endpoint/repos/$source_repository/commits/$source_branch/status" \
| jq -c \
--arg context "$source_context" \
--arg context "$source_base_context/$source_context" \
'.sha as $commit | .statuses | map(select( $context == .context )) | map({ "commit": $commit, "status": ( .id | tostring ) })' \
>&3
1 change: 1 addition & 0 deletions bin/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ load_source () {
"source_repository": .source.repository,
"source_access_token": .source.access_token,
"source_branch": ( .source.branch // "master" ),
"source_base_context": ( .source.base_context // "concourse-ci" ),
"source_context": ( .source.context // "default" ),
"source_endpoint": ( .source.endpoint // "https://api.github.com" ),
"skip_ssl_verification": ( .source.skip_ssl_verification // "false" )
Expand Down
8 changes: 7 additions & 1 deletion bin/out
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ load_source
eval $( jq -r '{
"params_commit": .params.commit,
"params_state": .params.state,
"params_context": ( .params.context // "" ),
"params_description": ( .params.description // "" ),
"params_description_path": ( .params.description_path // "" ),
"params_target_url": ( .params.target_url // ""),
Expand Down Expand Up @@ -73,11 +74,16 @@ fi
# execute
#

cntxt="$source_context"
if [[ -n "$params_context" ]] ; then
cntxt="$params_context"
fi

jq -c -n \
--arg state "$params_state" \
--arg target_url "$( echo "$target_url" | buildtpl )" \
--arg description "$( cat $description_path )" \
--arg context "$source_context" \
--arg context "$(echo "$source_base_context/$cntxt" | buildtpl)" \
'{
"context": $context,
"description": $description,
Expand Down
2 changes: 1 addition & 1 deletion test/all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ cd $dir

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

for test in $( find test -type f -perm +111 -print | grep -v 'test/all.sh' ) ; do
for test in $( find test -type f -print | grep -v 'test/all.sh' ) ; do
echo "==> $test"
$test
result=$?
Expand Down
2 changes: 1 addition & 1 deletion test/check/simple.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ HTTP/1.0 200 OK
"description": "Testing has completed successfully",
"id": 2,
"url": "https://api.github.com/repos/octocat/Hello-World/statuses/2",
"context": "test-context"
"context": "concourse-ci/test-context"
}
]
}
Expand Down
2 changes: 1 addition & 1 deletion test/out/default-target-url.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ if ! grep -q '^POST /repos/dpb587/test-repo/statuses/a1b2c3d4e5 ' $TMPDIR/http.r
exit 1
fi

if ! grep -q '^{"context":"test-context","description":"test-description","state":"success","target_url":"http://localhost/builds/123"}$' $TMPDIR/http.req-$$ ; then
if ! grep -q '^{"context":"concourse-ci/test-context","description":"test-description","state":"success","target_url":"http://localhost/builds/123"}$' $TMPDIR/http.req-$$ ; then
echo "FAILURE: Unexpected request body"
cat $TMPDIR/http.req-$$
exit 1
Expand Down
89 changes: 89 additions & 0 deletions test/out/put_context.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/bin/sh

set -eu

DIR=$( dirname "$0" )/../..

echo 'a1b2c3d4e5' > $TMPDIR/commit

cat <<EOF | nc -l -s 127.0.0.1 -p 9192 > $TMPDIR/http.req-$$ &
HTTP/1.0 200 OK
{
"created_at": "2012-07-20T01:19:13Z",
"updated_at": "2012-07-20T02:19:13Z",
"state": "success",
"target_url": "https://ci.example.com/1000/output",
"description": "Build has completed successfully",
"id": 1,
"url": "https://api.github.com/repos/octocat/Hello-World/statuses/1",
"context": "continuous-integration/jenkins",
"creator": {
"login": "octocat",
"id": 1,
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
"gravatar_id": "",
"url": "https://api.github.com/users/octocat",
"html_url": "https://github.com/octocat",
"followers_url": "https://api.github.com/users/octocat/followers",
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
"organizations_url": "https://api.github.com/users/octocat/orgs",
"repos_url": "https://api.github.com/users/octocat/repos",
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
"received_events_url": "https://api.github.com/users/octocat/received_events",
"type": "User",
"site_admin": false
}
}
EOF

BUILD_ID=123 BUILD_JOB_NAME=myjob $DIR/bin/out > $TMPDIR/resource-$$ <<EOF
{
"params": {
"description": "test-description",
"commit": "$TMPDIR/commit",
"context": "override-context/\$BUILD_JOB_NAME",
"state": "success",
"target_url": "https://ci.example.com/\$BUILD_ID/output"
},
"source": {
"access_token": "test-token",
"context": "test-context",
"endpoint": "http://127.0.0.1:9192",
"repository": "dpb587/test-repo"
}
}
EOF

if ! grep -q '^POST /repos/dpb587/test-repo/statuses/a1b2c3d4e5 ' $TMPDIR/http.req-$$ ; then
echo "FAILURE: Invalid HTTP method or URI"
cat $TMPDIR/http.req-$$
exit 1
fi

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
echo "FAILURE: Unexpected request body"
cat $TMPDIR/http.req-$$
exit 1
fi

if ! grep -q '"version":{"commit":"a1b2c3d4e5","status":"1"}' $TMPDIR/resource-$$ ; then
echo "FAILURE: Unexpected version output"
cat $TMPDIR/resource-$$
exit 1
fi

if ! grep -q '{"name":"created_at","value":"2012-07-20T01:19:13Z"}' $TMPDIR/resource-$$ ; then
echo "FAILURE: Unexpected created_at output"
cat $TMPDIR/resource-$$
exit 1
fi

if ! grep -q '{"name":"created_by","value":"octocat"}' $TMPDIR/resource-$$ ; then
echo "FAILURE: Unexpected creator output"
cat $TMPDIR/resource-$$
exit 1
fi
2 changes: 1 addition & 1 deletion test/out/simple.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ if ! grep -q '^POST /repos/dpb587/test-repo/statuses/a1b2c3d4e5 ' $TMPDIR/http.r
exit 1
fi

if ! grep -q '^{"context":"test-context","description":"test-description","state":"success","target_url":"https://ci.example.com/123/output"}$' $TMPDIR/http.req-$$ ; then
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
echo "FAILURE: Unexpected request body"
cat $TMPDIR/http.req-$$
exit 1
Expand Down
2 changes: 1 addition & 1 deletion test/out/target-url-path.sh
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ if ! grep -q '^POST /repos/dpb587/test-repo/statuses/a1b2c3d4e5 ' $TMPDIR/http.r
exit 1
fi

if ! grep -q '^{"context":"test-context","description":"test-description","state":"success","target_url":"https://ci.example.com/123/output-path"}$' $TMPDIR/http.req-$$ ; then
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
echo "FAILURE: Unexpected request body"
cat $TMPDIR/http.req-$$
exit 1
Expand Down
2 changes: 1 addition & 1 deletion test/out/wait-for-status.sh
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ if ! grep -q '^POST /repos/dpb587/test-repo/statuses/a1b2c3d4e5 ' $TMPDIR/http.r
exit 1
fi

if ! grep -q '^{"context":"test-context","description":"test-description","state":"success","target_url":"https://ci.example.com/123/output"}$' $TMPDIR/http.req-$$ ; then
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
echo "FAILURE: Unexpected request body"
cat $TMPDIR/http.req-$$
exit 1
Expand Down

0 comments on commit 758cc3b

Please sign in to comment.