Add `nodeDescription` for GitHub nodes (#261)

`nodeDescription` gives a short, readable description of the content at
a given node.

Test plan: View the included snapshot test.
This commit is contained in:
Dandelion Mané 2018-05-10 14:08:06 -07:00 committed by GitHub
parent d21ad1312b
commit ed1f17f8ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 90 additions and 0 deletions

View File

@ -79,6 +79,17 @@ Object {
}
`;
exports[`GitHub porcelain nodes have nice descriptions 1`] = `
Object {
"https://github.com/decentralion": "@decentralion",
"https://github.com/sourcecred/example-github/issues/2": "#2: A referencing issue.",
"https://github.com/sourcecred/example-github/issues/2#issuecomment-373768703": "comment by @decentralion on #2",
"https://github.com/sourcecred/example-github/pull/5": "#5: This pull request will be more contentious. I can feel it...",
"https://github.com/sourcecred/example-github/pull/5#discussion_r171460198": "review comment by @wchargin on #5",
"https://github.com/sourcecred/example-github/pull/5#pullrequestreview-100313899": "review by @wchargin on #5",
}
`;
exports[`GitHub porcelain posts have authors 1`] = `
Object {
"https://github.com/sourcecred/example-github/issues/2": Array [

View File

@ -24,6 +24,8 @@ import {
PULL_REQUEST_REVIEW_COMMENT_NODE_TYPE,
} from "./types";
import {nodeDescription} from "./render";
import {PLUGIN_NAME} from "./pluginName";
describe("GitHub porcelain", () => {
@ -331,4 +333,14 @@ describe("GitHub porcelain", () => {
expect(referenced.login()).toBe("wchargin");
});
});
it("nodes have nice descriptions", () => {
// This test really should be in its own file, but for expedience I am
// putting it here. TODO: Refactor general purpose testutils out of this
// file, and move this test to render.test.js (assuming we don't move the
// description method into the porcelain anyway...)
expectPropertiesToMatchSnapshot(allWrappers, (e) =>
nodeDescription(e.graph, e.address())
);
});
});

View File

@ -0,0 +1,67 @@
// @flow
/*
* Methods for rendering and displaying GitHub nodes.
*/
import stringify from "json-stable-stringify";
import {Graph} from "../../core/graph";
import type {Address} from "../../core/address";
import {
asEntity,
Issue,
PullRequest,
Comment,
PullRequestReview,
PullRequestReviewComment,
Author,
Repository,
} from "./porcelain";
/* Give a short description for the GitHub node at given address.
* Useful for e.g. displaying a title.
*/
export function nodeDescription(graph: Graph<any, any>, addr: Address) {
const entity = asEntity(graph, addr);
const type = entity.type();
switch (type) {
case "REPOSITORY": {
const repo = Repository.from(entity);
return `${repo.owner()}/${repo.name()}`;
}
case "ISSUE": {
const issue = Issue.from(entity);
return `#${issue.number()}: ${issue.title()}`;
}
case "PULL_REQUEST": {
const pr = PullRequest.from(entity);
return `#${pr.number()}: ${pr.title()}`;
}
case "COMMENT": {
const comment = Comment.from(entity);
const author = comment.authors()[0];
return `comment by @${author.login()} on #${comment.parent().number()}`;
}
case "PULL_REQUEST_REVIEW": {
const review = PullRequestReview.from(entity);
const author = review.authors()[0];
return `review by @${author.login()} on #${review.parent().number()}`;
}
case "PULL_REQUEST_REVIEW_COMMENT": {
const comment = PullRequestReviewComment.from(entity);
const author = comment.authors()[0];
const pr = comment.parent().parent();
return `review comment by @${author.login()} on #${pr.number()}`;
}
case "AUTHOR": {
const author = Author.from(entity);
return `@${author.login()}`;
}
default: {
// eslint-disable-next-line no-unused-expressions
(type: empty);
throw new Error(
`Tried to write description for invalid type ${stringify(addr)}`
);
}
}
}