-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
497 lines (476 loc) · 10.7 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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
package main
import (
"bytes"
"context"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"os"
"path/filepath"
"runtime"
"strings"
"text/template"
"github.com/shurcooL/githubv4"
"golang.org/x/oauth2"
"gopkg.in/yaml.v3"
)
var planningUpload = flag.Bool("planning", false, "upload planning data to Logilica")
var ciUpload = flag.Bool("ci", false, "upload CI build data to Logilica")
func main() {
flag.Parse()
if *planningUpload {
// client := CreateClient()
// LogilicaUploadPlanningData(client)
fmt.Println("Upload Planning data")
}
if *ciUpload {
fmt.Println("Upload CI Build data")
}
}
func CreateClient() *http.Client {
client := oauth2.NewClient(
context.TODO(),
oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: os.Getenv("TOKEN")},
))
return client
}
func LogilicaUploadPlanningData(client *http.Client) {
config := getOrganizations()
for _, org := range config.Organizations {
pid := CreateProjectIdOnLogilica(org.URL, org.Organization, org.Key)
gitbhubProjectId := GetProjectIdFromGithub(org.Organization, org.URL)
sprintInformation := GetSprintInformation(gitbhubProjectId, client)
CreateSprints(pid, sprintInformation) // creating a sprint on logilica
payload := PreparePayload(gitbhubProjectId, sprintInformation, client) // call graphql api's for preparing the payload
UploadPlanningData(pid, payload) // upload planning data call to logilica
}
}
func getOrganizations() *Config {
var c Config
_, b, _, _ := runtime.Caller(0)
basepath := filepath.Dir(b)
yamlFile, err := os.ReadFile(basepath + "/config/orgs.yaml")
if err != nil {
log.Printf("yamlFile.Get err #%v ", err)
}
fmt.Println(string(yamlFile))
if err := yaml.Unmarshal(yamlFile, &c); err != nil {
panic(err)
}
fmt.Println(c)
return &c
}
func CreateSprints(projectId string, sprintInformation SprintInformation) {
postBody, _ := json.Marshal(map[string]string{
"id": sprintInformation.id,
"name": sprintInformation.name,
"state": sprintInformation.state,
"goal": sprintInformation.goal,
})
logilicaUrl := fmt.Sprintf("https://logilica.io/api/import/v1/pm/%v/sprints", projectId)
contentType := "application/json"
client := &http.Client{}
req, err := http.NewRequest("POST", logilicaUrl, bytes.NewBuffer(postBody))
if err != nil {
fmt.Println(err)
}
req.Header.Add("Content-Type", contentType)
req.Header.Add("x-lgca-domain", "redhat")
req.Header.Add("X-lgca-token", os.Getenv("LOGILICA_TOKEN"))
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
}
fmt.Println(body)
}
func CreateProjectIdOnLogilica(url, organizationName, key string) string {
postBody, _ := json.Marshal(map[string]string{
"URL": url,
"orgName": organizationName,
"key": key,
})
logilicaUrl := "https://logilica.io/api/import/v1/pm/projects/create"
contentType := "application/json"
client := &http.Client{}
req, err := http.NewRequest("POST", logilicaUrl, bytes.NewBuffer(postBody))
if err != nil {
fmt.Println(err)
return ""
}
req.Header.Add("Content-Type", contentType)
req.Header.Add("x-lgca-domain", "redhat")
req.Header.Add("X-lgca-token", os.Getenv("LOGILICA_TOKEN"))
resp, err := client.Do(req)
if err != nil {
return ""
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return ""
}
fmt.Println(resp.StatusCode)
fmt.Println(string(body))
var projectResponse ProjectApiResponse
json.Unmarshal(body, &projectResponse)
return projectResponse.Id
}
func GetProjectIdFromGithub(organizationName string, url string) string {
var projectId string
src := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: os.Getenv("TOKEN")},
)
httpClient := oauth2.NewClient(context.Background(), src)
client := githubv4.NewClient(httpClient)
var listquery struct {
Organization struct {
Projectv2 struct {
Nodes []struct {
Id string
Title string
Url string
}
} `graphql:"projectsV2(first: 20)"`
} `graphql:"organization(login: $orgname)"`
}
variables := map[string]interface{}{
"orgname": githubv4.String(organizationName),
}
err := client.Query(context.Background(), &listquery, variables)
if err != nil {
fmt.Println(err)
}
nodes := listquery.Organization.Projectv2.Nodes
for _, node := range nodes {
if node.Url == url {
projectId = node.Id
break
}
}
return projectId
}
func PreparePayload(githubProjectId string, sprintInformation SprintInformation, client *http.Client) []Planning {
var projectQueryResponse ProjectQueryResponse
var payload []Planning
query := `node(id: "{{.Val}}") {
... on ProjectV2 {
id
title
items(first:100) {
totalCount
edges {
cursor
node {
itemID: id
type
status: fieldValueByName(name: "Status") {
# GitHub predefined a "Status" field, type ProjectV2ItemFieldSingleSelectValue, that cannot be deleted.
... on ProjectV2ItemFieldSingleSelectValue {
name
updatedAt
creator {
__typename
login
... on User {
login
name
}
... on Organization {
login
name
}
... on Bot {
login
}
... on EnterpriseUserAccount {
login
name
}
... on Mannequin {
login
email
}
}
}
}
content {
... on Issue {
__typename
title
author {
__typename
login
... on User {
login
name
}
... on Organization {
login
name
}
... on Bot {
login
}
... on EnterpriseUserAccount {
login
name
}
... on Mannequin {
login
email
}
}
body
bodyHTML
closed
closedAt
createdAt
id
number
lastEditedAt
milestone {
id
title
createdAt
}
state
stateReason
updatedAt
url
}
}
}
}
}
}
}
}`
// parametes to the query
ids := Parameters{githubProjectId}
tmpl, err := template.New("new").Parse(query)
buf := &bytes.Buffer{}
err = tmpl.Execute(buf, ids)
if err != nil {
panic(err)
}
gqlMarshalled, err := json.Marshal(graphQLRequest{Query: buf.String()})
if err != nil {
panic(err)
}
resp, err := client.Post("https://api.github.com/graphql", "application/json", strings.NewReader(string(gqlMarshalled)))
b, _ := httputil.DumpResponse(resp, true)
json.Unmarshal(b, &projectQueryResponse)
// get issue information
for _, edge := range projectQueryResponse.Data.Node.Items.Edges {
planningObj := Planning{
ID: edge.Node.Content.ID,
Origin: "GITHUB",
CreatedAt: int(edge.Node.Content.CreatedAt.Unix()),
Creator: Creator{
Name: edge.Node.Status.Creator.Name,
Email: edge.Node.Status.Creator.Login,
AccountID: "",
LastActivity: 0,
},
}
query = `node(id: "{{.Val}}") {
... on Issue {
id
title
timelineItems(first: 100) {
edges {
cursor
node {
... on ConvertedNoteToIssueEvent {
__typename
createdAt
actor {
login
... on User {
login
name
}
... on Organization {
login
name
}
... on Bot {
login
}
... on EnterpriseUserAccount {
login
name
}
... on Mannequin {
login
email
}
}
}
... on AddedToProjectEvent {
__typename
createdAt
actor {
__typename
login
... on User {
login
name
}
... on Organization {
login
name
}
... on Bot {
login
}
... on EnterpriseUserAccount {
login
name
}
... on Mannequin {
login
email
}
}
}
... on AssignedEvent {
__typename
createdAt
assignee {
__typename
... on User {
login
name
}
... on Organization {
login
name
}
... on Bot {
login
}
... on Mannequin {
login
email
}
}
actor {
__typename
... on User {
login
name
}
}
}
... on ClosedEvent {
__typename
createdAt
stateReason
actor {
__typename
... on User {
login
name
}
}
}
}
}
}
}
}
}`
// parametes to the query
ids = Parameters{edge.Node.Content.ID}
tmpl, err = template.New("new").Parse(query)
buf = &bytes.Buffer{}
err = tmpl.Execute(buf, ids)
if err != nil {
panic(err)
}
gqlMarshalled, err = json.Marshal(graphQLRequest{Query: buf.String()})
if err != nil {
panic(err)
}
resp, err = client.Post("https://api.github.com/graphql", "application/json", strings.NewReader(string(gqlMarshalled)))
b, _ = httputil.DumpResponse(resp, true)
json.Unmarshal(b, &projectQueryResponse)
payload = append(payload, planningObj)
}
return payload
}
func GetSprintInformation(gitbhubProjectId string, client *http.Client) SprintInformation {
var sprintInformation SprintInformation
query := `node(id: "{{.Val}}") {
... on ProjectV2 {
items(last: 20) {
nodes {
id
fieldValues(first: 20) {
nodes {
... on ProjectV2ItemFieldIterationValue {
iterationId
title
startDate
updatedAt
duration
}
}
}
}
}
}
}
}`
// parametes to the query
ids := Parameters{gitbhubProjectId}
tmpl, err := template.New("new").Parse(query)
buf := &bytes.Buffer{}
err = tmpl.Execute(buf, ids)
if err != nil {
panic(err)
}
gqlMarshalled, err := json.Marshal(graphQLRequest{Query: buf.String()})
if err != nil {
panic(err)
}
resp, err := client.Post("https://api.github.com/graphql", "application/json", strings.NewReader(string(gqlMarshalled)))
b, _ := httputil.DumpResponse(resp, true)
json.Unmarshal(b, &sprintInformation)
return sprintInformation
}
func UploadPlanningData(projectId string, payload []Planning) {
postBody, _ := json.Marshal(payload)
logilicaUrl := fmt.Sprintf("https://logilica.io/api/import/v1/pm/%v/issues/create", projectId)
contentType := "application/json"
client := &http.Client{}
req, err := http.NewRequest("POST", logilicaUrl, bytes.NewBuffer(postBody))
if err != nil {
fmt.Println(err)
}
req.Header.Add("Content-Type", contentType)
req.Header.Add("x-lgca-domain", "redhat")
req.Header.Add("X-lgca-token", os.Getenv("LOGILICA_TOKEN"))
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
}
fmt.Println(body)
}