diff --git a/go.mod b/go.mod index 83dcb3a31..9bc48ee86 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ replace github.com/fluxcd/notification-controller/api => ./api require ( cloud.google.com/go/pubsub/v2 v2.8.0 - code.gitea.io/sdk/gitea v0.25.1 + gitea.dev/sdk v1.5.1 github.com/AdaLogics/go-fuzz-headers v0.0.0-20240806141605-e8a1dd7889d6 github.com/Azure/azure-sdk-for-go/sdk/azcore v1.23.3 github.com/Azure/azure-sdk-for-go/sdk/messaging/azeventhubs/v2 v2.0.2 @@ -72,7 +72,7 @@ require ( cloud.google.com/go/compute/metadata v0.10.0 // indirect cloud.google.com/go/iam v1.12.0 // indirect dario.cat/mergo v1.0.0 // indirect - github.com/42wim/httpsig v1.2.4 // indirect + github.com/42wim/httpsig v1.3.0 // indirect github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.15.0 // indirect github.com/Azure/azure-sdk-for-go/sdk/containers/azcontainerregistry v0.2.3 // indirect github.com/Azure/azure-sdk-for-go/sdk/internal v1.13.0 // indirect @@ -108,7 +108,6 @@ require ( github.com/fxamacker/cbor/v2 v2.9.3 // indirect github.com/gabriel-vasile/mimetype v1.4.5 // indirect github.com/go-errors/errors v1.5.1 // indirect - github.com/go-fed/httpsig v1.1.0 // indirect github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect github.com/go-git/go-billy/v5 v5.9.2 // indirect github.com/go-git/go-git/v5 v5.19.3 // indirect diff --git a/go.sum b/go.sum index 756d4f119..c626d27a5 100644 --- a/go.sum +++ b/go.sum @@ -13,12 +13,12 @@ cloud.google.com/go/iam v1.12.0 h1:Aki3bX9aHUDKPHfnRJfDcTdVedvy6quGBQcTqx3DRXk= cloud.google.com/go/iam v1.12.0/go.mod h1:FEZ4lXpADAC2AIpQY7LANNjjwyQ2jK439CI2VaD+sLY= cloud.google.com/go/pubsub/v2 v2.8.0 h1:mDGFPDisDUcv0fEkiYQX+BRbenlzdnV6BdH2eb9YQ/M= cloud.google.com/go/pubsub/v2 v2.8.0/go.mod h1:OUQLW4ZkmJ0lVvXpP+2Kk0acWWO2pTg5nJ3xaq786ag= -code.gitea.io/sdk/gitea v0.25.1 h1:yywxWwoV+SdjHtbC6unBiXojWdZOtoHuGhEazEXeWuE= -code.gitea.io/sdk/gitea v0.25.1/go.mod h1:uDFWYBU8dgZsgOHwe6C/6olxvf8FHguNB3wW1i83fgg= dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk= dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk= -github.com/42wim/httpsig v1.2.4 h1:mI5bH0nm4xn7K18fo1K3okNDRq8CCJ0KbBYWyA6r8lU= -github.com/42wim/httpsig v1.2.4/go.mod h1:yKsYfSyTBEohkPik224QPFylmzEBtda/kjyIAJjh3ps= +gitea.dev/sdk v1.5.1 h1:bMBU3rPoU87a8OOgtfu5w4sZn2x/ydwNAkqFLfIN8c8= +gitea.dev/sdk v1.5.1/go.mod h1:cM7S3Y9TY3LraJoRHHDLlY3YnVTu4bTc9TEBGuFa2Jw= +github.com/42wim/httpsig v1.3.0 h1:HK2bieeZ99X9IvOoc1ST6yrhLek0f4MqlEGES2JCP3g= +github.com/42wim/httpsig v1.3.0/go.mod h1:lgC3Yrvp4lxi1odEF1ge3kQBu/VM+SRcZQu5YwiR3dU= github.com/AdaLogics/go-fuzz-headers v0.0.0-20240806141605-e8a1dd7889d6 h1:He8afgbRMd7mFxO99hRNu+6tazq8nFF9lIwo9JFroBk= github.com/AdaLogics/go-fuzz-headers v0.0.0-20240806141605-e8a1dd7889d6/go.mod h1:8o94RPi1/7XTJvwPpRSzSUedZrtlirdB3r9Z20bi2f8= github.com/Azure/azure-sdk-for-go/sdk/azcore v1.23.3 h1:qgnWTiWs1G7AqcKZegFvkJD1bmXJcaDU3uC0ELSgZ5U= @@ -180,8 +180,6 @@ github.com/gliderlabs/ssh v0.3.8 h1:a4YXD1V7xMF9g5nTkdfnja3Sxy1PVDCj1Zg4Wb8vY6c= github.com/gliderlabs/ssh v0.3.8/go.mod h1:xYoytBv1sV0aL3CavoDuJIQNURXkkfPA/wxQ1pL1fAU= github.com/go-errors/errors v1.5.1 h1:ZwEMSLRCapFLflTpT7NKaAc7ukJ8ZPEjzlxt8rPN8bk= github.com/go-errors/errors v1.5.1/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og= -github.com/go-fed/httpsig v1.1.0 h1:9M+hb0jkEICD8/cAiNqEB66R87tTINszBRTjwjQzWcI= -github.com/go-fed/httpsig v1.1.0/go.mod h1:RCMrTZvN1bJYtofsG4rd5NaO5obxQ5xBkdiS7xsT7bM= github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 h1:+zs/tPmkDkHx3U66DAb0lQFJrpS6731Oaa12ikc+DiI= github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376/go.mod h1:an3vInlBmSxCcxctByoQdvwPiA7DTK7jaaFDBTtu0ic= github.com/go-git/go-billy/v5 v5.9.2 h1:OXFSRyz4g20upsGDJgQG9Bak1l/ZEv8GHVYB52O71sE= diff --git a/internal/notifier/gitea.go b/internal/notifier/gitea.go index 646ca6167..5cfd057b3 100644 --- a/internal/notifier/gitea.go +++ b/internal/notifier/gitea.go @@ -22,7 +22,7 @@ import ( "fmt" "os" - "code.gitea.io/sdk/gitea" + "gitea.dev/sdk" eventv1 "github.com/fluxcd/pkg/apis/event/v1" "github.com/fluxcd/pkg/apis/meta" ctrl "sigs.k8s.io/controller-runtime" @@ -58,7 +58,6 @@ func NewGitea(ctx context.Context, opts *notifierOptions) (Interface, error) { } func (g *Gitea) Post(ctx context.Context, event eventv1.Event) error { - g.Client.SetContext(ctx) revString, ok := event.GetRevision() if !ok { return errors.New("missing revision metadata") @@ -86,7 +85,7 @@ func (g *Gitea) Post(ctx context.Context, event eventv1.Event) error { Page: 0, PageSize: 50, } - statuses, _, err := g.Client.ListStatuses(g.Owner, g.Repo, rev, listStatusesOpts) + statuses, _, err := g.Client.ListStatuses(ctx, g.Owner, g.Repo, rev, listStatusesOpts) if err != nil { return fmt.Errorf("could not list commit statuses: %w", err) } @@ -103,7 +102,7 @@ func (g *Gitea) Post(ctx context.Context, event eventv1.Event) error { "owner", g.Owner, "repo", g.Repo, "event", event, "status", status) } - st, rsp, err := g.Client.CreateStatus(g.Owner, g.Repo, rev, status) + st, rsp, err := g.Client.CreateStatus(ctx, g.Owner, g.Repo, rev, status) if err != nil { if g.Debug { ctrl.Log.Error(err, "gitea create commit failed", "status", status) diff --git a/internal/notifier/gitea_helpers.go b/internal/notifier/gitea_helpers.go index ccc1b1e98..2c800f2a5 100644 --- a/internal/notifier/gitea_helpers.go +++ b/internal/notifier/gitea_helpers.go @@ -25,7 +25,7 @@ import ( "strings" "time" - "code.gitea.io/sdk/gitea" + "gitea.dev/sdk" ) // GiteaClient holds the Gitea client and repository information. @@ -70,14 +70,14 @@ func NewGiteaClient(ctx context.Context, opts *notifierOptions, fetchUserLogin b tr.IdleConnTimeout = giteaIdleConnTimeout } - client, err := gitea.NewClient(host, gitea.SetContext(ctx), gitea.SetToken(token), gitea.SetHTTPClient(&http.Client{Transport: tr})) + client, err := gitea.NewClient(host, gitea.SetToken(token), gitea.SetHTTPClient(&http.Client{Transport: tr})) if err != nil { return nil, fmt.Errorf("failed creating Gitea client: %w", err) } var username string if fetchUserLogin { - user, _, err := client.GetMyUserInfo() + user, _, err := client.GetMyUserInfo(ctx) if err != nil { return nil, fmt.Errorf("failed to get authenticated user info: %w", err) } diff --git a/internal/notifier/gitea_pull_request_comment.go b/internal/notifier/gitea_pull_request_comment.go index 98203d554..2fae127ab 100644 --- a/internal/notifier/gitea_pull_request_comment.go +++ b/internal/notifier/gitea_pull_request_comment.go @@ -23,7 +23,7 @@ import ( "strconv" "strings" - "code.gitea.io/sdk/gitea" + "gitea.dev/sdk" eventv1 "github.com/fluxcd/pkg/apis/event/v1" ) @@ -62,7 +62,6 @@ func NewGiteaPullRequestComment(ctx context.Context, opts *notifierOptions) (Int // If the comment already exists (based on the comment key), it updates the existing comment // instead of creating a new one. func (g *GiteaPullRequestComment) Post(ctx context.Context, event eventv1.Event) error { - g.Client.SetContext(ctx) body := g.formatCommentBody(&event) prNumber, err := g.getPullRequestNumber(&event) if err != nil { @@ -80,7 +79,7 @@ func (g *GiteaPullRequestComment) Post(ctx context.Context, event eventv1.Event) // Find comment from the authenticated user matching the comment key commentKeyMarker := g.formatCommentKeyMarker(&event) existingComment, found, err := findInPages(g.MaxPages, func() ([]*gitea.Comment, bool, error) { - comments, response, err := g.Client.ListIssueComments(g.Owner, g.Repo, int64(prNumber), opts) + comments, response, err := g.Client.ListIssueComments(ctx, g.Owner, g.Repo, int64(prNumber), opts) if err != nil { return nil, false, fmt.Errorf("failed to list pull request comments: %w", err) } @@ -98,7 +97,7 @@ func (g *GiteaPullRequestComment) Post(ctx context.Context, event eventv1.Event) if found { // Existing comment found, update it - _, _, err = g.Client.EditIssueComment(g.Owner, g.Repo, existingComment.ID, gitea.EditIssueCommentOption{ + _, _, err = g.Client.EditIssueComment(ctx, g.Owner, g.Repo, existingComment.ID, gitea.EditIssueCommentOption{ Body: body, }) if err != nil { @@ -106,7 +105,7 @@ func (g *GiteaPullRequestComment) Post(ctx context.Context, event eventv1.Event) } } else { // No existing comment found, create a new one - _, _, err = g.Client.CreateIssueComment(g.Owner, g.Repo, int64(prNumber), gitea.CreateIssueCommentOption{ + _, _, err = g.Client.CreateIssueComment(ctx, g.Owner, g.Repo, int64(prNumber), gitea.CreateIssueCommentOption{ Body: body, }) if err != nil { diff --git a/internal/notifier/gitea_test.go b/internal/notifier/gitea_test.go index 7210382b3..f459d0458 100644 --- a/internal/notifier/gitea_test.go +++ b/internal/notifier/gitea_test.go @@ -38,7 +38,7 @@ import ( ) func TestGiteaProviderContext(t *testing.T) { - for _, stage := range []string{"version", "user", "list", "create"} { + for _, stage := range []string{"user", "list", "create"} { t.Run(stage, func(t *testing.T) { g := NewWithT(t) cancelled := make(chan struct{}, 1) @@ -51,8 +51,6 @@ func TestGiteaProviderContext(t *testing.T) { current := "list" body := "[]" switch { - case r.URL.Path == "/api/v1/version": - current, body = "version", `{"version":"1.18.3"}` case r.URL.Path == "/api/v1/user": current, body = "user", `{"login":"flux"}` case r.Method == http.MethodPost: @@ -171,14 +169,17 @@ func TestNewGiteaNoCertificate(t *testing.T) { certPool := x509.NewCertPool() tlsConfig := &tls.Config{RootCAs: certPool} - _, err := NewGitea(t.Context(), ¬ifierOptions{ + n, err := NewGitea(t.Context(), ¬ifierOptions{ CommitStatus: "kustomization/gitops-system/0c9c2e41", URL: srv.URL + "/foo/bar", Token: "foobar", TLSConfig: tlsConfig, }) - g.Expect(err).To(HaveOccurred()) - g.Expect(err).To(MatchError(ContainSubstring("tls: failed to verify certificate: x509: certificate signed by unknown authority"))) + g.Expect(err).ToNot(HaveOccurred()) + + event := testEvent() + event.Metadata[eventv1.MetaRevisionKey] = "main@sha1:69b59063470310ebbd88a9156325322a124e55a3" + g.Expect(n.Post(t.Context(), event)).To(MatchError(ContainSubstring("tls: failed to verify certificate: x509: certificate signed by unknown authority"))) } func TestNewGiteaWithProxyURL(t *testing.T) { diff --git a/internal/server/event_handlers_test.go b/internal/server/event_handlers_test.go index 5fe03bebe..d93329135 100644 --- a/internal/server/event_handlers_test.go +++ b/internal/server/event_handlers_test.go @@ -2242,7 +2242,7 @@ func TestGetNotificationParamsRedactsSecretValues(t *testing.T) { } func TestHandleEventConstructsNotifiersAsynchronously(t *testing.T) { - for _, providerType := range []string{apiv1.GiteaProvider, apiv1.GitHubProvider} { + for _, providerType := range []string{apiv1.GiteaPullRequestCommentProvider, apiv1.GitHubProvider} { t.Run(providerType, func(t *testing.T) { g := NewWithT(t) started := make(chan string, 1) @@ -2293,6 +2293,9 @@ func TestHandleEventConstructsNotifiersAsynchronously(t *testing.T) { Timeout: &metav1.Duration{Duration: 2 * time.Second}, }, } + if providerType == apiv1.GiteaPullRequestCommentProvider { + slowProvider.UID = "slow-uid" + } fastProvider := &apiv1.Provider{ Name: "fast", Namespace: "default", Spec: apiv1.ProviderSpec{Type: apiv1.GenericProvider, Address: fast.URL}, @@ -2317,6 +2320,9 @@ func TestHandleEventConstructsNotifiersAsynchronously(t *testing.T) { eventv1.Group + "/" + eventv1.MetaCommitKey: "main@sha1:69b59063470310ebbd88a9156325322a124e55a3", }, } + if providerType == apiv1.GiteaPullRequestCommentProvider { + event.Metadata[eventv1.Group+"/"+eventv1.MetaChangeRequestKey] = "1" + } scheme := runtime.NewScheme() g.Expect(apiv1.AddToScheme(scheme)).To(Succeed()) g.Expect(corev1.AddToScheme(scheme)).To(Succeed()) @@ -2338,7 +2344,7 @@ func TestHandleEventConstructsNotifiersAsynchronously(t *testing.T) { g.Eventually(returned, "5s", "10ms").Should(Receive(Equal(http.StatusAccepted))) cancel() g.Eventually(sent, "5s", "10ms").Should(Receive()) - wantPath := "/api/v1/version" + wantPath := "/api/v1/user" if providerType == apiv1.GitHubProvider { wantPath = "/app/installations/456/access_tokens" }