Skip to content

Commit

Permalink
Add required issue headings
Browse files Browse the repository at this point in the history
Adds basic mechanism to help users understand that issue
templates are not optional.

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
  • Loading branch information
alexellis committed Feb 6, 2021
1 parent d82e7ec commit 53d5691
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 8 deletions.
12 changes: 5 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
FROM ghcr.io/openfaas/classic-watchdog:0.1.2 as watchdog

FROM golang:1.15-alpine as build
FROM --platform=${TARGETPLATFORM:-linux/amd64} ghcr.io/openfaas/classic-watchdog:0.1.4 as watchdog
FROM --platform=${BUILDPLATFORM:-linux/amd64} golang:1.15-alpine3.12 as build

ENV CGO_ENABLED=0
ENV GO111MODULE=on

WORKDIR /go/src/github.com/alexellis/derek
COPY . .

RUN go test $(go list ./... | grep -v /vendor/) -cover

RUN CGO_ENABLED=0 GOOS=linux go build -mod=vendor -a -installsuffix cgo -o derek .
RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} CGO_ENABLED=${CGO_ENABLED} go test $(go list ./... | grep -v /vendor/) -cover
RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} CGO_ENABLED=${CGO_ENABLED} go build -mod=vendor -a -installsuffix cgo -o derek .

FROM alpine:3.12 as ship
FROM --platform=${TARGETPLATFORM:-linux/amd64} alpine:3.13 as ship

COPY --from=watchdog /fwatchdog /usr/bin/fwatchdog
RUN chmod +x /usr/bin/fwatchdog
Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,6 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
Expand Down
44 changes: 44 additions & 0 deletions handler/template_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package handler

import (
"log"
"strings"

"github.com/alexellis/derek/config"
"github.com/alexellis/derek/types"
"github.com/google/go-github/github"
)

func CheckIssueTemplateHeadings(req types.IssuesOuter, derekConfig *types.DerekRepoConfig, config config.Config) error {

body := req.Issue.Body

found := 0
for _, heading := range derekConfig.RequiredInIssues {
if strings.Contains(body, heading) {
found++
}
}

client, ctx := makeClient(req.Installation.ID, config)

if found != len(derekConfig.RequiredInIssues) {
log.Printf("Issue headings found: %d, wanted: %d", found, len(derekConfig.RequiredInIssues))
if _, _, err := client.Issues.AddLabelsToIssue(ctx, req.Repository.Owner.Login, req.Repository.Name, req.Issue.Number, []string{"invalid"}); err != nil {
return err
}

messageValue, err := createIssueComment(derekConfig.Messages, "template")
if err != nil {
msg := "Please complete the whole issue template, without deleting any headings."
messageValue = &github.IssueComment{
Body: &msg,
}
}
if _, _, err = client.Issues.CreateComment(ctx, req.Repository.Owner.Login, req.Repository.Name, req.Issue.Number, messageValue); err != nil {
return err
}
}

return nil
}
39 changes: 39 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,45 @@ func handleEvent(eventType string, bytesIn []byte, config config.Config) error {
}
break

case "issues":

req := types.IssuesOuter{}
if err := json.Unmarshal(bytesIn, &req); err != nil {
return fmt.Errorf("Cannot parse input %s", err.Error())
}

if req.Action == "opened" {
log.Printf("Owner: %s, repo: %s, action: %s", req.Repository.Owner.Login, req.Repository.Name, "issues")

customer, err := auth.IsCustomer(req.Repository.Owner.Login, &http.Client{})
if err != nil {
return fmt.Errorf("Unable to verify customer: %s/%s", req.Repository.Owner.Login, req.Repository.Name)
} else if customer == false {
return fmt.Errorf("No customer found for: %s/%s", req.Repository.Owner.Login, req.Repository.Name)
}

var derekConfig *types.DerekRepoConfig
if req.Repository.Private {
derekConfig, err = handler.GetPrivateRepoConfig(req.Repository.Owner.Login, req.Repository.Name, req.Installation.ID, config)
} else {
derekConfig, err = handler.GetRepoConfig(req.Repository.Owner.Login, req.Repository.Name)
}
if err != nil {
return fmt.Errorf("Unable to access maintainers file at: %s/%s\nError: %s",
req.Repository.Owner.Login,
req.Repository.Name,
err.Error())
}

if len(derekConfig.RequiredInIssues) > 0 {
err := handler.CheckIssueTemplateHeadings(req, derekConfig, config)
if err != nil {
return err
}
}
}

break
case "issue_comment":
req := types.IssueCommentOuter{}
if err := json.Unmarshal(bytesIn, &req); err != nil {
Expand Down
16 changes: 16 additions & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ type PullRequestOuter struct {
InstallationRequest
}

type IssuesOuter struct {
Repository Repository `json:"repository"`
Comment Comment `json:"comment"`
Action string `json:"action"`
Issue Issue `json:"issue"`
Sender Sender `json:"sender"`
InstallationRequest
}

type Sender struct {
Login string `json:"login"`
}

type IssueCommentOuter struct {
Repository Repository `json:"repository"`
Comment Comment `json:"comment"`
Expand All @@ -53,6 +66,7 @@ type Issue struct {
Labels []IssueLabel `json:"labels"`
Number int `json:"number"`
Title string `json:"title"`
Body string `json:"body"`
Locked bool `json:"locked"`
State string `json:"state"`
Milestone Milestone `json:"milestone"`
Expand Down Expand Up @@ -95,6 +109,8 @@ type DerekRepoConfig struct {
ContributingURL string `yaml:"contributing_url"`

Messages []Message `yaml:"custom_messages"`

RequiredInIssues []string `yaml:"required_in_issues"`
}

type Message struct {
Expand Down

0 comments on commit 53d5691

Please sign in to comment.