Skip to content

Commit

Permalink
feat: Add option, add time of done to record
Browse files Browse the repository at this point in the history
  • Loading branch information
korosuke613 committed Nov 30, 2020
1 parent 8d4ee6a commit 64a95f2
Show file tree
Hide file tree
Showing 11 changed files with 375 additions and 4 deletions.
20 changes: 20 additions & 0 deletions src/ApiExecutor.ts
Expand Up @@ -541,6 +541,25 @@ const removeMemberFromCard = async (
return params;
};

const addTimeOfDoneToRecord = async (
client: KintoneRestAPIClient,
appId: string,
recordId: string,
doneTime: Date
) => {
const params = {
app: appId,
id: recordId,
record: {
[CardApp.doneTime]: {
value: doneTime,
},
},
};
await client.record.updateRecord(params);
return params;
};

const commentCard = async (
client: KintoneRestAPIClient,
appId: string,
Expand Down Expand Up @@ -660,6 +679,7 @@ export const ApiExecutor = {
getRecordIdFromLabelOfSame,
removeMemberFromCard,
getRecordIdFromList,
addTimeOfDoneToRecord,
commentCard,
registerRecordIdToTrello,
addRecordIdToCardNameOfTrello,
Expand Down
1 change: 1 addition & 0 deletions src/Kintone.ts
Expand Up @@ -28,6 +28,7 @@ export const CardApp: AppFieldIDs = {
dueReminder: "DUE_REMINDER",
closed: "CLOSED",
member: "USER",
doneTime: "DONE_TIME",
};

export const ListApp: AppFieldIDs = {
Expand Down
13 changes: 11 additions & 2 deletions src/Setting.ts
Expand Up @@ -5,20 +5,29 @@ export interface Setting {
charactersOrLess?: number;
match?: string;
}>;
isAddDoneTime?: boolean;
doneListName?: string;
}

export class SettingGuardian {
public setting: Setting | undefined;
public setting: Setting;

constructor(setting: Setting = {}) {
this.setting = setting;
}

public isAddDoneTime(listName: string) {
if (this.setting.isAddDoneTime === undefined || !this.setting.isAddDoneTime)
return false;

return listName === (this.setting.doneListName ?? "Done");
}

public isSkipEvent(trelloAction: Action) {
if (this.setting?.excludes === undefined) {
return false;
}
const excludes = this.setting?.excludes;
const excludes = this.setting.excludes;

const isCharacterOrLess = (charactersOrLess: number) => {
return charactersOrLess >= trelloAction.data.card.name.length;
Expand Down
1 change: 1 addition & 0 deletions src/Trekin.ts
Expand Up @@ -62,6 +62,7 @@ export class Trekin {
return Promise.resolve("Skip this event");
}
this.worker.trelloAction = trelloAction;
this.worker.setting = this.guardian;
return this.worker.postAction();
}
}
27 changes: 26 additions & 1 deletion src/Worker.ts
Expand Up @@ -3,12 +3,14 @@ import { Apps } from "./Kintone";
import { KintoneClientCreator } from "./KintoneClientCreator";
import { ApiExecutor } from "./ApiExecutor";
import { KintoneRestAPIClient } from "@kintone/rest-api-client";
import { SettingGuardian } from "./Setting";

export class Worker {
public trelloAction: Action;
private readonly kintoneClientCreator: KintoneClientCreator;
private readonly apps: Apps;
private readonly trelloCert: Certificate;
public setting?: SettingGuardian;

constructor(apps: Apps, trelloCert: Certificate) {
this.kintoneClientCreator = new KintoneClientCreator(apps.baseUrl);
Expand Down Expand Up @@ -91,6 +93,7 @@ export class Worker {
}
case ActionType.UPDATE_CARD: {
result.push(await this.addRecordIdToCardNameOfTrello());
result.push(await this.addTimeOfDoneToRecord());
break;
}
case ActionType.ADD_LABEL_TO_CARD: {
Expand Down Expand Up @@ -174,7 +177,7 @@ export class Worker {
this.trelloAction.data
);
}
return cardRecordId;
return cardRecordId as string;
}

async getKintoneUserCodeIfNotExistsAddMember(client: KintoneRestAPIClient) {
Expand Down Expand Up @@ -447,4 +450,26 @@ export class Worker {
recordId as string
);
}

async addTimeOfDoneToRecord() {
if (
this.setting === undefined ||
this.trelloAction.data.listAfter === undefined ||
!this.setting.isAddDoneTime(this.trelloAction.data.listAfter.name)
)
// 設定がない or リストの移動でない or Doneへの移動でないなら何もせずreturn
return "Skip addTimeOfDoneToRecord";

const client = await this.kintoneClientCreator.createKintoneClient([
this.apps.cards.token,
]);
const recordId = await this.getCardRecordIdIfNotExistsCreateCard(client);
const doneTime = this.trelloAction.date;
return ApiExecutor.addTimeOfDoneToRecord(
client,
this.apps.cards.id,
recordId,
doneTime
);
}
}
64 changes: 63 additions & 1 deletion src/__tests__/Setting.test.ts
@@ -1,6 +1,6 @@
/* eslint-disable no-loop-func */
import { Trekin } from "../Trekin";
import { Action } from "../Trello";
import { Action, ListShort } from "../Trello";
import fs from "fs";
import { Worker } from "../Worker";

Expand Down Expand Up @@ -137,3 +137,65 @@ describe("skipのテスト", () => {
});
}
});

describe("addDoneTimeのテスト", () => {
const testcases = [
{
name:
"カスタムdone名とアフターリスト名が一致するのでAddDoneTime()でtrueを返す",
input: [
"./src/__tests__/trekin_settings/enableAddDoneTime_1.trekinrc.json5",
"./src/__tests__/trello_events/updateCard_idList_3.json",
],
expected: true,
},
{
name:
"カスタムdone名とアフターリスト名が一致しないのでAddDoneTime()でfalseを返す",
input: [
"./src/__tests__/trekin_settings/enableAddDoneTime_1.trekinrc.json5",
"./src/__tests__/trello_events/updateCard_idList_2.json",
],
expected: false,
},
{
name:
"デフォルトdone名とアフターリスト名が一致するのでAddDoneTime()でtrueを返す",
input: [
"./src/__tests__/trekin_settings/enableAddDoneTime_2.trekinrc.json5",
"./src/__tests__/trello_events/updateCard_idList_4.json",
],
expected: true,
},
{
name:
"デフォルトdone名とアフターリスト名が一致しないのでAddDoneTime()でfalseを返す",
input: [
"./src/__tests__/trekin_settings/enableAddDoneTime_2.trekinrc.json5",
"./src/__tests__/trello_events/updateCard_idList_2.json",
],
expected: false,
},
{
name: "isAddDoneTimeがfalseなのでAddDoneTime()でfalseを返す",
input: [
"./src/__tests__/trekin_settings/.trekinrc.json5",
"./src/__tests__/trello_events/updateCard_idList_4.json",
],
expected: false,
},
];

for (const { name, input, expected } of testcases) {
test(name, async () => {
const t = await createTrekin();
await t.readSetting(input[0]);
const action: Action = JSON.parse(await fs.readFileSync(input[1], "utf8"))
.body.action;
const actual = t.guardian.isAddDoneTime(
(action.data.listAfter as ListShort).name
);
expect(actual).toEqual(expected);
});
}
});
92 changes: 92 additions & 0 deletions src/__tests__/Worker.test.ts
@@ -0,0 +1,92 @@
/* eslint-disable no-loop-func */
import { Trekin } from "../Trekin";
import { Action } from "../Trello";
import fs from "fs";
import { Worker } from "../Worker";
import { KintoneRestAPIClient } from "@kintone/rest-api-client";
import { SettingGuardian } from "../Setting";
import { ApiExecutor } from "../ApiExecutor";
const JSON5 = require("json5");

jest.mock("@kintone/rest-api-client");
const KintoneMock = (KintoneRestAPIClient as unknown) as jest.Mock;

jest.mock("../ApiExecutor");
const getRecordIdFromCardMock = (ApiExecutor.getRecordIdFromCard as unknown) as jest.Mock;
const addTimeOfDoneToRecordMock = (ApiExecutor.addTimeOfDoneToRecord as unknown) as jest.Mock;

const createWorker = () => {
const w = new Worker(
{
baseUrl: "",
cards: { id: "", token: "" },
defaultKintoneUserCode: "",
labels: { id: "", token: "" },
lists: { id: "", token: "" },
members: { id: "", token: "" },
},
{ apiKey: "", apiToken: "" }
);

return w;
};

describe("addTimeOfDoneToRecordのテスト", () => {
const testcases = [
{
name: "変更日時を取得できる",
input: {
actionPath: "./src/__tests__/trello_events/updateCard_idList_3.json",
settingPath:
"./src/__tests__/trekin_settings/enableAddDoneTime_1.trekinrc.json5",
},
expected: "2020-07-31T02:53:23.814Z",
},
{
name: "doneじゃないのでスキップする",
input: {
actionPath: "./src/__tests__/trello_events/updateCard_idList_1.json",
settingPath:
"./src/__tests__/trekin_settings/enableAddDoneTime_1.trekinrc.json5",
},
expected: "Skip addTimeOfDoneToRecord",
},
{
name: "リスト移動じゃないのでスキップする",
input: {
actionPath: "./src/__tests__/trello_events/createCard_1.json",
settingPath:
"./src/__tests__/trekin_settings/enableAddDoneTime_1.trekinrc.json5",
},
expected: "Skip addTimeOfDoneToRecord",
},
];

for (const { name, input, expected } of testcases) {
test(name, async () => {
KintoneMock.mockImplementation(() => {
return true;
});
getRecordIdFromCardMock.mockImplementation(() => {
return "1";
});
addTimeOfDoneToRecordMock.mockImplementation(
(_client, _cardId, _recordId, doneTime) => {
return doneTime;
}
);

const w = await createWorker();
w.setting = new SettingGuardian(
JSON5.parse(fs.readFileSync(input.settingPath, "utf-8"))
);
const action: Action = JSON.parse(
await fs.readFileSync(input.actionPath, "utf8")
).body.action;
w.trelloAction = action;

const actual = await w.addTimeOfDoneToRecord();
expect(actual).toEqual(expected);
});
}
});
@@ -0,0 +1,4 @@
{
isAddDoneTime: true,
doneListName: "Done🎉"
}
@@ -0,0 +1,3 @@
{
isAddDoneTime: true,
}
77 changes: 77 additions & 0 deletions src/__tests__/trello_events/updateCard_idList_3.json
@@ -0,0 +1,77 @@
{
"body": {
"action": {
"id": "5f2387a3dfc88d4d3f6d23ae",
"idMemberCreator": "5ecb263b62993c0d35798ec6",
"data": {
"old": {
"name": "oldName",
"idList": "oldIdList"
},
"card": {
"idList": "newIdList",
"id": "newId",
"name": "これは新しいカードです!!",
"idShort": 27,
"shortLink": "hBE7fh7c"
},
"board": {
"id": "5f02821bf3e65f322beb3ea4",
"name": "trello APIテスト",
"shortLink": "Sqsia9EM"
},
"listBefore": {
"id": "5f237f512144fd2c583ae3f4",
"name": "fffff"
},
"listAfter": {
"id": "5f1e8b1ed6438e403c6f18fe",
"name": "Done🎉"
}
},
"type": "updateCard",
"date": "2020-07-31T02:53:23.814Z",
"limits": {},
"display": {
"translationKey": "action_move_card_from_list_to_list",
"entities": {
"card": {
"type": "card",
"idList": "5f1e8b1ed6438e403c6f18fe",
"id": "5f237f64ff024f3a01851dc9",
"shortLink": "hBE7fh7c",
"text": "fffffリスト"
},
"listBefore": {
"type": "list",
"id": "5f237f512144fd2c583ae3f4",
"text": "fffff"
},
"listAfter": {
"type": "list",
"id": "5f1e8b1ed6438e403c6f18fe",
"text": "aaaaa"
},
"memberCreator": {
"type": "member",
"id": "5ecb263b62993c0d35798ec6",
"username": "korosuke613",
"text": "Futa Hirakoba (平木場 風太)"
}
}
},
"memberCreator": {
"id": "5ecb263b62993c0d35798ec6",
"username": "korosuke613",
"activityBlocked": false,
"avatarHash": "afe1dce94ee1d794699a96d03c560dee",
"avatarUrl": "https://trello-members.s3.amazonaws.com/5ecb263b62993c0d35798ec6/afe1dce94ee1d794699a96d03c560dee",
"fullName": "Futa Hirakoba",
"idMemberReferrer": "5e82ef3619774982716eb912",
"initials": "FH",
"nonPublic": {},
"nonPublicAvailable": true
}
}
}
}

0 comments on commit 64a95f2

Please sign in to comment.