Skip to content

Commit

Permalink
Merge pull request #63 from metal-stack/additional-team-memberships
Browse files Browse the repository at this point in the history
Allow specifying additional team memberships when repo gets created.
  • Loading branch information
majst01 committed Jan 11, 2024
2 parents 66162ad + 313679b commit d3a5660
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 490 deletions.
35 changes: 17 additions & 18 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,66 +5,65 @@ go 1.21
require (
github.com/Masterminds/semver/v3 v3.2.1
github.com/atedja/go-multilock v0.0.0-20170315063113-31d195f255fb
github.com/bradleyfalzon/ghinstallation/v2 v2.8.0
github.com/bradleyfalzon/ghinstallation/v2 v2.9.0
github.com/go-git/go-billy/v5 v5.5.0
// IMPORTANT: keep this version as long as https://github.com/go-git/go-git/issues/328 is open
github.com/go-git/go-git/v5 v5.3.0
github.com/go-playground/validator/v10 v10.15.5
github.com/go-playground/validator/v10 v10.16.0
github.com/go-playground/webhooks/v6 v6.3.0
github.com/google/go-cmp v0.6.0
github.com/google/go-github/v56 v56.0.0
github.com/google/go-github/v57 v57.0.0
github.com/metal-stack/v v1.0.3
github.com/mitchellh/mapstructure v1.5.0
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.17.0
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.18.2
github.com/tidwall/gjson v1.17.0
github.com/tidwall/sjson v1.2.5
go.uber.org/zap v1.26.0
golang.org/x/sync v0.4.0
sigs.k8s.io/yaml v1.3.0
golang.org/x/sync v0.6.0
sigs.k8s.io/yaml v1.4.0
)

require (
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/imdario/mergo v0.3.15 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/sagikazarmark/locafero v0.3.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sergi/go-diff v1.3.1 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.10.0 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.13.0 // indirect
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.15.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.13.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
486 changes: 38 additions & 448 deletions go.sum

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion pkg/clients/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/metal-stack/metal-robot/pkg/config"
"go.uber.org/zap"

v3 "github.com/google/go-github/v56/github"
v3 "github.com/google/go-github/v57/github"
)

type Github struct {
Expand All @@ -18,6 +18,7 @@ type Github struct {
appID int64
installationID int64
organizationID string
owner string
atr *ghinstallation.AppsTransport
itr *ghinstallation.Transport
}
Expand Down Expand Up @@ -49,6 +50,8 @@ func (a *Github) initClients() error {
return fmt.Errorf("error finding organization installation %w", err)
}

a.owner = *installation.Account.Login

a.installationID = installation.GetID()

itr := ghinstallation.NewFromAppsTransport(atr, a.installationID)
Expand Down Expand Up @@ -84,3 +87,7 @@ func (a *Github) GitToken(ctx context.Context) (string, error) {
}
return t.GetToken(), nil
}

func (a *Github) Owner() string {
return a.owner
}
6 changes: 5 additions & 1 deletion pkg/config/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ type WebhookAction struct {
Args map[string]any `json:"args" description:"action configuration"`
}
type RepositoryMaintainersConfig struct {
Suffix *string `mapstructure:"suffix" description:"suffix for maintainers group"`
Suffix *string `mapstructure:"suffix" description:"suffix for maintainers group"`
AdditionalMemberships []struct {
TeamSlug string `mapstructure:"team" description:"the slug of the team"`
Permission string `mapstructure:"permission" description:"the permission for the team, must be one of "`
} `mapstructure:"additional-teams" description:"adds additional teams to this repository"`
}

type DocsPreviewCommentConfig struct {
Expand Down
9 changes: 2 additions & 7 deletions pkg/webhooks/github/actions/aggregate_releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

"github.com/Masterminds/semver/v3"
"github.com/atedja/go-multilock"
v3 "github.com/google/go-github/v56/github"
v3 "github.com/google/go-github/v57/github"
"github.com/metal-stack/metal-robot/pkg/clients"
"github.com/metal-stack/metal-robot/pkg/config"
"github.com/metal-stack/metal-robot/pkg/git"
Expand Down Expand Up @@ -113,17 +113,12 @@ func (r *AggregateReleases) AggregateRelease(ctx context.Context, p *AggregateRe

tag := p.TagName
trimmed := strings.TrimPrefix(tag, "v")
parsedVersion, err := semver.NewVersion(trimmed)
_, err := semver.NewVersion(trimmed)
if err != nil {
r.logger.Infow("skip applying release actions to aggregation repo because not a valid semver release tag", "target-repo", r.repoName, "source-repo", p.RepositoryName, "tag", p.TagName)
return nil //nolint:nilerr
}

if parsedVersion.Prerelease() != "" {
r.logger.Infow("skip applying release actions to aggregation repo because is a pre-release", "target-repo", r.repoName, "source-repo", p.RepositoryName, "tag", p.TagName)
return nil //nolint:nilerr
}

// preventing concurrent git repo modifications
var once sync.Once
r.lock.Lock()
Expand Down
16 changes: 15 additions & 1 deletion pkg/webhooks/github/actions/distribute_releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (

"errors"

"github.com/Masterminds/semver/v3"
"github.com/atedja/go-multilock"
v3 "github.com/google/go-github/v56/github"
v3 "github.com/google/go-github/v57/github"
"github.com/metal-stack/metal-robot/pkg/clients"
"github.com/metal-stack/metal-robot/pkg/config"
"github.com/metal-stack/metal-robot/pkg/git"
Expand Down Expand Up @@ -113,6 +114,19 @@ func (d *distributeReleases) DistributeRelease(ctx context.Context, p *distribut
return nil
}

trimmed := strings.TrimPrefix(tag, "v")

parsedVersion, err := semver.NewVersion(trimmed)
if err != nil {
d.logger.Infow("skip applying release actions to target repos because not a valid semver release tag", "source-repo", d.repoName, "trigger-repo", p.RepositoryName, "tag", p.TagName)
return nil //nolint:nilerr
}

if parsedVersion.Prerelease() != "" {
d.logger.Infow("skip applying release actions to target repos because is a pre-release", "source-repo", d.repoName, "trigger-repo", p.RepositoryName, "tag", p.TagName)
return nil //nolint:nilerr
}

token, err := d.client.GitToken(ctx)
if err != nil {
return fmt.Errorf("error creating git token %w", err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/webhooks/github/actions/docs_preview_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"fmt"

v3 "github.com/google/go-github/v56/github"
v3 "github.com/google/go-github/v57/github"
"github.com/metal-stack/metal-robot/pkg/clients"
"github.com/metal-stack/metal-robot/pkg/config"
"github.com/mitchellh/mapstructure"
Expand Down
4 changes: 2 additions & 2 deletions pkg/webhooks/github/actions/release_drafter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import (
"strings"

"github.com/Masterminds/semver/v3"
"github.com/google/go-github/v56/github"
"github.com/google/go-github/v57/github"
"github.com/metal-stack/metal-robot/pkg/clients"
"github.com/metal-stack/metal-robot/pkg/config"
"github.com/metal-stack/metal-robot/pkg/markdown"
"github.com/metal-stack/metal-robot/pkg/utils"
"github.com/mitchellh/mapstructure"
"go.uber.org/zap"

v3 "github.com/google/go-github/v56/github"
v3 "github.com/google/go-github/v57/github"
)

var (
Expand Down
2 changes: 1 addition & 1 deletion pkg/webhooks/github/actions/release_drafter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/Masterminds/semver/v3"
"github.com/google/go-cmp/cmp"
v3 "github.com/google/go-github/v56/github"
v3 "github.com/google/go-github/v57/github"
"go.uber.org/zap/zaptest"
)

Expand Down
58 changes: 49 additions & 9 deletions pkg/webhooks/github/actions/repository_maintainers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,25 @@ import (
"fmt"
"strings"

v3 "github.com/google/go-github/v56/github"
v3 "github.com/google/go-github/v57/github"
"github.com/metal-stack/metal-robot/pkg/clients"
"github.com/metal-stack/metal-robot/pkg/config"
"github.com/mitchellh/mapstructure"
"go.uber.org/zap"
)

type repositoryMaintainers struct {
logger *zap.SugaredLogger
client *clients.Github
suffix string
logger *zap.SugaredLogger
client *clients.Github
suffix string
additionalTeams repositoryAdditionalMemberships
}

type repositoryAdditionalMemberships []repositoryTeamMembership

type repositoryTeamMembership struct {
teamSlug string
permission string
}

type repositoryMaintainersParams struct {
Expand All @@ -38,10 +46,20 @@ func newCreateRepositoryMaintainers(logger *zap.SugaredLogger, client *clients.G
suffix = *typedConfig.Suffix
}

var additionalTeams repositoryAdditionalMemberships
for _, team := range typedConfig.AdditionalMemberships {
team := team
additionalTeams = append(additionalTeams, repositoryTeamMembership{
teamSlug: team.TeamSlug,
permission: team.Permission,
})
}

return &repositoryMaintainers{
logger: logger,
client: client,
suffix: suffix,
logger: logger,
client: client,
suffix: suffix,
additionalTeams: additionalTeams,
}, nil
}

Expand All @@ -59,13 +77,35 @@ func (r *repositoryMaintainers) CreateRepositoryMaintainers(ctx context.Context,
Privacy: v3.String("closed"),
})
if err != nil {
// Could be that team already exists...
if !strings.Contains(err.Error(), "Name must be unique for this org") {
if strings.Contains(err.Error(), "Name must be unique for this org") {
r.logger.Infow("maintainers team for repository already exists", "repository", p.RepositoryName, "team", name)
} else {
return fmt.Errorf("error creating maintainers team %w", err)
}
} else {
r.logger.Infow("created new maintainers team for repository", "repository", p.RepositoryName, "team", name)
}

memberships := []repositoryTeamMembership{
{
teamSlug: name,
permission: "maintain",
},
}
memberships = append(memberships, r.additionalTeams...)

for _, team := range memberships {
team := team

_, err := r.client.GetV3Client().Teams.AddTeamRepoBySlug(ctx, r.client.Organization(), team.teamSlug, r.client.Organization(), p.RepositoryName, &v3.TeamAddTeamRepoOptions{
Permission: team.permission,
})
if err != nil {
return fmt.Errorf("error adding team membership: %w", err)
} else {
r.logger.Infow("added team to repository", "repository", p.RepositoryName, "team", team.teamSlug, "permission", team.permission)
}
}

return nil
}
2 changes: 1 addition & 1 deletion pkg/webhooks/github/actions/yaml_translate_releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

"github.com/Masterminds/semver/v3"
"github.com/atedja/go-multilock"
v3 "github.com/google/go-github/v56/github"
v3 "github.com/google/go-github/v57/github"
"github.com/metal-stack/metal-robot/pkg/clients"
"github.com/metal-stack/metal-robot/pkg/config"
"github.com/metal-stack/metal-robot/pkg/git"
Expand Down

0 comments on commit d3a5660

Please sign in to comment.