Skip to content

Commit

Permalink
Prepare for sending to each individual
Browse files Browse the repository at this point in the history
  • Loading branch information
sj14 committed Oct 3, 2019
1 parent 4b17a27 commit 6b7a04e
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 18 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ require (
golang.org/x/oauth2 v0.0.0-20181106182150-f42d05182288
golang.org/x/sync v0.0.0-20190423024810-112230192c58 // indirect
)

go 1.13
6 changes: 2 additions & 4 deletions hoster/github/github.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package github

import (
"text/template"

"github.com/google/go-github/v25/github"
)

Expand All @@ -15,7 +13,7 @@ type reminder struct {
}

// AggregateReminder will generate the reminder message.
func AggregateReminder(token, owner, repo string, reviewers map[string]string, template *template.Template) string {
func AggregateReminder(token, owner, repo string, reviewers map[string]string) (*github.Repository, []reminder) {
var (
reminders []reminder
git = newClient(token)
Expand All @@ -42,7 +40,7 @@ func AggregateReminder(token, owner, repo string, reviewers map[string]string, t
// TODO: reactions/emojis
reminders = append(reminders, reminder{pr, missing, pr.GetComments(), owner, nil})
}
return execTemplate(template, repository, reminders)
return repository, reminders
}

const (
Expand Down
2 changes: 1 addition & 1 deletion hoster/github/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func DefaultTemplate() *template.Template {
}

// Exec the reminder message for the given merge request.
func execTemplate(template *template.Template, repository *github.Repository, reminders []reminder) string {
func ExecTemplate(template *template.Template, repository *github.Repository, reminders []reminder) string {
data := struct {
Repository *github.Repository
Reminders []reminder
Expand Down
26 changes: 19 additions & 7 deletions hoster/gitlab/gitlab.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package gitlab

import (
"text/template"

"github.com/xanzy/go-gitlab"
)

Expand All @@ -15,20 +13,34 @@ type reminder struct {
}

// AggregateReminder will generate the reminder message.
func AggregateReminder(host, token string, repo interface{}, reviewers map[string]string, template *template.Template) string {
func AggregateReminder(host, token string, repo interface{}, reviewers map[string]string) (gitlab.Project, []reminder) {
// setup gitlab client
git := newClient(host, token)

project, reminders := aggregate(git, repo, reviewers)

// prevent from sending the header only
if len(reminders) == 0 {
return ""
}
// if len(reminders) == 0 {
// return ""
// }

return execTemplate(template, project, reminders)
return project, reminders
}

// func AggregateUsersReminder(host, token string, repo interface{}, reviewers map[string]string, template *template.Template) string {
// // setup gitlab client
// git := newClient(host, token)

// project, reminders := aggregate(git, repo, reviewers)

// // prevent from sending the header only
// if len(reminders) == 0 {
// return ""
// }

// return ExecTemplate(template, project, reminders)
// }

// helper functions for easier testability (mocked gitlab client)
func aggregate(git clientWrapper, repo interface{}, reviewers map[string]string) (gitlab.Project, []reminder) {
project := git.loadProject(repo)
Expand Down
4 changes: 2 additions & 2 deletions hoster/gitlab/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ func DefaultTemplate() *template.Template {
return template.Must(template.New("default").Parse(defaultTemplate))
}

// Exec the reminder message for the given merge request.
func execTemplate(template *template.Template, project gitlab.Project, reminders []reminder) string {
// ExecTemplate execs the reminder message for the given merge requests.
func ExecTemplate(template *template.Template, project gitlab.Project, reminders []reminder) string {
data := struct {
Project gitlab.Project
Reminders []reminder
Expand Down
19 changes: 15 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func main() {
reviewersPath = flag.String("reviewers", "examples/reviewers.json", "path to the reviewers file")
templatePath = flag.String("template", "", "path to the template file")
webhook = flag.String("webhook", "", "slack/mattermost webhook URL")
channel = flag.String("channel", "", "mattermost channel (e.g. MyChannel) or user (e.g. @AnyUser)")
channelOrUser = flag.String("channel", "", "mattermost channel (e.g. MyChannel) or user (e.g. @AnyUser)")
)
flag.Parse()

Expand Down Expand Up @@ -50,9 +50,20 @@ func main() {
if len(ownerRespo) != 2 {
log.Fatalln("wrong repo format (use 'owner/repo')")
}
reminder = github.AggregateReminder(*token, ownerRespo[0], ownerRespo[1], reviewers, tmpl)
repo, reminders := github.AggregateReminder(*token, ownerRespo[0], ownerRespo[1], reviewers)
if len(reminders) == 0 {
// prevent from sending the header only
return
}
reminder = github.ExecTemplate(tmpl, repo, reminders)

} else {
reminder = gitlab.AggregateReminder(*host, *token, *repo, reviewers, tmpl)
project, reminders := gitlab.AggregateReminder(*host, *token, *repo, reviewers)
if len(reminders) == 0 {
// prevent from sending the header only
return
}
reminder = gitlab.ExecTemplate(tmpl, project, reminders)
}

if reminder == "" {
Expand All @@ -62,7 +73,7 @@ func main() {
fmt.Println(reminder)

if *webhook != "" {
if err := slackermost.Send(*channel, reminder, *webhook); err != nil {
if err := slackermost.Send(*channelOrUser, reminder, *webhook); err != nil {
log.Fatalf("failed sending slackermost message: %v", err)
}
}
Expand Down

0 comments on commit 6b7a04e

Please sign in to comment.