-
Notifications
You must be signed in to change notification settings - Fork 0
/
todo-utils.js
143 lines (130 loc) · 4.87 KB
/
todo-utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const utcToZonedTime = require('date-fns-tz/utcToZonedTime');
const formatDistanceToNowStrict = require('date-fns/formatDistanceToNowStrict');
const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
const _ = require('lodash');
const pluralize = require('pluralize')
const vscode = require('vscode');
function handleCustomTask(todo) {
const task = todo.task;
todo.group = `makerflow_${task.title}`;
todo.description = task.title;
todo.sourceDescription = null;
}
const handlePullRequestTodos = (todo) => {
const pr = todo.pr;
todo.group = `${pr.repository_uuid}_${pr.pullrequest_id}`
todo.description = `PR #${pr.pullrequest_id}: ${pr.pullrequest_title}`;
todo.link = pr.link;
todo.sourceDescription = `${pr.repository_name} | ${pluralize("comments", todo.meta.comments, true)} | ${pluralize("approvals", todo.meta.approvals, true)}`;
}
const handleSlackTodo = (todo) => {
let thisTodoEvents = todo.events;
let eventCount = thisTodoEvents.length;
const sourceCount = new Set(thisTodoEvents.map(e => e.channel_id)).size;
let sourceType = getSlackSourceType(todo);
todo.description = `${pluralize('message', eventCount, true)} in ${sourceCount === 1 ? 'a ' : 'multiple '}${pluralize(sourceType, sourceCount)}`;
let timingDescription = buildTimingDescription(thisTodoEvents);
if (sourceCount > 1) {
todo.sourceDescription = `${todo.group} | ${pluralize(sourceType, sourceCount, true)}`;
} else {
todo.sourceDescription = `${todo.group}`;
}
if ( todo.type === 'slack_channel') {
todo.sourceDescription += ` | ${_.uniq(thisTodoEvents.map(e => `#${e.channel_name}`)).join(", ")}`;
}
todo.sourceDescription += timingDescription
todo.browserLink = `https://slack.com/app_redirect?channel=${thisTodoEvents[0].channel_id}&team=${thisTodoEvents[0].team_id}`
todo.link = `slack://channel?id=${thisTodoEvents[0].channel_id}&team=${thisTodoEvents[0].team_id}`
}
const buildTimingDescription = (events) => {
let createdTimes = events.map(e => e.created_at);
let latestTime = _.max(createdTimes);
let latestTimeDescription = formatDistanceToNowStrict(utcToZonedTime(latestTime, timeZone));
return ` | ${latestTimeDescription}`;
}
const getSlackSourceType = (todo) => {
let sourceType = '';
if (todo.type === 'slack_channel') {
sourceType = 'channel';
}
if (todo.type === 'slack_im') {
sourceType = 'DM';
}
if (todo.type === 'slack_mpim') {
sourceType = 'group DM';
}
return sourceType;
}
const enrichTodo = (todo) => {
if (typeof todo === "undefined" || todo === null) return null;
todo.done = false;
if (todo.type.indexOf("slack") !== -1) {
handleSlackTodo(todo);
}
if (todo.type === "bitbucket") {
handlePullRequestTodos(todo);
}
if (todo.type === "github") {
handlePullRequestTodos(todo);
}
if (todo.sourceType === "makerflow" && todo.type === "makerflow") {
handleCustomTask(todo);
}
if (todo.sourceType === "makerflow" && todo.type === "onboarding") {
return null;
}
return todo;
}
const isSlackTodo = (todo) => {
return todo.type.indexOf("slack") !== -1;
}
const isGithubTodo = (todo) => {
return todo.type === "github";
}
const isBitbucketTodo = (todo) => {
return todo.type === "bitbucket";
}
const createQuickPickItem = (todo) => {
let item = {
label: todo.description,
description: todo.sourceDescription,
todo: todo
};
return item;
}
const executeAction = async (todo, functionToMarkAsDone) => {
if (isSlackTodo(todo) || isGithubTodo(todo) || isBitbucketTodo(todo)) {
const openDestination = isSlackTodo(todo) ? ' in Slack app' : ' in browser';
const items = [
{'label': 'Mark as done', 'description': 'Mark this todo as done', 'todo': todo},
{'label': 'Open', 'description': `Open ${openDestination}`, 'todo': todo},
]
if (isSlackTodo(todo)) {
items.push({'label': 'Open in browser', 'description': 'Open this slack workspace in browser', 'todo': todo})
}
const choice = await vscode.window.showQuickPick(items, {
placeHolder: `Mark as done or open ${openDestination}`,
});
if (!choice) return;
if (choice.label === 'Mark as done') {
await functionToMarkAsDone(todo);
} else if (choice.label === 'Open') {
vscode.env.openExternal(vscode.Uri.parse(todo.link));
} else if (choice.label === 'Open in browser') {
vscode.env.openExternal(vscode.Uri.parse(todo.browserLink));
}
}
}
module.exports = {
buildTimingDescription,
getSlackSourceType,
handleSlackTodo,
handlePullRequestTodos,
handleCustomTask,
enrichTodo,
isGithubTodo,
isBitbucketTodo,
isSlackTodo,
createQuickPickItem,
executeAction
}