Skip to content

Commit 9138386

Browse files
authored
Merge pull request #178 from snaka/fix-unstable-search-api-behavior
fix: Unstable search issue behavior
2 parents 866dae6 + 0b66380 commit 9138386

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

tagpr.go

+21-11
Original file line numberDiff line numberDiff line change
@@ -606,28 +606,38 @@ func (tp *tagpr) generatenNextLabels(prIssues []*github.Issue) []string {
606606
return nextLabels
607607
}
608608

609-
func buildChunkSearchIssuesQuery(queryBase string, shasStr string) (chunkQueries []string) {
610-
query := queryBase
609+
func buildChunkSearchIssuesQuery(qualifiers string, shasStr string) (chunkQueries []string) {
610+
// Longer than 256 characters are not supported in the query.
611+
// ref. https://docs.github.com/en/rest/reference/search#limitations-on-query-length
612+
//
613+
// However, although not explicitly stated in the documentation, the space separating
614+
// keywords is counted as one or more characters, so it is possible to exceed 256
615+
// characters if the text is filled to the very limit of 256 characters.
616+
// For this reason, the maximum number of chars in the KEYWORD section is limited to
617+
// the following number.
618+
const maxKeywordsLength = 200
619+
620+
// array of SHAs
621+
keywords := make([]string, 0, 25)
611622
// Make bulk requests with multiple SHAs of the maximum possible length.
612623
// If multiple SHAs are specified, the issue search API will treat it like an OR search,
613-
// and all the pull requests will be searched.u
624+
// and all the pull requests will be searched.
614625
// This is difficult to read from the current documentation, but that is the current
615626
// behavior and GitHub support has responded that this is the spec.
616627
for _, sha := range strings.Split(shasStr, "\n") {
617628
if strings.TrimSpace(sha) == "" {
618629
continue
619630
}
620-
// Longer than 256 characters are not supported in the query.
621-
// ref. https://docs.github.com/en/rest/reference/search#limitations-on-query-length
622-
if len(query)+1+len(sha) >= 256 {
623-
chunkQueries = append(chunkQueries, query)
624-
query = queryBase
631+
tempKeywords := append(keywords, sha)
632+
if len(strings.Join(tempKeywords, " ")) >= maxKeywordsLength {
633+
chunkQueries = append(chunkQueries, qualifiers + " " + strings.Join(keywords, " "))
634+
keywords = make([]string, 0, 25)
625635
}
626-
query += " " + sha
636+
keywords = append(keywords, sha)
627637
}
628638

629-
if query != queryBase {
630-
chunkQueries = append(chunkQueries, query)
639+
if len(keywords) > 0 {
640+
chunkQueries = append(chunkQueries, qualifiers + " " + strings.Join(keywords, " "))
631641
}
632642

633643
return chunkQueries

tagpr_test.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ def3db8
100100
c0fc143
101101
`,
102102
[]string{
103-
"repo:Songmu/tagpr is:pr is:closed 1a8bb97 1b7691b a9462b9 4d2b5e9 9ce4268 1eccbf8 1c3fbfc 968ade5 531c782 780bb71 6025fbf cc369ba a1f3e39 792bc85 3e3c4e1 37832de ac97702 d742186 217eb5d 0f900f7 5ef33d1 1d2ec15 2f37752 066ad7b 2e19b14 52b3706 f5134ae",
104-
"repo:Songmu/tagpr is:pr is:closed ea39bbf 76b0630 ee3c6e6 2336be4 423a209 63caa74 3296052 3c98d78 86b8739 2264ec5 5c1d87b 4ffe09c 7c5d0de 3de9ed0 1b6b58c 2b643ec 53bf089 e8e96d5 3dac4b0 0605ba4 86cb76d 358c7c1 a139f86 33c16b6 c91f8ff a109671 b4029bd",
105-
"repo:Songmu/tagpr is:pr is:closed f985b4f b74ef35 53d9ab3 6f57b07 0a84d90 43aa57d 75b6f79 def3db8 c0fc143",
103+
"repo:Songmu/tagpr is:pr is:closed 1a8bb97 1b7691b a9462b9 4d2b5e9 9ce4268 1eccbf8 1c3fbfc 968ade5 531c782 780bb71 6025fbf cc369ba a1f3e39 792bc85 3e3c4e1 37832de ac97702 d742186 217eb5d 0f900f7 5ef33d1 1d2ec15 2f37752 066ad7b 2e19b14",
104+
"repo:Songmu/tagpr is:pr is:closed 52b3706 f5134ae ea39bbf 76b0630 ee3c6e6 2336be4 423a209 63caa74 3296052 3c98d78 86b8739 2264ec5 5c1d87b 4ffe09c 7c5d0de 3de9ed0 1b6b58c 2b643ec 53bf089 e8e96d5 3dac4b0 0605ba4 86cb76d 358c7c1 a139f86",
105+
"repo:Songmu/tagpr is:pr is:closed 33c16b6 c91f8ff a109671 b4029bd f985b4f b74ef35 53d9ab3 6f57b07 0a84d90 43aa57d 75b6f79 def3db8 c0fc143",
106106
},
107107
},
108108
{
@@ -132,11 +132,9 @@ d742186
132132
2f37752
133133
066ad7b
134134
2e19b14
135-
52b3706
136-
f5134ae
137135
`,
138136
[]string{
139-
"repo:Songmu/tagpr is:pr is:closed 1a8bb97 1b7691b a9462b9 4d2b5e9 9ce4268 1eccbf8 1c3fbfc 968ade5 531c782 780bb71 6025fbf cc369ba a1f3e39 792bc85 3e3c4e1 37832de ac97702 d742186 217eb5d 0f900f7 5ef33d1 1d2ec15 2f37752 066ad7b 2e19b14 52b3706 f5134ae",
137+
"repo:Songmu/tagpr is:pr is:closed 1a8bb97 1b7691b a9462b9 4d2b5e9 9ce4268 1eccbf8 1c3fbfc 968ade5 531c782 780bb71 6025fbf cc369ba a1f3e39 792bc85 3e3c4e1 37832de ac97702 d742186 217eb5d 0f900f7 5ef33d1 1d2ec15 2f37752 066ad7b 2e19b14",
140138
},
141139
},
142140
}

0 commit comments

Comments
 (0)