Skip to content

Commit 48ab0d2

Browse files
s0Andaristwillo-icon
authored
Switch to pull request list API instead of search (#393)
* Switch to pull request list API instead of search To reduce the amount of rate-limit that we use, switch to a cheaper API for determining whether an existing PR exists for a given branch. * Ensure PR remains open when updating description * Update .changeset/clean-bees-count.md Co-authored-by: willo-icon <161356817+willo-icon@users.noreply.github.com> --------- Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com> Co-authored-by: willo-icon <161356817+willo-icon@users.noreply.github.com>
1 parent 89b41e6 commit 48ab0d2

File tree

4 files changed

+26
-25
lines changed

4 files changed

+26
-25
lines changed

.changeset/clean-bees-count.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@changesets/action": patch
3+
---
4+
5+
Ensure the PR remains open when updated

.changeset/green-eels-appear.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@changesets/action": patch
3+
---
4+
5+
Switch to cheaper API for querying existing PRs

src/run.test.ts

+6-18
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,9 @@ jest.mock("@actions/github/lib/utils", () => ({
3333
jest.mock("./gitUtils");
3434

3535
let mockedGithubMethods = {
36-
search: {
37-
issuesAndPullRequests: jest.fn(),
38-
},
3936
pulls: {
4037
create: jest.fn(),
38+
list: jest.fn(),
4139
},
4240
repos: {
4341
createRelease: jest.fn(),
@@ -65,9 +63,7 @@ describe("version", () => {
6563
let cwd = f.copy("simple-project");
6664
linkNodeModules(cwd);
6765

68-
mockedGithubMethods.search.issuesAndPullRequests.mockImplementationOnce(
69-
() => ({ data: { items: [] } })
70-
);
66+
mockedGithubMethods.pulls.list.mockImplementationOnce(() => ({ data: [] }));
7167

7268
mockedGithubMethods.pulls.create.mockImplementationOnce(() => ({
7369
data: { number: 123 },
@@ -104,9 +100,7 @@ describe("version", () => {
104100
let cwd = f.copy("simple-project");
105101
linkNodeModules(cwd);
106102

107-
mockedGithubMethods.search.issuesAndPullRequests.mockImplementationOnce(
108-
() => ({ data: { items: [] } })
109-
);
103+
mockedGithubMethods.pulls.list.mockImplementationOnce(() => ({ data: [] }));
110104

111105
mockedGithubMethods.pulls.create.mockImplementationOnce(() => ({
112106
data: { number: 123 },
@@ -139,9 +133,7 @@ describe("version", () => {
139133
let cwd = f.copy("ignored-package");
140134
linkNodeModules(cwd);
141135

142-
mockedGithubMethods.search.issuesAndPullRequests.mockImplementationOnce(
143-
() => ({ data: { items: [] } })
144-
);
136+
mockedGithubMethods.pulls.list.mockImplementationOnce(() => ({ data: [] }));
145137

146138
mockedGithubMethods.pulls.create.mockImplementationOnce(() => ({
147139
data: { number: 123 },
@@ -174,9 +166,7 @@ describe("version", () => {
174166
let cwd = f.copy("simple-project");
175167
linkNodeModules(cwd);
176168

177-
mockedGithubMethods.search.issuesAndPullRequests.mockImplementationOnce(
178-
() => ({ data: { items: [] } })
179-
);
169+
mockedGithubMethods.pulls.list.mockImplementationOnce(() => ({ data: [] }));
180170

181171
mockedGithubMethods.pulls.create.mockImplementationOnce(() => ({
182172
data: { number: 123 },
@@ -233,9 +223,7 @@ fluminis divesque vulnere aquis parce lapsis rabie si visa fulmineis.
233223
let cwd = f.copy("simple-project");
234224
linkNodeModules(cwd);
235225

236-
mockedGithubMethods.search.issuesAndPullRequests.mockImplementationOnce(
237-
() => ({ data: { items: [] } })
238-
);
226+
mockedGithubMethods.pulls.list.mockImplementationOnce(() => ({ data: [] }));
239227

240228
mockedGithubMethods.pulls.create.mockImplementationOnce(() => ({
241229
data: { number: 123 },

src/run.ts

+10-7
Original file line numberDiff line numberDiff line change
@@ -342,9 +342,11 @@ export async function runVersion({
342342
});
343343
}
344344

345-
let searchQuery = `repo:${repo}+state:open+head:${versionBranch}+base:${branch}+is:pull-request`;
346-
let searchResultPromise = octokit.rest.search.issuesAndPullRequests({
347-
q: searchQuery,
345+
const existingPullRequestsPromise = octokit.rest.pulls.list({
346+
...github.context.repo,
347+
state: "open",
348+
head: `${github.context.repo.owner}:${versionBranch}`,
349+
base: branch,
348350
});
349351
let changedPackages = await getChangedPackages(cwd, versionsByDirectory);
350352
let changedPackagesInfoPromises = Promise.all(
@@ -376,8 +378,8 @@ export async function runVersion({
376378

377379
await gitUtils.push(versionBranch, { force: true });
378380

379-
let searchResult = await searchResultPromise;
380-
core.info(JSON.stringify(searchResult.data, null, 2));
381+
let existingPullRequests = await existingPullRequestsPromise;
382+
core.info(JSON.stringify(existingPullRequests.data, null, 2));
381383

382384
const changedPackagesInfo = (await changedPackagesInfoPromises)
383385
.filter((x) => x)
@@ -391,7 +393,7 @@ export async function runVersion({
391393
prBodyMaxCharacters,
392394
});
393395

394-
if (searchResult.data.items.length === 0) {
396+
if (existingPullRequests.data.length === 0) {
395397
core.info("creating pull request");
396398
const { data: newPullRequest } = await octokit.rest.pulls.create({
397399
base: branch,
@@ -405,14 +407,15 @@ export async function runVersion({
405407
pullRequestNumber: newPullRequest.number,
406408
};
407409
} else {
408-
const [pullRequest] = searchResult.data.items;
410+
const [pullRequest] = existingPullRequests.data;
409411

410412
core.info(`updating found pull request #${pullRequest.number}`);
411413
await octokit.rest.pulls.update({
412414
pull_number: pullRequest.number,
413415
title: finalPrTitle,
414416
body: prBody,
415417
...github.context.repo,
418+
state: "open",
416419
});
417420

418421
return {

0 commit comments

Comments
 (0)