-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
128 lines (111 loc) · 3.57 KB
/
main.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"context"
"crypto/rand"
"encoding/json"
"errors"
"io/ioutil"
"log"
"os"
"time"
"github.com/getsentry/sentry-go"
"github.com/google/go-github/v32/github"
"github.com/hashicorp/go-multierror"
"github.com/sethvargo/go-githubactions"
"golang.org/x/oauth2"
)
type config struct {
githubToken string
sentryDSN string
sentryEnvironment string
sentryRelease string
sentryDebug bool
event *CompleteWorkflowRunEvent
}
type actionsService interface {
ListWorkflowJobs(ctx context.Context, owner, repo string, runID int64, opts *github.ListWorkflowJobsOptions) (*github.Jobs, *github.Response, error)
}
type CompleteWorkflowRunEvent struct {
github.WorkflowRunEvent
Action *string `json:"action,omitempty"`
WorkflowRun *github.WorkflowRun `json:"workflow_run,omitempty"`
Workflow *github.Workflow `json:"workflow,omitempty"`
}
func main() {
c := &config{
githubToken: githubactions.GetInput("GITHUB_TOKEN"),
sentryDSN: githubactions.GetInput("SENTRY_DSN"),
sentryEnvironment: githubactions.GetInput("SENTRY_ENVIRONMENT"),
sentryRelease: githubactions.GetInput("SENTRY_RELEASE"),
sentryDebug: githubactions.GetInput("SENTRY_DEBUG") == "true",
}
eventString, err := ioutil.ReadFile(os.Getenv("GITHUB_EVENT_PATH"))
if err != nil {
githubactions.Fatalf("Couldn't read event: %+v", err)
}
jsonErr := json.Unmarshal(eventString, &c.event)
if jsonErr != nil {
githubactions.Fatalf("failed to validate input: %+v", err)
}
validateErr := c.Validate()
if validateErr != nil {
githubactions.Fatalf("failed to validate input: %+v", validateErr)
}
ctx := context.Background()
defer sentry.RecoverWithContext(ctx)
client := c.githubClient(ctx)
sentryEvent, eventError := sentryEventFromWorkflowRun(ctx, c.event, client.Actions, rand.Reader)
if eventError != nil {
githubactions.Fatalf("failed creating event from actions run: %+v", eventError)
}
log.Printf("%#v", sentryEvent)
sentrySyncTransport := sentry.NewHTTPSyncTransport()
sentrySyncTransport.Timeout = time.Second * 30
sentryErr := sentry.Init(sentry.ClientOptions{
Dsn: c.sentryDSN,
Environment: c.sentryEnvironment,
Release: c.sentryRelease,
Debug: c.sentryDebug,
Transport: sentrySyncTransport,
AttachStacktrace: false,
})
if sentryErr != nil {
githubactions.Fatalf("sentry.Init: %s", sentryErr)
}
id := sentry.CaptureEvent(sentryEvent)
if id == nil {
githubactions.Fatalf("failed reporting event %+v", sentryEvent)
}
if sentryEvent.Level == sentry.LevelError {
sentryEvent.Type = ""
sentryEvent.Transaction = ""
sentryEvent.Spans = []*sentry.Span{}
id := sentry.CaptureEvent(sentryEvent)
if id == nil {
githubactions.Fatalf("failed reporting event %+v", sentryEvent)
}
}
}
func (c *config) Validate() error {
var resultErr *multierror.Error
if c.githubToken == "" {
resultErr = multierror.Append(resultErr, errors.New("input 'GITHUB_TOKEN' missing"))
}
if c.sentryDSN == "" {
resultErr = multierror.Append(resultErr, errors.New("input 'SENTRY_DSN' missing"))
}
if c.sentryEnvironment == "" {
resultErr = multierror.Append(resultErr, errors.New("input 'SENTRY_ENVIRONMENT' missing"))
}
if c.sentryRelease == "" {
resultErr = multierror.Append(resultErr, errors.New("input 'SENTRY_RELEASE' missing"))
}
return resultErr.ErrorOrNil()
}
func (c *config) githubClient(ctx context.Context) *github.Client {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: c.githubToken},
)
tc := oauth2.NewClient(ctx, ts)
return github.NewClient(tc)
}