-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
133 lines (112 loc) · 2.78 KB
/
main.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"github.com/joho/godotenv"
)
type GraphQLRequest struct {
Query string `json:"query"`
Variables struct {
RepoOwner string `json:"repoOwner"`
RepoName string `json:"repoName"`
Label string `json:"label"`
} `json:"variables"`
}
type Issue struct {
Title string `json:"title"`
URL string `json:"url"`
Created string `json:"createdAt"`
Author struct {
Login string `json:"login"`
} `json:"author"`
}
func loadEnv() {
err := godotenv.Load()
if err != nil {
log.Fatalf("Error loading .env file: %v", err) #load content from .env file
}
}
func fetchIssues(repoOwner, repoName, label, token string) ([]Issue, error) {
query := `
query($repoOwner: String!, $repoName: String!, $label: String!) {
repository(owner: $repoOwner, name: $repoName) {
issues(first: 10, labels: [$label], states: OPEN) {
nodes {
title
url
createdAt
author {
login
}
}
}
}
}
`
requestBody := GraphQLRequest{
Query: query,
}
requestBody.Variables.RepoOwner = repoOwner
requestBody.Variables.RepoName = repoName
requestBody.Variables.Label = label
jsonBody, _ := json.Marshal(requestBody)
req, err := http.NewRequest("POST", "https://api.github.com/graphql", bytes.NewBuffer(jsonBody))
if err != nil {
return nil, err
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body) // Use io.ReadAll instead of ioutil.ReadAll
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("GraphQL Response: %s", string(body))
}
var response struct {
Data struct {
Repository struct {
Issues struct {
Nodes []Issue `json:"nodes"`
} `json:"issues"`
} `json:"repository"`
} `json:"data"`
}
err = json.Unmarshal(body, &response)
if err != nil {
return nil, fmt.Errorf("Failed to parse JSON: %v", err)
}
return response.Data.Repository.Issues.Nodes, nil
}
func main() {
// Load environment variables
loadEnv()
repoOwner := os.Getenv("REPO_OWNER")
repoName := os.Getenv("REPO_NAME")
label := os.Getenv("LABEL")
token := os.Getenv("GIT_TOKEN")
if repoOwner == "" || repoName == "" || label == "" || token == "" {
log.Fatal("Required environment variables are missing")
}
// Fetch issues
issues, err := fetchIssues(repoOwner, repoName, label, token)
if err != nil {
log.Fatalf("Error fetching issues: %v", err)
}
// Display issues
for _, issue := range issues {
fmt.Printf("New issue: %s by %s\n", issue.Title, issue.Author.Login)
fmt.Printf("URL: %s\n", issue.URL)
}
}