Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iterate over all pages until github returns an empty array #82

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,32 @@ describe('Hide Comments', () => {
const client = new Client('secrets', 1, 'owner', 'repo')
const github = nock('https://api.github.com')
.get(`/repos/owner/repo/issues/1/comments`)
.query(true)
.reply(200, listComment)
.get(`/repos/owner/repo/issues/1/comments`)
.query(true)
.reply(200, [])

const response = await client.SelectComments(`bot`)

expect(response).toStrictEqual(['hide me'])
})

it('should paginate properly', async () => {
const client = new Client('secrets', 1, 'owner', 'repo')
const numPages = 5
const github = nock('https://api.github.com')
.get(`/repos/owner/repo/issues/1/comments`)
.query(true)
.times(numPages)
.reply(200, listComment)
.get(`/repos/owner/repo/issues/1/comments`)
.query(true)
.reply(200, [])

const response = await client.SelectComments(`bot`)
const expected = new Array(numPages).fill('hide me')

expect(response).toStrictEqual(expected)
})
})
30 changes: 21 additions & 9 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

35 changes: 25 additions & 10 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as core from '@actions/core'
import * as github from '@actions/github'

import {GitHub} from '@actions/github/lib/utils'
Expand Down Expand Up @@ -35,18 +36,32 @@ export class Client {
}

async SelectComments(userName: string): Promise<string[]> {
const resp = await this.octokit.rest.issues.listComments({
owner: this.owner,
repo: this.repo,
issue_number: this.issueNumber
})

const ids: string[] = []
for (const r of resp.data) {
if (r.user !== null && r.user.login !== userName) {
continue

// continually page through comments ...
// 3k comments is an absurd amount, feels like a pretty decent default limit
const maxPages = 100
for (let page = 1; page <= maxPages; page++) {
const resp = await this.octokit.rest.issues.listComments({
owner: this.owner,
repo: this.repo,
issue_number: this.issueNumber,
page
})

// ... until we've read them all
if (!resp.data || resp.data.length === 0) {
break
}

core.debug(`page ${page} contained ${resp.data.length} entries`)

for (const r of resp.data) {
if (r.user !== null && r.user.login !== userName) {
continue
}
ids.push(r.node_id)
}
ids.push(r.node_id)
}
return new Promise<string[]>(resolve => resolve(ids))
}
Expand Down