Skip to content

Commit f7c6fdc

Browse files
author
Aatman
authored
feat(globalrepo): add team to repo with different permissions (#3)
* feat(globalrepo): add team to repo with different permissions * chore: fix err msg * chore: update3 image
1 parent b306086 commit f7c6fdc

6 files changed

Lines changed: 167 additions & 1 deletion

File tree

apis/settings/v1beta1/globalrepository_types.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,15 @@ type GlobalRepositorySpec struct {
7070
MergeCommitMessage *string `json:"mergeCommitMessage,omitempty"`
7171

7272
RepositoryCollaborators *RepositoryCollaborators `json:"repositoryCollaborators,omitempty"`
73+
RepositoryTeams *RepositoryTeams `json:"repositoryTeams,omitempty"`
74+
}
75+
76+
type RepositoryTeams struct {
77+
PushPermission []string `json:"pushPermission,omitempty"`
78+
PullPermission []string `json:"pullPermission,omitempty"`
79+
AdminPermission []string `json:"adminPermission,omitempty"`
80+
MaintainPermission []string `json:"maintainPermission,omitempty"`
81+
TriagePermission []string `json:"triagePermission,omitempty"`
7382
}
7483

7584
// GlobalRepositoryStatus defines the observed state of GlobalRepository

apis/settings/v1beta1/zz_generated.deepcopy.go

Lines changed: 45 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/settings.github.com_globalrepositories.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,29 @@ spec:
112112
type: string
113113
type: array
114114
type: object
115+
repositoryTeams:
116+
properties:
117+
adminPermission:
118+
items:
119+
type: string
120+
type: array
121+
maintainPermission:
122+
items:
123+
type: string
124+
type: array
125+
pullPermission:
126+
items:
127+
type: string
128+
type: array
129+
pushPermission:
130+
items:
131+
type: string
132+
type: array
133+
triagePermission:
134+
items:
135+
type: string
136+
type: array
137+
type: object
115138
squashMergeCommitMessage:
116139
description: 'Can be one of: PR_BODY, COMMIT_MESSAGES, BLANK'
117140
type: string

controllers/settings/global_repository.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,67 @@ func (r *GlobalRepositoryReconciler) EditRepoSettings(ctx context.Context, gr *s
5757
return nil
5858
}
5959

60+
// pull - team members can pull, but not push to or administer this repository
61+
// push - team members can pull and push, but not administer this repository
62+
// admin - team members can pull, push and administer this repository
63+
// maintain - team members can manage the repository without access to sensitive or destructive actions.
64+
// triage - team members can proactively manage issues and pull requests without write access.
65+
func (r *GlobalRepositoryReconciler) EditRepoTeams(ctx context.Context, gr *settingsv1beta1.GlobalRepository, repoName string, reqLogger logr.Logger) error {
66+
if gr.Spec.RepositoryTeams == nil {
67+
return nil
68+
}
69+
70+
ghClient := gh.Login(ctx)
71+
72+
for _, adminTeam := range gr.Spec.RepositoryTeams.AdminPermission {
73+
err := addTeamToRepoPerm(ctx, gr, repoName, ghClient, adminTeam, "admin", reqLogger)
74+
if err != nil {
75+
return fmt.Errorf("failed to add admin perm for %s to %s", adminTeam, repoName)
76+
}
77+
}
78+
79+
for _, pullTeam := range gr.Spec.RepositoryTeams.PullPermission {
80+
err := addTeamToRepoPerm(ctx, gr, repoName, ghClient, pullTeam, "pull", reqLogger)
81+
if err != nil {
82+
return fmt.Errorf("failed to add pull perm for %s to %s", pullTeam, repoName)
83+
}
84+
}
85+
86+
for _, pushTeam := range gr.Spec.RepositoryTeams.PushPermission {
87+
err := addTeamToRepoPerm(ctx, gr, repoName, ghClient, pushTeam, "push", reqLogger)
88+
if err != nil {
89+
return fmt.Errorf("failed to add push perm for %s to %s", pushTeam, repoName)
90+
}
91+
}
92+
93+
for _, maintainTeam := range gr.Spec.RepositoryTeams.MaintainPermission {
94+
err := addTeamToRepoPerm(ctx, gr, repoName, ghClient, maintainTeam, "maintain", reqLogger)
95+
if err != nil {
96+
return fmt.Errorf("failed to add maintain perm for %s to %s", maintainTeam, repoName)
97+
}
98+
}
99+
100+
for _, triageTeam := range gr.Spec.RepositoryTeams.TriagePermission {
101+
err := addTeamToRepoPerm(ctx, gr, repoName, ghClient, triageTeam, "triage", reqLogger)
102+
if err != nil {
103+
return fmt.Errorf("failed to add triage perm for %s to %s", triageTeam, repoName)
104+
}
105+
}
106+
107+
return nil
108+
}
109+
110+
func addTeamToRepoPerm(ctx context.Context, gr *settingsv1beta1.GlobalRepository, repoName string, ghClient *github.Client, team string, perm string, reqLogger logr.Logger) error {
111+
_, err := ghClient.Teams.AddTeamRepoBySlug(ctx, gr.Spec.Organization, team, gr.Spec.Organization, repoName, &github.TeamAddTeamRepoOptions{
112+
Permission: perm,
113+
})
114+
if err != nil {
115+
return fmt.Errorf("failed to add perm for team for repo: %w", err)
116+
}
117+
reqLogger.Info("gave " + team + "" + perm + " permission")
118+
return nil
119+
}
120+
60121
func (r *GlobalRepositoryReconciler) EditRepoCollaboraters(ctx context.Context, gr *settingsv1beta1.GlobalRepository, repoName string, reqLogger logr.Logger) error {
61122
ghClient := gh.Login(ctx)
62123

controllers/settings/globalrepository_controller.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ func (r *GlobalRepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Req
8080
if err != nil {
8181
return reconcile.Result{}, err
8282
}
83+
84+
err = r.EditRepoTeams(ctx, gr, repo.GetName(), reqLogger)
85+
if err != nil {
86+
return reconcile.Result{}, err
87+
}
8388
}
8489

8590
return ctrl.Result{}, nil

docs/install.yaml

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,29 @@ spec:
118118
type: string
119119
type: array
120120
type: object
121+
repositoryTeams:
122+
properties:
123+
adminPermission:
124+
items:
125+
type: string
126+
type: array
127+
maintainPermission:
128+
items:
129+
type: string
130+
type: array
131+
pullPermission:
132+
items:
133+
type: string
134+
type: array
135+
pushPermission:
136+
items:
137+
type: string
138+
type: array
139+
triagePermission:
140+
items:
141+
type: string
142+
type: array
143+
type: object
121144
squashMergeCommitMessage:
122145
description: 'Can be one of: PR_BODY, COMMIT_MESSAGES, BLANK'
123146
type: string
@@ -703,7 +726,7 @@ spec:
703726
envFrom:
704727
- secretRef:
705728
name: github-operator-secret
706-
image: hunterthompson/github-operator:v1.1.0
729+
image: hunterthompson/github-operator:v1.4.0
707730
livenessProbe:
708731
httpGet:
709732
path: /healthz

0 commit comments

Comments
 (0)