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: replace github search api with graphql in success lifecycle method #857

Merged
merged 37 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
8929f7e
chore: add `loadAssociatedPRsQuery` helper to generate gql query
babblebey Jun 13, 2024
ce1c3e4
feat: implement graphql in associatedPRs fetch
babblebey Jun 13, 2024
d24079c
Merge branch 'master' into refactor/replace-search-api
babblebey Jun 13, 2024
76a2cd1
build: add `assert` to devDeps
babblebey Jun 13, 2024
b9bbd1f
do: extracted `loadAssociatedPRsQuery` to separate function
babblebey Jun 13, 2024
ca0bc4a
do (test): current attemption situation
babblebey Jun 13, 2024
f3be681
nits: prepend string `commit` to gal query aliases
babblebey Jun 14, 2024
1516ea3
nits: rename associatedPRs gql query builder helper
babblebey Jun 14, 2024
1eaffe0
Merge branch 'master' into refactor/replace-search-api
babblebey Jun 14, 2024
c58c3e2
fix: typo in `success.js`
babblebey Jun 14, 2024
6714074
fix: address test case `Add comment and labels to PRs associated with…
babblebey Jun 14, 2024
0299e79
do: address test case `Add comment and labels to PRs associated with …
babblebey Jun 14, 2024
e229ad3
do: address test case `Do not add comment and labels for unrelated PR…
babblebey Jun 14, 2024
813969b
do: address test case `Do not add comment and labels if no PR is asso…
babblebey Jun 14, 2024
b7963d7
do: address test case `Do not add comment and labels to PR/issues fro…
babblebey Jun 14, 2024
0d1f44f
do: address test case `Ignore missing and forbidden issues/PRs`
babblebey Jun 14, 2024
d8b5396
do: address test case `Add custom comment and labels`
babblebey Jun 14, 2024
703f7bb
do: address test case `Add custom label`
babblebey Jun 14, 2024
c4f5df3
do: address test case `Comment on issue/PR without ading a label`
babblebey Jun 14, 2024
5585c3b
do: address test case `Editing the release to include all release lin…
babblebey Jun 14, 2024
d336aa8
do: address test case `Editing the release to include all release lin…
babblebey Jun 14, 2024
4b7c9a9
do: address test case `Editing the release to include all release lin…
babblebey Jun 14, 2024
69e67cf
do: address other test case except "Make multiple search queries if n…
babblebey Jun 14, 2024
c909ed7
Merge branch 'refactor/replace-search-api' of https://github.com/sema…
babblebey Jun 14, 2024
96f5e3a
fix: address test case `Make multiple search queries if necessary`
babblebey Jun 14, 2024
db42a89
nits: make tiny changes in `success.js`
babblebey Jun 14, 2024
d97754d
fix: address `integration` tests
babblebey Jun 14, 2024
d268e51
Revert "build: add `assert` to devDeps"
babblebey Jun 14, 2024
6ca26fc
refactor: replace `isArray` method with `Object.values`
babblebey Jun 14, 2024
791dc93
fix(build): fix lint issues
babblebey Jun 14, 2024
5ebd038
chore: drop `getSearchQueries` integration from `success.js` and depr…
babblebey Jun 14, 2024
61eea50
Merge branch 'master' into refactor/replace-search-api
babblebey Jun 19, 2024
9b2a923
chore: remove deprecated `getSearchQueries` helper
babblebey Jun 19, 2024
9c58e02
fix: modify `buildAssociatedPRsQuery` returned string
babblebey Jun 21, 2024
737fd05
Merge branch 'master' into refactor/replace-search-api
babblebey Jun 24, 2024
807c400
feat: retrieve `body` property for `associatedPRs`
babblebey Jun 24, 2024
53c8024
Merge branch 'master' into refactor/replace-search-api
gr2m Jul 1, 2024
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
86 changes: 74 additions & 12 deletions lib/success.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isNil, uniqBy, template, flatten, isEmpty } from "lodash-es";
import { isNil, uniqBy, template, flatten, isEmpty, toArray } from "lodash-es";
import pFilter from "p-filter";
import AggregateError from "aggregate-error";
import issueParser from "issue-parser";
Expand Down Expand Up @@ -65,21 +65,62 @@ export default async function success(pluginConfig, context, { Octokit }) {
const releaseInfos = releases.filter((release) => Boolean(release.name));
const shas = commits.map(({ hash }) => hash);

const searchQueries = getSearchQueries(
`repo:${owner}/${repo}+type:pr+is:merged`,
shas,
).map(
async (q) =>
(await octokit.request("GET /search/issues", { q })).data.items,
);
// MOCK ONLY
// const mockReponse = {
// "data": {
// "repository": {
// "com1": {
// "associatedPullRequests": {
// "nodes": [
// {
// "number": 2
// }
// ]
// }
// },
// "com2": {
// "associatedPullRequests": {
// "nodes": [
// {
// "number": 1
// }
// ]
// }
// }
// }
// }
// };
// console.log(somedata)

const { data } = await octokit.graphql(loadAssociatedPRsQuery(shas), { owner, repo });

const associatedPRs = toArray(mockReponse.data.repository).map((item) => item.associatedPullRequests.nodes);
// console.log("norm: ", mockPRNodes);
babblebey marked this conversation as resolved.
Show resolved Hide resolved

// const searchQueries = getSearchQueries(
// `repo:${owner}/${repo}+type:pr+is:merged`,
// shas,
// ).map(
// async (q) =>
// (await octokit.request("GET /search/issues", { q })).data.items,
// );

// const searchQueriesResults = await Promise.all(searchQueries);
// console.log("searchQueriesResults: ", searchQueriesResults);
// const uniqueSearchQueriesResults = uniqBy(
// flatten(searchQueriesResults),
// "number",
// );
// console.log("uniqueSearchQueriesResults: ", uniqueSearchQueriesResults);

const searchQueriesResults = await Promise.all(searchQueries);
const uniqueSearchQueriesResults = uniqBy(
flatten(searchQueriesResults),
const uniqueAssociatedPRs = uniqBy(
flatten(associatedPRs),
"number",
);
// console.log(uniquePRs);

const prs = await pFilter(
uniqueSearchQueriesResults,
uniqueAssociatedPRs,
async ({ number }) => {
const commits = await octokit.paginate(
"GET /repos/{owner}/{repo}/pulls/{pull_number}/commits",
Expand Down Expand Up @@ -250,3 +291,24 @@ export default async function success(pluginConfig, context, { Octokit }) {
throw new AggregateError(errors);
}
}

export function loadAssociatedPRsQuery (shas) {
return `#graphql
babblebey marked this conversation as resolved.
Show resolved Hide resolved
query getAssociatedPRs($owner: String!, $repo: String!) {
repository(owner: $owner, name: $repo) {
${shas.map((sha) => {
return `"${sha.slice(0, 6)}": object(oid: ${sha}) {
...on Commit {
babblebey marked this conversation as resolved.
Show resolved Hide resolved
associatedPullRequests(first: 100) {
nodes {
url
number
}
}
}
}`
}).join("")}
}
}
`;
}
74 changes: 74 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"url-join": "^5.0.0"
},
"devDependencies": {
"assert": "^2.1.0",
"ava": "6.1.3",
"c8": "10.1.2",
"cpy": "11.0.1",
Expand Down
43 changes: 34 additions & 9 deletions test/success.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ import { repeat } from "lodash-es";
import sinon from "sinon";
import test from "ava";
import fetchMock from "fetch-mock";
import assert from "assert";
import { graphql } from "@octokit/graphql";

import { ISSUE_ID } from "../lib/definitions/constants.js";
import getReleaseLinks from "../lib/get-release-links.js";
import { TestOctokit } from "./helpers/test-octokit.js";

/* eslint camelcase: ["error", {properties: "never"}] */

import success from "../lib/success.js";
import success, { loadAssociatedPRsQuery } from "../lib/success.js";

test.beforeEach((t) => {
// Mock logger
Expand Down Expand Up @@ -57,14 +59,37 @@ test("Add comment and labels to PRs associated with release commits and issues s
.getOnce(`https://api.github.local/repos/${owner}/${repo}`, {
full_name: `${redirectedOwner}/${redirectedRepo}`,
})
.getOnce(
`https://api.github.local/search/issues?q=${encodeURIComponent(
`repo:${redirectedOwner}/${redirectedRepo}`,
)}+${encodeURIComponent("type:pr")}+${encodeURIComponent(
"is:merged",
)}+${commits.map((commit) => commit.hash).join("+")}`,
{ items: prs },
)
.postOnce("https://api.github.local/graphql", (url, options) => {
assert.strictEqual(options.headers.authorization, "token github_token");
assert.strictEqual(
options.body,
`${loadAssociatedPRsQuery(commits.map(c => c.hash))}`,
);
return {
data: {
repository: {
com1: {
associatedPullRequests: {
nodes: [
{
"number": 2
}
]
}
},
com2: {
associatedPullRequests: {
nodes: [
{
"number": 2
}
]
}
}
}
}
}
})
.getOnce(
`https://api.github.local/repos/${redirectedOwner}/${redirectedRepo}/pulls/1/commits`,
[{ sha: commits[0].hash }],
Expand Down
Loading