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

context improvements #17

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
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`)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you describe the use case for this a bit more?

Assuming it makes sense, I don't think it should be defaulted (since it'd be a large, breaking change). So I think this should be adjusted to actually be the full prefix (where the user sets it to concourse-ci/ if they want this behavior) and then remove the slashes deeper in code.

Copy link
Contributor Author

@christophermancini christophermancini Oct 13, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a few reasons for this change:

  • it will make this resource consistent with the github-pr-resources, providing a more unified user experience across Concourse
  • we have a staging cluster for testing new versions of Concourse, this allows us to have pipelines on this staging cluster use a prefix of concourse-staging-ci
  • if you are testing changes to a pipeline before applying them to the primary pipeline, you can use this to differentiate between the two, e.g. concourse-ci/dev

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W.r.t. breaking change, we could just generate a new 2.2.0 tag for the current version, then merge and generate a 3.0.0 tag. By not having the concourse-ci be the default, then it would deviate from the github-pr-resource. 🤔

* `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
31 changes: 31 additions & 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 All @@ -40,6 +41,7 @@ buildtpl () {
BUILD_NAME="${BUILD_NAME:-}" \
BUILD_JOB_NAME="${BUILD_JOB_NAME:-}" \
BUILD_PIPELINE_NAME="${BUILD_PIPELINE_NAME:-}" \
BUILD_TEAM_NAME="${BUILD_TEAM_NAME:-}" \
ATC_EXTERNAL_URL="${ATC_EXTERNAL_URL:-}" \
$envsubst
}
Expand All @@ -52,3 +54,32 @@ curlgh () {
fi
curl $skip_verify_arg -s -H "Authorization: token $source_access_token" $@
}

curlgh_all_pages_status () {
results=''
page=0
present='true'
while [ "$present" = "true" ]; do
page=$(($page+1))
current_results=$(curlgh "$@?page=$page")

# Save the first query as the return value, in case the response
# body is not expected, the response body can still be returned
# and behave like curlgh would in the non-happy path.
if [ -z "$results" ]; then
results="$current_results"
continue
fi

# If key "statuses" is not present, stop iterating loop
statuses=$(echo $current_results | jq -c '.statuses // []')
if [ "$statuses" != "[]" ]; then
# Identify "statuses" array in `current_results` and append it to "statuses" array in `results`
results=$(echo "$results" | jq --argjson s "$statuses" '.statuses += $s')
else
present='false'
fi
done

echo "$results"
}
2 changes: 1 addition & 1 deletion bin/in
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ eval $( jq -r '{
# lookup
#

curlgh "$source_endpoint/repos/$source_repository/commits/$version_commit/status" \
curlgh_all_pages_status "$source_endpoint/repos/$source_repository/commits/$version_commit/status" \
| jq -c \
--arg status "$version_status" \
'{
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)" \
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will help fix #8, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. It should not break status lookups via check as long as the user does not include any env vars in the resource source context.

'{
"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
Loading