Skip to content

Commit

Permalink
Merge branch 'main' into renovate/docker-alpine-3.x
Browse files Browse the repository at this point in the history
  • Loading branch information
chaspy committed Jan 24, 2021
2 parents 5f54f74 + 485465b commit b3e87c8
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .envrc.sample
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export DATADOG_API_KEY="secret-datadog-api-key
export DATADOG_API_KEY="secret-datadog-api-key"
export DATADOG_APP_KEY="secret-datadog-app-key"
export GITHUB_TOKEN="secret-github-personal-token"
export GITHUB_REPOSITORIES="chaspy/datadog-github-pr,chaspy/favsearch"
19 changes: 19 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Publish Docker image
on:
push:
tags:
- v*
jobs:
push_to_registry:
name: Push Docker image to Docker Hub
runs-on: ubuntu-latest
steps:
- name: Check out the repo
uses: actions/checkout@v2
- name: Push to Docker Hub
uses: docker/build-push-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
repository: chaspy/datadog-github-pr
tag_with_ref: true
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ RUN go build \

FROM alpine:3.13.0 as runner

COPY --from=builder /go/bin/main /app/main
COPY --from=builder /go/bin/datadog-github-pr /app/datadog-github-pr

ENTRYPOINT ["/app/datadog-github-pr"]
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,36 @@
# datadog-github-pr
Send a count of Pull-Request as custom metrics to Datadog

[![](https://mermaid.ink/img/eyJjb2RlIjoiZ3JhcGggTFJcbiAgICBBW0dpdEh1Yl1cbiAgICBCW2RhdGFkb2ctZ2l0aHViLXByXVxuICAgIENbRGF0YWRvZ11cbiAgICBCIC0tPnxnZXQgUHVsbCBSZXF1ZXN0c3wgQVxuICAgIEIgLS0-fHNuZWQgYSBjb3VudCBvZiBQdWxsIFJlcXVlc3RzIGFzIGN1c3RvbSBtZXRyaWNzfCBDIiwibWVybWFpZCI6e30sInVwZGF0ZUVkaXRvciI6ZmFsc2V9)](https://mermaid-js.github.io/mermaid-live-editor/#/edit/eyJjb2RlIjoiZ3JhcGggTFJcbiAgICBBW0dpdEh1Yl1cbiAgICBCW2RhdGFkb2ctZ2l0aHViLXByXVxuICAgIENbRGF0YWRvZ11cbiAgICBCIC0tPnxnZXQgUHVsbCBSZXF1ZXN0c3wgQVxuICAgIEIgLS0-fHNuZWQgYSBjb3VudCBvZiBQdWxsIFJlcXVlc3RzIGFzIGN1c3RvbSBtZXRyaWNzfCBDIiwibWVybWFpZCI6e30sInVwZGF0ZUVkaXRvciI6ZmFsc2V9)

## Preparation

Copy .envrc and lod it.

```
$ cp .envrc.sample .envrc
$ # edit .envrc
$ # source .envrc
```

The target repositories are specified by GITHUB_REPOSITORIES environment varibales, that should be written in org/reponame, separated by commas.

>export GITHUB_REPOSITORIES="chaspy/datadog-github-pr,chaspy/favsearch"
## How to run

### Local

```
$ go run main.go
```

### Binary

Get the binary file from [Releases](https://github.com/chaspy/datadog-github-pr/releases) and run it.

### Docker

```
$ docker run -e DATADOG_API_KEY="${DATADOG_API_KEY}" -e DATADOG_APP_KEY="${DATADOG_APP_KEY}" -e GITHUB_TOKEN="${GITHUB_TOKEN}" -e GITHUB_REPOSITORIES="${GITHUB_REPOSITORIES}" chaspy/datadog-github-pr:v0.1.1
```
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/cenkalti/backoff v2.2.1+incompatible // indirect
github.com/daixiang0/gci v0.2.8 // indirect
github.com/google/go-github v17.0.0+incompatible
github.com/google/go-github/v33 v33.0.0
github.com/google/go-querystring v1.0.0 // indirect
github.com/zorkian/go-datadog-api v2.30.0+incompatible
golang.org/x/oauth2 v0.0.0-20210113205817-d3ed898aa8a3
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrU
github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8=
github.com/golang/protobuf v1.4.2 h1:+Z5KGCizgyZCbGh1KZqA0fcLLkwbsjIzS4aV2v7wJX0=
github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
Expand All @@ -89,6 +90,7 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4rEjNlfyDHW9dolSY=
github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ=
github.com/google/go-github/v33 v33.0.0/go.mod h1:GMdDnVZY/2TsWgp/lkYnpSAh6TrzhANBBwm6k6TTEXg=
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
Expand Down
46 changes: 40 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"os"
"strconv"
"strings"
"time"

"github.com/google/go-github/github"
Expand All @@ -18,6 +19,7 @@ type PR struct {
Labels []*github.Label
User *string
RequestedReviewers []*github.User
Repo string
}

func main() {
Expand All @@ -38,7 +40,14 @@ func run() error {
return fmt.Errorf("failed to read Datadog Config: %w", err)
}

prs, err := getPullRequests(githubToken)
repositories, err := getRepositories()
if err != nil {
return fmt.Errorf("failed to get GitHub repository name: %w", err)
}

repositoryList := parseRepositories(repositories)

prs, err := getPullRequests(githubToken, repositoryList)
if err != nil {
return fmt.Errorf("failed to get PullRequests: %w", err)
}
Expand Down Expand Up @@ -90,7 +99,7 @@ func readGithubConfig() (string, error) {
return githubToken, nil
}

func getPullRequests(githubToken string) ([]*github.PullRequest, error) {
func getPullRequests(githubToken string, githubRepositories []string) ([]*github.PullRequest, error) {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: githubToken},
)
Expand All @@ -99,23 +108,48 @@ func getPullRequests(githubToken string) ([]*github.PullRequest, error) {

client := github.NewClient(tc)

prs, _, err := client.PullRequests.List(ctx, "quipper", "kubernetes-clusters", nil)
if err != nil {
return nil, fmt.Errorf("failed to get GitHub Pull Requests: %w", err)
prs := []*github.PullRequest{}

for _, githubRepository := range githubRepositories {
repo := strings.Split(githubRepository, "/")
org := repo[0]
name := repo[1]
prsInRepo, _, err := client.PullRequests.List(ctx, org, name, nil)
if err != nil {
return nil, fmt.Errorf("failed to get GitHub Pull Requests: %w", err)
}

prs = append(prs, prsInRepo...)
}

return prs, nil
}

func getRepositories() (string, error) {
githubRepositories := os.Getenv("GITHUB_REPOSITORIES")
if len(githubRepositories) == 0 {
return "", fmt.Errorf("missing environment variable: GITHUB_REPOSITORIES")
}

return githubRepositories, nil
}

func parseRepositories(repositories string) []string {
return strings.Split(repositories, ",")
}

func getPRInfos(prs []*github.PullRequest) []PR {
prInfos := []PR{}

for _, pr := range prs {
repos := strings.Split(*pr.URL, "/")

prInfos = append(prInfos, PR{
Number: pr.Number,
Labels: pr.Labels,
User: pr.User.Login,
RequestedReviewers: pr.RequestedReviewers,
Repo: repos[4] + "/" + repos[5],
})
}

Expand Down Expand Up @@ -157,7 +191,7 @@ func generateCustomMetrics(prInfos []PR) ([]datadog.Metric, error) {
{datadog.Float64(nowF), datadog.Float64(countf)},
},
Type: datadog.String("gauge"),
Tags: append([]string{"number:" + strconv.Itoa(*prInfo.Number), "author:" + *prInfo.User, "repo:" + "quipper/kubernetes-clusters"}, labelAndReviewer...),
Tags: append([]string{"number:" + strconv.Itoa(*prInfo.Number), "author:" + *prInfo.User, "repo:" + prInfo.Repo}, labelAndReviewer...),
})
}

Expand Down

0 comments on commit b3e87c8

Please sign in to comment.