-
Notifications
You must be signed in to change notification settings - Fork 1
/
github.go
66 lines (55 loc) · 1.29 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
package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/jdiez17/go-irc"
"io/ioutil"
"net/http"
"regexp"
"strconv"
)
var issueRegexp *regexp.Regexp = regexp.MustCompile(`#([0-9]+)`)
const (
githubIssueUrl string = "https://api.github.com/repos/%s/%s/issues/%d"
issueResponse = "#%d: %s (%s)"
)
type githubIssue struct {
Html_url string
Title string
}
func getGithubIssue(owner, repo string, issue int) (*githubIssue, error) {
rqurl := fmt.Sprintf(githubIssueUrl, owner, repo, issue)
res, err := http.Get(rqurl)
if err != nil {
return nil, err
}
if res.StatusCode == 404 {
return nil, errors.New("Issue not found.")
}
bytes, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
ghissue := new(githubIssue)
err = json.Unmarshal(bytes, ghissue)
if err != nil {
return nil, err
}
return ghissue, nil
}
func expandGithubIssue(c *irc.Connection, e *irc.Event) {
match := issueRegexp.FindString(e.Payload["message"])
issue := -1
if match != "" {
issue, _ = strconv.Atoi(match[1:])
}
if issue != -1 {
ghissue, err := getGithubIssue("MediaCrush", "MediaCrush", issue)
if err != nil {
return // Silently ignore errors
}
message := fmt.Sprintf(issueResponse, issue, ghissue.Title, ghissue.Html_url)
e.ReactToChannel(c, message)
}
}