Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(github-bots): Add support for projects_v2_item events #571

Merged
merged 1 commit into from
Sep 26, 2024
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
15 changes: 15 additions & 0 deletions modules/github-bots/sdk/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,21 @@ func Serve(b Bot) {
}
return nil

case ProjectsV2ItemHandler:
log.Debug("handling projects_v2_item event")

var pie schemas.Wrapper[ProjectsV2ItemEvent]
if err := event.DataAs(&pie); err != nil {
log.Errorf("failed to unmarshal projects_v2_item event: %v", err)
return err
}

if err := h(ctx, pie.Body); err != nil {
log.Errorf("failed to handle projects_v2_item event: %v", err)
return err
}
return nil

default:
return fmt.Errorf("unknown handler type %T", handler)
}
Expand Down
44 changes: 38 additions & 6 deletions modules/github-bots/sdk/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sdk

import (
"context"
"encoding/json"

"github.com/google/go-github/v61/github"
)
Expand Down Expand Up @@ -58,16 +59,47 @@ func (r CheckSuiteHandler) EventType() EventType {
return CheckSuiteEvent
}

type ProjectsV2ItemHandler func(ctx context.Context, pie ProjectsV2ItemEvent) error

func (r ProjectsV2ItemHandler) EventType() EventType {
return ProjectsV2ItemEventType
}

// https://github.com/google/go-github/blob/v60.0.0/github/event_types.go#L1062
//
// ProjectsV2ItemEvent represents a project_v2_item event. It's copied from go-github since
// their version only supports the `archived` action.
type ProjectsV2ItemEvent struct {
Action string `json:"action,omitempty"`
Changes json.RawMessage `json:"changes,omitempty"`
ProjectV2Item *ProjectV2Item `json:"projects_v2_item,omitempty"`
Organization *github.Organization `json:"organization,omitempty"`
Sender *github.User `json:"sender,omitempty"`
}

// https://github.com/google/go-github/blob/v60.0.0/github/event_types.go#L1085
type ProjectV2Item struct {
ID int64 `json:"id,omitempty"`
NodeID string `json:"node_id,omitempty"`
ProjectNodeID string `json:"project_node_id,omitempty"`
ContentNodeID string `json:"content_node_id,omitempty"`
ContentType string `json:"content_type,omitempty"`
CreatedAt *github.Timestamp `json:"created_at,omitempty"`
UpdatedAt *github.Timestamp `json:"updated_at,omitempty"`
ArchivedAt *github.Timestamp `json:"archived_at,omitempty"`
}

type EventType string

const (
// GitHub events (https://github.com/chainguard-dev/terraform-infra-common/tree/main/modules/github-events)
PullRequestEvent EventType = "dev.chainguard.github.pull_request"
WorkflowRunEvent EventType = "dev.chainguard.github.workflow_run"
IssueCommentEvent EventType = "dev.chainguard.github.issue_comment"
PushEvent EventType = "dev.chainguard.github.push"
CheckRunEvent EventType = "dev.chainguard.github.check_run"
CheckSuiteEvent EventType = "dev.chainguard.github.check_suite"
PullRequestEvent EventType = "dev.chainguard.github.pull_request"
WorkflowRunEvent EventType = "dev.chainguard.github.workflow_run"
IssueCommentEvent EventType = "dev.chainguard.github.issue_comment"
PushEvent EventType = "dev.chainguard.github.push"
CheckRunEvent EventType = "dev.chainguard.github.check_run"
CheckSuiteEvent EventType = "dev.chainguard.github.check_suite"
ProjectsV2ItemEventType EventType = "dev.chainguard.github.projects_v2_item"

// LoFo events
WorkflowRunArtifactEvent EventType = "dev.chainguard.lofo.workflow_run_artifacts"
Expand Down
Loading