-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub.go
157 lines (133 loc) · 3.18 KB
/
github.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package summaraizer
import (
"encoding/json"
"fmt"
"io"
"net/http"
)
// GitHub is a source that fetches comments from a GitHub issue or pull request.
type GitHub struct {
Token string
RepoOwner string
RepoName string
IssueNumber string
}
// Fetch fetches comments from a GitHub issue.
func (gh *GitHub) Fetch(writer io.Writer) error {
return fetchAndEncode(writer, func() (Comments, error) {
var comments Comments
issue := issue{
repo: repo{
Owner: gh.RepoOwner,
Name: gh.RepoName,
},
Number: gh.IssueNumber,
}
issueResponse := fetchIssue(gh.Token, issue)
comments = append(comments, Comment{
Author: issueResponse.User.Login,
Body: issueResponse.Body,
})
if issueResponse.Comments > 0 {
issueComments := fetchIssueComments(gh.Token, issue)
for _, issueComments := range *issueComments {
comments = append(comments, Comment{
Author: issueComments.User.Login,
Body: issueComments.Body,
})
}
}
return comments, nil
})
}
func fetchIssue(token string, issue issue) *issueResponse {
issueURL := fmt.Sprintf(
"https://api.github.com/repos/%s/%s/issues/%s",
issue.Owner,
issue.Name,
issue.Number,
)
request := newRequest("GET", issueURL, token)
resp, err := http.DefaultClient.Do(&request)
if err != nil {
fmt.Println("Error:", err)
return nil
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error:", err)
return nil
}
var issueResponse issueResponse
err = json.Unmarshal(body, &issueResponse)
if err != nil {
fmt.Println("Error:", err)
return nil
}
return &issueResponse
}
func fetchIssueComments(token string, issue issue) *issueCommentsResponse {
commentsURL := fmt.Sprintf(
"https://api.github.com/repos/%s/%s/issues/%s/comments",
issue.Owner,
issue.Name,
issue.Number,
)
request := newRequest("GET", commentsURL, token)
resp, err := http.DefaultClient.Do(&request)
if err != nil {
fmt.Println("Error:", err)
return nil
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error:", err)
return nil
}
var commentsResponse issueCommentsResponse
err = json.Unmarshal(body, &commentsResponse)
if err != nil {
fmt.Println("Error:", err)
return nil
}
return &commentsResponse
}
func newRequest(method, url string, token string) http.Request {
req, err := http.NewRequest(method, url, nil)
if err != nil {
panic(err)
}
addHeaders(req, token)
return *req
}
func addHeaders(req *http.Request, token string) {
req.Header.Add("Accept", "application/vnd.github+json")
// Public repos doesn't need Authorization header
if token != "" {
req.Header.Add("Authorization", "Bearer "+token)
}
req.Header.Add("X-GitHub-Api-Version", "2022-11-28")
}
type repo struct {
Owner string
Name string
}
type issue struct {
repo
Number string
}
type issueResponse struct {
Body string `json:"body"`
User userResponse `json:"user"`
Comments int `json:"comments"`
}
type userResponse struct {
Login string `json:"login"`
}
type issueCommentsResponse = []issueCommentResponse
type issueCommentResponse struct {
Body string `json:"body"`
User userResponse `json:"user"`
}