Skip to content

Commit

Permalink
style: make changes according to the review
Browse files Browse the repository at this point in the history
Signed-off-by: ttyS3 <ttys3.rust@gmail.com>
  • Loading branch information
ttys3 committed Dec 4, 2022
1 parent 2f1df5c commit 6efbf07
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 27 deletions.
64 changes: 38 additions & 26 deletions internal/notifier/gitea.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,52 @@
/*
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 {
BaseURL string
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)
Expand All @@ -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)
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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)
}

Expand Down
32 changes: 31 additions & 1 deletion internal/notifier/gitea_test.go
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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"
)
Expand All @@ -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")
}

0 comments on commit 6efbf07

Please sign in to comment.