Skip to content

Commit

Permalink
when matching SHA to PR only take merged into account. you can have m…
Browse files Browse the repository at this point in the history
…ultiple open PRs with the same commits, such as in a rebase
  • Loading branch information
hipstersmoothie committed Apr 3, 2020
1 parent 8e51e74 commit 3d8ddb5
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

exports[`buildSearchQuery generates a valid query 1`] = `
"{
hash_abc123: search(query: \\"repo:Andrew/test abc123\\", type: ISSUE, first: 1) {
hash_abc123: search(query: \\"repo:Andrew/test abc123\\", type: ISSUE, first: 10) {
edges {
node {
... on PullRequest {
Expand All @@ -21,7 +21,7 @@ exports[`buildSearchQuery generates a valid query 1`] = `
}
}
hash_3def78: search(query: \\"repo:Andrew/test 3def78\\", type: ISSUE, first: 1) {
hash_3def78: search(query: \\"repo:Andrew/test 3def78\\", type: ISSUE, first: 10) {
edges {
node {
... on PullRequest {
Expand Down
22 changes: 11 additions & 11 deletions packages/core/src/__tests__/__snapshots__/release.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,6 @@ exports[`Release generateReleaseNotes should allow user to configure section hea
- Adam Dierkens (adam@dierkens.com)"
`;

exports[`Release generateReleaseNotes should find ignore closed prs 1`] = `
"#### 🐛 Bug Fix
- Doom Patrol enabled (adam@dierkens.com)
- Autobots roll out! (adam@dierkens.com)
#### Authors: 1
- Adam Dierkens (adam@dierkens.com)"
`;

exports[`Release generateReleaseNotes should find matching PRs for shas through search 1`] = `
"#### 💥 Breaking Change
Expand All @@ -57,6 +46,17 @@ exports[`Release generateReleaseNotes should get extra user data for login 1`] =
- Adam Dierkens ([@adierkens](https://github.com/adierkens))"
`;

exports[`Release generateReleaseNotes should ignore closed prs 1`] = `
"#### 🐛 Bug Fix
- Doom Patrol enabled (adam@dierkens.com)
- Autobots roll out! (adam@dierkens.com)
#### Authors: 1
- Adam Dierkens (adam@dierkens.com)"
`;

exports[`Release generateReleaseNotes should include PR-less commits 1`] = `
"#### 🚀 Enhancement
Expand Down
24 changes: 13 additions & 11 deletions packages/core/src/__tests__/release.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -706,21 +706,24 @@ describe("Release", () => {
graphql.mockReturnValueOnce({
hash_1: {
edges: [
{ node: { labels: { edges: [{ node: { name: "major" } }] } } },
{
node: {
state: "MERGED",
labels: { edges: [{ node: { name: "major" } }] },
},
},
],
},
});
// PR with no label, should become patch
graphql.mockReturnValueOnce({
// PR with no label, should become patch
hash_2: {
edges: [{ node: { labels: undefined } }],
edges: [{ node: { state: "MERGED", labels: undefined } }],
},
});

expect(await gh.generateReleaseNotes("1234", "123")).toMatchSnapshot();
});

test("should find ignore closed prs", async () => {
test("should ignore closed prs", async () => {
const gh = new Release(git);

getGitLog.mockReturnValueOnce([
Expand All @@ -738,17 +741,16 @@ describe("Release", () => {
{
node: {
labels: {
edges: [{ node: { name: "major", state: "CLOSED" } }],
edges: [{ node: { name: "major" } }],
},
state: "CLOSED",
},
},
],
},
});
// PR with no label, should become patch
graphql.mockReturnValueOnce({
// PR with no label, should become patch
hash_2: {
edges: [{ node: { labels: undefined } }],
edges: [{ node: { labels: undefined, state: "MERGED" } }],
},
});

Expand Down
25 changes: 12 additions & 13 deletions packages/core/src/match-sha-to-pr.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import endent from "endent";

import { IExtendedCommit } from "./log-parse";
import { ILabelDefinition } from "./release";

interface ISearchEdge {
/** Graphql search node */
Expand Down Expand Up @@ -48,7 +49,7 @@ export function buildSearchQuery(
return endent`
${q}
hash_${commit}: search(query: "${subQuery}", type: ISSUE, first: 1) {
hash_${commit}: search(query: "${subQuery}", type: ISSUE, first: 10) {
edges {
node {
... on PullRequest {
Expand Down Expand Up @@ -99,23 +100,21 @@ export function processQueryResult(
return;
}

if (result.edges.length > 0) {
if (result.edges[0].node.state === "CLOSED") {
return;
}
// When matching SHA to PR only take merged into account. You can have
// multiple open PRs with the same commits, such as in a rebase.
const prs = result.edges.filter((edge) => edge.node.state === "MERGED");

const labels: {
/** The label */
name: string;
}[] = result.edges[0].node.labels
? result.edges[0].node.labels.edges.map((edge) => edge.node)
if (prs.length) {
const pr = prs[0].node;
const labels: ILabelDefinition[] = pr.labels
? pr.labels.edges.map((edge) => edge.node)
: [];
commit.pullRequest = {
number: result.edges[0].node.number,
body: result.edges[0].node.body,
number: pr.number,
body: pr.body,
};
commit.labels = [...labels.map((label) => label.name), ...commit.labels];
} else {
} else if (!result.edges.length) {
commit.labels = ["pushToBaseBranch", ...commit.labels];
}

Expand Down

0 comments on commit 3d8ddb5

Please sign in to comment.