Skip to content

Commit a7b075d

Browse files
committedAug 2, 2024··
fix: Unstable search behaviour
According to the GitHub API specification, The structure of a query consists of one or more KEYWORDs and one one more QUALIFIERs, as follows. ``` KEYWORD_1 KEYWORD_2 QUALIFIER_1 QUALIFIER_2 ``` The character limit of query does not apply to QUALIFIERs, so the part excluding the QUALIFIERs must be validated. See-also: https://docs.github.com/en/rest/search/search?apiVersion=2022-11-28#limitations-on-query-length
1 parent 7e9b8d6 commit a7b075d

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed
 

‎tagpr.go

+15-9
Original file line numberDiff line numberDiff line change
@@ -606,28 +606,34 @@ 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+
// array of SHAs
611+
keywords := make([]string, 0, 25)
611612
// Make bulk requests with multiple SHAs of the maximum possible length.
612613
// 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
614+
// and all the pull requests will be searched.
614615
// This is difficult to read from the current documentation, but that is the current
615616
// behavior and GitHub support has responded that this is the spec.
616617
for _, sha := range strings.Split(shasStr, "\n") {
617618
if strings.TrimSpace(sha) == "" {
618619
continue
619620
}
620621
// Longer than 256 characters are not supported in the query.
622+
// Note that the length limit does not include the qualifiers (ex. "repo:owner/repo", "is:close").
621623
// 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
624+
//
625+
// Also, from the results of the experiment, it is possible that when counting
626+
// the number of characters in the keyword part, one space character is counted
627+
// as three characters (possibly '%20').
628+
if len(strings.Join(keywords, "%20") + "%20" + sha) >= 256 {
629+
chunkQueries = append(chunkQueries, qualifiers + " " + strings.Join(keywords, " "))
630+
keywords = make([]string, 0, 25)
625631
}
626-
query += " " + sha
632+
keywords = append(keywords, sha)
627633
}
628634

629-
if query != queryBase {
630-
chunkQueries = append(chunkQueries, query)
635+
if len(keywords) > 0 {
636+
chunkQueries = append(chunkQueries, qualifiers + " " + strings.Join(keywords, " "))
631637
}
632638

633639
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)