This repository has been archived by the owner on Mar 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
event.go
85 lines (75 loc) · 2.14 KB
/
event.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
package handler
import (
"errors"
"github.com/google/go-github/v24/github"
)
var (
ErrEventNotSupported = errors.New("Event not supported")
ErrEventInvalid = errors.New("Not a valid event")
)
type Event struct {
Type string
Action string
Revision string
Ref string
Before string
*github.Repository
}
func (e *Event) Annotations() map[string]string {
return map[string]string{
annotationPrefix + "event_type": e.Type,
annotationPrefix + "event_action": e.Action,
annotationPrefix + "repo_name": *e.Repository.FullName,
annotationPrefix + "repo_url": *e.Repository.GitURL,
annotationPrefix + "repo_ssh": *e.Repository.SSHURL,
annotationPrefix + "ref": e.Ref,
annotationPrefix + "revision": e.Revision,
annotationPrefix + "before": e.Before,
}
}
func ParseEvent(ev interface{}) (*Event, error) {
event := &Event{}
switch e := ev.(type) {
case *github.PushEvent:
event.Type = "push"
event.Repository = pushEventRepoToRepo(e.GetRepo())
event.Revision = *e.After
event.Ref = *e.Ref
event.Before = *e.Before
case *github.DeleteEvent:
event.Type = "delete"
event.Repository = e.GetRepo()
event.Ref = formatRef(*e.RefType, *e.Ref)
case *github.CheckRunEvent:
event.Type = "check_run"
event.Action = *e.Action
event.Repository = e.GetRepo()
event.Revision = *e.CheckRun.HeadSHA
event.Ref = branchToRef(*e.CheckRun.CheckSuite.HeadBranch)
case *github.CheckSuiteEvent:
event.Type = "check_suite"
event.Action = *e.Action
event.Repository = e.GetRepo()
event.Revision = *e.CheckSuite.AfterSHA
event.Before = *e.CheckSuite.BeforeSHA
event.Ref = branchToRef(*e.CheckSuite.HeadBranch)
}
return event, nil
}
func formatRef(refType, ref string) string {
if refType == "branch" {
refType = "head"
}
return "refs/" + refType + "s/" + ref
}
func branchToRef(branch string) string {
return "refs/heads/" + branch
}
// FIXME: We should translate all fields or clean that mess up at upstream.
func pushEventRepoToRepo(r *github.PushEventRepository) *github.Repository {
return &github.Repository{
FullName: r.FullName,
GitURL: r.GitURL,
SSHURL: r.SSHURL,
}
}