This repository has been archived by the owner on Sep 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgithub.go
111 lines (88 loc) · 2.76 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
// Copyright 2014-2015 Chadev. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"github.com/danryan/hal"
)
var sourceHandler = hear(`source(.*)`, "source", "Give the URL to the named GitHub repo", func(res *hal.Response) error {
URL, err := getGitHubURL(strings.TrimSpace(res.Match[1]))
if err != nil {
hal.Logger.Error(fmt.Sprintf("unable to get GitHub URL: %v\n", err))
return res.Send(fmt.Sprintf("Fetching URL for %s failed, possibly misspelled or not a Chadev repo?", res.Match[1]))
}
return res.Send(URL)
})
var issueHandler = hear(`issue(.*)`, "issue", "Give the URL to the issue queue for the named GitHub repo", func(res *hal.Response) error {
args := make([]string, 2)
if res.Match[1] != "" {
res.Match[1] = strings.TrimSpace(res.Match[1]) // trim leading and tailing whitespace from the match
// check if we have a project name and issue nubmer "projectName issueID"
if strings.Contains(res.Match[1], " ") {
args = strings.Split(res.Match[1], " ")
} else {
args[0] = res.Match[1]
args[1] = ""
}
}
URL, err := getIssueURL(args[0])
if err != nil {
hal.Logger.Error(fmt.Sprintf("unable to get issue URL: %v\n", err))
return res.Send(fmt.Sprintf("Fetching issue queue URL for %s failed, possibly misspelled or not a Chadev repo?", args[0]))
}
if args[1] != "" {
URL, err = getIssueIDURL(URL, strings.TrimLeft(args[1], "#"))
if err != nil {
hal.Logger.Error(fmt.Sprintf("unable to get issue URL: %v\n", err))
return res.Send(fmt.Sprintf("Fetching issue URL for issue %s failed", args[1]))
}
}
return res.Send(URL)
})
func getGitHubURL(s string) (string, error) {
if s == "" {
s = "Chadev_ircbot"
}
hal.Logger.Info(s)
// build the GitHub URL
URL := fmt.Sprintf("https://github.com/chadev/%s", url.QueryEscape(s))
if !validateGitHubURL(URL) {
return "", errors.New("unable to get GitHub URL: no repo with URL: " + URL)
}
return URL, nil
}
func getIssueURL(s string) (string, error) {
if s == "" {
s = "Chadev_ircbot"
}
// build the URL
URL := fmt.Sprintf("https://github.com/chadev/%s/issues", url.QueryEscape(s))
if !validateGitHubURL(URL) {
return "", errors.New("unable to get GitHub URL: no repo with URL: " + URL)
}
return URL, nil
}
func getIssueIDURL(u, i string) (string, error) {
// build the URL
URL := fmt.Sprintf("%s/%s", u, i)
if !validateGitHubURL(URL) {
return "", errors.New("unable to get issue URL: no repo or issue with URL: " + URL)
}
return URL, nil
}
func validateGitHubURL(u string) bool {
// check if the URL is valid
resp, err := http.Get(u)
if err != nil {
return false
}
if resp.StatusCode != 200 {
return false
}
return true
}