forked from rokuosan/github-issue-cms
/
issue.go
70 lines (61 loc) · 1.5 KB
/
issue.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
package gh
import (
"context"
"errors"
"github.com/google/go-github/v56/github"
"go.uber.org/zap"
)
type CustomGitHubClient struct {
Client *github.Client
Logger *zap.SugaredLogger
}
func NewCustomGitHubClient(client *github.Client, logger *zap.SugaredLogger) *CustomGitHubClient {
return &CustomGitHubClient{
Client: client,
Logger: logger,
}
}
func (c *CustomGitHubClient) GetIssues(ctx context.Context, username, repository string) ([]*github.Issue, error) {
if c.Client == nil {
c.Logger.Error("client is nil")
return nil, errors.New("client is nil")
}
// Get Issues
if username == "" || repository == "" {
c.Logger.Error("Please set username and repository in gic.config.yaml")
return nil, errors.New("username and repository not set")
}
issuesAndPRs, _, err := c.Client.Issues.ListByRepo(
ctx,
username,
repository,
&github.IssueListByRepoOptions{
State: "all",
},
)
// Check Rate Limits
var rateLimitError *github.RateLimitError
if errors.As(err, &rateLimitError) {
c.Logger.Error("hit rate limit")
return nil, err
}
var abuseRateLimitError *github.AbuseRateLimitError
if errors.As(err, &abuseRateLimitError) {
c.Logger.Error("hit secondary rate limit")
return nil, err
}
if err != nil {
c.Logger.Error("failed to get issues")
return nil, err
}
// Filter issues
var issues []*github.Issue
for _, item := range issuesAndPRs {
// Skip if it is a pull request.
if item.IsPullRequest() {
continue
}
issues = append(issues, item)
}
return issues, nil
}