Skip to content

Commit aa7a790

Browse files
authored
Merge pull request #32 from keyoke/keyoke/features
refactor
2 parents 5235760 + d0b11fb commit aa7a790

1 file changed

Lines changed: 82 additions & 68 deletions

File tree

src/import-csv-discussions-action.ts

Lines changed: 82 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -143,69 +143,16 @@ class ImportCSVDiscussionsAction implements IContributedMenuSource {
143143
// Loop through each record in this batch and generate the JSON
144144
batch.forEach(async (record: any) => {
145145
this._logger.debug("record", record);
146-
147-
let header: Array<string> = [];
148-
let cols: Array<string> = [];
149-
150-
// build our html table header rows and body rows
151-
Object.keys(record).forEach(key => {
152-
if (key !== "Title" &&
153-
key !== "WorkItemId") {
154-
header.push(`<th>${key}</th>`);
155-
cols.push(`<td>${record[key]}</td>`);
156-
}
157-
});
158-
159-
this._logger.debug("header", header);
160-
this._logger.debug("cols", cols);
161-
162-
// put the table together with its title
163-
let discussion_comment = {
164-
text: `<table style="width:100%">
165-
<thead>
166-
<tr>
167-
<th colspan="${header.length}">${record.Title}</th>
168-
</tr>
169-
<tr>
170-
${header.join("\n")}
171-
</tr>
172-
</thead>
173-
<tbody>
174-
<tr>
175-
${cols.join("\n")}
176-
</tr>
177-
</tbody>
178-
</table>`
179-
}
180-
181-
this._logger.debug("discussion_comment", discussion_comment);
182-
183-
/* batch_payload.push({
184-
"method": "PATCH",
185-
"uri": `/_apis/wit/workitems/${record.WorkItemId}?api-version=4.1`,
186-
"headers": {
187-
"Content-Type": "application/json-patch+json"
188-
},
189-
"body": [{
190-
"op": "add",
191-
"path": "/fields/System.History",
192-
"value": `${discussion_comment}`
193-
}
194-
]
195-
}); */
196146
try {
197-
this._logger.info(`Adding comment for id '${record.WorkItemId}'`);
198-
199-
let json = await this.postComment(`${hostBaseUrl}${project.name}/_apis/wit/workItems/${record.WorkItemId}/comments?api-version=6.0-preview.3`, accessToken, JSON.stringify(discussion_comment));
200-
201-
this._logger.info(`Successfully added comment for id '${record.WorkItemId}'`);
202-
203-
// log any JSON to debug
204-
this._logger.debug("json", json);
147+
if(!await this.createComment(`${hostBaseUrl}${project.name}/_apis/wit/workItems/${record.WorkItemId}/comments?api-version=6.0-preview.3`, accessToken, record))
148+
{
149+
this._logger.info(`Failed to add comment.`, record);
150+
// Save this failure for later
151+
failures.push(Object.assign({}, record));
152+
}
205153
}
206154
catch (error) {
207155
this._logger.info(`Failed to add comment.`, record, error);
208-
209156
// Save this failure for later
210157
failures.push(Object.assign({}, record));
211158
}
@@ -287,16 +234,83 @@ class ImportCSVDiscussionsAction implements IContributedMenuSource {
287234
});
288235
}
289236

290-
async postComment(url: string, accessToken: string, body: string) : Promise<any> {
291-
let response: Response = await this._fetch(url, {
292-
method: 'POST',
293-
headers: {
294-
'Authorization': `Bearer ${accessToken}`,
295-
'Content-Type': 'application/json',
296-
},
297-
body: body
237+
async createComment(url: string, accessToken: string, record: any): Promise<boolean> {
238+
return new Promise(async (resolve, reject) => {
239+
let header: Array<string> = [];
240+
let cols: Array<string> = [];
241+
242+
// build our html table header rows and body rows
243+
Object.keys(record).forEach(key => {
244+
if (key !== "Title" &&
245+
key !== "WorkItemId") {
246+
header.push(`<th>${key}</th>`);
247+
cols.push(`<td>${record[key]}</td>`);
248+
}
249+
});
250+
251+
this._logger.debug("header", header);
252+
this._logger.debug("cols", cols);
253+
254+
// put the table together with its title
255+
let discussion_comment = {
256+
text: `<table style="width:100%">
257+
<thead>
258+
<tr>
259+
<th colspan="${header.length}">${record.Title}</th>
260+
</tr>
261+
<tr>
262+
${header.join("\n")}
263+
</tr>
264+
</thead>
265+
<tbody>
266+
<tr>
267+
${cols.join("\n")}
268+
</tr>
269+
</tbody>
270+
</table>`
271+
}
272+
273+
this._logger.debug("discussion_comment", discussion_comment);
274+
275+
/* batch_payload.push({
276+
"method": "PATCH",
277+
"uri": `/_apis/wit/workitems/${record.WorkItemId}?api-version=4.1`,
278+
"headers": {
279+
"Content-Type": "application/json-patch+json"
280+
},
281+
"body": [{
282+
"op": "add",
283+
"path": "/fields/System.History",
284+
"value": `${discussion_comment}`
285+
}
286+
]
287+
}); */
288+
289+
this._logger.info(`Adding comment for id '${record.WorkItemId}'`);
290+
291+
let response: Response = await this._fetch(url, {
292+
method: 'POST',
293+
headers: {
294+
'Authorization': `Bearer ${accessToken}`,
295+
'Content-Type': 'application/json',
296+
},
297+
body: JSON.stringify(discussion_comment)
298+
}).then(async (response: Response) => {
299+
if (response.status >= 200 && response.status < 300) {
300+
let json: string = await response.json();
301+
// log any JSON to debug
302+
this._logger.debug("json", json);
303+
resolve(true);
304+
}
305+
else {
306+
resolve(false);
307+
}
308+
}).catch((error: Error) => {
309+
reject(error);
310+
});
311+
312+
this._logger.info(`Successfully added comment for id '${record.WorkItemId}'`);
298313
});
299-
return await response.json();
300314
};
301315

302316
public execute(actionContext: any) {

0 commit comments

Comments
 (0)