Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <file contents>` |
| `-api-url` | `https://api.github.com` | for GitHub Enterprise Server, `https://HOST/api/v3` |
| `-github-timeout` | `20s` | per GitHub API request |
Expand Down
9 changes: 6 additions & 3 deletions github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
Expand Down Expand Up @@ -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()) }
}
Expand Down
5 changes: 3 additions & 2 deletions receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
40 changes: 31 additions & 9 deletions receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
}