Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
1b2a7a4
Monthly update for August 21
joonlim Aug 26, 2021
2f9eebc
Merge pull request #120 from GoogleCloudPlatform/joonlim-monthly-update
Saaarah Aug 27, 2021
2c214e4
New notifier: Google Chat
glasnt Jan 19, 2022
8bb57ce
Add error handling on message construction
glasnt Mar 18, 2022
5871851
Add unit tests
glasnt Mar 18, 2022
ac48f4d
Merge pull request #127 from glasnt/new-googlechat
prabenzom Mar 18, 2022
9df2977
Update README.MD
Aric1088 Apr 19, 2022
74f9d87
First release for the Google Chat Notifier (#129)
Aric1088 Apr 20, 2022
926d83e
return the branch, tag, or no value (#130)
glasnt Apr 22, 2022
140b8ad
Initial Library Update for Params/Templating (#131)
Aric1088 May 18, 2022
b0668da
Add bindings/templating to the Slack Notifier (#134)
Aric1088 Jun 1, 2022
513b2b7
Add bindings/templating to the SMTP-notifier (#132)
Aric1088 Jun 1, 2022
5d4a8e1
slack: Add color context to the body
fragoulis Jun 14, 2022
ccd6c88
slack: Slack does not respect the predefined colors
fragoulis Jun 14, 2022
de234b5
add bindings/templating to the bq notifier (#135)
Aric1088 Jun 14, 2022
a7045a4
add bindings/templating to http notifier (#136)
Aric1088 Jun 14, 2022
7a67b3c
New notifier - GitHub Issues (#141)
glasnt Jul 13, 2022
0decd9d
Monthly update for August 22 (#144)
emoryruscus Aug 9, 2022
20db36b
Update setup.sh to allow template files, add githubissues (#142)
glasnt Aug 24, 2022
49a019d
Update README.md
Aric1088 Aug 29, 2022
13e0da0
Update README.md
Aric1088 Aug 30, 2022
14dabbf
Update slack.yaml.example
Aric1088 Sep 6, 2022
7eed935
prevent invalid pubsub subscription names (#149)
glasnt Sep 15, 2022
199b43c
Update slack.yaml.example
Aric1088 Oct 5, 2022
4a3420d
Update slack.json
Aric1088 Oct 5, 2022
649ae0b
Fix validation of GCS bucket names
ptxmac Nov 8, 2022
2029e28
Update README.md (#154)
Aric1088 Nov 22, 2022
054371d
Merge pull request #139 from jfragoulis/slack-block-color-context
fayfaychan Jan 23, 2023
f695cd1
Merge pull request #152 from glasswing-dev/fix-gs-validation
fayfaychan Jan 23, 2023
02281bc
go: Update all dependencies to latest
minor-fixes Apr 24, 2023
ec3dbaa
Added a template function 'replace' to BlockKitTemplate parser, which…
ezeYaniv Jun 5, 2023
1d1284d
Merge branch 'update_deps' of https://github.com/minor-fixes/cloud-bu…
monicacinom Jun 7, 2023
699af20
Monthly update for June 23
monicacinom Jun 7, 2023
1abd8de
Monthly update for June 23
monicacinom Jun 7, 2023
c3bc1ae
Monthly update 2 for June 23
monicacinom Jun 7, 2023
96a0dfc
Monthly update 2 for June 23
monicacinom Jun 7, 2023
8153149
Monthly update 2 for June 23
monicacinom Jun 7, 2023
7c37ca7
Monthly update 2 for June 23
monicacinom Jun 7, 2023
c8095cf
Monthly update 2 for June 23
monicacinom Jun 7, 2023
f859907
Monthly update 2 for June 23
monicacinom Jun 7, 2023
9402be9
Monthly update 2 for June 23
monicacinom Jun 7, 2023
e6cceaf
Monthly update 2 for June 23
monicacinom Jun 7, 2023
843b3a1
Monthly update 2 for June 23
monicacinom Jun 7, 2023
7f34968
Monthly update 2 for June 23
monicacinom Jun 8, 2023
abea943
Monthly update 2 for June 23
monicacinom Jun 8, 2023
3efea17
Monthly update 2 for June 23
monicacinom Jun 8, 2023
0af3df0
Monthly update 2 for June 23
monicacinom Jun 8, 2023
f62871c
Monthly update 2 for June 23
monicacinom Jun 8, 2023
49a1c30
Monthly update 2 for June 23
monicacinom Jun 8, 2023
c69e860
Monthly update 2 for June 23
monicacinom Jun 8, 2023
17dd0b4
Merge pull request #174 from GoogleCloudPlatform/minor-fixes-update_deps
monicacinom Jun 8, 2023
96c9c3d
Allow replace function in template validation
vicpadilla Jul 11, 2023
603e6b4
Merge pull request #178 from GoogleCloudPlatform/allowrepl
monicacinom Jul 11, 2023
feba9bc
Build binaries without dependency on cgo (#179)
vicpadilla Jul 11, 2023
df71178
chore: bumping go/cloudbuild to have updated enums
gaPanza Oct 10, 2023
1764a7e
Merge pull request #1 from gr4vy/bump-version
gaPanza Oct 10, 2023
0cf3f10
Merge pull request #183 from gr4vy/master
seenkey Oct 30, 2023
49c1df3
Support secrets in HTTP urls (#184)
ekupershlak Nov 6, 2023
9f12535
Monthly update for November 23
fayfaychan Nov 6, 2023
0351254
Merge pull request #185 from GoogleCloudPlatform/emilyyfchen-monthly-…
fayfaychan Nov 6, 2023
1e547db
Bump google.golang.org/protobuf from 1.30.0 to 1.33.0
dependabot[bot] Mar 13, 2024
9c1cf36
Bump google.golang.org/protobuf in /samples/logging-sample
dependabot[bot] Mar 13, 2024
f718911
Merge pull request #192 from GoogleCloudPlatform/dependabot/go_module…
prabenzom Apr 2, 2024
2cff754
Merge pull request #191 from GoogleCloudPlatform/dependabot/go_module…
prabenzom Apr 2, 2024
1d5fdfb
monthly update for 04-2024
EmmaMunley Apr 17, 2024
92bc968
Merge pull request #193 from EmmaMunley/emmamunley-monthly-update
EmmaMunley Apr 18, 2024
97eedf1
Reset to the original repos master branch
bfrederix Apr 30, 2024
b5d87b4
Added .cloudbuild back
bfrederix Apr 30, 2024
14e4adf
Source check fix
bfrederix Apr 30, 2024
c313670
go mod tidy
bfrederix Apr 30, 2024
e6430c2
go mod tidy 1.21
bfrederix Apr 30, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .cloudbuild/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
substitutions:
# these variable are expected to remain static across environments
_GCR_IMAGE_NAME: gcr.io/bf-container-registry/bf-product/cloud-build-notifiers
_ARTIFACT_REGISTRY_IMAGE_NAME: us-docker.pkg.dev/bf-artifact-registry/bf-docker-registry/bf-asset/cloud-build-notifiers

steps:
- id: Docker Build
name: 'gcr.io/kaniko-project/executor:latest'
args:
- --dockerfile=slack/Dockerfile
- --build-arg=BRANCH_NAME=${BRANCH_NAME}
- --build-arg=BUILD_PROJECT=${PROJECT_ID}
- --build-arg=BUILD_ID=${BUILD_ID}
- --build-arg=BUILD_SERVICE=cloudbuild
- --destination=${_GCR_IMAGE_NAME}:${SHORT_SHA}
- --destination=${_ARTIFACT_REGISTRY_IMAGE_NAME}:${SHORT_SHA}
- --cache=true
23 changes: 23 additions & 0 deletions .cloudbuild/source-checks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
steps:
- id: gokart
name: golang:1.16
entrypoint: 'bash'
args:
- -c
- |
go install github.com/praetorian-inc/gokart@latest
gokart scan ./
waitFor: ['-']

- name: securego/gosec
args: ['./...']
waitFor: ['-']

- id: gofmt
name: golang
entrypoint: 'sh'
args:
- -c
- |
gofmt -s -d . && test -z $(gofmt -s -d .)
waitFor: ['-']
3 changes: 3 additions & 0 deletions .cloudbuild/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
steps:
- name: golang
args: ['test', './slack']
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*.dll
*.so
*.dylib
.idea

# Test binary, built with `go test -c`
*.test
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Cloud Build user and to have the
[gcloud CLI tool](https://cloud.google.com/sdk/gcloud/) installed and configured
for your Cloud Build project(s).

There are currently 3 supported notifier types:
There are currently 4 supported notifier types:

- [`bigquery`](./bigquery/README.md), which writes Build updates and related
data to a BigQuery table.
Expand Down
1 change: 1 addition & 0 deletions bigquery/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
FROM golang AS build-env
COPY . /go-src/
WORKDIR /go-src/bigquery
ENV CGO_ENABLED=0
RUN go test /go-src/bigquery
RUN go build -o /go-app .

Expand Down
7 changes: 5 additions & 2 deletions bigquery/bigquery.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ metadata:
name: example-bigquery-notifier
spec:
notification:
filter: build.build_trigger_id == "123e4567-e89b-12d3-a456-426614174000"
filter: build.status == Build.Status.SUCCESS
template:
type: golang
uri: gs://project-name/arbitary.json
delivery:
table: projects/project_id/datasets/dataset_name/tables/table_name
table: projects/project-name/test-data/tables/build_data

5 changes: 5 additions & 0 deletions bigquery/bq.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"build": {
"status": "{{.Build.Status}}"
}
}
63 changes: 43 additions & 20 deletions bigquery/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,25 @@
package main

import (
"bytes"
"context"
"errors"
"fmt"
"math/big"
"os"
"regexp"
"text/template"
"time"

"cloud.google.com/go/bigquery"
"cloud.google.com/go/civil"
cbpb "cloud.google.com/go/cloudbuild/apiv1/v2/cloudbuildpb"
"github.com/GoogleCloudPlatform/cloud-build-notifiers/lib/notifiers"
log "github.com/golang/glog"
"github.com/golang/protobuf/ptypes"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/google"
"github.com/google/go-containerregistry/pkg/v1/remote"
cbpb "google.golang.org/genproto/googleapis/devtools/cloudbuild/v1"
"google.golang.org/protobuf/types/known/timestamppb"
)

Expand All @@ -57,9 +58,12 @@ func main() {
}

type bqNotifier struct {
bqf bqFactory
filter notifiers.EventFilter
client bq
bqf bqFactory
filter notifiers.EventFilter
tmpl *template.Template
client bq
br notifiers.BindingResolver
tmplView *notifiers.TemplateView
}

type bqRow struct {
Expand All @@ -76,6 +80,7 @@ type bqRow struct {
Env []string
LogURL string
Substitutions []*substitution
JSON string
}

type substitution struct {
Expand Down Expand Up @@ -107,7 +112,6 @@ type actualBQFactory struct {
}

func (bqf *actualBQFactory) Make(ctx context.Context) (bq, error) {

projectID := os.Getenv("PROJECT_ID")
if projectID == "" {
return nil, errors.New("PROJECT_ID environment variable must be set")
Expand Down Expand Up @@ -152,7 +156,7 @@ func imageManifestToBuildImage(image string) (*buildImage, error) {
return &buildImage{SHA: sha.String(), ContainerSizeMB: containerSize}, nil
}

func (n *bqNotifier) SetUp(ctx context.Context, cfg *notifiers.Config, _ notifiers.SecretGetter, _ notifiers.BindingResolver) error {
func (n *bqNotifier) SetUp(ctx context.Context, cfg *notifiers.Config, bigQueryJson string, _ notifiers.SecretGetter, br notifiers.BindingResolver) error {
prd, err := notifiers.MakeCELPredicate(cfg.Spec.Notification.Filter)
if err != nil {
return fmt.Errorf("failed to make a CEL predicate: %v", err)
Expand Down Expand Up @@ -180,15 +184,19 @@ func (n *bqNotifier) SetUp(ctx context.Context, cfg *notifiers.Config, _ notifie
if err = n.client.EnsureTable(ctx, rs[2]); err != nil {
return err
}
return nil

tmpl, err := template.New("bq_json_template").Parse(bigQueryJson)
n.tmpl = tmpl
n.br = br

return nil
}

func parsePBTime(time *timestamppb.Timestamp) (civil.DateTime, error) {
newTime, err := ptypes.Timestamp(time)
if err != nil {
return civil.DateTime{}, fmt.Errorf("error parsing timestamp: %v", err)
if time == nil {
return civil.DateTime{}, fmt.Errorf("timestamp is nil")
}
newTime := time.AsTime()
return civil.DateTimeOf(newTime), nil
}

Expand Down Expand Up @@ -234,10 +242,7 @@ func (n *bqNotifier) SendNotification(ctx context.Context, build *cbpb.Build) er
if err != nil {
return fmt.Errorf("error parsing FinishTime: %v", err)
}
unixZeroTimestamp, err := ptypes.TimestampProto(time.Unix(0, 0))
if err != nil {
return err
}
unixZeroTimestamp := timestamppb.New(time.Unix(0, 0))
for _, step := range build.GetSteps() {
st := step.GetTiming().GetStartTime()
et := step.GetTiming().GetEndTime()
Expand Down Expand Up @@ -267,12 +272,29 @@ func (n *bqNotifier) SendNotification(ctx context.Context, build *cbpb.Build) er
}
logURL, err := notifiers.AddUTMParams(build.LogUrl, notifiers.StorageMedium)
if err != nil {
return fmt.Errorf("Error generating UTM params: %v", err)
return fmt.Errorf("error generating UTM params: %v", err)
}
substitutions := []*substitution{}
for key, value := range build.Substitutions {
substitutions = append(substitutions, &substitution{key, value})
}
var bindings map[string]string
if n.br != nil {
bindings, err = n.br.Resolve(ctx, nil, build)
if err != nil {
return fmt.Errorf("failed to resolve bindings: %w", err)
}
}

n.tmplView = &notifiers.TemplateView{
Build: &notifiers.BuildView{Build: build},
Params: bindings,
}
var buf bytes.Buffer
if err := n.tmpl.Execute(&buf, n.tmplView); err != nil {
return err
}

newRow := &bqRow{
ProjectID: build.ProjectId,
ID: build.Id,
Expand All @@ -287,6 +309,7 @@ func (n *bqNotifier) SendNotification(ctx context.Context, build *cbpb.Build) er
Env: build.Options.Env,
LogURL: logURL,
Substitutions: substitutions,
JSON: buf.String(),
}
return n.client.WriteRow(ctx, newRow)
}
Expand All @@ -310,22 +333,22 @@ func (bq *actualBQ) EnsureTable(ctx context.Context, tableName string) error {
bq.table = bq.dataset.Table(tableName)
schema, err := bigquery.InferSchema(bqRow{})
if err != nil {
return fmt.Errorf("Failed to infer schema: %v", err)
return fmt.Errorf("failed to infer schema: %v", err)
}
metadata, err := bq.dataset.Table(tableName).Metadata(ctx)
if err != nil {
log.Warningf("Error obtaining table metadata: %q;Creating new BigQuery table: %q", err, tableName)
// Create table if it does not exist.
if err := bq.table.Create(ctx, &bigquery.TableMetadata{Name: tableName, Description: "BigQuery Notifier Build Data Table", Schema: schema}); err != nil {
return fmt.Errorf("Failed to initialize table %v: ", err)
return fmt.Errorf("failed to initialize table %v: ", err)
}
} else if len(metadata.Schema) == 0 {
log.Warningf("No schema found for table, writing new schema for table: %v", tableName)
update := bigquery.TableMetadataToUpdate{
Schema: schema,
}
if _, err := bq.table.Update(ctx, update, metadata.ETag); err != nil {
return fmt.Errorf("Error: unable to update schema of table: %v", err)
return fmt.Errorf("error: unable to update schema of table: %v", err)
}
}

Expand All @@ -336,7 +359,7 @@ func (bq *actualBQ) WriteRow(ctx context.Context, row *bqRow) error {
ins := bq.table.Inserter()
log.V(2).Infof("Writing row: %v", row)
if err := ins.Put(ctx, row); err != nil {
return fmt.Errorf("Error inserting row into BQ: %v", err)
return fmt.Errorf("error inserting row into BQ: %v", err)
}
return nil
}
11 changes: 7 additions & 4 deletions bigquery/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ import (
"testing"

"cloud.google.com/go/bigquery"
cbpb "cloud.google.com/go/cloudbuild/apiv1/v2/cloudbuildpb"
"github.com/GoogleCloudPlatform/cloud-build-notifiers/lib/notifiers"
log "github.com/golang/glog"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/types"
"google.golang.org/api/googleapi"
cbpb "google.golang.org/genproto/googleapis/devtools/cloudbuild/v1"
"google.golang.org/protobuf/types/known/timestamppb"
)

Expand Down Expand Up @@ -244,7 +244,7 @@ func TestSetUp(t *testing.T) {
}} {
t.Run(tc.name, func(t *testing.T) {
n := &bqNotifier{bqf: &fakeBQFactory{&fakeBQ{}}}
err := n.SetUp(context.Background(), tc.cfg, nil, nil)
err := n.SetUp(context.Background(), tc.cfg, "", nil, nil)
if err != nil {
if tc.wantErr {
t.Logf("got expected error: %v", err)
Expand Down Expand Up @@ -373,7 +373,7 @@ func TestEnsureFunctions(t *testing.T) {
}} {
t.Run(tc.name, func(t *testing.T) {
n := &bqNotifier{bqf: &fakeBQFactory{&fakeBQ{}}}
err := n.SetUp(context.Background(), tc.cfg, nil, nil)
err := n.SetUp(context.Background(), tc.cfg, "", nil, nil)
if err != nil {
if tc.wantErr {
t.Logf("got expected error: %v", err)
Expand Down Expand Up @@ -581,14 +581,17 @@ func TestSendNotification(t *testing.T) {
FinishTime: timestamppb.Now(),
Tags: []string{},
Options: &cbpb.BuildOptions{Env: []string{}},
Steps: []*cbpb.BuildStep{
{Status: cbpb.Build_SUCCESS},
},
},
wantErr: true,
wantRow: false,
}} {
t.Run(tc.name, func(t *testing.T) {
fakeBQ := &fakeBQ{}
n := &bqNotifier{bqf: &fakeBQFactory{fakeBQ}}
err := n.SetUp(context.Background(), tc.cfg, nil, nil)
err := n.SetUp(context.Background(), tc.cfg, "{{.Build.Status}}", nil, nil)
if err != nil {
t.Fatalf("Setup(%v) got unexpected error: %v", tc.cfg, err)
}
Expand Down
34 changes: 34 additions & 0 deletions githubissues/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

FROM golang AS build-env
COPY . /go-src/
WORKDIR /go-src/githubissues
ENV CGO_ENABLED=0
RUN go test /go-src/githubissues
RUN go build -o /go-app .

# From the Cloud Run docs:
# https://cloud.google.com/run/docs/tutorials/pubsub#looking_at_the_code
# Use the official Debian slim image for a lean production container.
# https://hub.docker.com/_/debian
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
FROM debian:buster-slim
RUN set -x && apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \
ca-certificates && \
rm -rf /var/lib/apt/lists/*

FROM gcr.io/distroless/base
COPY --from=build-env /go-app /
ENTRYPOINT ["/go-app", "--alsologtostderr", "--v=0"]
22 changes: 22 additions & 0 deletions githubissues/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Cloud Build GitHub Issues Notifier

This notifier uses [GitHub Webhooks](https://docs.github.com/en/developers/webhooks-and-events/webhooks/creating-webhooks) to
create issues against your GitHub repo.

This notifier runs as a container via Google Cloud Run and responds to
events that Cloud Build publishes via its
[Pub/Sub topic](https://cloud.google.com/cloud-build/docs/send-build-notifications).

For detailed instructions on setting up this notifier,
see [Configuring GitHub Issue notifications](https://cloud.google.com/cloud-build/docs/configuring-notifications/configure-githubissues).

## Configuration Variables

This notifier expects the following fields in the `delivery` map to be set:

- `githubRepo`: The name of the repo to create an issue against (e.g. `youruser/yourrepo`)
- `githubToken`: The `secretRef: <github-token>` map that references the GitHub Issue token resource path in the `secrets` section.

This notifier also takes a custom `template` that can either be set inline, or as a uri, as a
JSON object specifying at minimum the customisable `title` and `body` (in Markdown) of the issue. See [GitHub's REST documentation](https://docs.github.com/en/rest/issues/issues#create-an-issue) for more body parameters. See TODO for more on templates.

Loading