Skip to content

Commit

Permalink
get-github-info fixes (#224)
Browse files Browse the repository at this point in the history
* get-github-info fixes

* Linting...
  • Loading branch information
emmatown committed Dec 2, 2019
1 parent 6905193 commit 938823f
Show file tree
Hide file tree
Showing 6 changed files with 396 additions and 19 deletions.
6 changes: 6 additions & 0 deletions .changeset/odd-games-remember.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@changesets/get-github-info": patch
"@changesets/changelog-github": patch
---

Fix cases where the wrong PR is returned when a commit is associated with multiple PRs
6 changes: 6 additions & 0 deletions .changeset/wicked-avocados-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@changesets/get-github-info": minor
"@changesets/changelog-github": minor
---

Show the PR author of a change rather than the author of the commit that added a changeset to account for cases when maintainers add a changeset to a PR and merge the PR with a merge commit
4 changes: 4 additions & 0 deletions packages/get-github-info/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@
"dependencies": {
"dataloader": "^1.4.0",
"node-fetch": "^2.5.0"
},
"devDependencies": {
"nock": "^11.7.0",
"prettier": "^1.18.2"
}
}
283 changes: 283 additions & 0 deletions packages/get-github-info/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,283 @@
import { getInfo } from ".";
import nock from "nock";
import prettier from "prettier";

process.env.GITHUB_TOKEN = "token";

let apiPath = `/graphql?access_token=${process.env.GITHUB_TOKEN}`;

test("associated with multiple PRs with only one merged", async () => {
nock("https://api.github.com")
.post(apiPath, ({ query }) => {
expect(prettier.format(query, { parser: "graphql" }))
.toMatchInlineSnapshot(`
"query {
a0: repository(owner: \\"emotion-js\\", name: \\"emotion\\") {
aa085003: object(expression: \\"a085003\\") {
... on Commit {
commitUrl
associatedPullRequests(first: 50) {
nodes {
number
url
mergedAt
}
}
author {
user {
login
url
}
}
}
}
}
}
"
`);
return true;
})
.reply(
200,
JSON.stringify({
data: {
a0: {
aa085003: {
commitUrl:
"https://github.com/emotion-js/emotion/commit/a085003d4c8ca284c116668d7217fb747802ed85",
associatedPullRequests: {
nodes: [
{
number: 973,
url: "https://github.com/emotion-js/emotion/pull/973",
mergedAt: null,
author: {
login: "mitchellhamilton",
url: "https://github.com/mitchellhamilton"
}
},
{
number: 1600,
url: "https://github.com/emotion-js/emotion/pull/1600",
mergedAt: null,
author: {
login: "mitchellhamilton",
url: "https://github.com/mitchellhamilton"
}
},
{
number: 1613,
url: "https://github.com/emotion-js/emotion/pull/1613",
mergedAt: "2019-11-07T06:43:58Z",
author: {
login: "Andarist",
url: "https://github.com/Andarist"
}
},
{
number: 1628,
url: "https://github.com/emotion-js/emotion/pull/1628",
mergedAt: null,
author: {
login: "Andarist",
url: "https://github.com/Andarist"
}
},
{
number: 1630,
url: "https://github.com/emotion-js/emotion/pull/1630",
mergedAt: null,
author: {
login: "Andarist",
url: "https://github.com/Andarist"
}
}
]
},
author: {
user: {
login: "Andarist",
url: "https://github.com/Andarist"
}
}
}
}
}
})
);
let result = await getInfo({ commit: "a085003", repo: "emotion-js/emotion" });
expect(result).toMatchObject({ pull: 1613, user: "Andarist" });
});

test("associated with multiple PRs with multiple merged gets the one that was merged first", async () => {
nock("https://api.github.com")
.post(apiPath, ({ query }) => {
expect(prettier.format(query, { parser: "graphql" }))
.toMatchInlineSnapshot(`
"query {
a0: repository(owner: \\"emotion-js\\", name: \\"emotion\\") {
aa085003: object(expression: \\"a085003\\") {
... on Commit {
commitUrl
associatedPullRequests(first: 50) {
nodes {
number
url
mergedAt
}
}
author {
user {
login
url
}
}
}
}
}
}
"
`);
return true;
})
.reply(
200,
JSON.stringify({
data: {
a0: {
aa085003: {
commitUrl:
"https://github.com/emotion-js/emotion/commit/a085003d4c8ca284c116668d7217fb747802ed85",
associatedPullRequests: {
nodes: [
{
number: 973,
url: "https://github.com/emotion-js/emotion/pull/973",
mergedAt: null,
author: {
login: "mitchellhamilton",
url: "https://github.com/mitchellhamilton"
}
},
{
number: 1600,
url: "https://github.com/emotion-js/emotion/pull/1600",
mergedAt: "2019-11-20T06:43:58Z",
author: {
login: "mitchellhamilton",
url: "https://github.com/mitchellhamilton"
}
},
{
number: 1613,
url: "https://github.com/emotion-js/emotion/pull/1613",
mergedAt: "2019-11-07T06:43:58Z",
author: {
login: "Andarist",
url: "https://github.com/Andarist"
}
},
{
number: 1628,
url: "https://github.com/emotion-js/emotion/pull/1628",
mergedAt: null,
author: {
login: "Andarist",
url: "https://github.com/Andarist"
}
},
{
number: 1630,
url: "https://github.com/emotion-js/emotion/pull/1630",
mergedAt: null,
author: {
login: "Andarist",
url: "https://github.com/Andarist"
}
}
]
},
author: {
user: {
login: "Andarist",
url: "https://github.com/Andarist"
}
}
}
}
}
})
);
let result = await getInfo({ commit: "a085003", repo: "emotion-js/emotion" });
expect(result).toMatchObject({ pull: 1613, user: "Andarist" });
});

test("gets the author of the associated pull request if it exists rather than the author of the changeset", async () => {
nock("https://api.github.com")
.post(apiPath, ({ query }) => {
expect(prettier.format(query, { parser: "graphql" }))
.toMatchInlineSnapshot(`
"query {
a0: repository(owner: \\"JedWatson\\", name: \\"react-select\\") {
ac7e9c69: object(expression: \\"c7e9c69\\") {
... on Commit {
commitUrl
associatedPullRequests(first: 50) {
nodes {
number
url
mergedAt
}
}
author {
user {
login
url
}
}
}
}
}
}
"
`);
return true;
})
.reply(
200,
JSON.stringify({
data: {
a0: {
ac7e9c69: {
commitUrl:
"https://github.com/JedWatson/react-select/commit/c7e9c697dada15ce3ff9a767bf914ad890080433",
associatedPullRequests: {
nodes: [
{
number: 3682,
url: "https://github.com/JedWatson/react-select/pull/3682",
mergedAt: "2019-10-02T07:37:15Z",
author: {
login: "lmvco",
url: "https://github.com/lmvco"
}
}
]
},
author: {
user: {
login: "JedWatson",
url: "https://github.com/JedWatson"
}
}
}
}
}
})
);
let result = await getInfo({
commit: "c7e9c69",
repo: "JedWatson/react-select"
});
expect(result).toMatchObject({ pull: 3682, user: "lmvco" });
});
53 changes: 35 additions & 18 deletions packages/get-github-info/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ function makeQuery(repos: any) {
)}) {
... on Commit {
commitUrl
associatedPullRequests(first: 1) {
associatedPullRequests(first: 50) {
nodes {
number
url
mergedAt
}
}
author {
Expand Down Expand Up @@ -119,26 +120,42 @@ export async function getInfo(request: {
}

const data = await GHDataLoader.load(request);
let user = null;
if (data.author && data.author.user) {
user = data.author.user;
}

let associatedPullRequest =
data.associatedPullRequests &&
data.associatedPullRequests.nodes &&
data.associatedPullRequests.nodes.length
? (data.associatedPullRequests.nodes as any[]).sort((a, b) => {
if (a.mergedAt === null && b.mergedAt === null) {
return 0;
}
if (a.mergedAt === null) {
return 1;
}
if (b.mergedAt === null) {
return -1;
}
a = new Date(a.mergedAt);
b = new Date(b.mergedAt);
return a > b ? 1 : a < b ? -1 : 0;
})[0]
: null;
if (associatedPullRequest) {
user = associatedPullRequest.author;
}
return {
user: data.author && data.author.user ? data.author.user.login : null,
pull:
data.associatedPullRequests &&
data.associatedPullRequests.nodes &&
data.associatedPullRequests.nodes[0]
? data.associatedPullRequests.nodes[0].number
: null,
user: user ? user.login : null,
pull: associatedPullRequest ? associatedPullRequest.number : null,
links: {
commit: `[\`${request.commit}\`](${data.commitUrl})`,
pull:
data.associatedPullRequests &&
data.associatedPullRequests.nodes &&
data.associatedPullRequests.nodes[0]
? `[#${data.associatedPullRequests.nodes[0].number}](${data.associatedPullRequests.nodes[0].url})`
: null,
user:
data.author && data.author.user
? `[@${data.author.user.login}](${data.author.user.url})`
: null
pull: associatedPullRequest
? `[#${associatedPullRequest.number}](${associatedPullRequest.url})`
: null,
user: user ? `[@${user.login}](${user.url})` : null
}
};
}

0 comments on commit 938823f

Please sign in to comment.