-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
86 lines (68 loc) · 1.91 KB
/
Copy pathutils.go
File metadata and controls
86 lines (68 loc) · 1.91 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
84
85
86
package main
import (
"context"
"regexp"
"github.com/google/go-github/v32/github"
"github.com/gookit/goutil/mathutil"
"golang.org/x/oauth2"
)
type GithubApi struct {
// owner and repo name
owner, repo string
client *github.Client
}
// NewGithub create
func NewGithub(owner, repo, token string) *GithubApi {
gha := &GithubApi{
owner: owner,
repo: repo,
}
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)
// create client
gha.client = github.NewClient(tc)
return gha
}
// AddLabelsToIssue by PR number
func (gha *GithubApi) AddLabelsToIssue(number int, labels []string) ([]*github.Label, *github.Response, error) {
return gha.client.Issues.AddLabelsToIssue(context.Background(), gha.owner, gha.repo, number, labels)
}
// GetPullRequest by PR number
func (gha *GithubApi) GetPullRequest(prNumber int) (*github.PullRequest, *github.Response, error) {
return gha.client.PullRequests.Get(context.Background(), gha.owner, gha.repo, prNumber)
}
// ListPullRequestFiles by PR number
func (gha *GithubApi) ListPullRequestFiles(prNumber int, options *github.ListOptions) ([]*github.CommitFile, *github.Response, error) {
return gha.client.PullRequests.ListFiles(context.Background(), gha.owner, gha.repo, prNumber, options)
}
// Repositories service get
func (gha *GithubApi) Repositories() *github.RepositoriesService {
return gha.client.Repositories
}
// Repo name
func (gha *GithubApi) Repo() string {
return gha.repo
}
// Owner name
func (gha *GithubApi) Owner() string {
return gha.owner
}
// Client get github client
func (gha *GithubApi) Client() *github.Client {
return gha.client
}
//
// helper func
//
func getPrNumber(ghRefer string) int {
// "refs/pull/:prNumber/merge"
rg := regexp.MustCompile(`^refs/pull/(\d+)/merge`)
ns := rg.FindStringSubmatch(ghRefer)
if len(ns) < 2 {
return 0
}
return mathutil.MustInt(ns[1])
}