Skip to content

Commit

Permalink
feat: add gitlab service
Browse files Browse the repository at this point in the history
Signed-off-by: GitHub <[email protected]>
  • Loading branch information
aslafy-z authored Nov 3, 2023
1 parent a8d185e commit 38b9052
Show file tree
Hide file tree
Showing 9 changed files with 640 additions and 38 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ require (
github.com/spf13/cobra v1.6.1
github.com/stretchr/testify v1.8.4
github.com/whilp/git-urls v0.0.0-20191001220047-6db9661140c0
github.com/xanzy/go-gitlab v0.93.2
golang.org/x/time v0.3.0
gomodules.xyz/notify v0.1.1
google.golang.org/api v0.132.0
Expand Down Expand Up @@ -113,7 +114,7 @@ require (
github.com/aws/aws-sdk-go-v2 v1.17.3
github.com/aws/aws-sdk-go-v2/config v1.18.8
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-retryablehttp v0.5.3
github.com/hashicorp/go-retryablehttp v0.7.2
)

replace github.com/prometheus/client_golang => github.com/prometheus/client_golang v1.14.0
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,11 @@ github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFb
github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-hclog v0.9.2 h1:CG6TE5H9/JXsFWJCfoIVpKFIkFe6ysEuHirp4DxCsHI=
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
github.com/hashicorp/go-retryablehttp v0.5.1/go.mod h1:9B5zBasrRhHXnJnui7y6sL7es7NDiJgTc6Er0maI1Xs=
github.com/hashicorp/go-retryablehttp v0.5.3 h1:QlWt0KvWT0lq8MFppF9tsJGF+ynG7ztc2KIPhzRGk7s=
github.com/hashicorp/go-retryablehttp v0.5.3/go.mod h1:9B5zBasrRhHXnJnui7y6sL7es7NDiJgTc6Er0maI1Xs=
github.com/hashicorp/go-retryablehttp v0.7.2 h1:AcYqCvkpalPnPF2pn0KamgwamS42TqUDDYFRKq/RAd0=
github.com/hashicorp/go-retryablehttp v0.7.2/go.mod h1:Jy/gPYAdjqffZ/yFGCFV2doI5wjtH1ewM9u8iYVjtX8=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/howeyc/gopass v0.0.0-20170109162249-bf9dde6d0d2c/go.mod h1:lADxMC39cJJqL93Duh1xhAs4I2Zs8mKS89XWXFGp9cs=
Expand Down Expand Up @@ -439,6 +441,8 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/whilp/git-urls v0.0.0-20191001220047-6db9661140c0 h1:qqllXPzXh+So+mmANlX/gCJrgo+1kQyshMoQ+NASzm0=
github.com/whilp/git-urls v0.0.0-20191001220047-6db9661140c0/go.mod h1:2rx5KE5FLD0HRfkkpyn8JwbVLBdhgeiOb2D2D9LLKM4=
github.com/xanzy/go-gitlab v0.93.2 h1:kNNf3BYNYn/Zkig0B89fma12l36VLcYSGu7OnaRlRDg=
github.com/xanzy/go-gitlab v0.93.2/go.mod h1:5ryv+MnpZStBH8I/77HuQBsMbBGANtVpLWC15qOjWAw=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
37 changes: 4 additions & 33 deletions pkg/services/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,15 @@ import (
"context"
"fmt"
"net/http"
"regexp"
"strings"
texttemplate "text/template"
"unicode/utf8"

"github.com/bradleyfalzon/ghinstallation/v2"
"github.com/google/go-github/v41/github"
log "github.com/sirupsen/logrus"
"github.com/spf13/cast"
giturls "github.com/whilp/git-urls"

httputil "github.com/argoproj/notifications-engine/pkg/util/http"
"github.com/argoproj/notifications-engine/pkg/util/text"
)

var (
gitSuffix = regexp.MustCompile(`\.git$`)
)

type GitHubOptions struct {
Expand Down Expand Up @@ -61,16 +53,16 @@ type GitHubPullRequestComment struct {
}

const (
repoURLtemplate = "{{.app.spec.source.repoURL}}"
revisionTemplate = "{{.app.status.operationState.syncResult.revision}}"
githubRepoURLtemplate = "{{.app.spec.source.repoURL}}"
githubRevisionTemplate = "{{.app.status.operationState.syncResult.revision}}"
)

func (g *GitHubNotification) GetTemplater(name string, f texttemplate.FuncMap) (Templater, error) {
if g.RepoURLPath == "" {
g.RepoURLPath = repoURLtemplate
g.RepoURLPath = githubRepoURLtemplate
}
if g.RevisionPath == "" {
g.RevisionPath = revisionTemplate
g.RevisionPath = githubRevisionTemplate
}

repoURL, err := texttemplate.New(name).Funcs(f).Parse(g.RepoURLPath)
Expand Down Expand Up @@ -276,27 +268,6 @@ type gitHubService struct {
client *github.Client
}

func trunc(message string, n int) string {
if utf8.RuneCountInString(message) > n {
return string([]rune(message)[0:n-3]) + "..."
}
return message
}

func fullNameByRepoURL(rawURL string) string {
parsed, err := giturls.Parse(rawURL)
if err != nil {
panic(err)
}

path := gitSuffix.ReplaceAllString(parsed.Path, "")
if pathParts := text.SplitRemoveEmpty(path, "/"); len(pathParts) >= 2 {
return strings.Join(pathParts[:2], "/")
}

return path
}

func (g gitHubService) Send(notification Notification, _ Destination) error {
if notification.GitHub == nil {
return fmt.Errorf("config is empty")
Expand Down
2 changes: 1 addition & 1 deletion pkg/services/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func TestNewGitHubService_GitHubOptions(t *testing.T) {
}
}

func TestGetTemplater_Github_PullRequestComment(t *testing.T) {
func TestGetTemplater_GitHub_PullRequestComment(t *testing.T) {
n := Notification{
GitHub: &GitHubNotification{
RepoURLPath: "{{.sync.spec.git.repo}}",
Expand Down
Loading

0 comments on commit 38b9052

Please sign in to comment.