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

fix(github): correctly return maxResults releases #1134

Merged
merged 3 commits into from
Dec 1, 2021
Merged
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
11 changes: 6 additions & 5 deletions src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,13 +576,15 @@ export class GitHub {
async *releaseIterator(maxResults: number = Number.MAX_SAFE_INTEGER) {
let results = 0;
let cursor: string | undefined = undefined;
while (results < maxResults) {
while (true) {
const response: ReleaseHistory | null = await this.releaseGraphQL(cursor);
if (!response) {
break;
}
for (let i = 0; i < response.data.length; i++) {
results += 1;
if ((results += 1) > maxResults) {
break;
}
yield response.data[i];
}
if (!response.pageInfo.hasNextPage) {
Expand Down Expand Up @@ -624,12 +626,11 @@ export class GitHub {
repo: this.repository.repo,
num: 25,
});
if (!response.repository.releases) {
if (!response.repository.releases.nodes.length) {
logger.warn('Could not find releases.');
return null;
}
const releases = (response.repository.releases.nodes ||
[]) as GraphQLRelease[];
const releases = response.repository.releases.nodes as GraphQLRelease[];
return {
pageInfo: response.repository.releases.pageInfo,
data: releases
Expand Down
15 changes: 10 additions & 5 deletions test/fixtures/releases.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"oid": "010b83b2a5c3d1bab0d6a5af028a288bc6dcb8a9"
},
"url": "https://github.com/googleapis/repo-automation-bots/releases/tag/flakybot-1.1.0",
"description": ""
"description": "",
"isDraft": true
},
{
"name": "@google-cloud/release-brancher release-brancher-v1.3.1",
Expand All @@ -22,7 +23,8 @@
"oid": "10675394580552bb73af8b4e1fcd87b759d3ccfd"
},
"url": "https://github.com/googleapis/repo-automation-bots/releases/tag/release-brancher-v1.3.1",
"description": "\n\n### Bug Fixes\n\n* **release-brancher:** use \"ci:\" (non-releasable) in init ([#2622](https://github.com/googleapis/repo-automation-bots/issues/2622)) ([c4b3ac5](https://github.com/googleapis/repo-automation-bots/commit/c4b3ac5e50f9579202a614c655e79eaf4bca362e))\n"
"description": "\n\n### Bug Fixes\n\n* **release-brancher:** use \"ci:\" (non-releasable) in init ([#2622](https://github.com/googleapis/repo-automation-bots/issues/2622)) ([c4b3ac5](https://github.com/googleapis/repo-automation-bots/commit/c4b3ac5e50f9579202a614c655e79eaf4bca362e))\n",
"isDraft": false
},
{
"name": "@google-cloud/release-brancher release-brancher-v1.3.0",
Expand All @@ -33,7 +35,8 @@
"oid": "3dde1e3df5aaae655bd8068fcb6cc70445f57512"
},
"url": "https://github.com/googleapis/repo-automation-bots/releases/tag/release-brancher-v1.3.0",
"description": "\n\n### Features\n\n* **release-brancher:** add argument to override pull request title ([#2615](https://github.com/googleapis/repo-automation-bots/issues/2615)) ([1f952c7](https://github.com/googleapis/repo-automation-bots/commit/1f952c73eba10bc8eedc5c5a16944622b8e84c46))\n\n\n### Bug Fixes\n\n* **release-brancher:** provide default branch name for PRs ([#2618](https://github.com/googleapis/repo-automation-bots/issues/2618)) ([24a15a8](https://github.com/googleapis/repo-automation-bots/commit/24a15a89a76e15153f0e6cce2c7d69d97464df5c))\n"
"description": "\n\n### Features\n\n* **release-brancher:** add argument to override pull request title ([#2615](https://github.com/googleapis/repo-automation-bots/issues/2615)) ([1f952c7](https://github.com/googleapis/repo-automation-bots/commit/1f952c73eba10bc8eedc5c5a16944622b8e84c46))\n\n\n### Bug Fixes\n\n* **release-brancher:** provide default branch name for PRs ([#2618](https://github.com/googleapis/repo-automation-bots/issues/2618)) ([24a15a8](https://github.com/googleapis/repo-automation-bots/commit/24a15a89a76e15153f0e6cce2c7d69d97464df5c))\n",
"isDraft": false
},
{
"name": "gcf-utils gcf-utils-v13.0.2",
Expand All @@ -44,7 +47,8 @@
"oid": "96496ea1233fc8decaea3049a9f529f1a3fd85e7"
},
"url": "https://github.com/googleapis/repo-automation-bots/releases/tag/gcf-utils-v13.0.2",
"description": "\n\n### Bug Fixes\n\n* **deps:** update dependency @googleapis/run to v5 ([#2527](https://github.com/googleapis/repo-automation-bots/issues/2527)) ([439484f](https://github.com/googleapis/repo-automation-bots/commit/439484fbf9fdb2545581472e5e88aa7c5bc8f0d9))\n"
"description": "\n\n### Bug Fixes\n\n* **deps:** update dependency @googleapis/run to v5 ([#2527](https://github.com/googleapis/repo-automation-bots/issues/2527)) ([439484f](https://github.com/googleapis/repo-automation-bots/commit/439484fbf9fdb2545581472e5e88aa7c5bc8f0d9))\n",
"isDraft": false
},
{
"name": "@google-automations/cron-utils cron-utils-v3.0.1",
Expand All @@ -55,7 +59,8 @@
"oid": "96496ea1233fc8decaea3049a9f529f1a3fd85e7"
},
"url": "https://github.com/googleapis/repo-automation-bots/releases/tag/cron-utils-v3.0.1",
"description": "\n\n### Bug Fixes\n\n* **deps:** update dependency @googleapis/run to v5 ([#2527](https://github.com/googleapis/repo-automation-bots/issues/2527)) ([439484f](https://github.com/googleapis/repo-automation-bots/commit/439484fbf9fdb2545581472e5e88aa7c5bc8f0d9))\n"
"description": "\n\n### Bug Fixes\n\n* **deps:** update dependency @googleapis/run to v5 ([#2527](https://github.com/googleapis/repo-automation-bots/issues/2527)) ([439484f](https://github.com/googleapis/repo-automation-bots/commit/439484fbf9fdb2545581472e5e88aa7c5bc8f0d9))\n",
"isDraft": false
}
],
"pageInfo": {
Expand Down
37 changes: 37 additions & 0 deletions test/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,43 @@ describe('GitHub', () => {
}
expect(releases).lengthOf(5);
});

it('iterates through up to 3 releases', async () => {
const graphql = JSON.parse(
readFileSync(resolve(fixturesPath, 'releases.json'), 'utf8')
);
req.post('/graphql').reply(200, {
data: graphql,
});
const generator = github.releaseIterator(3);
const releases: GitHubRelease[] = [];
for await (const release of generator) {
releases.push(release);
}
expect(releases).lengthOf(3);
});

it('iterates through a result withouth releases', async () => {
req.post('/graphql').reply(200, {
data: {
repository: {
releases: {
nodes: [],
pageInfo: {
endCursor: null,
hasNextPage: false,
},
},
},
},
});
const generator = github.releaseIterator();
const releases: GitHubRelease[] = [];
for await (const release of generator) {
releases.push(release);
}
expect(releases).lengthOf(0);
});
});

describe('createRelease', () => {
Expand Down