-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathhelpers.go
More file actions
83 lines (71 loc) · 1.92 KB
/
helpers.go
File metadata and controls
83 lines (71 loc) · 1.92 KB
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
package main
import (
"context"
"log"
forgejo "codeberg.org/mvdkleijn/forgejo-sdk/forgejo/v2"
"github.com/google/go-github/v34/github"
"github.com/ktrysmt/go-bitbucket"
gitlab "github.com/xanzy/go-gitlab"
)
// getUsername retrieves the username for the authenticated user from the git service
func getUsername(client interface{}, service string) string {
if client == nil {
log.Fatalf("Couldn't acquire a client to talk to %s", service)
}
if service == "github" {
ctx := context.Background()
user, _, err := client.(*github.Client).Users.Get(ctx, "")
if err != nil {
log.Fatal("Error retrieving username", err.Error())
}
return *user.Login
}
if service == "gitlab" {
user, _, err := client.(*gitlab.Client).Users.CurrentUser()
if err != nil {
log.Fatal("Error retrieving username", err.Error())
}
return user.Username
}
if service == "bitbucket" {
user, err := client.(*bitbucket.Client).User.Profile()
if err != nil {
log.Fatal("Error retrieving username", err.Error())
}
return user.Username
}
if service == "forgejo" {
user, _, err := client.(*forgejo.Client).GetMyUserInfo()
if err != nil {
log.Fatal("Error retrieving username", err.Error())
}
return user.UserName
}
return ""
}
// validGitlabProjectMembership checks if the given membership type is valid
func validGitlabProjectMembership(membership string) bool {
validMemberships := []string{"all", "owner", "member", "starred"}
for _, m := range validMemberships {
if membership == m {
return true
}
}
return false
}
// contains checks if a string exists in a slice of strings
func contains(list []string, x string) bool {
for _, item := range list {
if item == x {
return true
}
}
return false
}
// getCloneURL returns the appropriate clone URL based on the useHTTPSClone setting
func getCloneURL(httpsURL, sshURL string) string {
if useHTTPSClone != nil && *useHTTPSClone {
return httpsURL
}
return sshURL
}