From 48ab0d2f2e77ae169182d022591ef5c18c931ff2 Mon Sep 17 00:00:00 2001 From: Sam Lanning Date: Mon, 26 Aug 2024 17:38:29 +0100 Subject: [PATCH] Switch to pull request list API instead of search (#393) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 Co-authored-by: willo-icon <161356817+willo-icon@users.noreply.github.com> --- .changeset/clean-bees-count.md | 5 +++++ .changeset/green-eels-appear.md | 5 +++++ src/run.test.ts | 24 ++++++------------------ src/run.ts | 17 ++++++++++------- 4 files changed, 26 insertions(+), 25 deletions(-) create mode 100644 .changeset/clean-bees-count.md create mode 100644 .changeset/green-eels-appear.md diff --git a/.changeset/clean-bees-count.md b/.changeset/clean-bees-count.md new file mode 100644 index 00000000..27f95943 --- /dev/null +++ b/.changeset/clean-bees-count.md @@ -0,0 +1,5 @@ +--- +"@changesets/action": patch +--- + +Ensure the PR remains open when updated diff --git a/.changeset/green-eels-appear.md b/.changeset/green-eels-appear.md new file mode 100644 index 00000000..f2d1609f --- /dev/null +++ b/.changeset/green-eels-appear.md @@ -0,0 +1,5 @@ +--- +"@changesets/action": patch +--- + +Switch to cheaper API for querying existing PRs diff --git a/src/run.test.ts b/src/run.test.ts index ff9ccfdf..15d2e096 100644 --- a/src/run.test.ts +++ b/src/run.test.ts @@ -33,11 +33,9 @@ jest.mock("@actions/github/lib/utils", () => ({ jest.mock("./gitUtils"); let mockedGithubMethods = { - search: { - issuesAndPullRequests: jest.fn(), - }, pulls: { create: jest.fn(), + list: jest.fn(), }, repos: { createRelease: jest.fn(), @@ -65,9 +63,7 @@ describe("version", () => { let cwd = f.copy("simple-project"); linkNodeModules(cwd); - mockedGithubMethods.search.issuesAndPullRequests.mockImplementationOnce( - () => ({ data: { items: [] } }) - ); + mockedGithubMethods.pulls.list.mockImplementationOnce(() => ({ data: [] })); mockedGithubMethods.pulls.create.mockImplementationOnce(() => ({ data: { number: 123 }, @@ -104,9 +100,7 @@ describe("version", () => { let cwd = f.copy("simple-project"); linkNodeModules(cwd); - mockedGithubMethods.search.issuesAndPullRequests.mockImplementationOnce( - () => ({ data: { items: [] } }) - ); + mockedGithubMethods.pulls.list.mockImplementationOnce(() => ({ data: [] })); mockedGithubMethods.pulls.create.mockImplementationOnce(() => ({ data: { number: 123 }, @@ -139,9 +133,7 @@ describe("version", () => { let cwd = f.copy("ignored-package"); linkNodeModules(cwd); - mockedGithubMethods.search.issuesAndPullRequests.mockImplementationOnce( - () => ({ data: { items: [] } }) - ); + mockedGithubMethods.pulls.list.mockImplementationOnce(() => ({ data: [] })); mockedGithubMethods.pulls.create.mockImplementationOnce(() => ({ data: { number: 123 }, @@ -174,9 +166,7 @@ describe("version", () => { let cwd = f.copy("simple-project"); linkNodeModules(cwd); - mockedGithubMethods.search.issuesAndPullRequests.mockImplementationOnce( - () => ({ data: { items: [] } }) - ); + mockedGithubMethods.pulls.list.mockImplementationOnce(() => ({ data: [] })); mockedGithubMethods.pulls.create.mockImplementationOnce(() => ({ data: { number: 123 }, @@ -233,9 +223,7 @@ fluminis divesque vulnere aquis parce lapsis rabie si visa fulmineis. let cwd = f.copy("simple-project"); linkNodeModules(cwd); - mockedGithubMethods.search.issuesAndPullRequests.mockImplementationOnce( - () => ({ data: { items: [] } }) - ); + mockedGithubMethods.pulls.list.mockImplementationOnce(() => ({ data: [] })); mockedGithubMethods.pulls.create.mockImplementationOnce(() => ({ data: { number: 123 }, diff --git a/src/run.ts b/src/run.ts index ea346bf6..326b9618 100644 --- a/src/run.ts +++ b/src/run.ts @@ -342,9 +342,11 @@ export async function runVersion({ }); } - let searchQuery = `repo:${repo}+state:open+head:${versionBranch}+base:${branch}+is:pull-request`; - let searchResultPromise = octokit.rest.search.issuesAndPullRequests({ - q: searchQuery, + const existingPullRequestsPromise = octokit.rest.pulls.list({ + ...github.context.repo, + state: "open", + head: `${github.context.repo.owner}:${versionBranch}`, + base: branch, }); let changedPackages = await getChangedPackages(cwd, versionsByDirectory); let changedPackagesInfoPromises = Promise.all( @@ -376,8 +378,8 @@ export async function runVersion({ await gitUtils.push(versionBranch, { force: true }); - let searchResult = await searchResultPromise; - core.info(JSON.stringify(searchResult.data, null, 2)); + let existingPullRequests = await existingPullRequestsPromise; + core.info(JSON.stringify(existingPullRequests.data, null, 2)); const changedPackagesInfo = (await changedPackagesInfoPromises) .filter((x) => x) @@ -391,7 +393,7 @@ export async function runVersion({ prBodyMaxCharacters, }); - if (searchResult.data.items.length === 0) { + if (existingPullRequests.data.length === 0) { core.info("creating pull request"); const { data: newPullRequest } = await octokit.rest.pulls.create({ base: branch, @@ -405,7 +407,7 @@ export async function runVersion({ pullRequestNumber: newPullRequest.number, }; } else { - const [pullRequest] = searchResult.data.items; + const [pullRequest] = existingPullRequests.data; core.info(`updating found pull request #${pullRequest.number}`); await octokit.rest.pulls.update({ @@ -413,6 +415,7 @@ export async function runVersion({ title: finalPrTitle, body: prBody, ...github.context.repo, + state: "open", }); return {