-
Notifications
You must be signed in to change notification settings - Fork 13
/
git_provider.go
104 lines (92 loc) · 2.67 KB
/
git_provider.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
package issues
import (
"context"
"fmt"
"strconv"
"time"
"github.com/jenkins-x/go-scm/scm"
"github.com/jenkins-x/jx-helpers/v3/pkg/stringhelpers"
"github.com/pkg/errors"
)
type GitIssueProvider struct {
GitProvider *scm.Client
Owner string
Repository string
fullName string
}
func CreateGitIssueProvider(scmClient *scm.Client, owner string, repository string) (IssueProvider, error) {
if owner == "" {
return nil, fmt.Errorf("no owner specified")
}
if repository == "" {
return nil, fmt.Errorf("no repository specified")
}
fullName := scm.Join(owner, repository)
return &GitIssueProvider{
GitProvider: scmClient,
Owner: owner,
Repository: repository,
fullName: fullName,
}, nil
}
func (i *GitIssueProvider) GetIssue(key string) (*scm.Issue, error) {
ctx := context.Background()
n, err := issueKeyToNumber(key)
if err != nil {
return nil, err
}
issue, _, err := i.GitProvider.Issues.Find(ctx, i.fullName, n)
if err != nil {
return nil, errors.Wrapf(err, "failed to find issue %d in repository %s", n, i.fullName)
}
return issue, nil
}
func (i *GitIssueProvider) SearchIssues(query string) ([]*scm.Issue, error) {
ctx := context.Background()
opts := scm.SearchOptions{
Query: query,
}
searchIssues, _, err := i.GitProvider.Issues.Search(ctx, opts)
if err != nil {
return nil, errors.Wrapf(err, "failed to search issues with %v", opts)
}
var answer []*scm.Issue
for _, si := range searchIssues {
answer = append(answer, &si.Issue)
}
return answer, nil
}
func (i *GitIssueProvider) SearchIssuesClosedSince(_ time.Time) ([]*scm.Issue, error) {
// TODO
//return i.GitProvider.SearchIssuesClosedSince(i.Owner, i.Repository, t)
return nil, nil
}
func (i *GitIssueProvider) IssueURL(key string) string {
return stringhelpers.UrlJoin(i.GitProvider.BaseURL.String(), i.fullName, "issues", key)
}
func issueKeyToNumber(key string) (int, error) {
n, err := strconv.Atoi(key)
if err != nil {
return n, errors.Wrapf(err, "failed to convert issue key '%s' to number", key)
}
return n, nil
}
func (i *GitIssueProvider) CreateIssue(_ *scm.Issue) (*scm.Issue, error) {
return nil, errors.Errorf("TODO")
}
func (i *GitIssueProvider) CreateIssueComment(key string, comment string) error {
ctx := context.Background()
n, err := issueKeyToNumber(key)
if err != nil {
return err
}
ci := &scm.CommentInput{Body: comment}
_, _, err = i.GitProvider.Issues.CreateComment(ctx, i.fullName, n, ci)
if err != nil {
return errors.Wrapf(err, "failed to add comment to issue %d on repository %s", n, i.fullName)
}
return nil
}
func (i *GitIssueProvider) HomeURL() string {
return stringhelpers.UrlJoin(i.GitProvider.BaseURL.String(), i.Owner, i.Repository)
}