-
Notifications
You must be signed in to change notification settings - Fork 1
/
getRepoInfo.go
135 lines (113 loc) · 2.9 KB
/
getRepoInfo.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
package helpers
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"regexp"
)
type SEResponse struct {
Data struct {
Repository struct {
Name string `json:"name"`
Owner struct {
Login string `json:"login"`
} `json:"owner"`
Description *string `json:"description"`
PrimaryLanguage struct {
Name *string `json:"name"`
} `json:"primaryLanguage"`
} `json:"repository"`
} `json:"data"`
}
type CleanRepo struct {
Name string
Owner string
Description string
URL string
Language string
}
func parseGHURL(url string) (string, string, error) {
regex, err := regexp.Compile(`github\.com/([^/]+)/([^/]+)`)
if err != nil {
return "", "", fmt.Errorf("[parseGHURL][regexp.Compile]: %w", err)
}
matches := regex.FindStringSubmatch(url)
if len(matches) == 3 {
return matches[1], matches[2], nil
}
return "", "", fmt.Errorf("[parseGHURL]: No matches found%w", nil)
}
// DOCS: https://docs.github.com/en/graphql/reference/queries#repository
func GetRepoInfo(url string) (CleanRepo, error) {
token, err := GetKeys("GH_TOKEN")
if err != nil {
return CleanRepo{}, fmt.Errorf("[GetRepoInfo]%w", err)
}
owner, repo, err := parseGHURL(url)
if err != nil {
return CleanRepo{}, fmt.Errorf("[GetRepoInfo]%w", err)
}
query := fmt.Sprintf(`
query {
repository(owner: "%s", name: "%s") {
name
owner {
login
}
description
primaryLanguage {
name
}
}
}
}
`, owner, repo)
options := struct {
Query string `json:"query"`
}{
Query: query,
}
jsonData, err := json.Marshal(options)
if err != nil {
return CleanRepo{}, fmt.Errorf("[GetRepoInfo][json.Marshal]: %w", err)
}
req, err := http.NewRequest("POST", "https://api.github.com/graphql", bytes.NewBuffer(jsonData))
if err != nil {
return CleanRepo{}, fmt.Errorf("[GetRepoInfo][http.NewRequest]: %w", 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 CleanRepo{}, fmt.Errorf("[GetRepoInfo][client.Do]: %w", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return CleanRepo{}, fmt.Errorf("[GetRepoInfo][io.ReadAll]: %w", err)
}
var results SEResponse
err = json.Unmarshal(body, &results)
if err != nil {
return CleanRepo{}, fmt.Errorf("[GetRepoInfo][json.Unmarshal]: %w", err)
}
description := ""
if results.Data.Repository.Description != nil {
description = *results.Data.Repository.Description
}
language := "Markdown"
if results.Data.Repository.PrimaryLanguage.Name != nil {
language = *results.Data.Repository.PrimaryLanguage.Name
}
cleanRepo := CleanRepo{
Name: results.Data.Repository.Name,
Owner: results.Data.Repository.Owner.Login,
Description: description,
URL: url,
Language: language,
}
return cleanRepo, nil
}