Skip to content

Commit

Permalink
feat: github publish to create new issue (#1206)
Browse files Browse the repository at this point in the history
* feat: github publish to create new issue

* fix: resolved PR comments
  • Loading branch information
Harshita-mindfire committed Aug 26, 2021
1 parent 588fba0 commit 67abef0
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ENGINE_HOOKS } from "../../presets";
import { runEngineTestV5 } from "../../engine";
import { GithubImportPod, GithubPublishPod } from "@dendronhq/pods-core";
import { NoteProps, NoteUtils, VaultUtils } from "@dendronhq/common-all";
import _ from "lodash";

describe("GithubPod import pod", () => {
let result: any;
Expand Down Expand Up @@ -156,7 +157,6 @@ describe("GithubPod import pod", () => {

describe("github publish pod", () => {
let issue: NoteProps;

beforeEach(() => {
issue = {
id: "nCobWD86N10jWq6r",
Expand Down Expand Up @@ -242,4 +242,36 @@ describe("github publish pod", () => {
{ expect, preSetupHook: ENGINE_HOOKS.setupBasic }
);
});

test("create Issue", async () => {
await runEngineTestV5(
async ({ engine, vaults, wsRoot }) => {
const pod = new GithubPublishPod();
const vaultName = VaultUtils.getName(vaults[0]);
pod.getLabelsFromGithub = jest
.fn()
.mockReturnValue({ "area.misc": "sfgdjio", "type.bug": "gsfahhj" });
pod.createIssue = jest.fn().mockReturnValue("Issue Created");
const scratchIssue: NoteProps = _.omit(issue, "custom");
scratchIssue.custom = {};
await engine.writeNote(scratchIssue, { newNode: true });
const resp = await pod.execute({
engine,
vaults,
wsRoot,
config: {
fname: "foo",
vaultName,
dest: "stdout",
token: "asjska",
repository: "dendron",
owner: "dendronhq",
},
});
expect(resp).toEqual("Github: Issue Created");
},

{ expect, preSetupHook: ENGINE_HOOKS.setupBasic }
);
});
});
4 changes: 3 additions & 1 deletion packages/plugin-core/src/commands/PublishPod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { VSCodeUtils } from "../utils";
import { showPodQuickPickItemsV4 } from "../utils/pods";
import { DendronWorkspace } from "../workspace";
import { BaseCommand } from "./base";
import { ReloadIndexCommand } from "./ReloadIndex";

type CommandOpts = CommandInput & { noteByName: string; config: any };

Expand Down Expand Up @@ -82,7 +83,7 @@ export class PublishPodCommand extends BaseCommand<
vaults: DendronWorkspace.instance().vaultsv4,
wsRoot,
engine,
dendronConfig
dendronConfig,
});
await vscode.env.clipboard.writeText(link);
return link;
Expand All @@ -98,6 +99,7 @@ export class PublishPodCommand extends BaseCommand<
} else {
window.showInformationMessage("contents copied to clipboard");
}
await new ReloadIndexCommand().execute();
}

addAnalyticsPayload(opts?: CommandOpts) {
Expand Down
124 changes: 107 additions & 17 deletions packages/pods-core/src/builtin/GithubPod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
stringifyError,
Time,
DEngineClient,
ERROR_SEVERITY,
} from "@dendronhq/common-all";

const ID = "dendron.github";
Expand Down Expand Up @@ -114,7 +115,7 @@ export class GithubImportPod extends ImportPod<GithubImportPodConfig> {
}) as JSONSchemaType<GithubImportPodConfig>;
}

/*
/**
* method to fetch issues from github graphql api
*/
getDataFromGithub = async (opts: Partial<GithubAPIOpts>) => {
Expand Down Expand Up @@ -155,7 +156,7 @@ export class GithubImportPod extends ImportPod<GithubImportPodConfig> {
val: queryVal,
after: afterCursor,
});
} catch (error) {
} catch (error: any) {
this.L.error({
msg: "failed to import all the issues",
payload: stringifyError(error),
Expand Down Expand Up @@ -207,7 +208,7 @@ export class GithubImportPod extends ImportPod<GithubImportPodConfig> {
}
}

/*
/**
* method to add fromtmatter to notes: url, status and tags
*/
addFrontMatterToData = (
Expand Down Expand Up @@ -239,10 +240,9 @@ export class GithubImportPod extends ImportPod<GithubImportPodConfig> {
});
};

/*
/**
* method to get notes that are not already present in the vault
*/

getNewNotes(
notes: NoteProps[],
engine: DEngineClient,
Expand All @@ -260,7 +260,7 @@ export class GithubImportPod extends ImportPod<GithubImportPodConfig> {
});
}

/*
/**
* method to update the notes whose status has changed
*/
getUpdatedNotes(
Expand Down Expand Up @@ -401,7 +401,6 @@ export class GithubPublishPod extends PublishPod<GithubPublishPodConfig> {
* method to get all the labels of a repository in key value pair
*
*/

getLabelsFromGithub = async (opts: Partial<GithubPublishPodConfig>) => {
const { owner, repository, token } = opts;
let labelsHashMap: any;
Expand Down Expand Up @@ -431,7 +430,7 @@ export class GithubPublishPod extends PublishPod<GithubPublishPodConfig> {
[label.node.name]: label.node.id,
};
});
} catch (error) {
} catch (error: any) {
throw new DendronError({ message: stringifyError(error) });
}
return labelsHashMap;
Expand All @@ -440,7 +439,6 @@ export class GithubPublishPod extends PublishPod<GithubPublishPodConfig> {
/**
* method to update the issue in github
*/

updateIssue = async (opts: {
issueID: string;
token: string;
Expand All @@ -466,16 +464,98 @@ export class GithubPublishPod extends PublishPod<GithubPublishPodConfig> {
if (!_.isUndefined(result.updateIssue.issue.id)) {
resp = "Issue Updated";
}
} catch (error) {
} catch (error: any) {
resp = stringifyError(error);
throw new DendronError({ message: stringifyError(error) });
}

return resp;
};

/**
* method to get the repository id
*/
getRepositoryId = async (opts: Partial<GithubPublishPodConfig>) => {
let repositoryId: string;
const { owner, repository, token } = opts;
const query = `query repository($name: String!, $owner: String!)
{
repository(owner: $owner , name: $name) {
id
}
}`;
try {
const result: any = await graphql(query, {
headers: { authorization: `token ${token}` },
owner,
name: repository,
});
repositoryId = result.repository.id;
} catch (error: any) {
throw new DendronError({
message: stringifyError(error),
severity: ERROR_SEVERITY.MINOR,
});
}
return repositoryId;
};

/**
* method to create an issue in github
*/
createIssue = async (opts: {
token: string;
labelIDs: string[];
owner: string;
repository: string;
note: NoteProps;
engine: DEngineClient;
}) => {
const { token, labelIDs, owner, repository, note, engine } = opts;
const { title, body } = note;
let resp: string = "";
const repositoryId = await this.getRepositoryId({
token,
owner,
repository,
});
const mutation = `mutation createIssue($repositoryId: ID!, $title: String!, $body: String, $labelIDs: [ID!]){
createIssue(input: {repositoryId : $repositoryId , title: $title, body: $body, labelIds: $labelIDs}){
issue {
id
url
state
}
}
}`;
try {
const result: any = await graphql(mutation, {
headers: { authorization: `token ${token}` },
repositoryId,
title,
body,
labelIDs,
});
const issue = result.createIssue.issue;
if (!_.isUndefined(result.createIssue.issue.id)) {
note.custom.issueID = issue.id;
note.custom.url = issue.url;
note.custom.status = issue.state;
await engine.writeNote(note, { newNode: true });
resp = "Issue Created";
}
} catch (error: any) {
resp = stringifyError(error);
throw new DendronError({
message: stringifyError(error),
severity: ERROR_SEVERITY.MINOR,
});
}
return resp;
};

async plant(opts: PublishPodPlantOpts) {
const { config } = opts;
const { config, engine } = opts;
const { owner, repository, token } = config as GithubPublishPodConfig;
const labelsHashMap = await this.getLabelsFromGithub({
owner,
Expand All @@ -497,12 +577,22 @@ export class GithubPublishPod extends PublishPod<GithubPublishPodConfig> {
if (!_.isUndefined(tags) && labelIDs.length === 0) {
return "Github: The labels in the tag does not belong to selected repository";
}
const resp = await this.updateIssue({
issueID,
token,
status: status.toUpperCase(),
labelIDs,
});
const resp =
_.isUndefined(issueID) && _.isUndefined(status)
? await this.createIssue({
token,
owner,
repository,
labelIDs,
note,
engine,
})
: await this.updateIssue({
issueID,
token,
status: status.toUpperCase(),
labelIDs,
});

return "Github: ".concat(resp);
}
Expand Down

0 comments on commit 67abef0

Please sign in to comment.