Skip to content

Commit 77277ee

Browse files
committed
pr: Do not exceed GitHub comment mention limit
Fixes: https://trac.macports.org/ticket/63821
1 parent a79d1c8 commit 77277ee

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

pr/webhook/pull_request.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,17 +117,37 @@ func (receiver *Receiver) processPullRequest(event *github.PullRequestEvent) {
117117
mentionSymbol = "@"
118118
}
119119
if len(handles) > 0 && !strings.Contains(*event.PullRequest.Body, "[skip notification]") {
120-
body := "Notifying maintainers:\n"
120+
// GitHub will only notify the first 50 handles in a single comment
121+
const mentionLimit = 50
122+
// i counts from 0 to len(handles)
123+
// Use (i % mentionLimit) to limit each comment to containing
124+
// at most (mentionLimit) handles
125+
i := 0
126+
totalBatches := (len(handles) + mentionLimit - 1) / mentionLimit
127+
var body string
121128
for handle, ports := range handles {
129+
// Increment counter for each handle
130+
i++
131+
// Initialize body if first handle for this batch
132+
if (i % mentionLimit) == 1 {
133+
currentBatch := (i + mentionLimit - 1) / mentionLimit
134+
body = "Notifying maintainers (batch " + strconv.Itoa(currentBatch) +
135+
" of " + strconv.Itoa(totalBatches) + "):\n"
136+
}
122137
body += mentionSymbol + handle + " for port " + strings.Join(ports, ", ") + ".\n"
123138
err = receiver.githubClient.AddAssignees(owner, repo, number, []string{handle})
124139
if err != nil {
125140
log.Println(err)
126141
}
127-
}
128-
err = receiver.githubClient.CreateComment(owner, repo, number, &body)
129-
if err != nil {
130-
log.Println(err)
142+
// Create comment if last handle for this batch
143+
// (either once body contains (mentionLimit) handles,
144+
// or when there are no handles left)
145+
if ((i % mentionLimit) == 0) || (i == len(handles)) {
146+
err = receiver.githubClient.CreateComment(owner, repo, number, &body)
147+
if err != nil {
148+
log.Println(err)
149+
}
150+
}
131151
}
132152
}
133153

0 commit comments

Comments
 (0)