-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: ttyS3 <ttys3.rust@gmail.com>
- Loading branch information
Showing
9 changed files
with
280 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -95,6 +95,7 @@ spec: | |
- generic-hmac | ||
- github | ||
- gitlab | ||
- gitea | ||
- bitbucket | ||
- azuredevops | ||
- googlechat | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
/* | ||
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 ( | ||
"context" | ||
"crypto/tls" | ||
"crypto/x509" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"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 | ||
ProviderUID string | ||
Client *gitea.Client | ||
Debug bool | ||
} | ||
|
||
var _ Interface = &Gitea{} | ||
|
||
func NewGitea(providerUID string, addr string, token string, certPool *x509.CertPool) (*Gitea, error) { | ||
if len(token) == 0 { | ||
return nil, errors.New("gitea token cannot be empty") | ||
} | ||
|
||
host, id, err := parseGitAddress(addr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if _, err := url.Parse(host); err != nil { | ||
return nil, err | ||
} | ||
|
||
idComponents := strings.Split(id, "/") | ||
if len(idComponents) != 2 { | ||
return nil, fmt.Errorf("invalid repository id %q", id) | ||
} | ||
|
||
client, err := gitea.NewClient(host, gitea.SetToken(token)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if certPool != nil { | ||
tr := &http.Transport{ | ||
TLSClientConfig: &tls.Config{ | ||
RootCAs: certPool, | ||
}, | ||
} | ||
client.SetHTTPClient(&http.Client{Transport: tr}) | ||
} | ||
|
||
return &Gitea{ | ||
BaseURL: host, | ||
Token: token, | ||
Owner: idComponents[0], | ||
Repo: idComponents[1], | ||
ProviderUID: providerUID, | ||
Client: client, | ||
Debug: os.Getenv("NOTIFIER_GITEA_DEBUG") == "true", | ||
}, err | ||
} | ||
|
||
func (g *Gitea) Post(ctx context.Context, event eventv1.Event) error { | ||
revString, ok := event.Metadata[eventv1.MetaRevisionKey] | ||
if !ok { | ||
return errors.New("missing revision metadata") | ||
} | ||
rev, err := parseRevision(revString) | ||
if err != nil { | ||
return err | ||
} | ||
state, err := toGiteaState(event) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, desc := formatNameAndDescription(event) | ||
id := generateCommitStatusID(g.ProviderUID, event) | ||
|
||
status := gitea.CreateStatusOption{ | ||
State: state, | ||
TargetURL: "", | ||
Description: desc, | ||
Context: id, | ||
} | ||
|
||
listStatusesOpts := gitea.ListStatusesOption{ | ||
ListOptions: gitea.ListOptions{ | ||
Page: 0, | ||
PageSize: 50, | ||
}, | ||
} | ||
statuses, _, err := g.Client.ListStatuses(g.Owner, g.Repo, rev, listStatusesOpts) | ||
if err != nil { | ||
return fmt.Errorf("could not list commit statuses: %w", err) | ||
} | ||
if duplicateGiteaStatus(statuses, &status) { | ||
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 { | ||
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) | ||
if err != nil { | ||
if g.Debug { | ||
ctrl.Log.Error(err, "gitea create commit failed", "status", status) | ||
} | ||
return err | ||
} | ||
|
||
if g.Debug { | ||
ctrl.Log.Info("gitea create commit ok", "response", rsp, "response_status", st) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func toGiteaState(event eventv1.Event) (gitea.StatusState, error) { | ||
// progressing events | ||
if event.HasReason(meta.ProgressingReason) { | ||
// pending | ||
return gitea.StatusPending, nil | ||
} | ||
switch event.Severity { | ||
case eventv1.EventSeverityInfo: | ||
return gitea.StatusSuccess, nil | ||
case eventv1.EventSeverityError: | ||
return gitea.StatusFailure, nil | ||
default: | ||
return gitea.StatusError, errors.New("can't convert to gitea state") | ||
} | ||
} | ||
|
||
// duplicateGiteaStatus return true if the latest status | ||
// with a matching context has the same state and description | ||
func duplicateGiteaStatus(statuses []*gitea.Status, status *gitea.CreateStatusOption) bool { | ||
if status == nil || statuses == nil { | ||
return false | ||
} | ||
|
||
for _, s := range statuses { | ||
if s.Context == "" || s.State == "" || s.Description == "" { | ||
continue | ||
} | ||
|
||
if s.Context == status.Context { | ||
if s.State == status.State && s.Description == status.Description { | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
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 ( | ||
"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" | ||
) | ||
|
||
func TestNewGiteaBasic(t *testing.T) { | ||
g, err := NewGitea("0c9c2e41-d2f9-4f9b-9c41-bebc1984d67a", "https://try.gitea.io/foo/bar", "foobar", nil) | ||
assert.Nil(t, err) | ||
assert.Equal(t, g.Owner, "foo") | ||
assert.Equal(t, g.Repo, "bar") | ||
assert.Equal(t, g.BaseURL, "https://try.gitea.io") | ||
} | ||
|
||
func TestNewGiteaInvalidUrl(t *testing.T) { | ||
_, err := NewGitea("0c9c2e41-d2f9-4f9b-9c41-bebc1984d67a", "https://try.gitea.io/foo/bar/baz", "foobar", nil) | ||
assert.NotNil(t, err) | ||
} | ||
|
||
func TestNewGiteaEmptyToken(t *testing.T) { | ||
_, err := NewGitea("0c9c2e41-d2f9-4f9b-9c41-bebc1984d67a", "https://try.gitea.io/foo/bar", "", nil) | ||
assert.NotNil(t, err) | ||
} | ||
|
||
func TestGitea_Post(t *testing.T) { | ||
g, err := NewGitea("0c9c2e41-d2f9-4f9b-9c41-bebc1984d67a", "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") | ||
} |