Skip to content

Commit

Permalink
Github provider: Add support for checks API (#3352)
Browse files Browse the repository at this point in the history
* Github provider: Add support for checks API

This commit adds support for the checks API in the github provider.

Signed-off-by: Adolfo García Veytia (puerco) <puerco@stacklok.com>

* Regenerate mocks

---------

Signed-off-by: Adolfo García Veytia (puerco) <puerco@stacklok.com>
  • Loading branch information
puerco committed May 17, 2024
1 parent d736e5f commit ad49d69
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
39 changes: 39 additions & 0 deletions internal/providers/github/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ var (
ErrBranchNotFound = errors.New("branch not found")
// ErrNoPackageListingClient denotes if there is no package listing client available
ErrNoPackageListingClient = errors.New("no package listing client available")
// ErroNoCheckPermissions is a fixed error returned when the credentialed
// identity has not been authorized to use the checks API
ErroNoCheckPermissions = errors.New("missing permissions: check")
)

// GitHub is the struct that contains the shared GitHub client operations
Expand Down Expand Up @@ -976,3 +979,39 @@ func NewFallbackTokenClient(appConfig config.ProviderConfig) *github.Client {
packageListingClient = github.NewClient(fallbackTokenTC)
return packageListingClient
}

// StartCheckRun calls the GitHub API to initialize a new check using the
// supplied options.
func (c *GitHub) StartCheckRun(
ctx context.Context, owner, repo string, opts *github.CreateCheckRunOptions,
) (*github.CheckRun, error) {
if opts.StartedAt == nil {
opts.StartedAt = &github.Timestamp{Time: time.Now()}
}

run, resp, err := c.client.Checks.CreateCheckRun(ctx, owner, repo, *opts)
if err != nil {
// If error is 403 then it means we are missing permissions
if resp.StatusCode == 403 {
return nil, fmt.Errorf("missing permissions: check")
}
return nil, ErroNoCheckPermissions
}
return run, nil
}

// UpdateCheckRun updates an existing check run in GitHub. The check run is referenced
// using its run ID. This function returns the updated CheckRun srtuct.
func (c *GitHub) UpdateCheckRun(
ctx context.Context, owner, repo string, checkRunID int64, opts *github.UpdateCheckRunOptions,
) (*github.CheckRun, error) {
run, resp, err := c.client.Checks.UpdateCheckRun(ctx, owner, repo, checkRunID, *opts)
if err != nil {
// If error is 403 then it means we are missing permissions
if resp.StatusCode == 403 {
return nil, ErroNoCheckPermissions
}
return nil, fmt.Errorf("updating check: %w", err)
}
return run, nil
}
30 changes: 30 additions & 0 deletions internal/providers/github/mock/github.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pkg/providers/v1/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ type GitHub interface {
) ([]*github.IssueComment, error)
UpdateIssueComment(ctx context.Context, owner, repo string, number int64, comment string) error
AddAuthToPushOptions(ctx context.Context, options *git.PushOptions) error
StartCheckRun(context.Context, string, string, *github.CreateCheckRunOptions) (*github.CheckRun, error)
UpdateCheckRun(context.Context, string, string, int64, *github.UpdateCheckRunOptions) (*github.CheckRun, error)
}

// ImageLister is the interface for listing images
Expand Down

0 comments on commit ad49d69

Please sign in to comment.