-
Notifications
You must be signed in to change notification settings - Fork 4
/
github-todotxt.js
executable file
·203 lines (179 loc) · 5.4 KB
/
github-todotxt.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
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
#!/usr/bin/env node
// github-todotxt.js
// Get data from Github issues and format it for todo.txt
//
// Copyright 2016-2019 Evan Prodromou <[email protected]>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
const fs = require('fs')
const path = require('path')
const _ = require('lodash')
const yargs = require('yargs')
const Octokit = require('@octokit/rest')
const split = require('split')
const { argv } = yargs
.usage('Usage: $0 -t [token]')
.demand('t')
.alias('t', 'token')
.describe('t', 'OAuth token')
.alias('f', 'file')
.describe('f', 'todo.txt file')
.default('f', path.join(process.env.HOME, 'Dropbox', 'todo', 'todo.txt'))
.alias('q', 'quiet')
.describe('q', 'Minimize console output')
.alias('k', 'key')
.describe('k', 'Prefix for issues in todo.txt file')
.default('k', 'issue')
.env('GITHUB_TODOTXT')
.alias('c', 'config')
.describe('c', 'Config file')
.default('c', path.join(process.env.HOME, '.github-todotxt.json'))
.config('config')
.help('h')
.alias('h', 'help')
const projectCase = str => _.upperFirst(_.camelCase(str))
const markComplete = function (text, completed) {
const m = text.match(/^\s*\(([A-Z])\)\s*/)
if (m) {
return `x ${completed} ${text.substr(m[0].length)} pri:${m[1]}`
} else {
return `x ${completed} ${text}`
}
}
const getTodos = async function (filename, key) {
return new Promise((resolve, reject) => {
const todos = []
const ir = new RegExp(`${key}:(\\S+)`)
fs.createReadStream(filename)
.pipe(split())
.on('data', (line) => {
if (line.match(/\S/)) {
const m = line.match(ir)
const todo =
{text: line}
if (m) {
[, todo.issue] = m
}
todos.push(todo)
}
})
.on('error', reject)
.on('end', () => {
resolve(todos)
})
})
}
const writeTodos = async function (filename, todos) {
return new Promise((resolve, reject) => {
const ws = fs.createWriteStream(filename)
.on('error', reject)
.on('finish', resolve)
for (const todo of todos) {
ws.write(`${todo.text}\n`, 'utf-8')
}
ws.end()
})
}
const rename = async function (oldname, newname) {
return new Promise((resolve, reject) => {
fs.rename(oldname, newname, (err) => {
if (err) {
reject(err)
} else {
resolve()
}
})
})
}
const main = async function (argv) {
const token = argv.t
const filename = argv.f
const quiet = (argv.q != null)
const key = argv.k
const note = function (str) {
if (!quiet) {
return process.stdout.write(str)
}
}
const github = new Octokit({
auth: token
})
note(`Getting issues from Github...`)
const issues = await github.paginate('GET /issues')
note(`Done. (${issues.length})\n`)
note(`Getting todos from ${filename}...`)
const todos = await getTodos(filename, key)
note(`Done. (${todos.length})\n`)
let repo, id, todo, number
for (const issue of Array.from(issues)) {
repo = issue.repository.full_name;
({ number } = issue)
id = `${repo}#${number}`
todo = _.find(todos, {issue: id})
if (todo != null) {
if (todo.text.match(/^x/)) {
if (issue.state === 'open') {
note('not closing issue')
}
// XXX: close the github issue
} else {
if (issue.state === 'closed') {
note(`Marking line for issue ${id} complete.\n`)
const completed = (issue.closed_at)
? issue.closed_at.substr(0, 10)
: (new Date()).toISOString().substr(0, 10)
const {text} = todo
todo.text = markComplete(text, completed)
}
}
} else if (issue.state === 'open') {
note(`Adding line for issue ${id}.\n`)
const ts = issue.created_at.substr(0, 10)
const project = projectCase(repo.split('/')[1])
const { title } = issue
let line = `${ts} ${title} ${key}:${repo}#${number} +${project}`
if (issue.milestone != null) {
line += ` +${projectCase(issue.milestone.title)}`
}
todos.push({text: line})
}
}
// Todos with issues that aren't in the list should be marked as done.
for (todo of Array.from(todos)) {
if ((todo.issue == null)) {
continue
}
if (todo.text.match(/^x/)) {
continue
}
const issue = _.find(issues, (issue) => {
repo = issue.repository.full_name;
({ number } = issue)
id = `${repo}#${number}`
return id === todo.issue
})
if ((issue == null)) {
note(`Issue ${todo.issue} not assigned to you; marking it done.\n`)
const completed = (new Date()).toISOString().substr(0, 10)
todo.text = markComplete(todo.text, completed)
}
}
const backup = `${filename}.bak`
note(`Backing up ${filename} to ${backup}...`)
await rename(filename, backup)
note(`Done.\n`)
note(`Writing todos to ${filename}...`)
await writeTodos(filename, todos)
note(`Done.\n`)
}
main(argv)