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: filter repositories using topics #320

Merged
merged 7 commits into from
Feb 13, 2023
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
7 changes: 7 additions & 0 deletions cmd/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func configurePlatform(cmd *cobra.Command) {
flags.StringSliceP("group", "G", nil, "The name of a GitLab organization. All repositories in that group will be used.")
flags.StringSliceP("user", "U", nil, "The name of a user. All repositories owned by that user will be used.")
flags.StringSliceP("repo", "R", nil, "The name, including owner of a GitHub repository in the format \"ownerName/repoName\".")
flags.StringSliceP("topic", "", nil, "The topic of a GitHub/GitLab/Gitea repository. All repositories having at least one matching topic are targeted.")
flags.StringSliceP("project", "P", nil, "The name, including owner of a GitLab project in the format \"ownerName/repoName\".")
flags.BoolP("include-subgroups", "", false, "Include GitLab subgroups when using the --group flag.")
flags.BoolP("ssh-auth", "", false, `Use SSH cloning URL instead of HTTPS + token. This requires that a setup with ssh keys that have access to all repos and that the server is already in known_hosts.`)
Expand Down Expand Up @@ -119,6 +120,7 @@ func createGithubClient(flag *flag.FlagSet, verifyFlags bool, readOnly bool) (mu
orgs, _ := flag.GetStringSlice("org")
users, _ := flag.GetStringSlice("user")
repos, _ := flag.GetStringSlice("repo")
topics, _ := flag.GetStringSlice("topic")
forkMode, _ := flag.GetBool("fork")
forkOwner, _ := flag.GetString("fork-owner")
sshAuth, _ := flag.GetBool("ssh-auth")
Expand Down Expand Up @@ -149,6 +151,7 @@ func createGithubClient(flag *flag.FlagSet, verifyFlags bool, readOnly bool) (mu
Organizations: orgs,
Users: users,
Repositories: repoRefs,
Topics: topics,
}, mergeTypes, forkMode, forkOwner, sshAuth, readOnly)
if err != nil {
return nil, err
Expand All @@ -162,6 +165,7 @@ func createGitlabClient(flag *flag.FlagSet, verifyFlags bool) (multigitter.Versi
groups, _ := flag.GetStringSlice("group")
users, _ := flag.GetStringSlice("user")
projects, _ := flag.GetStringSlice("project")
topics, _ := flag.GetStringSlice("topic")
includeSubgroups, _ := flag.GetBool("include-subgroups")
sshAuth, _ := flag.GetBool("ssh-auth")

Expand All @@ -186,6 +190,7 @@ func createGitlabClient(flag *flag.FlagSet, verifyFlags bool) (multigitter.Versi
Groups: groups,
Users: users,
Projects: projRefs,
Topics: topics,
}, gitlab.Config{
IncludeSubgroups: includeSubgroups,
SSHAuth: sshAuth,
Expand All @@ -202,6 +207,7 @@ func createGiteaClient(flag *flag.FlagSet, verifyFlags bool) (multigitter.Versio
orgs, _ := flag.GetStringSlice("org")
users, _ := flag.GetStringSlice("user")
repos, _ := flag.GetStringSlice("repo")
topics, _ := flag.GetStringSlice("topic")
sshAuth, _ := flag.GetBool("ssh-auth")

if verifyFlags && len(orgs) == 0 && len(users) == 0 && len(repos) == 0 {
Expand Down Expand Up @@ -234,6 +240,7 @@ func createGiteaClient(flag *flag.FlagSet, verifyFlags bool) (multigitter.Versio
Organizations: orgs,
Users: users,
Repositories: repoRefs,
Topics: topics,
}, mergeTypes, sshAuth)
if err != nil {
return nil, err
Expand Down
21 changes: 21 additions & 0 deletions internal/scm/gitea/gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"code.gitea.io/sdk/gitea"
"github.com/lindell/multi-gitter/internal/scm"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"

internalHTTP "github.com/lindell/multi-gitter/internal/http"
)
Expand Down Expand Up @@ -67,6 +68,7 @@ type RepositoryListing struct {
Organizations []string
Users []string
Repositories []RepositoryReference
Topics []string
}

// RepositoryReference contains information to be able to reference a repository
Expand Down Expand Up @@ -96,6 +98,20 @@ func (g *Gitea) GetRepositories(ctx context.Context) ([]scm.Repository, error) {

repos := make([]scm.Repository, 0, len(allRepos))
for _, repo := range allRepos {
log := log.WithField("repo", repo.FullName)

if len(g.Topics) != 0 {
topics, err := g.getRepoTopics(ctx, repo)
if err != nil {
return repos, fmt.Errorf("could not fetch repository topics: %w", err)
}

if !scm.RepoContainsTopic(topics, g.Topics) {
log.Debug("Skipping repository since it does not match repository topics")
continue
}
}

convertedRepo, err := g.convertRepository(repo)
if err != nil {
return nil, err
Expand All @@ -106,6 +122,11 @@ func (g *Gitea) GetRepositories(ctx context.Context) ([]scm.Repository, error) {
return repos, nil
}

func (g *Gitea) getRepoTopics(ctx context.Context, repo *gitea.Repository) ([]string, error) {
topics, _, err := g.giteaClient(ctx).ListRepoTopics(repo.Owner.UserName, repo.Name, gitea.ListRepoTopicsOptions{})
return topics, err
}

func (g *Gitea) getRepositories(ctx context.Context) ([]*gitea.Repository, error) {
allRepos := []*gitea.Repository{}

Expand Down
4 changes: 4 additions & 0 deletions internal/scm/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ type RepositoryListing struct {
Organizations []string
Users []string
Repositories []RepositoryReference
Topics []string
}

// RepositoryReference contains information to be able to reference a repository
Expand Down Expand Up @@ -151,6 +152,9 @@ func (g *Github) GetRepositories(ctx context.Context) ([]scm.Repository, error)
case !g.Fork && !g.ReadOnly && !permissions["push"]:
log.Debug("Skipping repository since the token does not have push permissions and the run will not fork")
continue
case len(g.Topics) != 0 && !scm.RepoContainsTopic(r.Topics, g.Topics):
log.Debug("Skipping repository since it does not match repository topics")
continue
}

newRepo, err := g.convertRepo(r)
Expand Down
23 changes: 23 additions & 0 deletions internal/scm/github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ func Test_GetRepositories(t *testing.T) {
"name": "test1",
"full_name": "test-org/test1",
"private": false,
"topics": [
"frontend"
],
"owner": {
"login": "test-org",
"type": "Organization",
Expand Down Expand Up @@ -93,6 +96,10 @@ func Test_GetRepositories(t *testing.T) {
"name": "test2",
"full_name": "lindell/test2",
"private": false,
"topics": [
"backend",
"go"
],
"owner": {
"login": "lindell",
"type": "User",
Expand Down Expand Up @@ -163,6 +170,22 @@ func Test_GetRepositories(t *testing.T) {
}
}

// Topics
lindell marked this conversation as resolved.
Show resolved Hide resolved
{
gh, err := github.New("", "", transport.Wrapper, github.RepositoryListing{
Organizations: []string{"test-org"},
Topics: []string{"frontend", "backend"},
}, []scm.MergeType{scm.MergeTypeMerge}, false, "", false, false)
require.NoError(t, err)

repos, err := gh.GetRepositories(context.Background())
assert.NoError(t, err)
if assert.Len(t, repos, 1) {
assert.Equal(t, "master", repos[0].DefaultBranch())
assert.Equal(t, "test-org/test1", repos[0].FullName())
}
}

// Multiple
{
gh, err := github.New("", "", transport.Wrapper, github.RepositoryListing{
Expand Down
9 changes: 9 additions & 0 deletions internal/scm/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

internalHTTP "github.com/lindell/multi-gitter/internal/http"
"github.com/lindell/multi-gitter/internal/scm"
log "github.com/sirupsen/logrus"
"github.com/xanzy/go-gitlab"
)

Expand Down Expand Up @@ -54,6 +55,7 @@ type RepositoryListing struct {
Groups []string
Users []string
Projects []ProjectReference
Topics []string
}

// Config includes extra config parameters for the GitLab client
Expand Down Expand Up @@ -89,6 +91,12 @@ func (g *Gitlab) GetRepositories(ctx context.Context) ([]scm.Repository, error)

repos := make([]scm.Repository, 0, len(allProjects))
for _, project := range allProjects {
log := log.WithField("repo", project.NameWithNamespace)
if len(g.Topics) != 0 && !scm.RepoContainsTopic(project.Topics, g.Topics) {
log.Debug("Skipping repository since it does not match repository topics")
continue
}

p, err := g.convertProject(project)
if err != nil {
return nil, err
Expand Down Expand Up @@ -124,6 +132,7 @@ func (g *Gitlab) getProjects(ctx context.Context) ([]*gitlab.Project, error) {
if err != nil {
return nil, err
}

allProjects = append(allProjects, project)
}

Expand Down
15 changes: 15 additions & 0 deletions internal/scm/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,18 @@ type Repository interface {
// FullName returns the full id of the repository, usually ownerName/repoName
FullName() string
}

func RepoContainsTopic(repoTopics []string, filterTopics []string) bool {
repoTopicsMap := map[string]struct{}{}
for _, v := range repoTopics {
repoTopicsMap[v] = struct{}{}
}

for _, v := range filterTopics {
if _, ok := repoTopicsMap[v]; ok {
return true
}
}

return false
}