Skip to content

Commit

Permalink
feat(gitea): add gitea SCM source wrapper for PR creation
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeMac committed Jul 4, 2023
1 parent 56404c9 commit 3223c37
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 36 deletions.
37 changes: 32 additions & 5 deletions cmd/cup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@ package main
import (
"context"
"flag"
"fmt"
"net/http"
"net/url"
"os"
"path"
"strings"

"code.gitea.io/sdk/gitea"
"github.com/go-git/go-git/v5/plumbing/transport"
githttp "github.com/go-git/go-git/v5/plumbing/transport/http"
"go.flipt.io/cup"
"go.flipt.io/cup/internal/runtime"
"go.flipt.io/cup/internal/source/git"
giteasrc "go.flipt.io/cup/internal/source/gitea"
"go.flipt.io/cup/internal/source/local"
"golang.org/x/exp/slog"
)
Expand All @@ -20,6 +25,7 @@ var (
sourceType = flag.String("source", "local", "source type (local|git)")
gitRepo = flag.String("repository", "", "target upstream repository")
authBasic = flag.String("auth-basic", "", "basic authentication in the form username:password")
scmType = flag.String("scm-type", "", "SCM type (one of [gitea])")
)

func main() {
Expand All @@ -43,9 +49,13 @@ func main() {
logger.Error("Parsing Git URL", slog.String("url", *gitRepo), "error", err)
}

var auth transport.AuthMethod
if url.User.Username() != "" {
password, _ := url.User.Password()
var (
user = url.User.Username()
password string
auth transport.AuthMethod
)
if user != "" {
password, _ = url.User.Password()
auth = &githttp.BasicAuth{
Username: url.User.Username(),
Password: password,
Expand All @@ -56,14 +66,31 @@ func main() {
// via the transport auth
url.User = nil

source, err = git.NewSource(ctx, url.String(), git.WithAuth(auth))
gitsource, err := git.NewSource(ctx, url.String(), git.WithAuth(auth))
if err != nil {
logger.Error("Building Git Source", "error", err)
os.Exit(1)
}

source = gitsource

if *scmType == "gitea" {
repository := strings.TrimSuffix(path.Base(url.Path), ".git")

cli, err := gitea.NewClient(fmt.Sprintf("%s://%s", url.Scheme, url.Host), gitea.SetBasicAuth(user, password))
if err != nil {
logger.Error("Configuring Gitea Client", "error", err)
os.Exit(1)
}

source, err = giteasrc.New(gitsource, cli, user, repository)
if err != nil {
logger.Error("Building SCM Source", "error", err)
os.Exit(1)
}
}
default:
logger.Error("Source Unknown", slog.String("source", *sourceType))

}

manager, err := cup.NewService(source)
Expand Down
2 changes: 0 additions & 2 deletions cmd/flipt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,6 @@ func walkDocuments(source fs.FS, fn func(path string, document *ext.Document) er
return err
}

slog.Debug("Opening state files", "paths", paths)

for _, p := range paths {
fi, err := source.Open(p)
if err != nil {
Expand Down
39 changes: 25 additions & 14 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,45 @@ module go.flipt.io/cup
go 1.20

require (
github.com/Microsoft/go-winio v0.5.2 // indirect
code.gitea.io/sdk/gitea v0.15.1
github.com/go-git/go-billy/v5 v5.4.1
github.com/go-git/go-git/v5 v5.7.0
github.com/gobwas/glob v0.2.3
github.com/gofrs/uuid v4.4.0+incompatible
github.com/hashicorp/golang-lru/v2 v2.0.4
github.com/stretchr/testify v1.8.4
github.com/tetratelabs/wazero v1.2.1
go.uber.org/zap v1.24.0
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230518184743-7afd39499903 // indirect
github.com/acomagu/bufpipe v1.0.4 // indirect
github.com/benbjohnson/clock v1.3.0 // indirect
github.com/cloudflare/circl v1.3.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.4.1 // indirect
github.com/go-git/go-git/v5 v5.7.0 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/gofrs/uuid v4.4.0+incompatible // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/hashicorp/golang-lru/v2 v2.0.4 // indirect
github.com/hashicorp/go-version v1.2.1 // indirect
github.com/imdario/mergo v0.3.15 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/skeema/knownhosts v1.1.1 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/tetratelabs/wazero v1.2.1 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.10.0 // indirect
golang.org/x/mod v0.10.0 // indirect
golang.org/x/net v0.11.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.9.0 // indirect
golang.org/x/tools v0.9.1 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 3223c37

Please sign in to comment.