From 6efbf07bff69df17d3618dd8fbd5a9c83a3bddae Mon Sep 17 00:00:00 2001 From: ttyS3 Date: Sun, 4 Dec 2022 18:16:47 +0800 Subject: [PATCH] style: make changes according to the review Signed-off-by: ttyS3 --- internal/notifier/gitea.go | 64 +++++++++++++++++++-------------- internal/notifier/gitea_test.go | 32 ++++++++++++++++- 2 files changed, 69 insertions(+), 27 deletions(-) diff --git a/internal/notifier/gitea.go b/internal/notifier/gitea.go index 2e2b60a3d..c64e24ca0 100644 --- a/internal/notifier/gitea.go +++ b/internal/notifier/gitea.go @@ -1,19 +1,36 @@ +/* +Copyright 2022 The Flux authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + package notifier import ( - "code.gitea.io/sdk/gitea" "context" "crypto/tls" "crypto/x509" "errors" "fmt" - eventv1 "github.com/fluxcd/pkg/apis/event/v1beta1" - "github.com/fluxcd/pkg/apis/meta" "net/http" "net/url" "os" - ctrl "sigs.k8s.io/controller-runtime" "strings" + + "code.gitea.io/sdk/gitea" + eventv1 "github.com/fluxcd/pkg/apis/event/v1beta1" + "github.com/fluxcd/pkg/apis/meta" + ctrl "sigs.k8s.io/controller-runtime" ) type Gitea struct { @@ -21,15 +38,15 @@ type Gitea struct { Token string Owner string Repo string - client *gitea.Client - debug bool + Client *gitea.Client + Debug bool } var _ Interface = &Gitea{} -func NewGitea(addr string, token string, certPool *x509.CertPool) (*Gitea, error) { +func NewGitea(addr, token string, certPool *x509.CertPool) (*Gitea, error) { if len(token) == 0 { - return nil, errors.New("github token cannot be empty") + return nil, errors.New("gitea token cannot be empty") } host, id, err := parseGitAddress(addr) @@ -41,8 +58,8 @@ func NewGitea(addr string, token string, certPool *x509.CertPool) (*Gitea, error return nil, err } - comp := strings.Split(id, "/") - if len(comp) != 2 { + idComponents := strings.Split(id, "/") + if len(idComponents) != 2 { return nil, fmt.Errorf("invalid repository id %q", id) } @@ -60,18 +77,13 @@ func NewGitea(addr string, token string, certPool *x509.CertPool) (*Gitea, error client.SetHTTPClient(&http.Client{Transport: tr}) } - debug := false - if os.Getenv("NOTIFIER_GITEA_DEBUG") == "true" { - debug = true - } - return &Gitea{ BaseURL: host, Token: token, - Owner: comp[0], - Repo: comp[1], - client: client, - debug: debug, + Owner: idComponents[0], + Repo: idComponents[1], + Client: client, + Debug: os.Getenv("NOTIFIER_GITEA_DEBUG") == "true", }, err } @@ -103,32 +115,32 @@ func (g *Gitea) Post(ctx context.Context, event eventv1.Event) error { PageSize: 50, }, } - statuses, _, err := g.client.ListStatuses(g.Owner, g.Repo, rev, listStatusesOpts) + statuses, _, err := g.Client.ListStatuses(g.Owner, g.Repo, rev, listStatusesOpts) if err != nil { - return fmt.Errorf("could not list commit statuses: %v", err) + return fmt.Errorf("could not list commit statuses: %w", err) } if duplicateGiteaStatus(statuses, &status) { - if g.debug { + if g.Debug { ctrl.Log.Info("gitea skip posting duplicate status", "owner", g.Owner, "repo", g.Repo, "commit_hash", rev, "status", status) } return nil } - if g.debug { + if g.Debug { ctrl.Log.Info("gitea create commit begin", "base_url", g.BaseURL, "token", g.Token, "event", event, "status", status) } - st, rsp, err := g.client.CreateStatus(g.Owner, g.Repo, rev, status) + st, rsp, err := g.Client.CreateStatus(g.Owner, g.Repo, rev, status) if err != nil { - if g.debug { + if g.Debug { ctrl.Log.Error(err, "gitea create commit failed", "status", status) } return err } - if g.debug { + if g.Debug { ctrl.Log.Info("gitea create commit ok", "response", rsp, "response_status", st) } diff --git a/internal/notifier/gitea_test.go b/internal/notifier/gitea_test.go index a80a1c19c..fa06d67ed 100644 --- a/internal/notifier/gitea_test.go +++ b/internal/notifier/gitea_test.go @@ -1,5 +1,5 @@ /* -Copyright 2020 The Flux authors +Copyright 2022 The Flux authors Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -17,7 +17,12 @@ limitations under the License. package notifier import ( + "context" + eventv1 "github.com/fluxcd/pkg/apis/event/v1beta1" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "testing" + "time" "github.com/stretchr/testify/assert" ) @@ -39,3 +44,28 @@ func TestNewGiteaEmptyToken(t *testing.T) { _, err := NewGitea("https://try.gitea.io/foo/bar", "", nil) assert.NotNil(t, err) } + +func TestGitea_Post(t *testing.T) { + g, err := NewGitea("https://try.gitea.io/foo/bar", "foobar", nil) + assert.Nil(t, err) + + event := eventv1.Event{ + InvolvedObject: corev1.ObjectReference{ + Kind: "Kustomization", + Namespace: "flux-system", + Name: "podinfo-repo", + }, + Severity: "info", + Timestamp: metav1.Time{ + Time: time.Now(), + }, + Metadata: map[string]string{ + eventv1.MetaRevisionKey: "main/1234567890", + }, + Message: "Service/podinfo/podinfo configured", + Reason: "", + } + err = g.Post(context.Background(), event) + assert.NotNil(t, err) + assert.ErrorContains(t, err, "404 Not Found") +}