Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for github status checks #128

Merged
merged 1 commit into from
Jun 29, 2019
Merged
Changes from all commits
Commits
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
47 changes: 47 additions & 0 deletions internal/enforcer/enforcer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@
package enforcer

import (
"context"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"strings"
"text/tabwriter"

"github.com/autonomy/conform/internal/policy"
"github.com/autonomy/conform/internal/policy/commit"
"github.com/autonomy/conform/internal/policy/license"
"github.com/google/go-github/github"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"

Expand All @@ -23,6 +28,8 @@ import (
// Conform is a struct that conform.yaml gets decoded into.
type Conform struct {
Policies []*PolicyDeclaration `yaml:"policies"`

token string
}

// PolicyDeclaration allows a user to declare an arbitrary type along with a
Expand Down Expand Up @@ -51,6 +58,11 @@ func New() (*Conform, error) {
return nil, err
}

token, ok := os.LookupEnv("GITHUB_TOKEN")
if ok {
c.token = token
}

return c, nil
}

Expand All @@ -73,8 +85,10 @@ func (c *Conform) Enforce(setters ...policy.Option) {
for _, err := range check.Errors() {
fmt.Fprintf(w, "%s\t%s\t%s\t%v\t\n", p.Type, check.Name(), "FAILED", err)
}
c.SetStatus("failure", p.Type, check.Name(), check.Message())
} else {
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t\n", p.Type, check.Name(), "PASS", "<none>")
c.SetStatus("success", p.Type, check.Name(), check.Message())
}
}
}
Expand All @@ -87,6 +101,39 @@ func (c *Conform) Enforce(setters ...policy.Option) {
}
}

// SetStatus sets the status of a GitHub check.
// Valid statuses are "error", "failure", "pending", "success"
func (c *Conform) SetStatus(state, policy, check, message string) {
if c.token == "" {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a cheesy way to make this optional for now. We have more immediate use cases and I can circle back and refactor. I'm thinking of a Summarizer interface that can be GitHub, PlainText,etc..

return
}
statusCheckContext := strings.ReplaceAll(strings.ToLower(path.Join("conform", policy, check)), " ", "-")
description := message
repoStatus := &github.RepoStatus{}
repoStatus.Context = &statusCheckContext
repoStatus.Description = &description
repoStatus.State = &state

http.DefaultClient.Transport = roundTripper{c.token}
githubClient := github.NewClient(http.DefaultClient)

parts := strings.Split(os.Getenv("GITHUB_REPOSITORY"), "/")

_, _, err := githubClient.Repositories.CreateStatus(context.Background(), parts[0], parts[1], os.Getenv("GITHUB_SHA"), repoStatus)
if err != nil {
log.Fatal(err)
}
}

type roundTripper struct {
accessToken string
}

func (rt roundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
r.Header.Set("Authorization", fmt.Sprintf("Bearer %s", rt.accessToken))
return http.DefaultTransport.RoundTrip(r)
}

func (c *Conform) enforce(declaration *PolicyDeclaration, opts *policy.Options) (*policy.Report, error) {
if _, ok := policyMap[declaration.Type]; !ok {
return nil, errors.Errorf("Policy %q is not defined", declaration.Type)
Expand Down