Slack에 Github PR 목록 알림 메시지

김신영·2023년 11월 16일
0

Slack

목록 보기
1/1
post-thumbnail

Google Apps Script

function openPullRequestsAlarm() {

  var payload = {
    "repo1": getRepositoryPullRequests('repo1'),
    "repo2": getRepositoryPullRequests('repo2'),
    "repo3": getRepositoryPullRequests('repo3'),
    "repo4": getRepositoryPullRequests('repo4'),
    "repo5": getRepositoryPullRequests('repo5'),
  };

  sendToSlack(payload)
}

function getRepositoryPullRequests(repoName) {
  var apiUrl = 'https://api.github.com/repos/${organization}/' + repoName + '/pulls';
  var accessKey = PropertiesService.getScriptProperties().getProperty('accessKey')

  var headers = {
    Authorization: 'Bearer ' + accessKey,
  };

  var options = {
    headers: headers,
    method: 'GET',
    muteHttpExceptions: true
  };
  
  var response = UrlFetchApp.fetch(apiUrl, options);
  var data = JSON.parse(response.getContentText());

  if (data.length == 0){
    return "There are no open pull requests in this repository."
  }

  var parsedData = ""
  for (var i = 0; i < data.length; i++) {
    var pr = data[i];
    parsedData += `${pr.title} ${pr.html_url}\n`
  }

  return parsedData;
}

function sendToSlack(payload) {
  var url = "https://hooks.slack.com/workflows/xxxxxx/xxxxxxx/xxxxxxxxx"; // Slack Web Hook URL
   
  var options = {
    "method": "post",
    "contentType": "application/json",
    "payload": JSON.stringify(payload)
  };

  return UrlFetchApp.fetch(url, options);
 }

Project Properties 설정

업로드중..

  1. Github Access Token 발급
  2. Google Apps Script 프로젝트 Properties로 설정
profile
Hello velog!

0개의 댓글