Sua Blog
PR action
[PR template 만들기]
.github/pull_request_template.md 작성

[Reviewer 자동 지정]
1. Organization에 Team 생성 및 멤버 초대
2. .github/CODEOWNERS 에 팀을 리뷰어로 등록

만약 Private Repository를 사용해야 한다면 다음과 같은 계정을 사용해야지만 CODEOWNERS가 동작한다. ex) GitHub Pro / GitHub Team / GitHub Enterprise Cloud / GitHub Enterprise Server
[PR 생성시 telegram으로 메세지 보내기]
1. BotFather로 telegram bot 생성

2. 그룹에 bot초대 후 채널 ID확인
https://api.telegram.org/bot<봇ID>/getUpdates
3. Github repository > Settings > Secrets > Actions > New repository screts
- TELEGRAM_TOKEN : bot token 저장
- TELEGRAM_TO: 채널 id 저장
4. Github repository > Actions > set up a workflow yourself
// tg-notify.yml
name: tg-notify
on: [pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Update status
uses: appleboy/telegram-action@master
with:
to: ${{ secrets.TELEGRAM_TO }}
token: ${{ secrets.TELEGRAM_TOKEN }}
message: | #https://help.github.com/en/actions/reference/contexts-and-expression-syntax-for-github-actions#github-context
${{ github.event_name }} commit in ${{ github.repository }} by "${{ github.actor }}". [${{github.sha}}@${{ github.ref }}]
pull_request 시 실행할 action(telegram-action) 생성
5. pr 생성 후 확인
action이 일어나면 메세지가 온다.

(추가) Webhook 이용하기
Github Action을 이용할 때 받을 수 있는 action 의 payload가 한정적이다.
webhook을 이용하면 payload에 담겨오는 데이터가 많아 내가 원하는 정보를 사용할 수 있다. Action을 이용했을 때는 pr url을 받아 올 수 없어 메세지에서 바로 해당 pr로 이동하기 불편하여 action을 제거하고 webhook으로 연결을 바꿨다.
1. webhook을 받아 텔레그램 메세지를 보내는 API를 만든다.
// app.js
const express = require('express');
const bodyParser = require('body-parser');
const TelegramBot = require('node-telegram-bot-api');
require('dotenv').config();
// Initialize express and define a port
const app = express();
const PORT = 8080;
const token = process.env.TELEGRAM_TOKEN;
const chatId = process.env.CHAT_ID;
const bot = new TelegramBot(token, { polling: true });
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.post('/message', (req, res) => {
if (req.body.action === 'opened')
bot.sendMessage(
chatId,
`[Pull Requests Opened]\n${req.body.pull_request.html_url}\n# ${req.body.pull_request.title}\n\n${req.body.pull_request.body}`
);
res.status(200).end();
});
app.listen(PORT, () => console.log(`🚀 Server running on port ${PORT}`));
텔레그램 봇 api 모듈을 설치하고 .env에 있는 텔레그램 토큰과 채널 ID를 저장한다.
/message로 들어온 request가 opened(pr 생성됨)이면 봇으로 메세지를 전송한다.
해당 API를 서버에 Docker container로 띄워뒀다.
2. Webhook 연결
Github repository > Settings > Webhooks > Add webhook

Payload URL에 API 주소를 적고 받을 데이터 타입을 application/json으로 선택한다.
나는 pr 생성시에만 webhook을 호출하고 싶어서 event는 Let me select indinidual events를 선택했다.

Pull requests 선택 후 Add webhook을 클릭하면 webhook이 등록된다.
3. 확인
pr을 생성하고 메세지가 오는지 확인

참고
https://goodgid.github.io/Github-CODEOWNERS/
https://github.com/appleboy/telegram-action
https://cyaninfinite.com/getting-updates-from-github-via-telegram-bot/