-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvalidate.go
57 lines (50 loc) · 1.71 KB
/
validate.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
package main
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"regexp"
"github.com/Sirupsen/logrus"
)
var badgeMinimum int
const inadequateBadge = "badges/inadequate.svg"
const adequateBadge = "badges/adequate.svg"
var badgeRex = regexp.MustCompile(`(?i)(\[!\[[a-z0-9_ .]+\]\([0-9a-z.\/:&?=-]+\)\]\([0-9a-z.\/:&?-]+\))`)
func checkBadges(username string, reponame string, branch string) ([]string, error) {
githubRepo := fmt.Sprintf("github.com/%s/%s", username, reponame)
if branch == "" {
branch = "master"
}
resp, err := http.Get(fmt.Sprintf("https://raw.githubusercontent.com/%s/%s/%s/README.md", username, reponame, branch))
if err != nil {
// handle error
logrus.Debugf("could not fetch github repo: %s - err: %v", githubRepo, err)
return []string{}, err
}
logrus.Debug("resp: ", resp)
if resp.StatusCode != 200 {
logrus.Debugf("Github repo %s not found", githubRepo)
return []string{}, errors.New("github repo/branch not found")
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
// handle error
logrus.Debugf("could not read body from %s: %v", githubRepo, err)
return []string{}, err
}
logrus.Debug("body: ", string(body))
// The `All` variants of these functions apply to all
// matches in the input, not just the first. For
// example to find all matches for a regexp.
badgeMatch := badgeRex.FindAllString(string(body), -1)
logrus.Debug(badgeMatch)
logrus.Infof("Badge count: %v - min: %v", len(badgeMatch), badgeMinimum)
if len(badgeMatch) >= badgeMinimum {
logrus.Info("Congrats you haz all badges")
return append([]string{adequateBadge}, badgeMatch...), nil
}
logrus.Info("Needz moar badges")
return append([]string{inadequateBadge}, badgeMatch...), nil
}