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

Switch to pull request list API instead of search #393

Merged
merged 3 commits into from
Aug 26, 2024
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
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