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

Refactor - extract functionality into structs #15

Merged
merged 1 commit into from
May 4, 2020
Merged
Show file tree
Hide file tree
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
40 changes: 40 additions & 0 deletions internal/github/action.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package github

import (
"encoding/json"
"os"
"strconv"
"strings"
)

type action struct {
}

func (action action) repositoryOwner() string {
return strings.Split(os.Getenv("GITHUB_REPOSITORY"), "/")[0]
}

func (action action) repositoryName() string {
return strings.Split(os.Getenv("GITHUB_REPOSITORY"), "/")[1]
}

func (action action) pullRequestNumber() int {
s := strings.Split(os.Getenv("GITHUB_REF"), "/")[2]
pullRequestNumber, err := strconv.Atoi(s)
panicIfError((err))

return pullRequestNumber
}

func (action action) token() string {
return os.Getenv("GITHUB_TOKEN")
}

func (action action) specifiedLabels() []string {
var specifiedLabels []string

specifiedJSONLabels := os.Getenv("GITHUB_ACTION_INPUT_LABELS")
panicIfError(json.Unmarshal([]byte(specifiedJSONLabels), &specifiedLabels))

return specifiedLabels
}
43 changes: 43 additions & 0 deletions internal/github/label_checker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Package github checks pull requests for specified GitHub labels
*/
package github

import (
"fmt"
"strings"
)

// CheckLabels checks for the presence of the given GitHub labels
func CheckLabels() {
var githubAction action
pullRequest := newPullRequest(githubAction)
checkLabels(githubAction.specifiedLabels(), pullRequest.labels(), 1)
}

func checkLabels(specifiedLabels []string, pullRequestLabels []string, allowedNumberOfLabels int) {
counter := 0

for i := 0; i < len(pullRequestLabels); i++ {
if contains(specifiedLabels, pullRequestLabels[i]) {
counter++
}
}

if counter != allowedNumberOfLabels {
panic(`Label check failed: required one of ` + strings.Join(specifiedLabels, ", "))
}

fmt.Println(`Label check successful: required one of ` +
strings.Join(specifiedLabels, ", ") + `, and found: ` + pullRequestLabels[0])
}

func contains(slice []string, val string) bool {
for _, item := range slice {
if item == val {
return true
}
}

return false
}
122 changes: 0 additions & 122 deletions internal/github/labels.go

This file was deleted.

62 changes: 62 additions & 0 deletions internal/github/pull_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package github

import (
"context"

"github.com/shurcooL/githubv4"
"golang.org/x/oauth2"
)

type pullRequest struct {
repositoryOwner string
repository string
number int
apiClient *githubv4.Client
}

func newPullRequest(action action) *pullRequest {
return &pullRequest{action.repositoryOwner(),
action.repositoryName(),
action.pullRequestNumber(),
apiClient(action.token())}
}

func (pr pullRequest) labels() []string {
variables := map[string]interface{}{
"owner": githubv4.String(pr.repositoryOwner),
"name": githubv4.String(pr.repository),
"pullRequestNumber": githubv4.Int(pr.number),
}

var query struct {
Repository struct {
PullRequest struct {
Labels struct {
Nodes []struct {
Name string
}
} `graphql:"labels(first: 100)"`
} `graphql:"pullRequest(number: $pullRequestNumber)"`
} `graphql:"repository(owner: $owner, name: $name)"`
}

err := pr.apiClient.Query(context.Background(), &query, variables)
panicIfError(err)

labelNodes := query.Repository.PullRequest.Labels.Nodes

var labels []string

for i := 0; i < len(labelNodes); i++ {
labels = append(labels, labelNodes[i].Name)
}

return labels
}

func apiClient(token string) *githubv4.Client {
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
httpClient := oauth2.NewClient(context.Background(), tokenSource)

return githubv4.NewClient(httpClient)
}
9 changes: 9 additions & 0 deletions internal/github/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package github

import "log"

func panicIfError(err error) {
if err != nil {
log.Fatalf("Error that we cannot handle, %v", err)
}
}