diff --git a/README.md b/README.md index 125bde8..a5b2d86 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,7 @@ upper case with dashes as underscores. A flag on the command line wins. | `-listen` | `:8080` | listen address | | `-label` | `alert` | label on every issue; open issues are found by it | | `-label-from` | `severity` | comma-separated alert label names copied onto the issue | +| `-assignees` | | comma-separated GitHub logins assigned to each new issue, so GitHub notifies them; each needs access to the repository | | `-bearer-token-file` | | if set, `POST /webhook` requires `Authorization: Bearer ` | | `-api-url` | `https://api.github.com` | for GitHub Enterprise Server, `https://HOST/api/v3` | | `-github-timeout` | `20s` | per GitHub API request | diff --git a/github.go b/github.go index 33ad20b..08a105b 100644 --- a/github.go +++ b/github.go @@ -177,15 +177,18 @@ func (g *GitHub) OpenIssues(ctx context.Context, label string) ([]Issue, error) return all, nil } -func (g *GitHub) CreateIssue(ctx context.Context, title, body string, labels []string) (int, error) { +func (g *GitHub) CreateIssue(ctx context.Context, title, body string, labels, assignees []string) (int, error) { tok, err := g.tokens.Token(ctx) if err != nil { return 0, err } var out Issue u := fmt.Sprintf("%s/repos/%s/%s/issues", g.apiURL, g.owner, g.repo) - _, err = doJSON(ctx, g.client, http.MethodPost, u, "token "+tok, - map[string]any{"title": title, "body": body, "labels": labels}, &out) + in := map[string]any{"title": title, "body": body, "labels": labels} + if len(assignees) > 0 { + in["assignees"] = assignees + } + _, err = doJSON(ctx, g.client, http.MethodPost, u, "token "+tok, in, &out) return out.Number, err } diff --git a/main.go b/main.go index 0f17de0..eeefca0 100644 --- a/main.go +++ b/main.go @@ -41,6 +41,7 @@ func run(args []string) error { apiURL = fs.String("api-url", env("api-url", "https://api.github.com"), "GitHub API base URL") label = fs.String("label", env("label", "alert"), "label put on every issue and used to find open ones") labelKeys = fs.String("label-from", env("label-from", "severity"), "comma-separated alert label names copied onto the issue as name:value labels") + assignees = fs.String("assignees", env("assignees", ""), "comma-separated GitHub logins assigned to each new issue") appID = fs.String("app-id", env("app-id", ""), "GitHub App ID or client ID") install = fs.String("installation-id", env("installation-id", ""), "GitHub App installation ID") keyFile = fs.String("private-key-file", env("private-key-file", ""), "GitHub App private key (PEM)") @@ -93,6 +94,11 @@ func run(args []string) error { r.labelFromKeys = append(r.labelFromKeys, k) } } + for _, a := range strings.Split(*assignees, ",") { + if a = strings.TrimSpace(a); a != "" { + r.assignees = append(r.assignees, a) + } + } if *bearerFile != "" { r.bearer = func() (string, error) { return fileToken{path: *bearerFile}.Token(context.Background()) } } diff --git a/receiver.go b/receiver.go index 034bb06..fa5c94a 100644 --- a/receiver.go +++ b/receiver.go @@ -17,7 +17,7 @@ import ( // Issues is what the receiver needs from GitHub. type Issues interface { OpenIssues(ctx context.Context, label string) ([]Issue, error) - CreateIssue(ctx context.Context, title, body string, labels []string) (int, error) + CreateIssue(ctx context.Context, title, body string, labels, assignees []string) (int, error) UpdateIssue(ctx context.Context, number int, fields map[string]any) error Comment(ctx context.Context, number int, text string) error } @@ -29,6 +29,7 @@ type Receiver struct { gh Issues label string labelFromKeys []string // alert label names copied onto the issue as "name:value" labels + assignees []string // GitHub logins assigned to each new issue, so they are notified bearer func() (string, error) log *slog.Logger now func() time.Time @@ -97,7 +98,7 @@ func (r *Receiver) Handle(ctx context.Context, m *Message) error { case "firing": b := body(m, id) if existing == nil { - n, err := r.gh.CreateIssue(ctx, title(m), b, r.labels(m)) + n, err := r.gh.CreateIssue(ctx, title(m), b, r.labels(m), r.assignees) if err != nil { return err } diff --git a/receiver_test.go b/receiver_test.go index 796cf9e..10a40de 100644 --- a/receiver_test.go +++ b/receiver_test.go @@ -28,12 +28,15 @@ type fakeGitHub struct { } type fakeIssue struct { - Number int `json:"number"` - Title string `json:"title"` - Body string `json:"body"` - State string `json:"state"` - Reason string `json:"state_reason"` - Labels []string `json:"-"` + Number int `json:"number"` + Title string `json:"title"` + Body string `json:"body"` + State string `json:"state"` + Reason string `json:"state_reason"` + Labels []string `json:"-"` + Assignees []string `json:"-"` + // HasAssignees records whether the create request carried the key at all. + HasAssignees bool `json:"-"` } func newFake() *fakeGitHub { @@ -85,12 +88,16 @@ func (f *fakeGitHub) ServeHTTP(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(open[start:end]) case len(parts) == 4 && r.Method == http.MethodPost: var in struct { - Title string `json:"title"` - Body string `json:"body"` - Labels []string `json:"labels"` + Title string `json:"title"` + Body string `json:"body"` + Labels []string `json:"labels"` + Assignees *[]string `json:"assignees"` } json.NewDecoder(r.Body).Decode(&in) is := &fakeIssue{Number: len(f.issues) + 1, Title: in.Title, Body: in.Body, State: "open", Labels: in.Labels} + if in.Assignees != nil { + is.Assignees, is.HasAssignees = *in.Assignees, true + } f.issues = append(f.issues, is) w.WriteHeader(http.StatusCreated) json.NewEncoder(w).Encode(is) @@ -397,3 +404,18 @@ func TestBacktickFloodStaysBounded(t *testing.T) { t.Fatal("the alert itself was crowded out") } } + +func TestAssigneesOnNewIssue(t *testing.T) { + f, _, r := setup(t) + post(t, r, msg("firing", alert("firing", "db-0"))) + if f.issues[0].HasAssignees { + t.Errorf("no -assignees, but the create sent assignees %v", f.issues[0].Assignees) + } + + f, _, r = setup(t) + r.assignees = []string{"octocat", "hubot"} + post(t, r, msg("firing", alert("firing", "db-0"))) + if got := f.issues[0].Assignees; len(got) != 2 || got[0] != "octocat" || got[1] != "hubot" { + t.Errorf("assignees = %v", got) + } +}