-
Notifications
You must be signed in to change notification settings - Fork 91
/
github.go
82 lines (75 loc) · 1.6 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
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
)
// Valid exposes a method to validate a type
type Valid interface {
OK() error
}
// DecodeJSON marshalls JSON into a struct
func DecodeJSON(r io.Reader, v interface{}) error {
err := json.NewDecoder(r).Decode(v)
if err != nil {
return err
}
obj, ok := v.(Valid)
if !ok {
return nil // no OK method
}
err = obj.OK()
if err != nil {
return err
}
return nil
}
// GithubResponse is for marshalling Github push event JSON payloads
type GithubResponse struct {
Compare string
Commits []struct {
Added []string `json:"added"`
ID string `json:"id"`
} `json:"commits"`
Repository struct {
Name string `json:"name"`
Owner struct {
Email interface{} `json:"email"`
Name string `json:"name"`
} `json:"owner"`
} `json:"repository"`
}
// OK validates a marshalled struct
func (g *GithubResponse) OK() error {
if len(g.Compare) < 1 {
return errors.New("missing compare URL")
}
if len(g.Commits) < 1 {
return errors.New("empty payload")
}
for _, c := range g.Commits {
if len(c.ID) < 1 {
return errors.New("missing commit ID")
}
}
if len(g.Repository.Name) < 1 {
return errors.New("missing repository name")
}
return nil
}
// getDiffURL returns the URL of the Github Diff API endpoint for a particular commit
func (g GithubResponse) getDiffURL(commitID string) string {
return fmt.Sprintf(
"%s/%s",
g.getDiffURLStem(),
commitID,
)
}
func (g *GithubResponse) getDiffURLStem() string {
return fmt.Sprintf(
"https://api.github.com/repos/%s/%s/commits",
g.Repository.Owner.Name,
g.Repository.Name,
)
}