From a6fe0b2b815c8c9155f4c0b853178559e2f54b55 Mon Sep 17 00:00:00 2001 From: Peter Boothe Date: Thu, 29 Nov 2018 10:22:39 -0500 Subject: [PATCH 1/2] Allows users to change the default label Some people are restricted by their orgs to need something other than alert:boom: --- cmd/github_receiver/main.go | 3 ++- issues/issues.go | 15 +++++++++------ issues/issues_test.go | 6 +++--- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/cmd/github_receiver/main.go b/cmd/github_receiver/main.go index 7f61469..4f73ee9 100644 --- a/cmd/github_receiver/main.go +++ b/cmd/github_receiver/main.go @@ -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.") ) @@ -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) } diff --git a/issues/issues.go b/issues/issues.go index 5e4cdef..0a579ac 100644 --- a/issues/issues.go +++ b/issues/issues.go @@ -84,11 +84,13 @@ type Client struct { GithubClient *github.Client // org is the github user or organization name (e.g. github.com//). 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}, @@ -96,16 +98,17 @@ func NewClient(org, authToken string) *Client { 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] } @@ -113,7 +116,7 @@ func (c *Client) CreateIssue(repo, title, body string, extra []string) (*github. 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. @@ -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) diff --git a/issues/issues_test.go b/issues/issues_test.go index 5e89c6b..845bcfb 100644 --- a/issues/issues_test.go +++ b/issues/issues_test.go @@ -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() @@ -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() @@ -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() From 165b8ae5819bd53eaf70c9830dbba765f5915302 Mon Sep 17 00:00:00 2001 From: Peter Boothe Date: Fri, 30 Nov 2018 11:27:27 -0500 Subject: [PATCH 2/2] Improved comments on the label flag. --- cmd/github_receiver/main.go | 2 +- issues/issues.go | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/cmd/github_receiver/main.go b/cmd/github_receiver/main.go index 4f73ee9..971413b 100644 --- a/cmd/github_receiver/main.go +++ b/cmd/github_receiver/main.go @@ -40,7 +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") + alertLabel = flag.String("alertlabel", "alert:boom:", "The default label applied to all alerts. Also used to search the repo to discover exisitng alerts.") extraLabels = flag.StringArray("label", nil, "Extra labels to add to issues at creation time.") ) diff --git a/issues/issues.go b/issues/issues.go index 0a579ac..8b2302d 100644 --- a/issues/issues.go +++ b/issues/issues.go @@ -84,7 +84,8 @@ type Client struct { GithubClient *github.Client // org is the github user or organization name (e.g. github.com//). org string - // alertLabel is the label applied to all alerts. + // alertLabel is the label applied to all alerts. It is also used as + // the label to search to discover all existing alerts. alertLabel string }