GH Porcelain: add `Repository.from` (#257)

I think the absence of this method when I added the `Repository` class
was a bug.

Test plan: There are new unit tests.
This commit is contained in:
Dandelion Mané 2018-05-10 11:24:58 -07:00 committed by GitHub
parent 5c44dd0373
commit 9d24190c03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 0 deletions

View File

@ -168,6 +168,10 @@ class GithubEntity<T: NodePayload> {
} }
export class Repository extends GithubEntity<RepositoryNodePayload> { export class Repository extends GithubEntity<RepositoryNodePayload> {
static from(e: Entity): Repository {
assertEntityType(e, REPOSITORY_NODE_TYPE);
return (e: any);
}
// TODO: Now that the Repository is a node in the graph, re-write methods // TODO: Now that the Repository is a node in the graph, re-write methods
// that find issues and PRs to find neighbors of the repository rather than // that find issues and PRs to find neighbors of the repository rather than
// any matching nodes in the graph. Then, behavior will be correct in the // any matching nodes in the graph. Then, behavior will be correct in the

View File

@ -6,6 +6,7 @@ import exampleRepoData from "./demoData/example-github.json";
import { import {
asEntity, asEntity,
Porcelain, Porcelain,
Repository,
Issue, Issue,
PullRequest, PullRequest,
Comment, Comment,
@ -169,6 +170,7 @@ describe("GitHub porcelain", () => {
describe("has type coercion that", () => { describe("has type coercion that", () => {
it("allows refining types when correct", () => { it("allows refining types when correct", () => {
const _unused_repo: Repository = Repository.from(repo);
const _unused_issue: Issue = Issue.from(issueOrPRByNumber(1)); const _unused_issue: Issue = Issue.from(issueOrPRByNumber(1));
const _unused_pr: PullRequest = PullRequest.from(issueOrPRByNumber(3)); const _unused_pr: PullRequest = PullRequest.from(issueOrPRByNumber(3));
const _unused_author: Author = Author.from( const _unused_author: Author = Author.from(
@ -179,6 +181,9 @@ describe("GitHub porcelain", () => {
); );
}); });
it("throws an error on bad type refinement", () => { it("throws an error on bad type refinement", () => {
expect(() => Repository.from(issueOrPRByNumber(1))).toThrowError(
"to have type REPOSITORY"
);
expect(() => PullRequest.from(issueOrPRByNumber(1))).toThrowError( expect(() => PullRequest.from(issueOrPRByNumber(1))).toThrowError(
"to have type PULL_REQUEST" "to have type PULL_REQUEST"
); );