Skip to content

Commit

Permalink
drop the now redundant *github type from the handlers (#324)
Browse files Browse the repository at this point in the history
#270 means
we're forwarding the full event now, which means we can drop the extra,
now redundant *github type from the bot handlers
  • Loading branch information
joshrwolf authored May 2, 2024
1 parent a892381 commit 678fae6
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 19 deletions.
14 changes: 7 additions & 7 deletions modules/github-bots/blocker/blocker.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ import (
func New() sdk.Bot {
name := "blocker"

handler := sdk.PullRequestHandler(func(ctx context.Context, pre github.PullRequestEvent, pr *github.PullRequest) error {
handler := sdk.PullRequestHandler(func(ctx context.Context, pre github.PullRequestEvent) error {
log := clog.FromContext(ctx)

cli := sdk.NewGitHubClient(ctx, *pre.Repo.Owner.Login, *pre.Repo.Name, name)
defer cli.Close(ctx)

owner, repo := *pr.Base.Repo.Owner.Login, *pr.Base.Repo.Name
owner, repo := *pre.Repo.Owner.Login, *pre.Repo.Name

hasBlockingLabel := slices.ContainsFunc(pr.Labels, func(l *github.Label) bool {
hasBlockingLabel := slices.ContainsFunc(pre.PullRequest.Labels, func(l *github.Label) bool {
return strings.HasPrefix(*l.Name, "blocking/")
})

Expand All @@ -35,15 +35,15 @@ func New() sdk.Bot {
conclusion = "failure"
log.Debug("PR %d has blocking label")
}
sha := *pr.Head.SHA
sha := *pre.PullRequest.Head.SHA

// If there are no check runs for the current head SHA, create one with the conclusion.
crs, _, err := cli.Client().Checks.ListCheckRunsForRef(ctx, owner, repo, sha, &github.ListCheckRunsOptions{CheckName: github.String(name)})
if err != nil {
return fmt.Errorf("listing check runs: %w", err)
}
if len(crs.CheckRuns) == 0 {
log.Infof("Creating CheckRun for PR %d sha %s with conclusion %s", *pr.Number, sha, conclusion)
log.Infof("Creating CheckRun for PR %d sha %s with conclusion %s", *pre.PullRequest.Number, sha, conclusion)
if _, _, err := cli.Client().Checks.CreateCheckRun(ctx, owner, repo, github.CreateCheckRunOptions{
Name: name,
HeadSHA: sha,
Expand All @@ -57,12 +57,12 @@ func New() sdk.Bot {

// No change, nothing else to do.
if *crs.CheckRuns[0].Conclusion == conclusion {
log.Debugf("CheckRun for PR %d sha %s already has conclusion %s", *pr.Number, sha, conclusion)
log.Debugf("CheckRun for PR %d sha %s already has conclusion %s", *pre.Number, sha, conclusion)
return nil
}

// If there's already a check run, update its conclusion.
log.Infof("Updating CheckRun for PR %d sha %s with conclusion %s", *pr.Number, sha, conclusion)
log.Infof("Updating CheckRun for PR %d sha %s with conclusion %s", *pre.Number, sha, conclusion)
if _, _, err = cli.Client().Checks.UpdateCheckRun(ctx, owner, repo, *crs.CheckRuns[0].ID, github.UpdateCheckRunOptions{
Name: name,
Status: github.String("completed"),
Expand Down
8 changes: 4 additions & 4 deletions modules/github-bots/dnm/dnm.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ const label = "blocking/dnm"
func New() sdk.Bot {
name := "dnm"

handler := sdk.PullRequestHandler(func(ctx context.Context, pre github.PullRequestEvent, pr *github.PullRequest) error {
handler := sdk.PullRequestHandler(func(ctx context.Context, pre github.PullRequestEvent) error {
cli := sdk.NewGitHubClient(ctx, *pre.Repo.Owner.Login, *pre.Repo.Name, name)
defer cli.Close(ctx)

// If the title contains some variant of "dnm" and the PR doesn't have the label, add it -- this will no-op if it already has it.
for _, dnm := range []string{name, "do not merge", "donotmerge", "do-not-merge"} {
if strings.Contains(strings.ToLower(*pr.Title), dnm) {
return cli.AddLabel(ctx, pr, label)
if strings.Contains(strings.ToLower(*pre.PullRequest.Title), dnm) {
return cli.AddLabel(ctx, pre.PullRequest, label)
}
}

// If it has the label and the title doesn't match, remove the label -- this will no-op if it already doesn't have it.
return cli.RemoveLabel(ctx, pr, label)
return cli.RemoveLabel(ctx, pre.PullRequest, label)
})

return sdk.NewBot(name,
Expand Down
6 changes: 3 additions & 3 deletions modules/github-bots/sdk/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func Serve(b Bot) {
return err
}

if err := h(ctx, wre.Body, wr); err != nil {
if err := h(ctx, wre.Body); err != nil {
logger.Errorf("failed to handle workflow run event: %v", err)
return err
}
Expand All @@ -149,7 +149,7 @@ func Serve(b Bot) {
return err
}

if err := h(ctx, pre.Body, pr); err != nil {
if err := h(ctx, pre.Body); err != nil {
logger.Errorf("failed to handle pull request event: %v", err)
return err
}
Expand All @@ -170,7 +170,7 @@ func Serve(b Bot) {
return err
}

if err := h(ctx, ice.Body, ic); err != nil {
if err := h(ctx, ice.Body); err != nil {
logger.Errorf("failed to handle issue comment event: %v", err)
return err
}
Expand Down
6 changes: 3 additions & 3 deletions modules/github-bots/sdk/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ type EventHandlerFunc interface {
EventType() EventType
}

type PullRequestHandler func(ctx context.Context, pre github.PullRequestEvent, pr *github.PullRequest) error
type PullRequestHandler func(ctx context.Context, pre github.PullRequestEvent) error

func (r PullRequestHandler) EventType() EventType {
return PullRequestEvent
}

type WorkflowRunHandler func(ctx context.Context, wre github.WorkflowRunEvent, wr *github.WorkflowRun) error
type WorkflowRunHandler func(ctx context.Context, wre github.WorkflowRunEvent) error

func (r WorkflowRunHandler) EventType() EventType {
return WorkflowRunEvent
}

type IssueCommentHandler func(ctx context.Context, ice github.IssueCommentEvent, ic *github.IssueComment) error
type IssueCommentHandler func(ctx context.Context, ice github.IssueCommentEvent) error

func (r IssueCommentHandler) EventType() EventType {
return IssueCommentEvent
Expand Down
4 changes: 2 additions & 2 deletions modules/github-bots/time/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (
func New() sdk.Bot {
name := "time"

handler := sdk.PullRequestHandler(func(ctx context.Context, pre github.PullRequestEvent, pr *github.PullRequest) error {
handler := sdk.PullRequestHandler(func(ctx context.Context, pre github.PullRequestEvent) error {
cli := sdk.NewGitHubClient(ctx, *pre.Repo.Owner.Login, *pre.Repo.Name, name)
defer cli.Close(ctx)

return cli.SetComment(ctx, pr, name, fmt.Sprintf("The time is now %s", time.Now().Format(time.RFC3339)))
return cli.SetComment(ctx, pre.PullRequest, name, fmt.Sprintf("The time is now %s", time.Now().Format(time.RFC3339)))
})

return sdk.NewBot(name,
Expand Down

0 comments on commit 678fae6

Please sign in to comment.