Skip to content

Commit

Permalink
Merge pull request #160 from Pix4D/omit-target-url
Browse files Browse the repository at this point in the history
Add optional omit_target_url
  • Loading branch information
marco-m-pix4d authored Dec 17, 2024
2 parents a55a812 + 2743c08 commit a6cffbf
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 60 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ on: [push]
name: ci

env:
go-version: 1.22.x
task-version: v3.28.0
go-version: 1.23.x
task-version: v3.40.0

jobs:
all:
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: ${{ env.go-version }}
- name: Install Task
run: go install github.com/go-task/task/v3/cmd/task@${{ env.task-version }}
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
# By default, actions/checkout will persist the GITHUB_TOKEN, so that further
# steps in the job can perform authenticated git commands (that is: WRITE to
Expand Down
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Optional `source.omit_target_url`. If set to true, will omit the GitHub Commit status API `target_url` (see README).
- pkg/sets: add Intersect() and Add() methods.

## [v0.12.1] - 2024-04-03
Expand All @@ -17,7 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- `source.log_level`.
- The README was wrongly still mentioning `silent` as a way to silent the logging. Actually `silent` was remapped to `info` since [v0.8.2](#v082---2022-11-18).
- Starting from this version, levels `silent` and `off` will cause an error.
- Starting from this version, levels `silent` and `off` will cause an error.
- The only supported log levels are `debug`, `info`, `warn`, `error`. The default level, `info`, is recommended for normal operation.

### Fixed
Expand All @@ -40,7 +41,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [v0.11.0] - 2023-09-22

### Added
### Added

- Retry GitHub API HTTP request when rate limited or when transient server error:
- New generic and customizable `retry` package, usable with any API (not GitHub specific).
Expand Down Expand Up @@ -107,7 +108,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Minor breaking change

- `source.log_level`. If you were previously silencing the logging with level `silent`, it will now be interpreted as invalid and automatically remapped to `info`. If you really want no logging, the log level to use is `off`.
Note that there is no reason to disable logging, since it is not visible in the build log in the UI, and the default of `info` helps to, for example, know which version of cogito is being used.
Note that there is no reason to disable logging, since it is not visible in the build log in the UI, and the default of `info` helps to, for example, know which version of cogito is being used.

## [v0.8.1] - 2022-09-07

Expand Down
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ In case of doubts about how to tackle testing something, feel free to ask.

## Required

* Go, version >= 1.22
* Go, version >= 1.23
* Docker, version >= 20
* [Task], version >= 3.27
* [Task], version >= 3.40

## Optional

Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.22-alpine as builder
FROM golang:1.23-alpine AS builder

ARG BUILD_INFO

Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ With reference to the [GitHub Commit status API], the `POST` parameters (`state`
GitHub hostname. This allows to post commit statuses to repositories hosted by GitHub Enterprise (GHE) instances. For example: github.mycompany.org will be expanded by cogito to https://github.mycompany.org/api/v3 \
Default: `github.com`

- `omit_target_url`:\
If set to true, will omit the GitHub Commit status API `target_url` (the URL to the build on Concourse).\n
Default: `false`.

- `log_url`. **DEPRECATED, no-op, will be removed**\
A Google Hangout Chat webhook. Useful to obtain logging for the `check` step for Concourse < v7.x

Expand Down
8 changes: 5 additions & 3 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ vars:
"version": {"ref": "pizza"}
}
#
GOLANGCI_VERSION: v1.54.2
GOTESTSUM_VERSION: v1.10.1
GOLANGCI_VERSION: v1.62.2
GOTESTSUM_VERSION: v1.12.0

tasks:

Expand All @@ -44,7 +44,9 @@ tasks:
- golangci-lint run ./...

test:env:
desc: "Run what is passed on the command-line with a shell environment containing the secrets needed for the integration tests. Example: task test:env -- go test -count=1 -run 'TestFooIntegration' ./pkg/update"
desc: |
Run what is passed on the command-line with a shell environment containing the secrets needed for the integration tests.
Example: task test:env -- go test -count=1 -run 'TestFooIntegration' ./pkg/update"
cmds:
- '{{ .CLI_ARGS }}'
env: &test-env
Expand Down
21 changes: 11 additions & 10 deletions cmd/cogito/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,25 @@ import (
)

func main() {
// The "Concourse resource protocol" expects:
// - stdin, stdout and command-line arguments for the protocol itself
// - stderr for logging
// See: https://concourse-ci.org/implementing-resource-types.html
if err := mainErr(os.Stdin, os.Stdout, os.Stderr, os.Args); err != nil {
fmt.Fprintf(os.Stderr, "cogito: error: %s\n", err)
os.Exit(1)
}
}

func mainErr(in io.Reader, out io.Writer, logOut io.Writer, args []string) error {
// The "Concourse resource protocol" expects:
// - stdin, stdout and command-line arguments for the protocol itself
// - stderr for logging
//
// See: https://concourse-ci.org/implementing-resource-types.html
func mainErr(stdin io.Reader, stdout io.Writer, stderr io.Writer, args []string) error {
cmd := path.Base(args[0])
validCmds := sets.From("check", "in", "out")
if !validCmds.Contains(cmd) {
return fmt.Errorf("invoked as '%s'; want: one of %v", cmd, validCmds)
}

input, err := io.ReadAll(in)
input, err := io.ReadAll(stdin)
if err != nil {
return fmt.Errorf("reading stdin: %s", err)
}
Expand All @@ -52,7 +53,7 @@ func mainErr(in io.Reader, out io.Writer, logOut io.Writer, args []string) error
return a
}
log := slog.New(slog.NewTextHandler(
logOut,
stderr,
&slog.HandlerOptions{
Level: level,
ReplaceAttr: removeTime,
Expand All @@ -61,12 +62,12 @@ func mainErr(in io.Reader, out io.Writer, logOut io.Writer, args []string) error

switch cmd {
case "check":
return cogito.Check(log, input, out, args[1:])
return cogito.Check(log, input, stdout, args[1:])
case "in":
return cogito.Get(log, input, out, args[1:])
return cogito.Get(log, input, stdout, args[1:])
case "out":
putter := cogito.NewPutter(log)
return cogito.Put(log, input, out, args[1:], putter)
return cogito.Put(log, input, stdout, args[1:], putter)
default:
return fmt.Errorf("cli wiring error; please report")
}
Expand Down
70 changes: 35 additions & 35 deletions cmd/cogito/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
)

func TestRunCheckSuccess(t *testing.T) {
in := strings.NewReader(`
stdin := strings.NewReader(`
{
"source": {
"owner": "the-owner",
Expand All @@ -30,16 +30,16 @@ func TestRunCheckSuccess(t *testing.T) {
"log_level": "debug"
}
}`)
var out bytes.Buffer
var logOut bytes.Buffer
var stdout bytes.Buffer
var stderr bytes.Buffer

err := mainErr(in, &out, &logOut, []string{"check"})
err := mainErr(stdin, &stdout, &stderr, []string{"check"})

assert.NilError(t, err, "\nout: %s\nlogOut: %s", out.String(), logOut.String())
assert.NilError(t, err, "\nstdout: %s\nstderr: %s", stdout.String(), stderr.String())
}

func TestRunGetSuccess(t *testing.T) {
in := strings.NewReader(`
stdin := strings.NewReader(`
{
"source": {
"owner": "the-owner",
Expand All @@ -49,12 +49,12 @@ func TestRunGetSuccess(t *testing.T) {
},
"version": {"ref": "pizza"}
}`)
var out bytes.Buffer
var logOut bytes.Buffer
var stdout bytes.Buffer
var stderr bytes.Buffer

err := mainErr(in, &out, &logOut, []string{"in", "dummy-dir"})
err := mainErr(stdin, &stdout, &stderr, []string{"in", "dummy-dir"})

assert.NilError(t, err, "\nout: %s\nlogOut: %s", out.String(), logOut.String())
assert.NilError(t, err, "\nstdout: %s\nstderr: %s", stdout.String(), stderr.String())
}

func TestRunPutSuccess(t *testing.T) {
Expand All @@ -69,7 +69,7 @@ func TestRunPutSuccess(t *testing.T) {
chatReply := googlechat.MessageReply{}
var gchatUrl *url.URL
googleChatSpy := testhelp.SpyHttpServer(&chatMsg, chatReply, &gchatUrl, http.StatusOK)
in := bytes.NewReader(testhelp.ToJSON(t, cogito.PutRequest{
stdin := bytes.NewReader(testhelp.ToJSON(t, cogito.PutRequest{
Source: cogito.Source{
Owner: "the-owner",
Repo: "the-repo",
Expand All @@ -80,14 +80,14 @@ func TestRunPutSuccess(t *testing.T) {
},
Params: cogito.PutParams{State: wantState},
}))
var out bytes.Buffer
var logOut bytes.Buffer
var stdout bytes.Buffer
var stderr bytes.Buffer
inputDir := testhelp.MakeGitRepoFromTestdata(t, "../../cogito/testdata/one-repo/a-repo",
testhelp.HttpsRemote(gitHubSpyURL.Host, "the-owner", "the-repo"), "dummySHA", wantGitRef)

err = mainErr(in, &out, &logOut, []string{"out", inputDir})
err = mainErr(stdin, &stdout, &stderr, []string{"out", inputDir})

assert.NilError(t, err, "\nout: %s\nlogOut: %s", out.String(), logOut.String())
assert.NilError(t, err, "\nstdout: %s\nstderr: %s", stdout.String(), stderr.String())
//
gitHubSpy.Close() // Avoid races before the following asserts.
assert.Equal(t, ghReq.State, string(wantState))
Expand All @@ -104,7 +104,7 @@ func TestRunPutSuccessIntegration(t *testing.T) {

gitHubCfg := testhelp.GitHubSecretsOrFail(t)
googleChatCfg := testhelp.GoogleChatSecretsOrFail(t)
in := bytes.NewReader(testhelp.ToJSON(t, cogito.PutRequest{
stdin := bytes.NewReader(testhelp.ToJSON(t, cogito.PutRequest{
Source: cogito.Source{
Owner: gitHubCfg.Owner,
Repo: gitHubCfg.Repo,
Expand All @@ -114,8 +114,8 @@ func TestRunPutSuccessIntegration(t *testing.T) {
},
Params: cogito.PutParams{State: cogito.StateError},
}))
var out bytes.Buffer
var logOut bytes.Buffer
var stdout bytes.Buffer
var stderr bytes.Buffer
inputDir := testhelp.MakeGitRepoFromTestdata(t, "../../cogito/testdata/one-repo/a-repo",
testhelp.HttpsRemote(github.GhDefaultHostname, gitHubCfg.Owner, gitHubCfg.Repo), gitHubCfg.SHA,
"ref: refs/heads/a-branch-FIXME")
Expand All @@ -125,27 +125,27 @@ func TestRunPutSuccessIntegration(t *testing.T) {
t.Setenv("BUILD_TEAM_NAME", "the-test-team")
t.Setenv("BUILD_NAME", "42")

err := mainErr(in, &out, &logOut, []string{"out", inputDir})
err := mainErr(stdin, &stdout, &stderr, []string{"out", inputDir})

assert.NilError(t, err, "\nout:\n%s\nlogOut:\n%s", out.String(), logOut.String())
assert.Assert(t, cmp.Contains(logOut.String(),
assert.NilError(t, err, "\nstdout:\n%s\nstderr:\n%s", stdout.String(), stderr.String())
assert.Assert(t, cmp.Contains(stderr.String(),
`level=INFO msg="commit status posted successfully" name=cogito.put name=ghCommitStatus state=error`))
assert.Assert(t, cmp.Contains(logOut.String(),
assert.Assert(t, cmp.Contains(stderr.String(),
`level=INFO msg="state posted successfully to chat" name=cogito.put name=gChat state=error`))
}

func TestRunFailure(t *testing.T) {
type testCase struct {
name string
args []string
in string
stdin string
wantErr string
}

test := func(t *testing.T, tc testCase) {
in := strings.NewReader(tc.in)
stdin := strings.NewReader(tc.stdin)

err := mainErr(in, nil, io.Discard, tc.args)
err := mainErr(stdin, nil, io.Discard, tc.args)

assert.ErrorContains(t, err, tc.wantErr)
}
Expand All @@ -159,16 +159,16 @@ func TestRunFailure(t *testing.T) {
{
name: "check, wrong stdin",
args: []string{"check"},
in: `
stdin: `
{
"fruit": "banana"
"fruit": "banana"
}`,
wantErr: `check: parsing request: json: unknown field "fruit"`,
},
{
name: "peeking for log_level",
args: []string{"check"},
in: "",
stdin: "",
wantErr: "peeking into JSON for log_level: unexpected end of JSON input",
},
}
Expand All @@ -182,28 +182,28 @@ func TestRunFailure(t *testing.T) {
}

func TestRunSystemFailure(t *testing.T) {
in := iotest.ErrReader(errors.New("test read error"))
stdin := iotest.ErrReader(errors.New("test read error"))

err := mainErr(in, nil, io.Discard, []string{"check"})
err := mainErr(stdin, nil, io.Discard, []string{"check"})

assert.ErrorContains(t, err, "test read error")
}

func TestRunPrintsBuildInformation(t *testing.T) {
in := strings.NewReader(`
stdin := strings.NewReader(`
{
"source": {
"owner": "the-owner",
"repo": "the-repo",
"access_token": "the-secret"
}
}
}`)
var logBuf bytes.Buffer
var stderr bytes.Buffer
wantLog := "This is the Cogito GitHub status resource. unknown"

err := mainErr(in, io.Discard, &logBuf, []string{"check"})
err := mainErr(stdin, io.Discard, &stderr, []string{"check"})
assert.NilError(t, err)
haveLog := logBuf.String()
haveLog := stderr.String()

assert.Assert(t, strings.Contains(haveLog, wantLog),
"\nhave: %s\nwant: %s", haveLog, wantLog)
Expand Down
3 changes: 3 additions & 0 deletions cogito/ghcommitsink.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ func (sink GitHubCommitStatusSink) Send() error {
"state", ghState, "owner", sink.Request.Source.Owner,
"repo", sink.Request.Source.Repo, "git-ref", sink.GitRef,
"context", context, "buildURL", buildURL, "description", description)
if sink.Request.Source.OmitTargetURL {
buildURL = ""
}
if err := commitStatus.Add(sink.GitRef, ghState, buildURL, description); err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion cogito/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ type Source struct {
LogLevel string `json:"log_level"`
LogUrl string `json:"log_url"` // DEPRECATED
ContextPrefix string `json:"context_prefix"`
OmitTargetURL bool `json:"omit_target_url"`
ChatAppendSummary bool `json:"chat_append_summary"`
ChatNotifyOnStates []BuildState `json:"chat_notify_on_states"`
Sinks []string `json:"sinks"`
Expand All @@ -189,6 +190,7 @@ func (src Source) String() string {
fmt.Fprintf(&bld, "gchat_webhook: %s\n", redact(src.GChatWebHook))
fmt.Fprintf(&bld, "log_level: %s\n", src.LogLevel)
fmt.Fprintf(&bld, "context_prefix: %s\n", src.ContextPrefix)
fmt.Fprintf(&bld, "omit_target_url: %t\n", src.OmitTargetURL)
fmt.Fprintf(&bld, "chat_append_summary: %t\n", src.ChatAppendSummary)
fmt.Fprintf(&bld, "chat_notify_on_states: %s\n", src.ChatNotifyOnStates)
// Last one: no newline.
Expand Down Expand Up @@ -253,7 +255,6 @@ func (src *Source) Validate() error {
if src.GChatWebHook == "" {
mandatory = append(mandatory, "gchat_webhook")
}

}

if len(mandatory) > 0 {
Expand Down
Loading

0 comments on commit a6cffbf

Please sign in to comment.