Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added Gitea support #105

Merged
merged 1 commit into from
Apr 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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