-
Notifications
You must be signed in to change notification settings - Fork 134
/
models.go
105 lines (97 loc) · 2.7 KB
/
models.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
package models
import (
"encoding/json"
"errors"
"fmt"
"github.com/diggerhq/digger/libs/orchestrator/github/models"
"github.com/google/go-github/v55/github"
)
type GithubAction struct {
Action string `json:"action"`
ActionPath string `json:"action_path"`
ActionRef string `json:"action_ref"`
ActionRepository string `json:"action_repository"`
ActionStatus string `json:"action_status"`
Actor string `json:"actor"`
BaseRef string `json:"base_ref"`
Env string `json:"env"`
Event interface{} `json:"event"`
EventName string `json:"event_name"`
EventPath string `json:"event_path"`
Path string `json:"path"`
RefType string `json:"ref_type"`
Repository string `json:"repository"`
RepositoryOwner string `json:"repository_owner"`
}
func (g *GithubAction) ToEventPackage() models.EventPackage {
return models.EventPackage{
Event: g.Event,
EventName: g.EventName,
Actor: g.Actor,
Repository: g.Repository,
}
}
func (g *GithubAction) UnmarshalJSON(data []byte) error {
type Alias GithubAction
aux := struct {
*Alias
}{
Alias: (*Alias)(g),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
var rawEvent json.RawMessage
auxEvent := struct {
Event *json.RawMessage `json:"event"`
}{
Event: &rawEvent,
}
if err := json.Unmarshal(data, &auxEvent); err != nil {
return err
}
switch g.EventName {
case "pull_request":
var event github.PullRequestEvent
if err := json.Unmarshal(rawEvent, &event); err != nil {
return err
}
g.Event = event
case "issue_comment":
var event github.IssueCommentEvent
if err := json.Unmarshal(rawEvent, &event); err != nil {
return err
}
g.Event = event
case "push":
var event github.PushEvent
if err := json.Unmarshal(rawEvent, &event); err != nil {
return err
}
g.Event = event
case "workflow_dispatch":
var event github.WorkflowDispatchEvent
if err := json.Unmarshal(rawEvent, &event); err != nil {
return err
}
g.Event = event
case "schedule":
// TODO: Find appropriate event for workflow scheduled run
var event github.WorkflowDispatchEvent
if err := json.Unmarshal(rawEvent, &event); err != nil {
return err
}
g.Event = event
default:
return errors.New("unknown GitHub event: " + g.EventName)
}
return nil
}
func GetGitHubContext(ghContext string) (*GithubAction, error) {
parsedGhContext := new(GithubAction)
err := json.Unmarshal([]byte(ghContext), &parsedGhContext)
if err != nil {
return &GithubAction{}, fmt.Errorf("error parsing GitHub context JSON: %v", err)
}
return parsedGhContext, nil
}