発端
- GitHub でレビューコメントきたら Slack に通知ほしい
- かといって全部のコメント通知されてもウザい
- 定期的に GitHub をみにいってコメントが増えてたら Slack に投げる、みたいなの bot を作ろう
- 全3回の予定。
調べた
- 今回は GitHub から PR を取得するだけ
- ラベルの絞り込み、コメントの取得、 Slack に投げるところは次回以降
- ラベルは何に使ってるかっていうと、投げるチャンネルを決めるため。チーム内の PR には特定のラベルをつける、という運用をしていて、拾った PR はチームのチャンネルに投げる想定。
function getPulls() {
var owner = "owner";
var repo = "repo";
var token = "accessToken"
var options =
{
"method" : "get",
"headers" : {
"Authorization": "token " + token
}
};
var url = "https://api.github.com/repos/" + owner + "/" + repo + "/pulls?per_page=100";
var response = UrlFetchApp.fetch(url, options);
var json = JSON.parse(response.getContentText());
var results = [];
for (var i = 0; i < json.length; i++) {
var hash = json[i];
var labels = getLabels(hash["number"], owner, repo, options);
if (hash && labels.indexOf(targetLabel) >= 0) {
var commentCount = countComments(hash["number"], owner, repo, options);
var updatedInfo = getUpdatedInfo(hash["number"], commentCount);
var isUpdated = updatedInfo.isCreated || updatedInfo.newCommentCount > 0
if (!isUpdated) {
continue;
}
results.push({
"url": hash["html_url"],
"title": hash["title"],
"user": hash["user"]["login"],
"createdAt": hash["created_at"],
"avatar_url": hash["user"]["avatar_url"],
"number": hash["number"],
"commentCount": commentCount,
"newCommentCount": updatedInfo.newCommentCount
});
}
}
return results;
}
解説とか注意点とか