Last active
October 14, 2020 13:44
-
-
Save inokappa/a52298dbf4695ca4f2498cf2ae3d9149 to your computer and use it in GitHub Desktop.
Backlog で完了となっていない課題を Google SpreadSheet に取得するスクリプトサンプル
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Backlog API | |
var backlog_team = "hage" | |
var backlog_api_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; | |
var backlog_assigneeid = "12345"; | |
// SpreadSheet | |
var sheet_id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; | |
var sheet_name = "my_issues"; | |
function change_issue_status(issue_key, status_code, update_comment) { | |
//Logger.log("issue_key: " + issue_key); | |
//Logger.log("status_code: " + update_comment); | |
// Backlog 課題のステータスを更新 | |
var options = {'method' : 'patch', 'contentType': 'application/x-www-form-urlencoded'}; | |
if (update_comment != '') { | |
query = "&statusId=" + status_code + "&comment=" + update_comment; | |
} else { | |
query = "&statusId=" + status_code; | |
} | |
var res = UrlFetchApp.fetch("https://" + backlog_team + ".backlog.jp/api/v2/issues/" + issue_key + "?apiKey=" + backlog_api_key + query, options); | |
if (res.getResponseCode() != 200) { | |
return false; | |
} | |
return res | |
} | |
function get_issues_list() { | |
// 完了以外の Backlog 課題の取得 | |
var res = UrlFetchApp.fetch("https://" + backlog_team + ".backlog.jp/api/v2/issues?apiKey=" + backlog_api_key + "&assigneeId[]=" + backlog_assigneeid + "&order=&statusId[]=3&statusId[]=2&statusId[]=1&count=100"); | |
if (res.getResponseCode() != 200) { | |
return false; | |
} | |
return res | |
} | |
function main(action) { | |
// スプレットシートを取得 | |
var MySheet = SpreadsheetApp.openById(sheet_id); | |
// スプレットシートの書き込む位置を指定 | |
var range= MySheet.getSheetByName(sheet_name).getRange(2, 1); | |
// Backlog 課題の取得 | |
var res = get_issues_list(); | |
//Logger.log("response: " + res); | |
// res の JSON を解析して課題を取得する | |
var issuelist = JSON.parse(res.getContentText()); | |
// 一旦、シートをクリアにする | |
MySheet.getSheetByName(sheet_name).getRange("A2:I"+MySheet.getLastRow()+"").clearContent(); | |
//Logger.log("length: " + issuelist.length); | |
for(var i=0; i<issuelist.length; i++) { | |
// スプレッドシートに書き込む | |
var issue_url = "https://" + backlog_team + ".backlog.jp/view/" + issuelist[i]["issueKey"] | |
range.offset(i, 0).setValue('=HYPERLINK(\"' + issue_url + '\",\"' + issuelist[i]["issueKey"] + '\")') | |
range.offset(i, 1).setValue(issuelist[i]["summary"]); | |
range.offset(i, 2).setValue(issuelist[i]["createdUser"]["name"]); | |
range.offset(i, 3).setValue(issuelist[i]["priority"]["name"]); | |
range.offset(i, 4).setValue(issuelist[i]["status"]["name"]); | |
range.offset(i, 5).setValue(issuelist[i]["dueDate"]); | |
var list = SpreadsheetApp.newDataValidation().requireValueInList(['未対応','処理中', '処理済み', '完了'], true).build(); | |
range.offset(i, 6).setDataValidation(list).setValue(issuelist[i]["status"]["name"]); | |
} | |
if (action == undefined) { | |
slack_notification(); | |
} | |
} | |
function onGet(event) { | |
main('on_manual'); | |
} | |
function onUpdate(event) { | |
// スプレットシートを取得 | |
var MySheet = SpreadsheetApp.openById(sheet_id); | |
var range= MySheet.getSheetByName(sheet_name).getRange(1, 1); | |
// ステータス変更 | |
for(var i=1; i<MySheet.getLastRow(); i++) { | |
var issue_key = range.offset(i, 0).getValue(); | |
var current_status = range.offset(i, 4).getValue(); | |
var update_status = range.offset(i, 6).getValue(); | |
var update_comment = range.offset(i, 7).getValue(); | |
// Logger.log("comment: " + update_comment); | |
status_code = ''; | |
if (update_status != current_status) { | |
switch (update_status) { | |
case '未対応': | |
status_code = '1'; | |
break; | |
case '処理中': | |
status_code = '2'; | |
break; | |
case '処理済み': | |
status_code = '3'; | |
break; | |
case '完了': | |
status_code = '4'; | |
break; | |
} | |
change_issue_status(issue_key, status_code, update_comment); | |
} | |
} | |
main('on_manual'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment