-
Notifications
You must be signed in to change notification settings - Fork 16
/
types.go
110 lines (99 loc) · 2.21 KB
/
types.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
package main
import (
"context"
"fmt"
"strings"
"time"
"github.com/google/go-github/v32/github"
)
type Repo struct {
Owner string
Name string
HTMLURL string
URL string
Topics []string
}
func (r *Repo) FullName() string {
return fmt.Sprintf("%s/%s", r.Owner, r.Name)
}
func (r *Repo) HasTopic(topic string) bool {
for _, t := range r.Topics {
if t == topic {
return true
}
}
return false
}
func NewRepoFromURL(ctx context.Context, gh *github.Client, url string) (*Repo, error) {
s := strings.Split(url, "/")
s = s[len(s)-2:]
repo, _, err := gh.Repositories.Get(ctx, s[0], s[1])
if err != nil {
return nil, err
}
return &Repo{
Owner: repo.Owner.GetLogin(),
Name: repo.GetName(),
HTMLURL: repo.GetHTMLURL(),
URL: repo.GetURL(),
Topics: repo.Topics,
}, nil
}
type Issue struct {
Title string
HTMLURL string
Description string
Author string
Assignee string
CreatedAt time.Time
State string
Labels []string
Repo *Repo
}
func NewIssue(issue *github.Issue, repo *Repo) *Issue {
// r, _, _ := gh.Reactions.ListIssueReactions(context.TODO(), repo.Owner, repo.Name, issue.GetNumber(), nil)
// r[0].
iss := &Issue{
Title: issue.GetTitle(),
HTMLURL: issue.GetHTMLURL(),
Description: strings.TrimSpace(issue.GetBody()),
Author: issue.GetUser().GetLogin(),
State: issue.GetState(),
CreatedAt: issue.GetCreatedAt(),
Repo: repo,
}
if assignee := issue.GetAssignee(); assignee != nil {
iss.Assignee = assignee.GetLogin()
}
for _, label := range issue.Labels {
iss.Labels = append(iss.Labels, label.GetName())
}
return iss
}
type PullRequest struct {
*Issue
Merged bool
MergedBy string
MergedAt time.Time
}
func NewPullRequest(pr *github.PullRequest, issue *github.Issue, repo *Repo) (*PullRequest, error) {
p := &PullRequest{
Issue: NewIssue(issue, repo),
Merged: pr.GetMerged(),
MergedAt: pr.GetMergedAt(),
}
if p.Merged {
p.MergedBy = pr.GetMergedBy().GetLogin()
}
return p, nil
}
func (pr *PullRequest) HasOneOfLabels(labels ...string) bool {
for _, label := range pr.Labels {
for _, other := range labels {
if label == other {
return true
}
}
}
return false
}