-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrello2wiki.py
More file actions
237 lines (196 loc) · 8.26 KB
/
trello2wiki.py
File metadata and controls
237 lines (196 loc) · 8.26 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
# Copyright 2022 Eric Thrift
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""trello2wiki (pywikibot script)
This script reads data from a Trello board and constructs a wiki page for each
label on the board, listing the cards sharing that label and the status of each
(taken from the card comments).
The goal of this script is to produce a "status updates" overview, which allows
all items for a given label to be viewed along with their comments, without
having to open individual cards. It also allows users to track changes to the
project using Special:RecentChanges in the wiki.
Card data are presented as a table with three columns: name, comments, and due
date. The Name field is linked to the matching Trello card, which will contain
full information including the task description.
OPTIONS:
-key:KEY (required)
API key for Trello
-token:TOKEN (reuiqred)
API token for Trello
-board:BOARD (required)
The ID of the Trello board from which to retrieve data
-category:CATEGORY
Wikitext string containing the category or categories for the pages,
e.g., [[Category:Foo]]
-pagename_prefix:PREFIX
Prefix to be added to each pagename. You may wish to use a space,
hyphen, or other separator, e.g., 'Trello '. A page will be generated
for each label found on the Trello board.
-preface:PREFACE
Comment for editors, to be included at the top of each page. This might
be a warning that the page is generated automatically, e.g.,
'<!-- DO NOT EDIT! This file is updated by a bot. -->'.
-lists:true
Create a page for each list on the board.
-outline:true
Output a definition list with lists and cards for the entire board,
and save to a single page.
-labels:true
Create a page for each label on the board.
"""
import pywikibot
import requests
import json
import dateutil.parser
from datetime import datetime
import urllib.parse
import markdown
import pypandoc
headers = {
"Accept": "application/json"
}
html_footer = '</table>'
# <a href="{}">View on Trello</a></p></html>
# We use Markdown on the comments, so table cell contents are wrapped in <p>s
# Do the same for other cells to get matching margins
row = ( '<tr><td><p><a href="https://trello.com/c/{id}">{name}</a></p></td>'
'<td>{description}</td><td>{comments}</td><td><p>{due}</p></td></tr>' )
def _get_lists(key='', token='', lists_url='', headers='', query='', **kwargs):
query = {
'key': key,
'token': token,
}
response = requests.request(
"GET",
lists_url,
headers=headers,
params=query
)
lists = json.loads(response.text)
return {l['id']: l['name'] for l in lists}
def _read_board(key='', token='', board_url='', headers='', query='', **kwargs):
query = {
'key': key,
'token': token,
'actions': 'commentCard',
'fields': ['id', 'name', 'labels', 'desc', 'due', 'idList']
}
response = requests.request(
"GET",
board_url,
headers=headers,
params=query
)
cards = json.loads(response.text)
labels = {}
lists = {}
for c in cards:
data = {}
data['labels'] = [l['name'] for l in c['labels']]
comments_list = [markdown.markdown(a['data']['text']) for a in
c['actions']]
data['comments'] = '<HR>'.join(comments_list) # separate paragraphs
data['name'] = c.get('name')
data['id'] = c.get('id')
data['description'] = markdown.markdown(c.get('desc'))
data['due'] = c.get('due')
if data['due']:
d = dateutil.parser.isoparse(data['due'])
data['due'] = d.strftime("%Y-%m-%d")
else:
data['due'] = '--' # allow sorting by date
listname = c['idList']
if not listname in lists.keys():
lists[listname] = []
lists[listname].append(data)
for name in data['labels']:
if not name in labels.keys():
labels[name] = []
labels[name].append(data)
return labels, lists
def _generate(target, cards, header, footer, options):
out = [header] + [row.format(**card) for card in cards] + [footer]
mw = pypandoc.convert_text(' '.join(out), 'mediawiki', format='html')
mw = mw.replace('{|', '{| class="wikitable sortable"')
mw = mw.replace('|-', '|- style="vertical-align: top;"')
# optional hack to remove pandoc-applied column widths
mw = mw.replace('!width="25%"|', '!')
mw = mw.replace('\n\n\n', '\n\n')
target.text = '\n\n'.join([options['preface'], mw, options['category']])
target.save('Updated from {}'.format(options['trello_url']))
def run(*args):
print("running...")
options = {}
local_args = pywikibot.handle_args(args)
site = pywikibot.Site()
required = ['key', 'token', 'board']
optional = ['category', 'pagename_prefix', 'preface', 'lists', 'labels']
for option in optional:
options[option] = ''
for arg in local_args:
option, sep, value = arg.partition(':')
option = option.strip('-')
options[option] = value
for option in required:
if not options.get(option, False):
value = pywikibot.input('Please enter a value for ' + option)
options[option] = value
print(options)
board_url = 'https://api.trello.com/1/boards/{}/cards/open'
lists_url = 'https://api.trello.com/1/boards/{}/lists/open'
filter_url = 'https://trello.com/b/{}?filter=label:'
trello_url = 'https://trello.com/b/{}'
options['board_url'] = board_url.format(options['board'])
options['filter_url'] = filter_url.format(options['board'])
options['trello_url'] = trello_url.format(options['board'])
options['lists_url'] = lists_url.format(options['board'])
labels, lists = _read_board(**options)
header = ( '<table><tr><th>Name</th><th>Description</th>'
'<th>Comments</th><th>Due</th></tr>')
if options.get('lists', None):
print('processing lists...')
listnames = _get_lists(**options)
for (list, cards) in lists.items():
if not listnames.get(list, None): # hidden, etc.
continue
trello_link = board_url
pagename = ' '.join([options['pagename_prefix'], listnames[list]])
footer = html_footer.format(trello_link)
target = pywikibot.Page(site, pagename)
_generate(target, cards, header, footer, options)
if options.get('outline', None):
listnames = _get_lists(**options)
pagename = ' '.join([options['pagename_prefix'], 'outline'])
target = pywikibot.Page(site, pagename)
dl = ['<dl>']
for (list, cards) in lists.items():
if not listnames.get(list, None): # hidden, etc.
continue
dl.append('<dt>{}</dt>'.format(listnames[list]))
dl.extend(['<dd>{}</dd>'.format(c['name']) for c in cards])
dl.append('</dl>')
mw = pypandoc.convert_text(' '.join(dl), 'mediawiki', format='html')
target.text = '\n\n'.join([options['preface'], mw, options['category']])
target.save('Updated from {}'.format(options['trello_url']))
if options.get('labels', None):
for (label, cards) in labels.items():
cards = sorted(cards, key=lambda d: d['due'])
trello_link = options['filter_url'] + urllib.parse.quote(label)
footer = html_footer.format(trello_link)
pagename = ' '.join([options['pagename_prefix'], label])
target = pywikibot.Page(site, pagename)
_generate(target, cards, header, footer, options)
if __name__ == '__main__':
run()