-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathjira.omnifocusjs
224 lines (201 loc) · 7.98 KB
/
jira.omnifocusjs
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*{
"type": "action",
"targets": ["omnifocus"],
"author": "Bastian Kuhn",
"identifier": "de.bastian-kuhn.omnifocus.sync.jira",
"version": "1.3",
"description": "Sync elements from Jira with Omnifocus",
"label": "🔹 Jira: Sync",
"shortLabel": "Jira: sync"
}*/
(() => {
const action = new PlugIn.Action(function (selection, sender) {
// START EDIT
const configs = [{
user: "", // use email address for jira cloud
password: "", // you can use api tokens, see https://id.atlassian.com/manage-profile/security/api-tokens
jiraUrl: "",
omnifocusTagToUse: "Jira", // you can also use the format "Parent1 : Parent2 : Child"
jiraQuery: "assignee=currentuser() and resolution is empty",
processComments: false, // if true, a subtask is added for each ticket comment
omnifocusCommentTagToUse: "Jira-Comment",
// },
// {
// user: "",
// password: "",
// jiraUrl: "",
// omnifocusTagToUse: "",
// jiraQuery: "assignee=currentuser() and resolution is empty",
// processComments: false,
// omnifocusCommentTagToUse: "",
//
}]
// END EDIT
configs.forEach((config) => {
const user = config.user;
const password = config.password;
const jiraUrl = config.jiraUrl;
const omnifocusTagToUse = config.omnifocusTagToUse;
const jiraQuery = config.jiraQuery;
const processComments = config.processComments;
const omnifocusCommentTagToUse = config.omnifocusCommentTagToUse;
const urlParams = "/rest/api/2/search?jql=" + encodeURIComponent(jiraQuery);
const url = jiraUrl + urlParams;
const data = Data.fromString(user + ":" + password);
const credentials = data.toBase64();
const request = URL.FetchRequest.fromString(url);
request.method = "GET";
request.headers = { Authorization: `Basic ${credentials}` };
const requestPromise = request.fetch();
// Find the tag
let tag = null;
// If user gave an absolute path, parse it and process it
const parsedOmnifocusTagToUse = omnifocusTagToUse.split(" : ");
if (parsedOmnifocusTagToUse.length > 1) {
let currentTag = null;
let i;
for (i = 0; i < parsedOmnifocusTagToUse.length; i++) {
let newTag;
if (currentTag) {
newTag = currentTag.childNamed(parsedOmnifocusTagToUse[i]);
} else {
newTag = tags.byName(parsedOmnifocusTagToUse[i]);
}
if (!newTag) break;
currentTag = newTag;
}
// after all ran, check if its a success, then apply tag
if (
currentTag &&
i === parsedOmnifocusTagToUse.length &&
currentTag.name ===
parsedOmnifocusTagToUse[parsedOmnifocusTagToUse.length - 1]
) {
tag = currentTag;
}
}
if (!tag) {
// if tag is not found yet
tag =
tags.byName(omnifocusTagToUse) ||
flattenedTags.byName(omnifocusTagToUse) ||
new Tag(omnifocusTagToUse);
}
if (!tag) {
console.error(new Error("could not create tag"));
return;
}
const tasks = tag.tasks;
const omnifocusComments = {};
if (processComments) {
// Get all comment tagged tasks
var commentTag =
tags.byName(omnifocusCommentTagToUse) ||
flattenedTags.byName(omnifocusCommentTagToUse) ||
new Tag(omnifocusCommentTagToUse);
const commentTasks = commentTag.tasks;
commentTasks.forEach((comment) => {
const re = new RegExp(`^[A-Za-z0-9-]+ [0-9]+`);
let commentId;
try {
commentId = comment.name.match(re);
if (!commentId) return;
} catch {
return;
}
omnifocusComments[commentId] = comment;
});
}
// cache object instead of array so search will be faster later
let omnifocusTasks = {};
tasks.forEach((task) => {
const re = new RegExp(`^[A-Za-z0-9-]+`);
let taskId;
try {
taskId = task.name.match(re)[0];
if (!taskId) return;
} catch {
return;
}
omnifocusTasks[taskId] = task;
// Reset Jira Match Flag
omnifocusTasks[taskId].jiraToOmnifocusMatched = false;
});
requestPromise.then((response) => {
let createdTasks = 0;
let checkedTickets = 0;
let createdComments = 0;
if (response.mimeType == "application/json") {
const jsonResponse = JSON.parse(response.bodyString);
// ADD THE TASKS
for (const issue of jsonResponse.issues) {
// Search if we need to add a new Task
checkedTickets++;
if (omnifocusTasks[issue.key]) {
// Make sure the task opens again
omnifocusTasks[issue.key].markIncomplete();
omnifocusTasks[issue.key].jiraToOmnifocusMatched = true;
continue;
}
// There was no Matching Task, so we create the Task
const taskName = issue.key + " " + issue.fields.summary;
const newTask = new Task(taskName, inbox.beginning);
const taskUrl = jiraUrl + "/browse/" + issue.key;
createdTasks++;
newTask.addTag(tag);
newTask.note =
taskUrl + "\n" + (issue.fields.description || "No description");
}
//Close Tasks or Update comments if enabled
for (const [task_id, task] of Object.entries(omnifocusTasks)) {
if (!task.jiraToOmnifocusMatched) {
var can_delete = true;
if(task.hasChildren){
task.children.forEach(subtask => {
if (!subtask.completed){
can_delete = false;
}
});
}
if(can_delete) {
task.markComplete();
}
}
// Better check anyway, may the ticket is just reasigned
if (processComments) {
const commentUrlParams = "/rest/api/2/issue/"+task_id +"/comment"
const commentUrl = jiraUrl + commentUrlParams;
const commentRequest = URL.FetchRequest.fromString(commentUrl);
commentRequest.method = "GET";
commentRequest.headers = { Authorization: `Basic ${credentials}` };
var requestPromiseComment = commentRequest.fetch();
requestPromiseComment.then((commentResponse) => {
const jsonComments = JSON.parse(commentResponse.bodyString);
for (const comment of jsonComments.comments) {
const comment_id = task_id + " " + comment.id;
if (!omnifocusComments[comment_id] ) {
const taskName = comment_id + " Comment:\n" + comment.body;
const newTask = new Task(taskName, task);
newTask.addTag(commentTag);
createdComments++;
}
}
});
}
};
}
console.log(`NOTICE: Synced ${jiraUrl}. Checked ${checkedTickets} Tickets, Created ${createdTasks} new tasks, Added ${createdComments} Subtickets for Comments`);
});
requestPromise.catch((err) => {
console.log(`DEBUG: catch error ${err}`);
});
});
});
action.validate = function (selection, sender) {
//
// This action is always valid.
//
return true;
};
return action;
})();