-
Notifications
You must be signed in to change notification settings - Fork 55
/
github.go
126 lines (105 loc) · 2.92 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
package developer
import (
"encoding/json"
"fmt"
"github.com/dustin/go-humanize"
"github.com/sapphire-cord/sapphire"
"io/ioutil"
"net/http"
"strings"
)
// View a GitHub repository details.
// cooldown: 3
// aliases: gh
// usage: <repo:string>
func Github(ctx *sapphire.CommandContext) {
repo := strings.Split(ctx.Arg(0).AsString(), "/")
if len(repo) < 2 {
ctx.Reply("Invalid repository. it must be in the format `username/repository`")
return
}
res, err := http.Get("https://api.github.com/repos/" + repo[0] + "/" + repo[1])
if err != nil {
ctx.Error(err)
return
}
defer res.Body.Close()
if res.StatusCode != 200 {
ctx.Reply("Could not fetch that repository, are you sure it exists?")
return
}
buf, err := ioutil.ReadAll(res.Body)
if err != nil {
ctx.Error(err)
return
}
var data struct {
Name string `json:"full_name"`
Language string `json:"language"`
URL string `json:"html_url"`
Description string `json:"description"`
Size int `json:"size"`
Watchers int `json:"subscribers_count"`
Forks int `json:"forks_count"`
Stargazers int `json:"stargazers_count"`
OpenIssues int `json:"open_issues"`
Owner struct {
AvatarURL string `json:"avatar_url"`
} `json:"owner"`
License struct {
Name string `json:"name"`
URL string `json:"url"`
} `json:"license"`
Fork bool `json:"fork"`
Archived bool `json:"archived"`
Parent struct {
Name string `json:"full_nane"`
URL string `json:"html_url"`
} `json:"parent"`
}
err = json.Unmarshal(buf, &data)
if err != nil {
ctx.Error(err)
return
}
license := "None"
if data.License.Name != "" {
if data.License.URL != "" {
license = fmt.Sprintf("[%s](%s)", data.License.Name, data.License.URL)
} else {
license = data.License.Name
}
}
description := "No Description"
if data.Description != "" {
description = data.Description
}
footer := make([]string, 0)
if data.Fork {
footer = append(footer, fmt.Sprintf("❯ **Forked** from [%s](%s)", data.Parent.Name, data.Parent.URL))
}
if data.Archived {
footer = append(footer, "❯ This repository is **Archived**")
}
footerText := ""
if len(footer) > 0 {
footerText = strings.Join(footer, "\n")
}
ctx.BuildEmbed(sapphire.NewEmbed().
SetColor(0xDFAC7C).
SetTitle(data.Name).
SetAuthor("GitHub", "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png").
SetURL(data.URL).
SetThumbnail(data.Owner.AvatarURL).
SetDescription(fmt.Sprintf("%s\n\n❯ **Language:** %s\n❯ **Forks:** %s\n❯ **License:** %s\n❯ **Open Issues:** %s\n❯ **Watchers:** %s\n❯ **Stars:** %s\n❯ **Clone Size:** %s\n%s",
description,
data.Language,
humanize.Comma(int64(data.Forks)),
license,
humanize.Comma(int64(data.OpenIssues)),
humanize.Comma(int64(data.Watchers)),
humanize.Comma(int64(data.Stargazers)),
humanize.Bytes(uint64(data.Size*1024)),
footerText,
)))
}