-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathci.go
206 lines (145 loc) · 3.99 KB
/
ci.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
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
205
206
package main
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"github.com/google/go-github/v39/github"
"golang.org/x/oauth2"
)
var githubToken string
var ciMode bool
var repo string = "cryogenicplanet.github.io"
var owner string = "CryogenicPlanet"
var issue int = 49 // ISSUE AND PR ARE THE SAME FOR GITHUB API PURPOSES
var markdownString string
type Issue struct {
Number int `json:"number"`
}
// Type for this payload are defined here
// https://github.com/actions/toolkit/blob/e2eeb0a784f4067a75f0c6cd2cc9703f3cbc7744/packages/github/src/interfaces.ts#L15
type Payload struct {
Issues Issue `json:"issues"`
PullRequest Issue `json:"pull_request"`
}
type IssueComment struct {
Body string `json:"body"`
Id int64 `json:"id"`
}
const DEEP_REPORT_TITLE = "# Depp Report"
func checkIfPublic() bool {
isPublic := os.Getenv("IS_PUBLIC")
if isPublic != "" {
// Public repo no need to proxy
return true
}
return false
}
func checkPrComments() int64 {
client := github.NewClient(nil)
ctx := context.Background()
var response *http.Response
if checkIfPublic() {
// Public repo no need to proxy
issueData, _, err := client.Issues.Get(ctx, owner, repo, issue)
check(err)
issueUrl := issueData.GetCommentsURL()
link, err := url.Parse(issueUrl)
check(err)
client.BareDo(ctx, &http.Request{Method: "GET", URL: link})
} else {
query := url.Values{}
query.Add("repo", repo)
query.Add("issue", fmt.Sprint(issue))
query.Add("owner", owner)
// fmt.Printf("%+v\n", issueData)
link, err := url.Parse("https://depp-serverless.vercel.app/api/proxy/issueComments?" + query.Encode())
check(err)
response, err = http.Get(link.String())
check(err)
}
body, err := ioutil.ReadAll(response.Body)
check(err)
issueComments := []IssueComment{}
json.Unmarshal(body, &issueComments)
for _, comment := range issueComments {
if strings.Contains(comment.Body, DEEP_REPORT_TITLE) {
return comment.Id
}
}
return -1
}
func makePrComment(deployUrl string) {
setGithubRepoFromEnv()
setIssueNumberFromEnv()
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: githubToken},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
body := DEEP_REPORT_TITLE + "\n Report Deploy URL is " + deployUrl + "\n \n" + markdownString
commentId := checkPrComments()
if checkIfPublic() {
if commentId == -1 {
_, _, err := client.Issues.CreateComment(ctx, owner, repo, issue, &github.IssueComment{Body: &body})
check(err)
} else {
_, _, err := client.Issues.EditComment(ctx, owner, repo, commentId, &github.IssueComment{Body: &body})
check(err)
}
} else {
query := url.Values{}
query.Add("repo", repo)
if commentId == -1 {
query.Add("issue", fmt.Sprint(issue))
} else {
query.Add("commentId", fmt.Sprint(commentId))
}
query.Add("owner", owner)
query.Add("commentBody", body)
link, err := url.Parse("https://depp-serverless.vercel.app/api/proxy/updateComment?" + query.Encode())
check(err)
resp, err := http.Get(link.String())
if err != nil {
fmt.Println("[WARN]", err)
}
if resp.StatusCode > 300 {
fmt.Println("Failed response", resp.Status, resp)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("[Error]", err)
}
fmt.Println("Failed to comment on PR :(", owner, repo, issue)
panic(string(body))
}
}
fmt.Println("Commented on PR!", owner, repo, issue)
}
func setGithubRepoFromEnv() {
repoUrl := os.Getenv("GITHUB_REPOSITORY")
if repoUrl == "" {
panic("ENV GITHUB_REPOSITORY not found, do not use -ci in local environment")
}
splits := strings.Split(repoUrl, "/")
owner = splits[0]
repo = splits[1]
}
func setIssueNumberFromEnv() {
prNumber := os.Getenv("PR_NUMBER")
if prNumber == "" {
panic("ENV PR_NUMBER not set, please set ENV PR_NUMBER when using -ci")
}
no, err := strconv.Atoi(prNumber)
if err != nil {
// handle error
fmt.Println(err)
os.Exit(2)
}
issue = no
}