-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathissuer.go
More file actions
204 lines (182 loc) · 5.52 KB
/
issuer.go
File metadata and controls
204 lines (182 loc) · 5.52 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package prod_error_2_github_issue
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
"log"
"os"
"regexp"
"strconv"
"time"
)
type PubSubMessage struct {
Data []byte `json:"data"`
}
type ProductionError struct {
InsertId string `json:"insertId"`
JsonPayload struct {
Error string `json:"Error"`
Locals map[string]interface{} `json:"Locals"`
Stack string `json:"Stack"`
} `json:"jsonPayload"`
LogName string `json:"logName"`
ReceiveTimestamp time.Time `json:"receiveTimestamp"`
Resource struct {
Labels struct {
ConfigurationName string `json:"configuration_name"`
Location string `json:"location"`
ProjectId string `json:"project_id"`
RevisionName string `json:"revision_name"`
ServiceName string `json:"service_name"`
} `json:"labels"`
Type string `json:"type"`
} `json:"resource"`
Severity string `json:"severity"`
Timestamp time.Time `json:"timestamp"`
}
type Service struct {
ServiceName string `json:"serviceName"`
Repo string `json:"repo"`
}
type Issuer struct {
GithubClient *github.Client
GithubIssue *github.Issue
GithubOwner string
ProdError ProductionError
ServiceList []Service
ActualRepo string
ProductionType string
}
func init() {
_, isFound := os.LookupEnv("GITHUB_TOKEN")
if isFound == false {
log.Fatalln(errors.New("couldn't find a token"))
}
_, isFound = os.LookupEnv("GITHUB_OWNER")
if isFound == false {
log.Fatalln(errors.New("couldn't find owner"))
}
reposList := make([]Service, 0)
err := json.Unmarshal([]byte(os.Getenv("GITHUB_SERVICES")), &reposList)
if err != nil || len(reposList) == 0 {
log.Fatalln(errors.New("could not parse GITHUB_SERVICES or count of services = 0. Check provided value"))
}
}
func CreateGithubIssue(ctx context.Context, m PubSubMessage) error {
issuer := Issuer{}
issuer.initGithubClient(ctx)
issuer.buildIssueFromErrorMessage(m)
ok := issuer.findActualRepo()
if !ok {
log.Fatalln(`issuerError: Possible reasons:
- Repository was not found. Please check map, contains repos names
- Couldn't parse PubSub message successfully'`)
}
existingIssue, err := issuer.getExistingIssue(ctx)
if err != nil {
return err
}
if existingIssue != nil {
return issuer.updateExistingIssue(ctx, existingIssue)
}
return issuer.publishNewIssue(ctx)
}
func (i *Issuer) initGithubClient(ctx context.Context) {
tc := oauth2.NewClient(ctx, oauth2.StaticTokenSource(&oauth2.Token{AccessToken: os.Getenv("GITHUB_TOKEN")}))
i.GithubClient = github.NewClient(tc)
i.GithubOwner = os.Getenv("GITHUB_OWNER")
i.ProductionType = os.Getenv("ENV_TYPE")
i.ServiceList = make([]Service, 0)
err := json.Unmarshal([]byte(os.Getenv("GITHUB_SERVICES")), &i.ServiceList)
if err != nil {
log.Fatalln(err)
}
//Check for correct GITHUB_OWNER and GITHUB_TOKEN
_, _, err = i.GithubClient.Repositories.List(ctx, i.GithubOwner, nil)
if err != nil {
log.Fatalln(err)
}
}
func (i *Issuer) buildIssueFromErrorMessage(m PubSubMessage) {
var issueTitle string
var issueBody string
var newError ProductionError
err := json.Unmarshal(m.Data, &newError)
if err == nil {
issueTitle = fmt.Sprintf("%s err: %s", i.ProductionType, newError.JsonPayload.Error)
beautifiedLocals, err := json.MarshalIndent(newError.JsonPayload.Locals, "", " ")
if err == nil {
issueBody = fmt.Sprintf("## Stack:\n```%s```\n## Locals:\n```%s\n```",
newError.JsonPayload.Stack, string(beautifiedLocals))
} else {
issueBody = fmt.Sprintf("## Stack:\n```%s```\n## Locals:\n(Couldn't jsonify properly...)\n```%s\n```",
newError.JsonPayload.Stack, newError.JsonPayload.Locals)
}
} else {
issueTitle = "Production error"
issueBody = string(m.Data)
}
i.ProdError = newError
i.GithubIssue = &github.Issue{Title: &issueTitle, Body: &issueBody}
}
func (i *Issuer) findActualRepo() bool {
for _, s := range i.ServiceList {
if s.ServiceName == i.ProdError.Resource.Labels.ServiceName {
i.ActualRepo = s.Repo
return true
}
}
return false
}
func (i *Issuer) getExistingIssue(ctx context.Context) (*github.Issue, error) {
issues, _, err := i.GithubClient.Issues.ListByRepo(ctx, i.GithubOwner, i.ActualRepo, nil)
if err != nil {
return nil, err
}
if len(issues) == 0 {
return nil, nil
}
re, _ := regexp.Compile(fmt.Sprintf(`%s \(\d*\)$`, *i.GithubIssue.Title))
for _, issue := range issues {
if re.MatchString(*issue.Title) {
return issue, nil
}
}
return nil, err
}
func (i *Issuer) updateExistingIssue(ctx context.Context, existingIssue *github.Issue) error {
newTitle, err := incrementCounter(*existingIssue.Title)
if err != nil {
return err
}
_, _, err = i.GithubClient.Issues.Edit(ctx, i.GithubOwner, i.ActualRepo, *existingIssue.Number,
&github.IssueRequest{
Title: &newTitle,
Body: existingIssue.Body,
})
return err
}
func (i *Issuer) publishNewIssue(ctx context.Context) error {
// add error counter to the title
*i.GithubIssue.Title = *i.GithubIssue.Title + ` (1)`
newIssue := github.IssueRequest{
Title: i.GithubIssue.Title,
Body: i.GithubIssue.Body,
}
_, _, err := i.GithubClient.Issues.Create(ctx, i.GithubOwner, i.ActualRepo, &newIssue)
return err
}
func incrementCounter(t string) (string, error) {
re, _ := regexp.Compile(`\(\d*\)$`)
res := re.FindStringSubmatchIndex(t)
counter, err := strconv.Atoi(t[res[0]+1 : res[1]-1])
if err != nil {
return "", err
}
counter = counter + 1
newTitle := t[:res[0]] + "(" + strconv.Itoa(counter) + ")"
return newTitle, nil
}