Skip to content

Commit

Permalink
Allows users to change the default label
Browse files Browse the repository at this point in the history
Some people are restricted by their orgs to need something other than
alert:boom:
  • Loading branch information
pboothe committed Nov 29, 2018
1 parent 2d57a94 commit a6fe0b2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
3 changes: 2 additions & 1 deletion cmd/github_receiver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var (
enableAutoClose = flag.Bool("enable-auto-close", false, "Once an alert stops firing, automatically close open issues.")
enableInMemory = flag.Bool("enable-inmemory", false, "Perform all operations in memory, without using github API.")
receiverPort = flag.String("port", "9393", "The port for accepting alertmanager webhook messages.")
alertLabel = flag.String("alertlabel", "alert:boom:", "The default label applied to all alerts")
extraLabels = flag.StringArray("label", nil, "Extra labels to add to issues at creation time.")
)

Expand Down Expand Up @@ -106,7 +107,7 @@ func main() {
if *enableInMemory {
client = local.NewClient()
} else {
client = issues.NewClient(*githubOrg, token)
client = issues.NewClient(*githubOrg, token, *alertLabel)
}
serveReceiverHandler(client)
}
15 changes: 9 additions & 6 deletions issues/issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,36 +84,39 @@ type Client struct {
GithubClient *github.Client
// org is the github user or organization name (e.g. github.com/<org>/<repo>).
org string
// alertLabel is the label applied to all alerts.
alertLabel string
}

// NewClient creates an Client authenticated using the Github authToken.
// Future operations are only performed on the given github "org/repo".
func NewClient(org, authToken string) *Client {
func NewClient(org, authToken, alertLabel string) *Client {
ctx := context.Background()
tokenSource := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: authToken},
)
client := &Client{
GithubClient: github.NewClient(oauth2.NewClient(ctx, tokenSource)),
org: org,
alertLabel: alertLabel,
}
return client
}

// CreateIssue creates a new Github issue. New issues are unassigned. Issues are
// labeled with with an alert named "alert:boom:". Labels are created automatically
// labeled with with an alert named alertLabel. Labels are created automatically
// if they do not already exist in a repo.
func (c *Client) CreateIssue(repo, title, body string, extra []string) (*github.Issue, error) {
labels := make([]string, len(extra)+1)
labels[0] = "alert:boom:"
labels[0] = c.alertLabel
for i := range extra {
labels[i+1] = extra[i]
}
// Construct a minimal github issue request.
issueReq := github.IssueRequest{
Title: &title,
Body: &body,
Labels: &labels, // Search using: label:"alert:boom:"
Labels: &labels, // Search using: label:alertLabel
}

// Enforce a timeout on the issue creation.
Expand Down Expand Up @@ -151,9 +154,9 @@ func (c *Client) ListOpenIssues() ([]*github.Issue, error) {
// number of "closed" issues. By only listing "open" issues we limit the
// number of issues returned.
//
// The search depends on all relevant issues including the "alert:boom:" label.
// The search depends on all relevant issues including the alertLabel label.
issues, resp, err := c.GithubClient.Search.Issues(
ctx, `is:issue in:title is:open org:`+c.org+` label:"alert:boom:"`, sopts)
ctx, `is:issue in:title is:open org:`+c.org+` label:"`+c.alertLabel+`"`, sopts)
updateRateMetrics("search", resp, err)
if err != nil {
log.Printf("Failed to list open github issues: %v\n", err)
Expand Down
6 changes: 3 additions & 3 deletions issues/issues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func teardownServer() {
}

func TestCreateIssue(t *testing.T) {
client := issues.NewClient("fake-owner", "FAKE-AUTH-TOKEN")
client := issues.NewClient("fake-owner", "FAKE-AUTH-TOKEN", "alert:boom:")
client.GithubClient.BaseURL = setupServer()
defer teardownServer()

Expand Down Expand Up @@ -98,7 +98,7 @@ func TestCreateIssue(t *testing.T) {
}

func TestListOpenIssues(t *testing.T) {
client := issues.NewClient("owner", "FAKE-AUTH-TOKEN")
client := issues.NewClient("owner", "FAKE-AUTH-TOKEN", "alert:boom:")
// Override public github API with local server.
client.GithubClient.BaseURL = setupServer()
defer teardownServer()
Expand All @@ -125,7 +125,7 @@ func TestListOpenIssues(t *testing.T) {
}

func TestCloseIssue(t *testing.T) {
client := issues.NewClient("owner", "FAKE-AUTH-TOKEN")
client := issues.NewClient("owner", "FAKE-AUTH-TOKEN", "alert:boom:")
client.GithubClient.BaseURL = setupServer()
defer teardownServer()

Expand Down

0 comments on commit a6fe0b2

Please sign in to comment.