Skip to content

Commit

Permalink
Merge pull request #21 from m-lab/add_alert_flag
Browse files Browse the repository at this point in the history
Allows users to change the default label
  • Loading branch information
pboothe authored Nov 30, 2018
2 parents 2d57a94 + 165b8ae commit 41d73dc
Show file tree
Hide file tree
Showing 3 changed files with 15 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. Also used to search the repo to discover exisitng 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)
}
16 changes: 10 additions & 6 deletions issues/issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,36 +84,40 @@ 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. It is also used as
// the label to search to discover all existing 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 +155,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 41d73dc

Please sign in to comment.