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

Fix erroneous lookup in github webhook handler. #3403

Merged
merged 1 commit into from
May 22, 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
8 changes: 1 addition & 7 deletions internal/controlplane/handlers_githubwebhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -712,13 +712,7 @@ func extractArtifactVersionFromPayload(ctx context.Context, payload map[string]a
if err != nil {
return nil, err
}
// Previous lookup was done at this path
// ".package.package_version.container_metadata.tag.name",
// which is unfortunately not have available under go-github,
// as field "container_metadata" is missing form its structs.
// I suggest using the following, although it's still to be
// verified whether that's the right one.
tag, err := util.JQReadFrom[string](ctx, ".package.package_version.tag_name", payload)
tag, err := util.JQReadFrom[string](ctx, ".package.package_version.container_metadata.tag.name", payload)
if err != nil {
return nil, err
}
Expand Down
79 changes: 69 additions & 10 deletions internal/controlplane/handlers_githubwebhooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"crypto/hmac"
"crypto/sha256"
"database/sql"
_ "embed"
"encoding/hex"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -51,6 +52,9 @@ import (
"github.com/stacklok/minder/internal/util/testqueue"
)

//go:embed package-published.json
var rawPackageEventPublished string

// MockClient is a mock implementation of the GitHub client.
type MockClient struct {
mock.Mock
Expand Down Expand Up @@ -453,6 +457,7 @@ func (s *UnitTestSuite) TestHandlGitHubWebHook() {
name string
event string
payload any
rawPayload []byte
statusCode int
queued bool
}{
Expand Down Expand Up @@ -507,16 +512,21 @@ func (s *UnitTestSuite) TestHandlGitHubWebHook() {
// https://docs.github.com/en/webhooks/webhook-events-and-payloads#package
event: "package",
// https://pkg.go.dev/github.com/google/go-github/[email protected]/github#PackageEvent
payload: &github.PackageEvent{
payload: &packageEvent{
Action: github.String("published"),
Package: &github.Package{
Package: &pkg{
Name: github.String("package-name"),
PackageType: github.String("package-type"),
// .package.package_version.container_metadata.tag.name
PackageVersion: &github.PackageVersion{
PackageVersion: &packageVersion{
ID: github.Int64(1),
Version: github.String("version"),
TagName: github.String("tagname"),
ContainerMedatata: &containerMetadata{
Tag: &tag{
Digest: github.String("digest"),
Name: github.String("tag"),
},
},
},
Owner: &github.User{
Login: github.String("login"),
Expand All @@ -535,20 +545,35 @@ func (s *UnitTestSuite) TestHandlGitHubWebHook() {
statusCode: http.StatusOK,
queued: true,
},
{
name: "package published raw payload",
// https://docs.github.com/en/webhooks/webhook-events-and-payloads#package
event: "package",
// https://pkg.go.dev/github.com/google/go-github/[email protected]/github#PackageEvent
rawPayload: []byte(rawPackageEventPublished),
statusCode: http.StatusOK,
queued: true,
},
{
name: "package updated",
// https://docs.github.com/en/webhooks/webhook-events-and-payloads#package
event: "package",
// https://pkg.go.dev/github.com/google/go-github/[email protected]/github#PackageEvent
payload: &github.PackageEvent{
payload: &packageEvent{
Action: github.String("updated"),
Package: &github.Package{
Package: &pkg{
Name: github.String("package-name"),
PackageType: github.String("package-type"),
PackageVersion: &github.PackageVersion{
// .package.package_version.container_metadata.tag.name
PackageVersion: &packageVersion{
ID: github.Int64(1),
Version: github.String("version"),
TagName: github.String("tagname"),
ContainerMedatata: &containerMetadata{
Tag: &tag{
Digest: github.String("digest"),
Name: github.String("tag"),
},
},
},
Owner: &github.User{
Login: github.String("login"),
Expand Down Expand Up @@ -1636,8 +1661,13 @@ func (s *UnitTestSuite) TestHandlGitHubWebHook() {
ts := httptest.NewServer(http.HandlerFunc(srv.HandleGitHubWebHook()))
defer ts.Close()

packageJson, err := json.Marshal(tt.payload)
require.NoError(t, err, "failed to marshal package event")
var packageJson []byte
if tt.payload != nil {
packageJson, err = json.Marshal(tt.payload)
require.NoError(t, err, "failed to marshal package event")
} else {
packageJson = tt.rawPayload
}

mac := hmac.New(sha256.New, []byte("test"))
mac.Write(packageJson)
Expand Down Expand Up @@ -1955,6 +1985,35 @@ type garbage struct {
Garbage *string `json:"garbage,omitempty"`
}

type packageEvent struct {
Action *string `json:"action,omitempty"`
Repo *github.Repository `json:"repository,omitempty"`
Org *github.Organization `json:"org,omitempty"`
Package *pkg `json:"package,omitempty"`
}

type pkg struct {
Name *string `json:"name,omitempty"`
PackageType *string `json:"package_type,omitempty"`
PackageVersion *packageVersion `json:"package_version,omitempty"`
Owner *github.User `json:"owner,omitempty"`
}

type packageVersion struct {
ID *int64 `json:"id,omitempty"`
Version *string `json:"version,omitempty"`
ContainerMedatata *containerMetadata `json:"container_metadata,omitempty"`
}

type containerMetadata struct {
Tag *tag `json:"tag,omitempty"`
}

type tag struct {
Digest *string `json:"digest,omitempty"`
Name *string `json:"name,omitempty"`
}

type branchProtectionConfigurationEvent struct {
Action *string `json:"action,omitempty"`
Repo *github.Repository `json:"repo,omitempty"`
Expand Down
Loading