Skip to content

Commit

Permalink
feat: added Gitea support (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
lindell authored Apr 25, 2021
1 parent 6da7803 commit 0f89791
Show file tree
Hide file tree
Showing 6 changed files with 531 additions and 11 deletions.
80 changes: 70 additions & 10 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/lindell/multi-gitter/internal/domain"
"github.com/lindell/multi-gitter/internal/http"
"github.com/lindell/multi-gitter/internal/multigitter"
"github.com/lindell/multi-gitter/internal/scm/gitea"
"github.com/lindell/multi-gitter/internal/scm/github"
"github.com/lindell/multi-gitter/internal/scm/gitlab"

Expand Down Expand Up @@ -55,9 +56,9 @@ func configurePlatform(cmd *cobra.Command) {
flags.StringSliceP("repo", "R", nil, "The name, including owner of a GitHub repository in the format \"ownerName/repoName\"")
flags.StringSliceP("project", "P", nil, "The name, including owner of a GitLab project in the format \"ownerName/repoName\"")

flags.StringP("platform", "p", "github", "The platform that is used. Available values: github, gitlab")
flags.StringP("platform", "p", "github", "The platform that is used. Available values: github, gitlab, gitea")
_ = cmd.RegisterFlagCompletionFunc("platform", func(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return []string{"github", "gitlab"}, cobra.ShellCompDirectiveDefault
return []string{"github", "gitlab", "gitea"}, cobra.ShellCompDirectiveDefault
})

// Autocompletion for organizations
Expand Down Expand Up @@ -215,6 +216,8 @@ func getVersionController(flag *flag.FlagSet, verifyFlags bool) (multigitter.Ver
return createGithubClient(flag, verifyFlags)
case "gitlab":
return createGitlabClient(flag, verifyFlags)
case "gitea":
return createGiteaClient(flag, verifyFlags)
}
}

Expand All @@ -223,7 +226,6 @@ func createGithubClient(flag *flag.FlagSet, verifyFlags bool) (multigitter.Versi
orgs, _ := flag.GetStringSlice("org")
users, _ := flag.GetStringSlice("user")
repos, _ := flag.GetStringSlice("repo")
mergeTypeStrs, _ := flag.GetStringSlice("merge-type") // Only used for the merge command

if verifyFlags && len(orgs) == 0 && len(users) == 0 && len(repos) == 0 {
return nil, errors.New("no organization, user or repo set")
Expand All @@ -242,13 +244,9 @@ func createGithubClient(flag *flag.FlagSet, verifyFlags bool) (multigitter.Versi
}
}

// Convert all defined merge types (if any)
mergeTypes := make([]domain.MergeType, len(mergeTypeStrs))
for i, mt := range mergeTypeStrs {
mergeTypes[i], err = domain.ParseMergeType(mt)
if err != nil {
return nil, err
}
mergeTypes, err := getMergeTypes(flag)
if err != nil {
return nil, err
}

vc, err := github.New(token, gitBaseURL, http.NewLoggingRoundTripper, github.RepositoryListing{
Expand Down Expand Up @@ -298,6 +296,50 @@ func createGitlabClient(flag *flag.FlagSet, verifyFlags bool) (multigitter.Versi
return vc, nil
}

func createGiteaClient(flag *flag.FlagSet, verifyFlags bool) (multigitter.VersionController, error) {
giteaBaseURL, _ := flag.GetString("base-url")
orgs, _ := flag.GetStringSlice("org")
users, _ := flag.GetStringSlice("user")
repos, _ := flag.GetStringSlice("repo")

if verifyFlags && len(orgs) == 0 && len(users) == 0 && len(repos) == 0 {
return nil, errors.New("no organization, user or repository set")
}

if giteaBaseURL == "" {
return nil, errors.New("no base-url set")
}

token, err := getToken(flag)
if err != nil {
return nil, err
}

repoRefs := make([]gitea.RepositoryReference, len(repos))
for i := range repos {
repoRefs[i], err = gitea.ParseRepositoryReference(repos[i])
if err != nil {
return nil, err
}
}

mergeTypes, err := getMergeTypes(flag)
if err != nil {
return nil, err
}

vc, err := gitea.New(token, giteaBaseURL, gitea.RepositoryListing{
Organizations: orgs,
Users: users,
Repositories: repoRefs,
}, mergeTypes)
if err != nil {
return nil, err
}

return vc, nil
}

func getToken(flag *flag.FlagSet) (string, error) {
if OverrideVersionController != nil {
return "", nil
Expand All @@ -310,6 +352,8 @@ func getToken(flag *flag.FlagSet) (string, error) {
token = ght
} else if ght := os.Getenv("GITLAB_TOKEN"); ght != "" {
token = ght
} else if ght := os.Getenv("GITEA_TOKEN"); ght != "" {
token = ght
}
}

Expand All @@ -320,6 +364,22 @@ func getToken(flag *flag.FlagSet) (string, error) {
return token, nil
}

func getMergeTypes(flag *flag.FlagSet) ([]domain.MergeType, error) {
mergeTypeStrs, _ := flag.GetStringSlice("merge-type") // Only used for the merge command

// Convert all defined merge types (if any)
var err error
mergeTypes := make([]domain.MergeType, len(mergeTypeStrs))
for i, mt := range mergeTypeStrs {
mergeTypes[i], err = domain.ParseMergeType(mt)
if err != nil {
return nil, err
}
}

return mergeTypes, nil
}

// nopWriter is a writer that does nothing
type nopWriter struct{}

Expand Down
6 changes: 5 additions & 1 deletion docs/README.template.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ go get github.com/lindell/multi-gitter

## Token

To use multi-gitter, a token that is allowed to list repositories and create pull requests is needed. This token can either be set in the `GITHUB_TOKEN` or `GITLAB_TOKEN` environment variable, or by using the `--token` flag.
To use multi-gitter, a token that is allowed to list repositories and create pull requests is needed. This token can either be set in the `GITHUB_TOKEN`, `GITLAB_TOKEN`, `GITEA_TOKEN` environment variable, or by using the `--token` flag.

### GitHub
[How to generate a GitHub personal access token](https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token). Make sure to give to `repo` permissions.
Expand All @@ -82,6 +82,10 @@ To use multi-gitter, a token that is allowed to list repositories and create pul

[How to generate a GitLab personal access token](https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html). Make sure to give to it the `api` permission.

### Gitea

In Gitea, access tokens can be generated under Settings -> Applications -> Manage Access Tokens

## Usage
{{range .Commands}}
* [{{ .Name }}](#-usage-of-{{ .Name }}) {{ .Short }}{{end}}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/lindell/multi-gitter
go 1.16

require (
code.gitea.io/sdk/gitea v0.14.0
github.com/go-git/go-git/v5 v5.2.0
github.com/google/go-github/v33 v33.0.0
github.com/pkg/errors v0.9.1
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ cloud.google.com/go/storage v1.5.0/go.mod h1:tpKbwo567HUNpVclU5sGELwQWBDZ8gh0Zeo
cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohlUTyfDhBk=
cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs=
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
code.gitea.io/sdk/gitea v0.14.0 h1:m4J352I3p9+bmJUfS+g0odeQzBY/5OXP91Gv6D4fnJ0=
code.gitea.io/sdk/gitea v0.14.0/go.mod h1:89WiyOX1KEcvjP66sRHdu0RafojGo60bT9UqW17VbWs=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
Expand Down Expand Up @@ -77,6 +79,7 @@ github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.m
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 h1:BHsljHzVlRcyQhjrss6TZTdY2VfCqZPbv5k3iBFa2ZQ=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
Expand Down Expand Up @@ -173,6 +176,8 @@ github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerX
github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4=
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-version v1.2.1 h1:zEfKbn2+PDgroKdiOzqiE8rsmLqU2uwi5PB5pBJ3TkI=
github.com/hashicorp/go-version v1.2.1/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90=
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=
Expand Down
Loading

0 comments on commit 0f89791

Please sign in to comment.