Skip to content

Merge pull request #19 from FlandreDaisuki/add-ci-workflow #29

Merge pull request #19 from FlandreDaisuki/add-ci-workflow

Merge pull request #19 from FlandreDaisuki/add-ci-workflow #29

name: Push Change to Discord Webhook URL
on:
push:
branches:
- main
# Allow manual trigger
workflow_dispatch:
jobs:
send-to-discord-webhook-url:
runs-on: ubuntu-latest
steps:
- name: crawl-commit-message-headers
id: crawl-commit-message-headers
uses: actions/github-script@v7
with:
script: |
core.exportVariable('commits',
JSON.stringify(
context.payload.commits.map(commit => {
const [commitMessageHeaders] = commit.message.split('\n');
const commitId = commit.id.slice(0, 7);
const commitUrl = commit.url;
return [commitMessageHeaders, commitId, commitUrl];
}).reverse()
)
);
- name: build-discord-webhook-payload
id: build-discord-webhook-payload
uses: actions/github-script@v7
env:
# # sandbox webhook url
# DISCORD_WEBHOOK_URL: ${{ secrets.SANDBOX_DISCORD_WEBHOOK_URL }}
# real webhook url
DISCORD_WEBHOOK_URL: ${{ secrets.SPLIT_GAME_CHANNEL_DISCORD_WEBHOOK_URL }}
avatarHost: "https://raw.githubusercontent.com/side-project-at-SPT/split-front/main/src/assets/images/"
with:
script: |
const { commits, DISCORD_WEBHOOK_URL: url, avatarHost } = process.env;
const availableAvatarUrls = ['1', '2', '3', '4'].map(id => `${avatarHost}${id}.png`);
const emojiMap = {
bump: '🔼 ', // 更新
feat: '🚀 ', // 新功能
fix: '🔧 ', // 修復
docs: '📚 ', // 文件
style: '🎨 ', // 格式
refactor: '🔨 ', // 重構
perf: '⚡ ', // 性能
test: '🧪 ', // 測試
build: '🏗️ ', // 構建
ci: '🔬 ', // CI
chore: '🧹 ', // 雜務
revert: '⏪ ', // 回滾
default: '' // 其他
};
const wrapper = cmh => {
const [firstWord, ...rest] = cmh.split(':');
const getEmoji = firstWord => emojiMap[firstWord] || emojiMap.default;
const emoji = getEmoji(firstWord);
return `${emoji}${cmh}`;
};
const content = JSON.parse(commits).map(([commitMessageHeaders, commitId, commitUrl]) => {
return wrapper(commitMessageHeaders) + ' ' + `([#${commitId}](${commitUrl}))`;
}).join('\n');
const discordWebhookPayload = {
content: '# 企鵝搶地後端 update' + '\n' + content,
username: '企鵝探險日誌',
avatar_url: availableAvatarUrls.sort(() => Math.random() - 0.5)[0]
};
const req = new Request(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(discordWebhookPayload)
});
const res = await fetch(req);
if (!res.ok) {
throw new Error(`HTTP error! status: ${res.status}`);
}