-
Notifications
You must be signed in to change notification settings - Fork 30
/
add_collabotor.go
121 lines (107 loc) · 3.47 KB
/
add_collabotor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package importcmd
import (
"context"
"github.com/jenkins-x/go-scm/scm"
"github.com/jenkins-x/jx-helpers/v3/pkg/boot"
"github.com/jenkins-x/jx-helpers/v3/pkg/scmhelpers"
"github.com/jenkins-x/jx-logging/v3/pkg/log"
"github.com/pkg/errors"
)
func (o *ImportOptions) AddAndAcceptCollaborator(newRepository bool) error {
ctx := context.Background()
githubAppMode, err := o.IsGitHubAppMode()
if err != nil {
return err
}
if githubAppMode {
return nil
}
userName := o.getCurrentUser()
scmClient := o.ScmFactory.ScmClient
if scmClient == nil {
return errors.Errorf("no SCM client")
}
owner := o.Organisation
if owner == "" {
return errors.Errorf("no organisation")
}
repoName := o.Repository
if repoName == "" {
repoName = o.AppName
}
if repoName == "" {
repoName = o.GitRepositoryOptions.Name
}
if repoName == "" {
return errors.Errorf("no repository")
}
fullRepoName := repoName
if owner != userName {
fullRepoName = scm.Join(owner, fullRepoName)
}
permission := "admin"
pipelineUserName := o.PipelineUserName
if !newRepository {
// lets check if the pipeline user is already a collaborator
collaborator, _, err := scmClient.Repositories.IsCollaborator(ctx, fullRepoName, pipelineUserName)
if err != nil {
return errors.Wrapf(err, "failed to check if %s is a collaborator on %s", pipelineUserName, fullRepoName)
}
if collaborator {
return nil
}
}
// If the user creating the repo is not the pipeline user, add the pipeline user as a contributor to the repo
if pipelineUserName != "" && pipelineUserName != userName { // TODO: not sure why: && o.ScmFactory.GitServerURL == o.PipelineServer {
// Make the invitation
_, _, _, err = scmClient.Repositories.AddCollaborator(ctx, fullRepoName, pipelineUserName, permission)
if err != nil {
return errors.Wrapf(err, "failed to add %s as a collaborator to %s", pipelineUserName, fullRepoName)
}
if o.OperatorNamespace == "" {
o.OperatorNamespace = boot.GitOperatorNamespace
}
if o.BootSecretName == "" {
o.BootSecretName = boot.SecretName
}
bootSecret, err := boot.LoadBootSecret(o.KubeClient, o.OperatorNamespace, o.OperatorNamespace, o.BootSecretName, pipelineUserName)
if err != nil {
return errors.Wrapf(err, "failed to load the boot secret")
}
if bootSecret.Username == "" {
bootSecret.Username = pipelineUserName
}
f := scmhelpers.Factory{
GitKind: o.ScmFactory.GitKind,
GitServerURL: o.ScmFactory.GitServerURL,
GitUsername: bootSecret.Username,
GitToken: bootSecret.Password,
}
bootScmClient, err := f.Create()
if err != nil {
return errors.Wrapf(err, "failed to create SCM client for boot user %s on server %s", f.GitUsername, f.GitServerURL)
}
o.BootScmClient = bootScmClient
if o.ScmFactory.GitKind == "gitea" {
// AddCollaborator doesn't use invitations
return nil
}
// Get all invitations for the pipeline user
invites, _, err := bootScmClient.Users.ListInvitations(ctx)
if err != nil {
return errors.Wrapf(err, "failed to list invites")
}
for i := range invites {
invite := invites[i]
repository := invite.Repo
if repository != nil && repository.Name == repoName {
_, err = bootScmClient.Users.AcceptInvitation(ctx, invite.ID)
if err != nil {
log.Logger().Warnf("failed to accept invitation %v on repository %s: %s", invite.ID, invite.Repo.FullName, err.Error())
}
log.Logger().Infof("accepted invitation %v for repository %s", invite.ID, info(invite.Repo.FullName))
}
}
}
return nil
}