-
Notifications
You must be signed in to change notification settings - Fork 0
/
github-comments-fetcher.go
306 lines (253 loc) · 8.15 KB
/
github-comments-fetcher.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"time"
)
var (
ownerFlag string
repoFlag string
issueNumberFlag string
)
type File struct {
Name string `json:"name"`
Type string `json:"type"`
}
// GitHub issue/PR struct
type Issue struct {
Title string `json:"title"`
Body string `json:"body"`
User User `json:"user"`
DateTime time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// GitHub comment struct
type Comment struct {
Body string `json:"body"`
User User `json:"user"`
DateTime time.Time `json:"created_at"`
}
// GitHub user struct
type User struct {
Login string `json:"login"`
}
func init() {
flag.StringVar(&ownerFlag, "O", "", "Repository owner")
flag.StringVar(&ownerFlag, "owner", "", "Repository owner")
flag.StringVar(&repoFlag, "R", "", "Repository name")
flag.StringVar(&repoFlag, "repo", "", "Repository name")
flag.StringVar(&issueNumberFlag, "I", "", "Reference number of the issue or PR")
flag.StringVar(&issueNumberFlag, "issueNumber", "", "Reference number of the issue or PR")
}
func main() {
// GitHub API endpoint to fetch issue/PR and comments
apiURL := "https://api.github.com/repos/{owner}/{repo}/issues/{issueNumber}"
// Parse command-line flags
flag.Parse()
// Get the absolute path to github-tree-inputs.txt
inputsFilePath := getAbsolutePath("github-comments-fetcher-inputs.txt")
// Initialize variables to store inputs
var currentOwner string
var currentRepo string
var accessToken string
var currentIssueNumber string
// Check if github-comments-fetcher-inputs.txt exists
_, err := os.Stat(inputsFilePath)
if err == nil {
// The file exists, so read existing inputs from the file
currentOwner, currentRepo, currentIssueNumber = readInputsFromFile(inputsFilePath)
// Check if the owner and repo fields are empty
if currentOwner == "" || currentRepo == "" {
panic("The 'owner' and 'repo' fields in github-comments-fetcher-inputs.txt cannot be empty")
}
// Update inputs if flags were provided
if ownerFlag != "" {
currentOwner = ownerFlag
}
if repoFlag != "" {
currentRepo = repoFlag
}
if issueNumberFlag != "" {
currentIssueNumber = issueNumberFlag
}
// Update the inputs in the file
updateInputsInFile(inputsFilePath, currentOwner, currentRepo, currentIssueNumber)
} else {
// The "github-comments-fetcher-inputs.txt" doesn't exist, so create it
// Create a new inputs struct
newInputs := struct {
Owner string `json:"owner"`
Repo string `json:"repo"`
IssueNumber string `json:"issueNumber"`
}{
Owner: ownerFlag,
Repo: repoFlag,
IssueNumber: issueNumberFlag,
}
// Convert to JSON
newInputsJSON, err := json.MarshalIndent(newInputs, "", " ")
if err != nil {
panic(fmt.Errorf("failed to marshal new inputs: %w", err))
}
// Write to the file
err = os.WriteFile(inputsFilePath, newInputsJSON, 0644)
if err != nil {
panic(fmt.Errorf("failed to write new inputs to file: %w", err))
}
}
// Retrieve access token from environment
accessToken = os.Getenv("GITHUB_ACCESS_TOKEN")
if accessToken == "" {
panic("GitHub access token not found in environment")
}
// GitHub repository information
owner := currentOwner
repo := currentRepo
issueNumber := currentIssueNumber // Replace with the issue or PR number you want to fetch
// Create the HTTP client
client := &http.Client{}
// Create the request
req, err := http.NewRequest("GET", apiURL, nil)
if err != nil {
log.Fatalf("Failed to create request: %s", err)
}
// Add the access token to the request header (optional)
if accessToken != "" {
req.Header.Set("Authorization", "Bearer "+accessToken)
}
// Replace the placeholders in the API URL with the repository and issue information
req.URL.Path = fmt.Sprintf("/repos/%s/%s/issues/%s", owner, repo, issueNumber)
// Send the request
resp, err := client.Do(req)
if err != nil {
log.Fatalf("Failed to send request: %s", err)
}
defer resp.Body.Close()
// Read the response body
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatalf("Failed to read response body: %s", err)
}
// Check the response status code
if resp.StatusCode != http.StatusOK {
log.Fatalf("Request failed with status: %s", resp.Status)
}
// Parse the response body as an Issue
var issue Issue
err = json.Unmarshal(body, &issue)
if err != nil {
log.Fatalf("Failed to parse issue response body: %s", err)
}
// Create or open the output file
file, err := os.Create("comments.txt")
if err != nil {
log.Fatalf("Failed to create file: %s", err)
}
defer file.Close()
// Write the issue details to the file
issueLine := fmt.Sprintf("Issue Title: %s\nIssue Body: %s\nIssue Author: %s\nCreated At: %s\nUpdated At: %s\n\n",
issue.Title, issue.Body, issue.User.Login, issue.DateTime.Format("2006-01-02 15:04:05"), issue.UpdatedAt.Format("2006-01-02 15:04:05"))
_, err = file.WriteString(issueLine)
if err != nil {
log.Fatalf("Failed to write issue details to file: %s", err)
}
// Fetch comments
apiCommentsURL := fmt.Sprintf("https://api.github.com/repos/%s/%s/issues/%s/comments", owner, repo, issueNumber)
reqComments, err := http.NewRequest("GET", apiCommentsURL, nil)
if err != nil {
log.Fatalf("Failed to create comments request: %s", err)
}
if accessToken != "" {
reqComments.Header.Set("Authorization", "Bearer "+accessToken)
}
respComments, err := client.Do(reqComments)
if err != nil {
log.Fatalf("Failed to send comments request: %s", err)
}
defer respComments.Body.Close()
bodyComments, err := io.ReadAll(respComments.Body)
if err != nil {
log.Fatalf("Failed to read comments response body: %s", err)
}
if respComments.StatusCode != http.StatusOK {
log.Fatalf("Comments request failed with status: %s", respComments.Status)
}
var comments []Comment
err = json.Unmarshal(bodyComments, &comments)
if err != nil {
log.Fatalf("Failed to parse comments response body: %s", err)
}
// Write the comments to the file
for i, comment := range comments {
if i > 0 {
_, err = file.WriteString("\n") // Leave two-line space between comment blocks
if err != nil {
log.Fatalf("Failed to write space to file: %s", err)
}
}
commentHeader := fmt.Sprintf("Comment %d by %s at %s", i+1, comment.User.Login, comment.DateTime.Format("2006-01-02 15:04:05"))
_, err = file.WriteString(commentHeader + ":\n")
if err != nil {
log.Fatalf("Failed to write comment header to file: %s", err)
}
commentBody := fmt.Sprintf("%s\n", comment.Body)
_, err = file.WriteString(commentBody)
if err != nil {
log.Fatalf("Failed to write comment body to file: %s", err)
}
}
fmt.Println("Issue details and comments have been fetched and saved to comments.txt.")
}
func readInputsFromFile(filePath string) (owner, repo, issueNumber string) {
// Read the contents of the file
fileData, err := os.ReadFile(filePath)
if err != nil {
panic(fmt.Errorf("failed to read inputs from file: %w", err))
}
// Unmarshal the JSON data into a struct
var inputs struct {
Owner string `json:"owner"`
Repo string `json:"repo"`
IssueNumber string `json:"issueNumber"`
}
err = json.Unmarshal(fileData, &inputs)
if err != nil {
panic(fmt.Errorf("failed to parse inputs from file: %w", err))
}
return inputs.Owner, inputs.Repo, inputs.IssueNumber
}
func getAbsolutePath(filePath string) string {
currentDir, err := os.Getwd()
if err != nil {
panic(fmt.Errorf("failed to get current directory: %w", err))
}
return filepath.Join(currentDir, filePath)
}
func updateInputsInFile(filePath, owner, repo, issueNumber string) {
// Create the new inputs struct
newInputs := struct {
Owner string `json:"owner"`
Repo string `json:"repo"`
IssueNumber string `json:"issueNumber"`
}{
Owner: owner,
Repo: repo,
IssueNumber: issueNumber,
}
// Convert to JSON
newInputsJSON, err := json.MarshalIndent(newInputs, "", " ")
if err != nil {
panic(fmt.Errorf("failed to marshal new inputs: %w", err))
}
// Write to the file
err = os.WriteFile(filePath, newInputsJSON, 0644)
if err != nil {
panic(fmt.Errorf("failed to write updated inputs to file: %w", err))
}
}