Skip to content

Commit

Permalink
Switch to pull request list API instead of search (#393)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

---------

Co-authored-by: Mateusz Burzyński <[email protected]>
Co-authored-by: willo-icon <[email protected]>
  • Loading branch information
3 people authored Aug 26, 2024
1 parent 89b41e6 commit 48ab0d2
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 25 deletions.
5 changes: 5 additions & 0 deletions .changeset/clean-bees-count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@changesets/action": patch
---

Ensure the PR remains open when updated
5 changes: 5 additions & 0 deletions .changeset/green-eels-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@changesets/action": patch
---

Switch to cheaper API for querying existing PRs
24 changes: 6 additions & 18 deletions src/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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 },
Expand Down Expand Up @@ -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 },
Expand Down Expand Up @@ -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 },
Expand Down Expand Up @@ -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 },
Expand Down Expand Up @@ -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 },
Expand Down
17 changes: 10 additions & 7 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand All @@ -405,14 +407,15 @@ 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({
pull_number: pullRequest.number,
title: finalPrTitle,
body: prBody,
...github.context.repo,
state: "open",
});

return {
Expand Down

0 comments on commit 48ab0d2

Please sign in to comment.