/
git.go
114 lines (83 loc) · 2.64 KB
/
git.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
package tiktokmod
import (
"context"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
)
// gitmyhub - creates connection with github to prepare API request
func gitmyhub(tiktok *TikTokConf) (ctx context.Context, client *github.Client) {
gitKey := tiktok.Config.GitToken
ctx = context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: gitKey},
)
tc := oauth2.NewClient(ctx, ts)
client = github.NewClient(tc)
return ctx, client
}
// RetrieveOrgRepo - retrieve list of repositories for an Organization
func RetrieveOrgRepo(tiktok *TikTokConf, orgName string) (repos []*github.Repository) {
ctx, client := gitmyhub(tiktok)
opt := &github.RepositoryListByOrgOptions{
Type: "all",
}
repos, _, err := client.Repositories.ListByOrg(ctx, orgName, opt)
if err != nil {
errTrap(tiktok, "Error in `retrieveRepo` function in `git.go`", err)
return
}
return repos
}
// RetrieveRepo - retrieve list of repositories for the API user
func RetrieveRepo(tiktok *TikTokConf) (repos []*github.Repository) {
ctx, client := gitmyhub(tiktok)
opt := &github.RepositoryListOptions{
Sort: "updated",
Type: "all",
Direction: "desc",
ListOptions: github.ListOptions{PerPage: 100},
}
repos, _, err := client.Repositories.List(ctx, "", opt)
if err != nil {
errTrap(tiktok, "Error in `retrieveRepo` function in `git.go`", err)
return
}
return repos
}
// RetrieveUsers - retrieve list of Users
func RetrieveUsers(tiktok *TikTokConf, org string) (users []*github.User) {
ctx, client := gitmyhub(tiktok)
opt := &github.ListMembersOptions{
ListOptions: github.ListOptions{PerPage: 100},
}
users, _, err := client.Organizations.ListMembers(ctx, org, opt)
if err != nil {
errTrap(tiktok, "Error in `RetrieveUsers` function in `git.go`", err)
return
}
return users
}
// GitPRList - retrieve all PRs in a repo
func GitPRList(tiktok *TikTokConf, repoName string, org string) (pulls []*github.PullRequest, err error) {
ctx, client := gitmyhub(tiktok)
opt := &github.PullRequestListOptions{
Sort: "updated",
Direction: "desc",
}
pulls, _, err = client.PullRequests.List(ctx, org, repoName, opt)
if err != nil {
errTrap(tiktok, "Error in `GitPRList` function in `git.go`", err)
return pulls, err
}
return pulls, nil
}
// GitPR - retrieve a single PR in a repo
func GitPR(tiktok *TikTokConf, repoName string, PRID int, org string) (pull *github.PullRequest, err error) {
ctx, client := gitmyhub(tiktok)
pull, _, err = client.PullRequests.Get(ctx, org, repoName, PRID)
if err != nil {
errTrap(tiktok, "Error in `GitPR` function in `git.go`", err)
return pull, err
}
return pull, nil
}