Skip to content

Commit

Permalink
Add new pr-commenter custom task
Browse files Browse the repository at this point in the history
Part of tektoncd#483

This custom task is meant to be used alongside PR status report updates to add/update/delete a comment equivalent to the Prow one listing job failures with links to logs and the command to re-test.

I initially made an attempt to update the `github-add-comment` task in the catalog to do this (tektoncd/catalog#1051) but decided that a custom task frankly made a lot more sense. Its behavior is a pretty direct emulation of how Prow and Lighthouse do this, looking for existing comments by the bot user with a given tag in the comment, and updating/deleting that comment if it exists, creating a new comment otherwise. It's a bit simplified, and instead of using `ProwJob`s to know what should still be in the comment and what should be removed, it specifically just adds or removes the specific job it's reporting on while preserving whatever else was there.

Signed-off-by: Andrew Bayer <[email protected]>
  • Loading branch information
abayer committed Sep 6, 2022
1 parent b488d2f commit f9db552
Show file tree
Hide file tree
Showing 33 changed files with 3,586 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@

# Virtualenv files
.venv

tekton/ci/custom-tasks/pr-commenter/.bin
41 changes: 41 additions & 0 deletions tekton/ci/custom-tasks/pr-commenter/.golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
linters-settings:
gomodguard:
blocked:
modules:
- github.com/ghodss/yaml:
recommendations:
- sigs.k8s.io/yaml
linters:
disable-all: true
enable:
- deadcode
- errcheck
- gofmt
- goimports
- gomodguard
- gosec
- gocritic
- revive
- misspell
- unconvert
output:
uniq-by-line: false
issues:
exclude-rules:
- path: _test\.go
linters:
- errcheck
- gosec
max-issues-per-linter: 0
max-same-issues: 0
include:
# Enable off-by-default rules for revive requiring that all exported elements have a properly formatted comment.
- EXC0012
- EXC0014
run:
issues-exit-code: 1
build-tags:
- e2e
skip-files: []
skip-dirs: []
timeout: 10m
133 changes: 133 additions & 0 deletions tekton/ci/custom-tasks/pr-commenter/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
MODULE = $(shell env GO111MODULE=on $(GO) list -m)
DATE ?= $(shell date +%FT%T%z)
BIN = $(CURDIR)/.bin

GOLANGCI_VERSION = v1.47.2

GO = go
TIMEOUT_UNIT = 5m
TIMEOUT_E2E = 20m
V = 0
Q = $(if $(filter 1,$V),,@)
M = $(shell printf "\033[34;1m🐱\033[0m")

export GO111MODULE=on

COMMANDS=$(patsubst cmd/%,%,$(wildcard cmd/*))
BINARIES=$(addprefix bin/,$(COMMANDS))

.PHONY: all
all: fmt $(BINARIES) | $(BIN) ; $(info $(M) building executable…) @ ## Build program binary

$(BIN):
@mkdir -p $@
$(BIN)/%: | $(BIN) ; $(info $(M) building $(PACKAGE)…)
$Q tmp=$$(mktemp -d); \
env GO111MODULE=off GOPATH=$$tmp GOBIN=$(BIN) $(GO) get $(PACKAGE) \
|| ret=$$?; \
rm -rf $$tmp ; exit $$ret

FORCE:

bin/%: cmd/% FORCE
$Q $(GO) build $(LDFLAGS) -v -o $@ ./$<

.PHONY: cross
cross: amd64 arm arm64 s390x ppc64le ## build cross platform binaries

.PHONY: amd64
amd64:
GOOS=linux GOARCH=amd64 go build $(LDFLAGS) ./cmd/...

.PHONY: arm
arm:
GOOS=linux GOARCH=arm go build $(LDFLAGS) ./cmd/...

.PHONY: arm64
arm64:
GOOS=linux GOARCH=arm64 go build $(LDFLAGS) ./cmd/...

.PHONY: s390x
s390x:
GOOS=linux GOARCH=s390x go build $(LDFLAGS) ./cmd/...

.PHONY: ppc64le
ppc64le:
GOOS=linux GOARCH=ppc64le go build $(LDFLAGS) ./cmd/...

KO = $(or ${KO_BIN},${KO_BIN},$(BIN)/ko)
$(BIN)/ko: PACKAGE=github.com/google/ko

.PHONY: apply
apply: | $(KO) ; $(info $(M) ko apply -R -f config/) @ ## Apply config to the current cluster
$Q $(KO) apply -R -f config

.PHONY: resolve
resolve: | $(KO) ; $(info $(M) ko resolve -R -f config/) @ ## Resolve config to the current cluster
$Q $(KO) resolve --push=false --oci-layout-path=$(BIN)/oci -R -f config

## Tests
TEST_UNIT_TARGETS := test-unit-verbose test-unit-race
test-unit-verbose: ARGS=-v
test-unit-race: ARGS=-race
$(TEST_UNIT_TARGETS): test-unit
.PHONY: $(TEST_UNIT_TARGETS) test-unit
test-unit: ## Run unit tests
$(GO) test -timeout $(TIMEOUT_UNIT) $(ARGS) ./...

TEST_E2E_TARGETS := test-e2e-short test-e2e-verbose test-e2e-race
test-e2e-short: ARGS=-short
test-e2e-verbose: ARGS=-v
test-e2e-race: ARGS=-race
$(TEST_E2E_TARGETS): test-e2e
.PHONY: $(TEST_E2E_TARGETS) test-e2e
test-e2e: ## Run end-to-end tests
$(GO) test -timeout $(TIMEOUT_E2E) -tags e2e $(ARGS) ./test/...

.PHONY: check tests
check tests: test-unit test-e2e
## Linters configuration and targets
# TODO(vdemeester) gofmt and goimports checks (run them with -w and make a diff)

GOLINT = $(BIN)/golint
$(BIN)/golint: PACKAGE=golang.org/x/lint/golint

.PHONY: golint
golint: | $(GOLINT) ; $(info $(M) running golint…) @ ## Run golint
$Q $(GOLINT) -set_exit_status ./...

.PHONY: vet
vet: | ; $(info $(M) running go vet…) @ ## Run go vet
$Q go vet ./...

GOLANGCILINT = $(BIN)/golangci-lint
$(BIN)/golangci-lint: ; $(info $(M) getting golangci-lint $(GOLANGCI_VERSION))
cd tools; GOBIN=$(BIN) go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLANGCI_VERSION)

.PHONY: golangci-lint
golangci-lint: | $(GOLANGCILINT) ; $(info $(M) running golangci-lint…) @ ## Run golangci-lint
$Q $(GOLANGCILINT) run --max-issues-per-linter=0 --max-same-issues=0 --deadline 5m

GOIMPORTS = $(BIN)/goimports
$(BIN)/goimports: PACKAGE=golang.org/x/tools/cmd/goimports

.PHONY: goimports
goimports: | $(GOIMPORTS) ; $(info $(M) running goimports…) ## Run goimports
$Q $(GOIMPORTS) -l -e -w pkg cmd

.PHONY: fmt
fmt: ; $(info $(M) running gofmt…) @ ## Run gofmt on all source files
$Q $(GO) fmt ./...

# Misc

.PHONY: clean
clean: ; $(info $(M) cleaning…) @ ## Cleanup everything
@rm -rf $(BIN)
@rm -rf bin
@rm -rf test/tests.* test/coverage.*

.PHONY: help
help:
@grep -hE '^[ a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) | \
awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-17s\033[0m %s\n", $$1, $$2}'
45 changes: 45 additions & 0 deletions tekton/ci/custom-tasks/pr-commenter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Dogfooding Job PR Commenter

This folder contains a Custom Task that can be used to comment on tektoncd PRs with information
about failing Tekton runs in the dogfooding cluster. It will add a new comment with up-to-date
information on all failing Tekton runs for the PR, pulling information about runs other than the
one triggering the task from earlier comments, and will delete the comment when updating or when
all runs have passed.

## Configuration

All configuration of the custom task is done via [environment variables on the deployment](./config/500-controller.yaml).
The `GITHUB_TOKEN` secret is the same GitHub OAuth token used in a number of other places in dogfooding.

The `RETEST_PREFIX` environment variable is there so that if we, in the future, change the command used
to re-run a Tekton job from `/test ...` to something else, we just need to change the value in the
deployment for that to be reflected in the comment.

## Example `Run`

```yaml
apiVersion: tekton.dev/v1alpha1
kind: Run
metadata:
name: example-pr-comment
namespace: tekton-ci
spec:
ref:
apiVersion: custom.tekton.dev/v0
kind: PRCommenter
params:
- name: repo
value: plumbing
- name: prNumber
value: 1234
- name: sha
value: abcd1234
- name: jobName
value: check-pr-has-kind-label
- name: isSuccess
value: "false"
- name: isOptional
value: "false"
- name: logURL
value: https://prow.tekton.dev/view/gs/tekton-prow/pr-logs/pull/tektoncd_plumbing/1185/check-pr-has-kind-label/1564708061786935296
```
Loading

0 comments on commit f9db552

Please sign in to comment.