-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
docs_issue_generation.go
864 lines (819 loc) · 26.2 KB
/
docs_issue_generation.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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
// Copyright 2022 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"regexp"
"strconv"
"strings"
"time"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
)
// docsIssue contains details about each formatted commit to be committed to the docs repo.
type docsIssue struct {
sourceCommitSha string
title string
body string
labels []string
}
// parameters stores the GitHub API token, a dry run flag to output the issues it would create, and the
// start and end times of the search.
type parameters struct {
Token string // GitHub API token
DryRun bool
StartTime time.Time
EndTime time.Time
}
// pageInfo contains pagination information for querying the GraphQL API.
type pageInfo struct {
HasNextPage bool `json:"hasNextPage"`
EndCursor string `json:"endCursor"`
}
// gqlCockroachPRCommit contains details about commits within PRs in the cockroach repo.
type gqlCockroachPRCommit struct {
Data struct {
Repository struct {
PullRequest struct {
Commits struct {
Edges []struct {
Node struct {
Commit struct {
Oid string `json:"oid"`
MessageHeadline string `json:"messageHeadline"`
MessageBody string `json:"messageBody"`
} `json:"commit"`
} `json:"node"`
} `json:"edges"`
PageInfo pageInfo `json:"pageInfo"`
} `json:"commits"`
} `json:"pullRequest"`
} `json:"repository"`
} `json:"data"`
}
// gqlCockroachPR contains details about PRs within the cockroach repo.
type gqlCockroachPR struct {
Data struct {
Search struct {
Nodes []struct {
Title string `json:"title"`
Number int `json:"number"`
Body string `json:"body"`
BaseRefName string `json:"baseRefName"`
Commits struct {
Edges []struct {
Node struct {
Commit struct {
Oid string `json:"oid"`
MessageHeadline string `json:"messageHeadline"`
MessageBody string `json:"messageBody"`
} `json:"commit"`
} `json:"node"`
} `json:"edges"`
PageInfo pageInfo `json:"pageInfo"`
} `json:"commits"`
} `json:"nodes"`
PageInfo pageInfo `json:"pageInfo"`
} `json:"search"`
} `json:"data"`
}
// gqlDocsIssue contains details about existing issues within the docs repo.
type gqlDocsIssue struct {
Data struct {
Search struct {
Nodes []struct {
Number int `json:"number"`
Body string `json:"body"`
} `json:"nodes"`
PageInfo pageInfo `json:"pageInfo"`
} `json:"search"`
} `json:"data"`
}
type gqlSingleIssue struct {
Data struct {
Repository struct {
Issue struct {
Body string `json:"body"`
} `json:"issue"`
} `json:"repository"`
} `json:"data"`
}
// gqlDocsRepoLabels contains details about the labels within the cockroach repo. In order to create issues using the
// GraphQL API, we need to use the label IDs and no
type gqlDocsRepoLabels struct {
Data struct {
Repository struct {
ID string `json:"id"`
Labels struct {
Edges []struct {
Node struct {
Name string `json:"name"`
ID string `json:"id"`
} `json:"node"`
} `json:"edges"`
PageInfo pageInfo `json:"pageInfo"`
} `json:"labels"`
} `json:"repository"`
} `json:"data"`
}
type gqlCreateIssueMutation struct {
Data struct {
CreateIssue struct {
ClientMutationID interface{} `json:"clientMutationId"`
} `json:"createIssue"`
} `json:"data"`
}
type cockroachPR struct {
Title string `json:"title"`
Number int `json:"number"`
Body string `json:"body"`
BaseRefName string `json:"baseRefName"`
Commits []cockroachCommit
}
type cockroachCommit struct {
Sha string `json:"oid"`
MessageHeadline string `json:"messageHeadline"`
MessageBody string `json:"messageBody"`
}
type epicIssueRefInfo struct {
epicRefs map[string]int
epicNone bool
issueCloseRefs map[string]int
issueInformRefs map[string]int
isBugFix bool
}
// Regex components for finding and validating issue and epic references in a string
var (
ghIssuePart = `(#\d+)` // e.g., #12345
ghIssueRepoPart = `([\w.-]+[/][\w.-]+#\d+)` // e.g., cockroachdb/cockroach#12345
ghURLPart = `(https://github.com/[-a-z0-9]+/[-._a-z0-9/]+/issues/\d+)` // e.g., https://github.com/cockroachdb/cockroach/issues/12345
jiraIssuePart = `([[:alpha:]]+-\d+)` // e.g., DOC-3456
exalateJiraRefPart = `Jira issue: ` + jiraIssuePart // e.g., Jira issue: CRDB-54321
jiraBaseUrlPart = "https://cockroachlabs.atlassian.net/browse/"
jiraURLPart = jiraBaseUrlPart + jiraIssuePart // e.g., https://cockroachlabs.atlassian.net/browse/DOC-3456
issueRefPart = ghIssuePart + "|" + ghIssueRepoPart + "|" + ghURLPart + "|" + jiraIssuePart + "|" + jiraURLPart
afterRefPart = `[,.;]?(?:[ \t\n\r]+|$)`
)
// RegExes of each issue part
var (
ghIssuePartRE = regexp.MustCompile(ghIssuePart)
ghIssueRepoPartRE = regexp.MustCompile(ghIssueRepoPart)
ghURLPartRE = regexp.MustCompile(ghURLPart)
jiraIssuePartRE = regexp.MustCompile(jiraIssuePart)
jiraURLPartRE = regexp.MustCompile(jiraURLPart)
)
// Fully composed regexs used to match strings.
var (
fixIssueRefRE = regexp.MustCompile(`(?im)(?i:close[sd]?|fix(?:e[sd])?|resolve[sd]?):?\s+(?:(?:` + issueRefPart + `)` + afterRefPart + ")+")
informIssueRefRE = regexp.MustCompile(`(?im)(?:part of|see also|informs):?\s+(?:(?:` + issueRefPart + `)` + afterRefPart + ")+")
epicRefRE = regexp.MustCompile(`(?im)epic:?\s+(?:(?:` + jiraIssuePart + "|" + jiraURLPart + `)` + afterRefPart + ")+")
epicNoneRE = regexp.MustCompile(`(?im)epic:?\s+(?:(none)` + afterRefPart + ")+")
githubJiraIssueRefRE = regexp.MustCompile(issueRefPart)
jiraIssueRefRE = regexp.MustCompile(jiraIssuePart + "|" + jiraURLPart)
releaseNoteNoneRE = regexp.MustCompile(`(?i)release note:? [nN]one`)
allRNRE = regexp.MustCompile(`(?i)release note:? \(.*`)
nonBugFixRNRE = regexp.MustCompile(`(?i)release note:? \(([^b]|b[^u]|bu[^g]|bug\S|bug [^f]|bug f[^i]|bug fi[^x]).*`)
bugFixRNRE = regexp.MustCompile(`(?i)release note:? \(bug fix\):.*`)
releaseJustificationRE = regexp.MustCompile(`(?i)release justification:.*`)
prNumberRE = regexp.MustCompile(`Related PR: \[?https://github.com/cockroachdb/cockroach/pull/(\d+)\D`)
commitShaRE = regexp.MustCompile(`Commit: \[?https://github.com/cockroachdb/cockroach/commit/(\w+)\W`)
exalateJiraRefRE = regexp.MustCompile(`\\n` + exalateJiraRefPart + `\\n`)
)
const (
docsOrganization = "cockroachdb"
docsRepo = "docs"
)
// the heart of the script to fetch and manipulate all data and create the individual docs issues
func docsIssueGeneration(params parameters) {
repoID, labelMap, err := searchDocsRepoLabels(params.Token)
if err != nil {
fmt.Println(err)
}
prs, err := searchCockroachPRs(params.StartTime, params.EndTime, params.Token)
if err != nil {
fmt.Println(err)
}
docsIssues := constructDocsIssues(prs)
if params.DryRun {
fmt.Printf("Start time: %#v\n", params.StartTime.Format(time.RFC3339))
fmt.Printf("End time: %#v\n", params.EndTime.Format(time.RFC3339))
if len(docsIssues) > 0 {
fmt.Printf("Dry run is enabled. The following %d docs issue(s) would be created:\n", len(docsIssues))
fmt.Println(docsIssues)
} else {
fmt.Println("No docs issues need to be created.")
}
} else {
for _, di := range docsIssues {
err := di.createDocsIssues(params.Token, repoID, labelMap)
if err != nil {
fmt.Println(err)
}
}
}
}
// searchDocsRepoLabels passes in a GitHub API token and returns the repo ID of the docs repo as well as a map
// of all the labels and their respective label IDs in GitHub.
func searchDocsRepoLabels(token string) (string, map[string]string, error) {
var labels = map[string]string{}
var repoID string
repoID, hasNextPage, nextCursor, err := searchDocsRepoLabelsSingle("", labels, token)
if err != nil {
fmt.Println(err)
return "", nil, err
}
for hasNextPage {
_, hasNextPage, nextCursor, err = searchDocsRepoLabelsSingle(nextCursor, labels, token)
if err != nil {
fmt.Println(err)
return "", nil, err
}
}
return repoID, labels, nil
}
// searchDocsRepoLabelsSingle runs once per page of 100 labels within the docs repo. It returns the repo ID,
// whether or not there is another page after the one that just ran, the next cursor value used to query the next
// page, and any error.
func searchDocsRepoLabelsSingle(
cursor string, m map[string]string, token string,
) (string, bool, string, error) {
docsIssueGQLQuery := fmt.Sprintf(`query ($cursor: String) {
repository(owner: "%s", name: "%s") {
id
labels(first: 100, after: $cursor) {
edges {
node {
name
id
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
}`, docsOrganization, docsRepo)
var search gqlDocsRepoLabels
queryVariables := map[string]interface{}{}
if cursor != "" {
queryVariables["cursor"] = cursor
}
err := queryGraphQL(docsIssueGQLQuery, queryVariables, token, &search)
if err != nil {
fmt.Println(err)
return "", false, "", err
}
repoID := search.Data.Repository.ID
for _, x := range search.Data.Repository.Labels.Edges {
m[x.Node.Name] = x.Node.ID
}
pageInfo := search.Data.Repository.Labels.PageInfo
return repoID, pageInfo.HasNextPage, pageInfo.EndCursor, nil
}
// queryGraphQL is the function that interfaces directly with the GitHub GraphQL API. Given a query, variables, and
// token, it will return a struct containing the requested data or an error if one exists.
func queryGraphQL(
query string, queryVariables map[string]interface{}, token string, out interface{},
) error {
const graphQLURL = "https://api.github.com/graphql"
client := &http.Client{}
bodyInt := map[string]interface{}{
"query": query,
}
if queryVariables != nil {
bodyInt["variables"] = queryVariables
}
requestBody, err := json.Marshal(bodyInt)
if err != nil {
return err
}
req, err := http.NewRequest("POST", graphQLURL, bytes.NewBuffer(requestBody))
if err != nil {
return err
}
req.Header.Set("Authorization", "token "+token)
res, err := client.Do(req)
if err != nil {
return err
}
bs, err := io.ReadAll(res.Body)
if err != nil {
return err
}
// unmarshal (convert) the byte slice into an interface
var tmp interface{}
err = json.Unmarshal(bs, &tmp)
if err != nil {
fmt.Println("Error: unable to unmarshal JSON into an empty interface")
fmt.Println(string(bs[:]))
return err
}
err = json.Unmarshal(bs, out)
if err != nil {
return err
}
return nil
}
func searchCockroachPRs(
startTime time.Time, endTime time.Time, token string,
) ([]cockroachPR, error) {
prCommitsToExclude, err := searchDocsIssues(startTime, token)
if err != nil {
fmt.Println(err)
}
hasNextPage, nextCursor, prs, err := searchCockroachPRsSingle(startTime, endTime, "", prCommitsToExclude, token)
if err != nil {
fmt.Println(err)
return nil, err
}
result := prs
for hasNextPage {
hasNextPage, nextCursor, prs, err = searchCockroachPRsSingle(startTime, endTime, nextCursor, prCommitsToExclude, token)
if err != nil {
fmt.Println(err)
return nil, err
}
result = append(result, prs...)
}
return result, nil
}
// searchDocsIssues returns a map containing all the product change docs issues that have been created since the given
// start time. For reference, it's structured as map[crdb_pr_number]map[crdb_commit]docs_pr_number.
func searchDocsIssues(startTime time.Time, token string) (map[int]map[string]int, error) {
var result = map[int]map[string]int{}
hasNextPage, nextCursor, err := searchDocsIssuesSingle(startTime, "", result, token)
if err != nil {
fmt.Println(err)
return nil, err
}
for hasNextPage {
hasNextPage, nextCursor, err = searchDocsIssuesSingle(startTime, nextCursor, result, token)
if err != nil {
fmt.Println(err)
return nil, err
}
}
return result, nil
}
// searchDocsIssuesSingle searches one page of docs issues at a time. These docs issues will ultimately be excluded
// from the PRs through which we iterate to create new product change docs issues. This function returns a bool to
// check if there are more than 100 results, the cursor to query for the next page of results, and an error if
// one exists.
func searchDocsIssuesSingle(
startTime time.Time, cursor string, m map[int]map[string]int, token string,
) (bool, string, error) {
query := `query ($cursor: String, $ghSearchQuery: String!) {
search(first: 100, query: $ghSearchQuery, type: ISSUE, after: $cursor) {
nodes {
... on Issue {
number
body
}
}
pageInfo {
hasNextPage
endCursor
}
}
}`
var search gqlDocsIssue
today := timeutil.Now().Format(time.RFC3339)
queryVariables := map[string]interface{}{
"ghSearchQuery": fmt.Sprintf(`repo:%s/%s is:issue label:C-product-change created:%s..%s`, docsOrganization, docsRepo, startTime.Format(time.RFC3339), today),
}
if cursor != "" {
queryVariables["cursor"] = cursor
}
err := queryGraphQL(query, queryVariables, token, &search)
if err != nil {
fmt.Println(err)
return false, "", err
}
for _, x := range search.Data.Search.Nodes {
prNumber, commitSha, err := parseDocsIssueBody(x.Body)
if err != nil {
fmt.Println(err)
}
if prNumber != 0 && commitSha != "" {
_, ok := m[prNumber]
if !ok {
m[prNumber] = make(map[string]int)
}
m[prNumber][commitSha] = x.Number
}
}
pageInfo := search.Data.Search.PageInfo
return pageInfo.HasNextPage, pageInfo.EndCursor, nil
}
func parseDocsIssueBody(body string) (int, string, error) {
prMatches := prNumberRE.FindStringSubmatch(body)
if len(prMatches) < 2 {
return 0, "", fmt.Errorf("error: No PR number found in issue body")
}
prNumber, err := strconv.Atoi(prMatches[1])
if err != nil {
fmt.Println(err)
return 0, "", err
}
commitShaMatches := commitShaRE.FindStringSubmatch(body)
if len(commitShaMatches) < 2 {
return 0, "", fmt.Errorf("error: No commit SHA found in issue body")
}
return prNumber, commitShaMatches[1], nil
}
func searchCockroachPRsSingle(
startTime time.Time,
endTime time.Time,
cursor string,
prCommitsToExclude map[int]map[string]int,
token string,
) (bool, string, []cockroachPR, error) {
var search gqlCockroachPR
var result []cockroachPR
query := `query ($cursor: String, $ghSearchQuery: String!) {
search(first: 100, query: $ghSearchQuery, type: ISSUE, after: $cursor) {
nodes {
... on PullRequest {
title
number
body
baseRefName
commits(first: 100) {
edges {
node {
commit {
oid
messageHeadline
messageBody
}
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
}
pageInfo {
hasNextPage
endCursor
}
}
}`
queryVariables := map[string]interface{}{
"ghSearchQuery": fmt.Sprintf(
`repo:cockroachdb/cockroach is:pr is:merged merged:%s..%s`,
startTime.Format(time.RFC3339),
endTime.Format(time.RFC3339),
),
}
if cursor != "" {
queryVariables["cursor"] = cursor
}
err := queryGraphQL(query, queryVariables, token, &search)
if err != nil {
fmt.Println(err)
return false, "", nil, err
}
for _, x := range search.Data.Search.Nodes {
var commits []cockroachCommit
for _, y := range x.Commits.Edges {
matchingDocsIssue := prCommitsToExclude[x.Number][y.Node.Commit.Oid]
if nonBugFixRNRE.MatchString(y.Node.Commit.MessageBody) && matchingDocsIssue == 0 {
commit := cockroachCommit{
Sha: y.Node.Commit.Oid,
MessageHeadline: y.Node.Commit.MessageHeadline,
MessageBody: y.Node.Commit.MessageBody,
}
commits = append(commits, commit)
}
}
// runs if there are more than 100 results
if x.Commits.PageInfo.HasNextPage {
additionalCommits, err := searchCockroachPRCommits(x.Number, x.Commits.PageInfo.EndCursor, prCommitsToExclude, token)
if err != nil {
return false, "", nil, err
}
commits = append(commits, additionalCommits...)
}
if len(commits) > 0 {
pr := cockroachPR{
Number: x.Number,
Title: x.Title,
Body: x.Body,
BaseRefName: x.BaseRefName,
Commits: commits,
}
result = append(result, pr)
}
}
pageInfo := search.Data.Search.PageInfo
return pageInfo.HasNextPage, pageInfo.EndCursor, result, nil
}
func searchCockroachPRCommits(
pr int, cursor string, prCommitsToExclude map[int]map[string]int, token string,
) ([]cockroachCommit, error) {
hasNextPage, nextCursor, commits, err := searchCockroachPRCommitsSingle(pr, cursor, prCommitsToExclude, token)
if err != nil {
fmt.Println(err)
return nil, err
}
result := commits
for hasNextPage {
hasNextPage, nextCursor, commits, err = searchCockroachPRCommitsSingle(pr, nextCursor, prCommitsToExclude, token)
if err != nil {
fmt.Println(err)
return nil, err
}
result = append(result, commits...)
}
return result, nil
}
func searchCockroachPRCommitsSingle(
prNumber int, cursor string, prCommitsToExclude map[int]map[string]int, token string,
) (bool, string, []cockroachCommit, error) {
var result []cockroachCommit
var search gqlCockroachPRCommit
query := `query ($cursor: String, $prNumber: Int!) {
repository(owner: "cockroachdb", name: "cockroach") {
pullRequest(number: $prNumber) {
commits(first: 100, after: $cursor) {
edges {
node {
commit {
oid
messageHeadline
messageBody
}
}
}
}
}
}
}`
queryVariables := map[string]interface{}{
"prNumber": prNumber,
}
if cursor != "" {
queryVariables["cursor"] = cursor
}
err := queryGraphQL(
query,
queryVariables,
token,
&search,
)
if err != nil {
fmt.Println(err)
return false, "", nil, err
}
for _, x := range search.Data.Repository.PullRequest.Commits.Edges {
matchingDocsIssue := prCommitsToExclude[prNumber][x.Node.Commit.Oid]
if nonBugFixRNRE.MatchString(x.Node.Commit.MessageHeadline) && matchingDocsIssue == 0 {
commit := cockroachCommit{
Sha: x.Node.Commit.Oid,
MessageHeadline: x.Node.Commit.MessageHeadline,
MessageBody: x.Node.Commit.MessageHeadline,
}
result = append(result, commit)
}
}
pageInfo := search.Data.Repository.PullRequest.Commits.PageInfo
return pageInfo.HasNextPage, pageInfo.EndCursor, result, nil
}
// extractStringsFromMessage takes in a commit message or PR body as well as two regular expressions. The first
// regular expression checks for a valid formatted epic or issue reference. If one is found, it searches that exact
// string for the individual issue references. The output is a map where the key is each epic or issue ref and the
// value is the count of references of that ref.
func extractStringsFromMessage(
message string, firstMatch, secondMatch *regexp.Regexp,
) map[string]int {
ids := map[string]int{}
if allMatches := firstMatch.FindAllString(message, -1); len(allMatches) > 0 {
for _, x := range allMatches {
matches := secondMatch.FindAllString(x, -1)
for _, match := range matches {
ids[match]++
}
}
}
return ids
}
func extractFixIssueIDs(message string) map[string]int {
return extractStringsFromMessage(message, fixIssueRefRE, githubJiraIssueRefRE)
}
func extractInformIssueIDs(message string) map[string]int {
return extractStringsFromMessage(message, informIssueRefRE, githubJiraIssueRefRE)
}
func extractEpicIDs(message string) map[string]int {
return extractStringsFromMessage(message, epicRefRE, jiraIssueRefRE)
}
func containsEpicNone(message string) bool {
if allMatches := epicNoneRE.FindAllString(message, -1); len(allMatches) > 0 {
return true
}
return false
}
func containsBugFix(message string) bool {
if allMatches := bugFixRNRE.FindAllString(message, -1); len(allMatches) > 0 {
return true
}
return false
}
// getJiraIssueFromGitHubIssue takes a GitHub issue and returns the appropriate Jira key from the issue body.
func getJiraIssueFromGitHubIssue(org, repo string, issue int, token string) string {
query := `query ($org: String!, $repo: String!, $issue: Int!) {
repository(owner: $org, name: $repo) {
issue(number: $issue) {
body
}
}
}`
var search gqlSingleIssue
queryVariables := map[string]interface{}{
"org": org,
"repo": repo,
"issue": issue,
}
err := queryGraphQL(query, queryVariables, token, &search)
if err != nil {
fmt.Println(err)
return ""
}
var jiraIssue string
exalateRef := exalateJiraRefRE.FindString(search.Data.Repository.Issue.Body)
if len(exalateRef) > 0 {
jiraIssue = jiraIssuePartRE.FindString(exalateRef)
}
return jiraIssue
}
func getJiraIssueFromRef(ref string) string {
if ghURLPartRE.MatchString(ref) || jiraURLPartRE.MatchString(ref) {
return ref
} else if ghIssueRepoPartRE.MatchString(ref) {
split := strings.Split(ref, "#")
return "https://github.com/" + split[0] + "/issues/" + split[1]
} else if ghIssuePartRE.MatchString(ref) {
return "https://github.com/cockroachdb/cockroach/issues/" + strings.Replace(ref, "#", "", 1)
} else if jiraIssuePartRE.MatchString(ref) {
return "https://cockroachlabs.atlassian.net/browse/" + ref
} else {
return "Malformed epic/issue ref (" + ref + ")"
}
}
func extractIssueEpicRefs(prBody, commitBody string) string {
refInfo := epicIssueRefInfo{
epicRefs: extractEpicIDs(commitBody + "\n" + prBody),
epicNone: containsEpicNone(commitBody + "\n" + prBody),
issueCloseRefs: extractFixIssueIDs(commitBody + "\n" + prBody),
issueInformRefs: extractInformIssueIDs(commitBody + "\n" + prBody),
isBugFix: containsBugFix(commitBody + "\n" + prBody),
}
var builder strings.Builder
if len(refInfo.epicRefs) > 0 {
builder.WriteString("Epic:")
for x := range refInfo.epicRefs {
builder.WriteString(" " + getJiraIssueFromRef(x))
}
builder.WriteString("\n")
}
if len(refInfo.issueCloseRefs) > 0 {
builder.WriteString("Fixes:")
for x := range refInfo.issueCloseRefs {
builder.WriteString(" " + getJiraIssueFromRef(x))
}
builder.WriteString("\n")
}
if len(refInfo.issueInformRefs) > 0 {
builder.WriteString("Informs:")
for x := range refInfo.issueInformRefs {
builder.WriteString(" " + getJiraIssueFromRef(x))
}
builder.WriteString("\n")
}
if refInfo.epicNone && builder.Len() == 0 {
builder.WriteString("Epic: none\n")
}
return builder.String()
}
// getIssues takes a list of commits from GitHub as well as the PR number associated with those commits and outputs a
// formatted list of docs issues with valid release notes
func constructDocsIssues(prs []cockroachPR) []docsIssue {
var result []docsIssue
for _, pr := range prs {
for _, commit := range pr.Commits {
rns := formatReleaseNotes(commit.MessageBody, pr.Number, pr.Body, commit.Sha)
for i, rn := range rns {
x := docsIssue{
sourceCommitSha: commit.Sha,
title: formatTitle(commit.MessageHeadline, pr.Number, i+1, len(rns)),
body: rn,
labels: []string{
"C-product-change",
pr.BaseRefName,
},
}
result = append(result, x)
}
}
}
return result
}
func formatTitle(title string, prNumber int, index int, totalLength int) string {
result := fmt.Sprintf("PR #%d - %s", prNumber, title)
if totalLength > 1 {
result += fmt.Sprintf(" (%d of %d)", index, totalLength)
}
return result
}
// formatReleaseNotes generates a list of docsIssue bodies for the docs repo based on a given CRDB sha
func formatReleaseNotes(
commitMessage string, prNumber int, prBody string, crdbSha string,
) []string {
rnBodySlice := []string{}
if releaseNoteNoneRE.MatchString(commitMessage) {
return rnBodySlice
}
epicIssueRefs := extractIssueEpicRefs(prBody, commitMessage)
splitString := strings.Split(commitMessage, "\n")
releaseNoteLines := []string{}
var rnBody string
for _, x := range splitString {
validRn := allRNRE.MatchString(x)
bugFixRn := bugFixRNRE.MatchString(x)
releaseJustification := releaseJustificationRE.MatchString(x)
if len(releaseNoteLines) > 0 && (validRn || releaseJustification) {
rnBody = fmt.Sprintf(
"Related PR: https://github.com/cockroachdb/cockroach/pull/%s\n"+
"Commit: https://github.com/cockroachdb/cockroach/commit/%s\n"+
"%s\n---\n\n%s",
strconv.Itoa(prNumber),
crdbSha,
epicIssueRefs,
strings.Join(releaseNoteLines, "\n"),
)
rnBodySlice = append(rnBodySlice, strings.TrimSuffix(rnBody, "\n"))
rnBody = ""
releaseNoteLines = []string{}
}
if (validRn && !bugFixRn) || (len(releaseNoteLines) > 0 && !bugFixRn && !releaseJustification) {
releaseNoteLines = append(releaseNoteLines, x)
}
}
if len(releaseNoteLines) > 0 { // commit whatever is left in the buffer to the rnBodySlice set
rnBody = fmt.Sprintf(
"Related PR: https://github.com/cockroachdb/cockroach/pull/%s\n"+
"Commit: https://github.com/cockroachdb/cockroach/commit/%s\n"+
"%s\n---\n\n%s",
strconv.Itoa(prNumber),
crdbSha,
epicIssueRefs,
strings.Join(releaseNoteLines, "\n"),
)
rnBodySlice = append(rnBodySlice, strings.TrimSuffix(rnBody, "\n"))
}
return rnBodySlice
}
// TODO: Redo this function
func (di docsIssue) createDocsIssues(
token string, repoID string, labelMap map[string]string,
) error {
var output gqlCreateIssueMutation
mutation := `mutation ($repoId: ID!, $title: String!, $body: String!, $labelIds: [ID!]) {
createIssue(
input: {repositoryId: $repoId, title: $title, body: $body, labelIds: $labelIds}
) {
clientMutationId
}
}`
var labelIds []string
for _, x := range di.labels {
labelIds = append(labelIds, labelMap[x])
}
mutationVariables := map[string]interface{}{
"repoId": repoID,
"title": di.title,
"body": di.body,
}
if len(labelIds) > 0 {
mutationVariables["labelIds"] = labelIds
}
err := queryGraphQL(mutation, mutationVariables, token, &output)
if err != nil {
fmt.Println(err)
return err
}
return nil
}