-
Notifications
You must be signed in to change notification settings - Fork 149
/
provider_user.go
118 lines (91 loc) · 3.56 KB
/
provider_user.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
package gitproviders
import (
"context"
"errors"
"fmt"
"github.com/fluxcd/go-git-providers/gitprovider"
)
type userGitProvider struct {
domain string
provider gitprovider.Client
}
var _ GitProvider = userGitProvider{}
func (p userGitProvider) RepositoryExists(ctx context.Context, repoUrl RepoURL) (bool, error) {
userRepoRef := gitprovider.UserRepositoryRef{
UserRef: gitprovider.UserRef{Domain: p.domain, UserLogin: repoUrl.Owner()},
RepositoryName: repoUrl.RepositoryName(),
}
if _, err := p.provider.UserRepositories().Get(ctx, userRepoRef); err != nil {
if errors.Is(err, gitprovider.ErrNotFound) {
return false, nil
}
return false, fmt.Errorf("could not get verify repository exists %w", err)
}
return true, nil
}
func (p userGitProvider) DeployKeyExists(ctx context.Context, repoUrl RepoURL) (bool, error) {
userRepo, err := p.getUserRepo(ctx, repoUrl)
if err != nil {
return false, fmt.Errorf("error getting user repo reference for owner %s, repo %s, %w", repoUrl.Owner(), repoUrl.RepositoryName(), err)
}
return deployKeyExists(ctx, userRepo)
}
func (p userGitProvider) UploadDeployKey(ctx context.Context, repoUrl RepoURL, deployKey []byte) error {
userRepo, err := p.getUserRepo(ctx, repoUrl)
if err != nil {
return fmt.Errorf("error getting user repo reference for owner %s, repo %s, %w", repoUrl.Owner(), repoUrl.RepositoryName(), err)
}
fmt.Println("uploading deploy key")
deployKeyInfo := gitprovider.DeployKeyInfo{
Name: deployKeyName,
Key: deployKey,
ReadOnly: gitprovider.BoolVar(false),
}
return uploadDeployKey(ctx, userRepo, deployKeyInfo)
}
func (p userGitProvider) GetDefaultBranch(ctx context.Context, repoUrl RepoURL) (string, error) {
repoInfoRef, err := p.getRepoInfoFromUrl(ctx, repoUrl)
if err != nil {
return "main", err
}
return *repoInfoRef.DefaultBranch, nil
}
func (p userGitProvider) GetRepoVisibility(ctx context.Context, repoUrl RepoURL) (*gitprovider.RepositoryVisibility, error) {
repoInfoRef, err := p.getRepoInfoFromUrl(ctx, repoUrl)
if err != nil {
return nil, err
}
return repoInfoRef.Visibility, nil
}
func (p userGitProvider) getRepoInfoFromUrl(ctx context.Context, repoUrl RepoURL) (*gitprovider.RepositoryInfo, error) {
repo, err := p.getUserRepo(ctx, repoUrl)
if err != nil {
return nil, err
}
repoInfo := repo.Get()
return &repoInfo, nil
}
func (p userGitProvider) getUserRepo(ctx context.Context, repoUrl RepoURL) (gitprovider.UserRepository, error) {
repo, err := p.provider.UserRepositories().Get(ctx, newUserRepositoryRef(p.domain, repoUrl.Owner(), repoUrl.RepositoryName()))
if err != nil {
return nil, fmt.Errorf("error getting user repository %w", err)
}
return repo, nil
}
func (p userGitProvider) CreatePullRequest(ctx context.Context, repoUrl RepoURL, prInfo PullRequestInfo) (gitprovider.PullRequest, error) {
userRepo, err := p.getUserRepo(ctx, repoUrl)
if err != nil {
return nil, fmt.Errorf("error getting user repo for owner %s, repo %s, %w", repoUrl.Owner(), repoUrl.RepositoryName(), err)
}
return createPullRequest(ctx, userRepo, prInfo)
}
func (p userGitProvider) GetCommits(ctx context.Context, repoUrl RepoURL, targetBranch string, pageSize int, pageToken int) ([]gitprovider.Commit, error) {
userRepo, err := p.getUserRepo(ctx, repoUrl)
if err != nil {
return nil, fmt.Errorf("error getting repo for owner %s, repo %s, %w", repoUrl.Owner(), repoUrl.RepositoryName(), err)
}
return getCommits(ctx, userRepo, targetBranch, pageSize, pageToken)
}
func (p userGitProvider) GetProviderDomain() string {
return getProviderDomain(p.provider.ProviderID())
}