jointrashposs/__misc/gas/code.gs

87 lines
2.6 KiB
Text
Raw Normal View History

// 変更したら「デプロイ」を行うこと
2023-12-21 11:32:32 +01:00
const env = PropertiesService.getScriptProperties().getProperties();
// Misskey
const MI_API_TOKEN = env.MISSKEY_API_TOKEN ?? null;
// Cloudflare
const CF_API_TOKEN = env.CF_API_TOKEN ?? null;
const CF_ZONE_ID = env.CF_ZONE_ID ?? null;
// Github 前のコミットSHA
const GH_PREVIOUS_COMMIT_SHA = env.GH_PREVIOUS_COMMIT_SHA ?? null;
2023-12-21 11:32:32 +01:00
function doPost(e) {
const params = JSON.parse(e.postData.getDataAsString());
// プロダクションのデプロイ以外・デプロイ成功以外には反応しない
if (params?.deployment?.environment !== 'Production' || params?.action !== 'created') {
return;
}
// Cloudflareのキャッシュパージをやる
if (CF_API_TOKEN && CF_ZONE_ID) {
purgeCfCache();
}
// デプロイされたことをMisskeyに通知する
if (MI_API_TOKEN) {
2023-12-24 07:58:00 +01:00
postToMisskey(params);
2023-12-21 11:32:32 +01:00
}
// 今回のSHAを保存
PropertiesService.getScriptProperties().setProperty('GH_PREVIOUS_COMMIT_SHA', params.deployment.sha);
}
function getChangeLog(d) {
if (!GH_PREVIOUS_COMMIT_SHA) return null;
const res = JSON.parse(UrlFetchApp.fetch(`https://api.github.com/repos/misskey-dev/misskey-hub-next/compare/${GH_PREVIOUS_COMMIT_SHA}...${d.deployment.sha}`).getContentText());
return res.commits.map((commit) => `・${commit.commit.message.split('\n')[0]}`).filter((v) => !v.startsWith('・Merge branch \'master\' of')).reverse().join('\n');
2023-12-21 11:32:32 +01:00
}
function postToMisskey(d) {
var data = {
i: MI_API_TOKEN,
2023-12-22 12:19:28 +01:00
text: `$[tada 📢] **Misskey Hub が更新されました!**
早速チェックしましょう ▶ ${d?.repository?.homepage ?? 'https://misskey-hub.net/'}
**【今回の更新点】**
${getChangeLog(d) ?? "取得できませんでした…"}`,
2023-12-21 11:32:32 +01:00
visibility: 'public',
cw: null,
localOnly: false,
reactionAcceptance: 'nonSensitiveOnly',
poll: null,
};
var options = {
contentType: "application/json",
method: "POST",
payload: JSON.stringify(data),
};
const createResult = JSON.parse(UrlFetchApp.fetch("https://misskey.io/api/notes/create", options).getContentText());
Logger.log(createResult);
}
function purgeCfCache() {
var data = {
purge_everything: true,
};
var options = {
contentType: "application/json",
method: "POST",
headers: {
'Authorization': `Bearer ${CF_API_TOKEN}`,
},
payload: JSON.stringify(data),
};
const createResult = JSON.parse(UrlFetchApp.fetch(`https://api.cloudflare.com/client/v4/zones/${CF_ZONE_ID}/purge_cache`, options).getContentText());
Logger.log(createResult);
}