Skip to content

Commit

Permalink
Migrate to using template for Github PR commens (#12)
Browse files Browse the repository at this point in the history
* Migrate to using template for gh comment msg

* Spelling

* Streamline arg flags with "-"format

* Update error message in template.go

Co-authored-by: Bjørn <[email protected]>

* edit message-template description

* Use pointer w message-template

Co-authored-by: Bjørn <[email protected]>
  • Loading branch information
JacobValdemar and Bjørn authored Aug 11, 2020
1 parent 5642ac2 commit c617f32
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ start:
./release-manager-bot \
--release-manager-auth-token $(HAMCTL_AUTH_TOKEN) \
--release-manager-url http://localhost:8081/ \
--github-privateKey "`cat $(GITHUB_PRIVATE_KEY_PATH)`" \
--github-integrationID 75542
--github-private-key "`cat $(GITHUB_PRIVATE_KEY_PATH)`" \
--github-integration-id 75542
14 changes: 9 additions & 5 deletions issue_release_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type PRCreateHandler struct {

releaseManagerAuthToken string
releaseManagerURL string
messageTemplate string
}

func (handler *PRCreateHandler) Handles() []string {
Expand Down Expand Up @@ -125,11 +126,14 @@ func (handler *PRCreateHandler) Handle(ctx context.Context, eventType, deliveryI
return nil
}

botMessage += "Auto-Release Policy detected for service " + serviceName + " at (base)branch " + prBase + "\n\n"
botMessage += "Merging this Pull Request will release the service to the following environments:\n"

for i := 0; i < len(autoReleaseEnvironments); i++ {
botMessage += autoReleaseEnvironments[i] + "\n"
messageData := BotMessageData{
Branch: prBase,
AutoReleaseEnvironments: autoReleaseEnvironments,
Template: handler.messageTemplate,
}
botMessage, err = BotMessage(messageData)
if err != nil {
return errors.Wrapf(err, "creating bot message")
}

logger.Debug().Msgf("%s", botMessage)
Expand Down
13 changes: 8 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ func main() {
var httpServerConfig baseapp.HTTPConfig
pflag.StringVar(&httpServerConfig.Address, "http-address", "localhost", "http listen address")
pflag.IntVar(&httpServerConfig.Port, "http-port", 8080, "http listen port")
pflag.StringVar(&httpServerConfig.PublicURL, "http-public_url", "https://localhost:8080", "http public url")
pflag.StringVar(&httpServerConfig.PublicURL, "http-public-url", "https://localhost:8080", "http public url")

var githubappConfig githubapp.Config
pflag.StringVar(&githubappConfig.V3APIURL, "github-v3_api_url", "https://api.github.com/", "github v3 api url")
pflag.Int64Var(&githubappConfig.App.IntegrationID, "github-integrationID", 0, "github App ID (App->General->About->App ID)")
pflag.StringVar(&githubappConfig.App.WebhookSecret, "github-webhookSecret", "", "github webhook secret")
pflag.StringVar(&githubappConfig.App.PrivateKey, "github-privateKey", "", "github app private key content")
pflag.StringVar(&githubappConfig.V3APIURL, "github-v3-api-url", "https://api.github.com/", "github v3 api url")
pflag.Int64Var(&githubappConfig.App.IntegrationID, "github-integration-id", 0, "github App ID (App->General->About->App ID)")
pflag.StringVar(&githubappConfig.App.WebhookSecret, "github-webhook-secret", "", "github webhook secret")
pflag.StringVar(&githubappConfig.App.PrivateKey, "github-private-key", "", "github app private key content")

messageTemplate := pflag.String("message-template", "'{{.Branch}}' will auto-release to: {{range .AutoReleaseEnvironments}}\n {{.}}{{end}}", "Template string used when commenting on pull requests on Github. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].")

pflag.Parse()

Expand Down Expand Up @@ -66,6 +68,7 @@ func main() {
ClientCreator: cc,
releaseManagerAuthToken: *releaseManagerAuthToken,
releaseManagerURL: *releaseManagerURL,
messageTemplate: *messageTemplate,
}

webhookHandler := githubapp.NewDefaultEventDispatcher(githubappConfig, pullRequestHandler)
Expand Down
31 changes: 31 additions & 0 deletions template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"strings"
"text/template"

"github.com/pkg/errors"
)

type BotMessageData struct {
Template string
Branch string
AutoReleaseEnvironments []string
}

func BotMessage(data BotMessageData) (string, error) {
var message strings.Builder

template := template.New("test")
template, err := template.Parse(data.Template)
if err != nil {
return "", errors.Wrapf(err, "parsing template: '%s'", data.Template)
}

err = template.Execute(&message, data)
if err != nil {
return "", errors.Wrap(err, "applying template to data")
}

return message.String(), nil
}

0 comments on commit c617f32

Please sign in to comment.