LoginSignup
22
21

More than 5 years have passed since last update.

サーバレスかつ無料でgithubのPRリストを毎日スラックに送る - Google Apps Script

Last updated at Posted at 2016-06-08

image

背景

最近はrailsで開発していますが、毎朝PRのリストをslackになげてくれたら楽だなーと思い、Google Apps Scriptでつくりました。

毎日定時に行うという処理はcronが必要でサーバを用意する必要がありますが、Google Apps Scriptはcronのように定期処理を設定できるので、サーバレスかつ無料でいけます。

以下のようにslackに出すことができました。

image

処理の流れ

  1. Apps ScriptからGithub APIを読んでPRのリストを取得
  2. 結果をScript内で、処理して、Slackになげる

上記のようにするので、slackとgithubのAPIトークンが必要です。

実装手順

Tokenの取得

githubに関しては以下のように設定しました。

image

Apps Scriptの準備

以下をみながらApps ScriptのスクリプトをGoogle Drive上に新規作成をします

コードを書く

以下のコードをコピペします。
slack_token, github_token, owner, repo, channelは適宜かえてください。たとえば、githubのレポジトリのurlが https://github.com/xxx/yyy なら、ownerはxxxでrepoはyyyです。

function execute() {
  var slack_token = "xxx";
  var github_token = "xxx";
  var owner = "xxx";
  var repo = "xxx";
  var channel = "xxx";
  notifyPullRequestToSlack(slack_token, channel, github_token, owner, repo);
}


function notifyPullRequestToSlack(slack_token, channel, github_token, owner, repo) {
  var result = getPullRequestLists(github_token, owner, repo);
  var messages = [];
  for (var i = 0; i < result.length; i++) {
    messages.push("<" + result[i]["url"] + "|" + result[i]["title"] + " by " + result[i]["user"] + ">");
  }
  slack(slack_token, channel, messages.join("\n"));
}

function slack(slack_token, channel, message) {
  var url        = 'https://slack.com/api/chat.postMessage';
  var channel    = '#' + channel;
  var icon_url   = "https://h2.slack-edge.com/3429/plugins/github/assets/service_96.png";
  var method     = 'post';

  var payload = {
    "token": slack_token,
    "channel": channel,
    "text": message,
    "icon_url": icon_url,
    "username": "PRくん"
  };

  var params = {
    'method' : method,
    'payload' : payload
  };

  var response = UrlFetchApp.fetch(url, params);
}

function getPullRequestLists(token, owner, repo) {
  var resultList = [];
  var options =
  {
    "method" : "get",
    "headers" : {
      "Authorization": "token " + token 
    }
  };
  var url = "https://api.github.com/repos/" + owner + "/" + repo + "/pulls";
  var response = UrlFetchApp.fetch(url, options);
  var json = JSON.parse(response.getContentText());
  for (var i = 0; i < json.length; i++) {
    var hash = json[i];

    if (hash && "url" in hash) {
      resultList.push({
        "url": hash["html_url"],
        "title": hash["title"],
        "user": hash["user"]["login"],
        "avatar_url": hash["user"]["avatar_url"]
      });
    }
  }
  return resultList;
}

Apps Script上でcronの設定

以下のように設定できます。注意点としてcronの実行時間は1時間のうちのどこかというふうにしか選べません。
例えば、9-10pmなら午後9-10時のうちのどこかに発動するので正確なcronには向いてません。

image

image

以下で自分が設定したいものに適切に設定します。

image

結び

Google Apps Scriptをつかうとサーバレス&無料でいろいろできちゃいます。

22
21
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
21