-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub.go
191 lines (160 loc) · 5.71 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
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
package plugins
import (
"context"
"fmt"
"net/url"
"strconv"
"strings"
"golang.org/x/oauth2"
"sync"
"regexp"
badger "github.com/dgraph-io/badger/v2"
"github.com/dustin/go-humanize"
"github.com/google/go-github/v31/github"
base "github.com/jriddick/geoffrey/bot"
"github.com/jriddick/geoffrey/irc"
"github.com/jriddick/geoffrey/msg"
"github.com/mvdan/xurls"
log "github.com/sirupsen/logrus"
)
func init() {
base.RegisterHandler(GitHubHandler)
}
// Waiter so we can wait for it to finish before returning
var ghWg sync.WaitGroup
// Holds the authenticated client
var client *github.Client
// Regex replacer for cleaning githubs
var ghReplacer = regexp.MustCompile("[\r\n]+")
// GitHub link matcher
var matcher = regexp.MustCompile("github\\.com/(?P<Username>[a-zA-Z0-9]+)(/(?P<Repository>[a-zA-Z0-9]+))?")
// GitHubHandler extracts information from GitHub
// when a link is posted.
var GitHubHandler = base.Handler{
Name: "GitHub",
Description: "Extracts information from GitHub when a link is posted.",
Event: irc.Message,
Init: func(bot *base.Bot) (bool, error) {
// Get the configuration
config := bot.Config()
// Get the settings
if settings, ok := config.Settings["github"]; ok {
if authentication, ok := settings.(map[interface{}]interface{})["authentication"]; ok {
if key, ok := authentication.(string); ok {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: key},
)
tc := oauth2.NewClient(ctx, ts)
client = github.NewClient(tc)
}
}
}
if client == nil {
log.Warnf("[github] Could not get authentication details.")
client = github.NewClient(nil)
}
return true, nil
},
Run: func(bot *base.Bot, msg *msg.Message) (bool, error) {
// Get configuration
config := bot.Config()
// Check if channel message
if msg.Params[0] == config.Identification.Nick {
return false, nil
}
// Extract the urls
urls := xurls.Relaxed.FindAllString(msg.Trailing, -1)
// Add the amount of urls needed
wg.Add(len(urls))
// Check if we have nothing to do
if len(urls) < 1 {
return false, nil
}
// Get the database
db := bot.Db()
if client == nil {
log.Warnf("[github] Could not get authentication details.")
client = github.NewClient(nil)
}
// Open a read transacation to the database
db.View(func(txn *badger.Txn) error {
// Download the information from the webpage
for _, text := range urls {
match := matcher.FindStringSubmatch(text)
if len(match) > 0 {
if _, err := url.Parse(text); err != nil {
log.Errorf("[github] Could not parse url '%s': %v", text, err)
} else {
// Look for the URL in the database
value, err := txn.Get([]byte(text))
// Check if it was found or not
if err != nil {
if err != badger.ErrKeyNotFound {
log.Errorf("[github] Could not query the database: %v", err)
} else {
log.Infof("[github] Fetching GitHub information for url '%s'", text)
if match[3] != "" {
repo, _, err := client.Repositories.Get(context.Background(), match[1], match[3])
if err != nil {
log.Errorf("[github] GitHub returned an error response for URL '%s': %v", text, err)
} else {
sendMsg := fmt.Sprintf("[%s]", irc.Foreground("GitHub", irc.Green))
sendMsg += fmt.Sprintf("[%s] %s ", irc.Foreground(repo.GetOrganization().GetLogin(), irc.Blue), repo.GetName())
commits, _, err := client.Repositories.ListCommits(context.Background(), match[1], match[3], &github.CommitsListOptions{})
if err != nil {
log.Errorf("[github] Could not fetch commits for '%s/%s': %v", match[1], match[2], err)
} else {
commitMessage := commits[0].GetCommit().GetMessage()
if len(commitMessage) > 50 {
commitMessage = commitMessage[0:47]
commitMessage += "..."
}
sendMsg += fmt.Sprintf("(%s) ", irc.Foreground(commitMessage, irc.Orange))
sendMsg += fmt.Sprintf("(%s) ", irc.Foreground(humanize.Time(repo.GetUpdatedAt().Time), irc.Blue))
sendMsg += fmt.Sprintf("(%s ⭐) ", irc.Foreground(strconv.Itoa(repo.GetStargazersCount()), irc.Brown))
sendMsg += fmt.Sprintf("(%s 🍴) ", irc.Foreground(strconv.Itoa(repo.GetForksCount()), irc.Brown))
bot.Send(msg.Params[0], sendMsg)
}
}
} else {
user, _, err := client.Users.Get(context.Background(), match[1])
if err != nil {
log.Errorf("[github] GitHub returned an error response for URL '%s': %v", text, err)
} else {
sendMsg := fmt.Sprintf("[%s]", irc.Foreground("GitHub", irc.Green))
sendMsg += fmt.Sprintf("[%s] %s ", irc.Foreground(user.GetType(), irc.Blue), user.GetName())
if user.GetPublicRepos() > 0 {
sendMsg += fmt.Sprintf("(repositories: %s) ", irc.Foreground(strconv.Itoa(user.GetPublicRepos()), irc.Purple))
}
if user.GetPublicGists() > 0 {
sendMsg += fmt.Sprintf("(gists: %s) ", irc.Foreground(strconv.Itoa(user.GetPublicGists()), irc.Purple))
}
if user.GetType() == "User" && user.Company != nil {
sendMsg += fmt.Sprintf("company: %s)", irc.Foreground(strings.TrimSpace(user.GetCompany()), irc.Teal))
}
bot.Send(msg.Params[0], sendMsg)
}
}
}
} else {
value.Value(func(val []byte) error {
bot.Send(msg.Params[0], fmt.Sprintf("[%s] %s",
irc.Foreground("GitHub", irc.Blue),
irc.Bold(
string(val),
),
))
return nil
})
}
}
}
}
return nil
})
// Wait for it to complete
ghWg.Wait()
return true, nil
},
}