-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag-archiver.js
75 lines (67 loc) · 2.82 KB
/
tag-archiver.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
// tag-archiver.js
//
// Delete tags not applied to any incomplete tasks.
//
// As part of the "archiving" process, the following will happen:
// 1. A note is added to each *complete* tasks that had the archived tag.
// 2. A single "report" task is created with a summary (and marked complete).
// 3. Archived tags are deleted.
//
// For #1, any *complete* tasks that have the tag will have a new note attached
// to it with the archived tag's name. For example if "projact-a" is archived,
// the note will have the content "milkscript:archive-tag #project-a".
//
// For #2, the "report" task will be named "milkscript:tag-archiver - archive
// report" and contain a note with a summary of tags archived (deleted) and the
// number of tasks impacted.
//
// A special "input" task can be created to control this script. Name the task
// "milkscript:tag-archiver" and include a single note. The note should contain
// a list of regex or strings (one per line) indicating tags to ignore in the
// archiving process. The input tasks's completion status is not considered. For
// example, the following note content would ignore tags starting with a period,
// as well as the tag "waiting":
//
// /\..+/
// waiting
//
// WARNING: This script has the potential to make significant changes to your
// tasks and tags. Proceed with caution!
function readInputTask(task) {
const notes = task.getNotes();
const keepLines = notes[0].getContent().split(/\n/).map(i => i.trim());
return keepLines.map(i => {
if (i.startsWith('/') && i.endsWith('/')) {
return (tagName) => {
const re = new RegExp(i.slice(1, i.length - 1));
return tagName.match(re) !== null
}
} else {
return (tagName) => tagName.trim() === i;
}
});
}
const tasks = rtm.getTasks('name:"milkscript:tag-archiver"').filter(t => t.getName() === "milkscript:tag-archiver");
if (tasks.length === 1) {
const keepTests = readInputTask(tasks[0]);
const tagsToArchive = rtm.getTags().filter(tag => {
const tagName = tag.getName();
const onKeepList = keepTests.find(test => test(tagName));
return !onKeepList && tag.getTasks('status:incomplete').length === 0;
});
const reportNotes = [];
tagsToArchive.forEach(tag => {
const tagName = tag.getName();
console.log(`Archiving tag ${tagName}`);
// archive
const tasks = rtm.getTasks(`status:complete AND tag:"${tagName}"`);
tasks.forEach(task => {
task.addNote(`milkscript:archive-tag #${tagName}`);
});
tag.delete();
reportNotes.push(`${tagName} - ${tasks.length} completed tasks updated`)
});
if (reportNotes.length !== 0) {
rtm.addTask('milkscript:tag-archiver - archive report').addNote(reportNotes.join('\n')).complete();
}
}