Skip to content

Commit

Permalink
Adds Drone and Github workflow for Grafana's fork
Browse files Browse the repository at this point in the history
This commit adds the files that are specific to Grafana's fork
that are not intended to be included in the upstream. This
files are already present in the commit history (they were deleted),
but this is an attempt of a fresh start on top of the upstream's `main`.
At the moment, this should the only commit we expect to skip when
sending our changes.
  • Loading branch information
npazosmendez committed Sep 21, 2022
1 parent b2707b1 commit c10d5d9
Show file tree
Hide file tree
Showing 15 changed files with 700 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .drone/Tests.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM golang:alpine as builder
RUN apk --no-cache add make gcc git cairo-dev musl-dev
98 changes: 98 additions & 0 deletions .drone/cmd/coverage/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package main

import (
"bufio"
"flag"
"fmt"
"os"
"sort"
"strconv"
"strings"
)

type aggregation struct {
NumStmts int
NumCoveredStmts int
}

func (a *aggregation) CoveragePct() string {
pct := (float64(a.NumCoveredStmts) / float64(a.NumStmts)) * 100
return fmt.Sprintf("%.1f%%", pct)
}

func main() {
covFilename := flag.String("f", "coverage.out", "Output of `go test -coverprofile=coverage.out ./...`")
flag.Parse()

f, err := os.Open(*covFilename)
if err != nil {
panic(err)
}

agg := make(map[string]*aggregation)

s := bufio.NewScanner(f)
s.Split(bufio.ScanLines)

s.Scan() // First line specifies the mode; it doesn't affect what we do so we can just skip it.

for s.Scan() {
line := s.Text()

cols := strings.Fields(line)
key := strings.Split(cols[0], ":")[0]

numStmts, err := strconv.Atoi(cols[1])
if err != nil {
panic(err)
}
numTimesCovered, err := strconv.Atoi(cols[2])
if err != nil {
panic(err)
}

if val, ok := agg[key]; ok {
val.NumStmts += numStmts
if numTimesCovered > 0 {
val.NumCoveredStmts += numStmts
}
} else {
numCoveredStmts := 0
if numTimesCovered > 0 {
numCoveredStmts = numStmts
}

agg[key] = &aggregation{
NumStmts: numStmts,
NumCoveredStmts: numCoveredStmts,
}
}
}

keys := make([]string, 0, len(agg))
for k := range agg {
keys = append(keys, k)
}
sort.Strings(keys)

fmt.Println("Go coverage report:")
fmt.Println("<details>")
fmt.Println("<summary>Click to expand.</summary>")
fmt.Println("")
fmt.Println("| File | % |")
fmt.Println("| ---- | - |")

totalStmts := 0
totalCoveredStmts := 0

for _, k := range keys {
a := agg[k]
fmt.Printf("| %s | %s |\n", k, a.CoveragePct())

totalStmts += a.NumStmts
totalCoveredStmts += a.NumCoveredStmts
}

fmt.Printf("| total | %.1f%% |\n", (float64(totalCoveredStmts)/float64(totalStmts))*100)
fmt.Println("</details>")
}
97 changes: 97 additions & 0 deletions .drone/cmd/ghcomment/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package main

import (
"context"
"flag"
"io/ioutil"
"os"
"strconv"
"strings"

"github.com/shurcooL/githubv4"

"github.com/go-graphite/carbonapi/drone/pkg/github"
)

const CommenterLogin = "grafanabot"

func main() {
grafanabotPat := getRequiredEnv("GRAFANABOT_PAT")

repoOwner := getRequiredEnv("DRONE_REPO_OWNER")
repoName := getRequiredEnv("DRONE_REPO_NAME")

pullRequest, err := strconv.Atoi(getRequiredEnv("DRONE_PULL_REQUEST"))
if err != nil {
panic(err)
}

commentTypeIdentifier := flag.String("id", "", "String that identifies the comment type being submitted")
commentBodyFilename := flag.String("bodyfile", "", "A file containing the comment body")
flag.Parse()

if *commentTypeIdentifier == "" {
panic("Required argument: -i")
}
if *commentBodyFilename == "" {
panic("Required argument: -b")
}

api := github.NewAPI(context.Background(), grafanabotPat)

err = minimizeOutdatedComments(api, repoOwner, repoName, pullRequest, *commentTypeIdentifier)
if err != nil {
panic(err)
}

commentBody, err := ioutil.ReadFile(*commentBodyFilename)
if err != nil {
panic(err)
}

err = addComment(api, repoOwner, repoName, pullRequest, string(commentBody))
if err != nil {
panic(err)
}
}

func getRequiredEnv(k string) string {
v, p := os.LookupEnv(k)
if !p {
panic("Missing required env var: " + k)
}

return v
}

func minimizeOutdatedComments(api *github.API, repoOwner string, repoName string, pullRequestNo int, commentTypeIdentifier string) error {
prComments, err := api.ListPullRequestComments(repoOwner, repoName, pullRequestNo)
if err != nil {
return err
}

for _, comment := range prComments {
if comment.Author.Login == CommenterLogin && strings.Contains(comment.Body, commentTypeIdentifier) && !comment.IsMinimized {
err := api.MinimizeComment(comment.ID, githubv4.ReportedContentClassifiersOutdated)
if err != nil {
return err
}
}
}

return nil
}

func addComment(api *github.API, repoOwner string, repoName string, pullRequestNo int, commentBody string) error {
pullRequestNodeID, err := api.GetPullRequestNodeID(repoOwner, repoName, pullRequestNo)
if err != nil {
return err
}

_, err = api.AddComment(pullRequestNodeID, commentBody)
if err != nil {
return err
}

return nil
}
93 changes: 93 additions & 0 deletions .drone/drone.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
local drone = import 'lib/drone/drone.libsonnet';
local images = import 'lib/drone/images.libsonnet';
local triggers = import 'lib/drone/triggers.libsonnet';
local vault = import 'lib/vault/vault.libsonnet';

local pipeline = drone.pipeline;
local step = drone.step;
local withInlineStep = drone.withInlineStep;
local withStep = drone.withStep;
local withSteps = drone.withSteps;

local imagePullSecrets = { image_pull_secrets: ['dockerconfigjson'] };

local commentCoverageLintReport = {
step: step('coverage + lint', $.commands, image=$.image, environment=$.environment),
commands: [
// Build drone utilities.
'scripts/build-drone-utilities.sh',
// Generate the raw coverage report.
'go test -coverprofile=coverage.out ./...',
// Process the raw coverage report.
'.drone/coverage > coverage_report.out',
// Generate the lint report.
'scripts/generate-lint-report.sh',
// Combine the reports.
'cat coverage_report.out > report.out',
'echo "" >> report.out',
'cat lint.out >> report.out',
// Submit the comment to GitHub.
'.drone/ghcomment -id "Go coverage report:" -bodyfile report.out',
],
environment: {
GRAFANABOT_PAT: { from_secret: 'gh_token' },
},
image: images._images.goLint,
};

local buildAndPushImages = {
// step builds the pipeline step to build and push a docker image
step(app): step(
'%s: build and push' % app,
[],
image=buildAndPushImages.pluginName,
settings=buildAndPushImages.settings(app),
),

pluginName: 'plugins/gcr',

// settings generates the CI Pipeline step settings
settings(app): {
repo: $._repo(app),
registry: $._registry,
dockerfile: './Dockerfile',
json_key: { from_secret: 'gcr_admin' },
mirror: 'https://mirror.gcr.io',
build_args: ['cmd=' + app],
},

// image generates the image for the given app
image(app): $._registry + '/' + $._repo(app),

_repo(app):: 'kubernetes-dev/' + app,
_registry:: 'us.gcr.io',
};

local runTests = {
step: step('run tests', $.commands, image=$.image),
commands: [
'make test'
],
image: images._images.testRunner,
settings: {

}
};

[
pipeline('test')
+ withStep(runTests.step)
+ imagePullSecrets
+ triggers.pr
+ triggers.main,

pipeline('coverageLintReport')
+ withStep(commentCoverageLintReport.step)
+ triggers.pr,
]
+ [
vault.secret('dockerconfigjson', 'secret/data/common/gcr', '.dockerconfigjson'),
vault.secret('gh_token', 'infra/data/ci/github/grafanabot', 'pat'),
vault.secret('gcr_admin', 'infra/data/ci/gcr-admin', 'service-account'),
vault.secret('argo_token', 'infra/data/ci/argo-workflows/trigger-service-account', 'token'),
]
79 changes: 79 additions & 0 deletions .drone/drone.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
---
depends_on: null
image_pull_secrets:
- dockerconfigjson
kind: pipeline
name: test
steps:
- commands:
- make test
depends_on: []
entrypoint: null
environment: {}
image: us.gcr.io/kubernetes-dev/carbonapi/test-runner:latest
name: run tests
settings: {}
trigger:
branch:
- main
event:
include:
- pull_request
- push
type: docker
---
depends_on: null
kind: pipeline
name: coverageLintReport
steps:
- commands:
- scripts/build-drone-utilities.sh
- go test -coverprofile=coverage.out ./...
- .drone/coverage > coverage_report.out
- scripts/generate-lint-report.sh
- cat coverage_report.out > report.out
- echo "" >> report.out
- cat lint.out >> report.out
- .drone/ghcomment -id "Go coverage report:" -bodyfile report.out
depends_on: []
entrypoint: null
environment:
GRAFANABOT_PAT:
from_secret: gh_token
image: golangci/golangci-lint:v1.45
name: coverage + lint
settings: {}
trigger:
event:
include:
- pull_request
type: docker
---
get:
name: .dockerconfigjson
path: secret/data/common/gcr
kind: secret
name: dockerconfigjson
---
get:
name: pat
path: infra/data/ci/github/grafanabot
kind: secret
name: gh_token
---
get:
name: service-account
path: infra/data/ci/gcr-admin
kind: secret
name: gcr_admin
---
get:
name: token
path: infra/data/ci/argo-workflows/trigger-service-account
kind: secret
name: argo_token
---
kind: signature
hmac: 786972e7020e02e75b297bf8f7a67669cf11bd5bf02f61ac62ec63d0fca2a6c1

...
16 changes: 16 additions & 0 deletions .drone/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module github.com/go-graphite/carbonapi/drone

go 1.18

require (
github.com/shurcooL/githubv4 v0.0.0-20220520033151-0b4e3294ff00
golang.org/x/oauth2 v0.0.0-20220630143837-2104d58473e0
)

require (
github.com/golang/protobuf v1.5.2 // indirect
github.com/shurcooL/graphql v0.0.0-20220606043923-3cf50f8a0a29 // indirect
golang.org/x/net v0.0.0-20220706163947-c90051bbdb60 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.0 // indirect
)
Loading

0 comments on commit c10d5d9

Please sign in to comment.