diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml deleted file mode 100644 index 515fee0..0000000 --- a/.github/workflows/deploy.yml +++ /dev/null @@ -1,30 +0,0 @@ -name: SkeetFrameworkDiscordUtils - -on: - release: - types: [created] - -jobs: - build: - runs-on: ubuntu-latest - - strategy: - matrix: - node-version: [18.x] - - steps: - - uses: actions/checkout@v4 - - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v3 - with: - node-version: ${{ matrix.node-version }} - - run: npm i -g yarn - - run: yarn install - - run: yarn test - - name: Disord Notification - run: yarn discord - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - DISCORD_TOKEN: ${{ secrets.DISCORD_TOKEN }} - DISCORD_CHANGELOG_CHANNEL_ID: ${{ secrets.DISCORD_CHANGELOG_CHANNEL_ID }} - DISCORD_CHANNEL_ID_JA: ${{ secrets.DISCORD_CHANNEL_ID_JA }} diff --git a/.github/workflows/discord.yml b/.github/workflows/discord.yml new file mode 100644 index 0000000..a4a4ffc --- /dev/null +++ b/.github/workflows/discord.yml @@ -0,0 +1,41 @@ +name: SkeetFrameworkDiscord + +on: + release: + types: [created] + +jobs: + build: + runs-on: ubuntu-latest + + strategy: + matrix: + node-version: [18.x] + + steps: + - uses: actions/checkout@v4 + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v3 + with: + node-version: ${{ matrix.node-version }} + - run: npm i -g yarn + - run: yarn install + - name: Disord Notification Epics + run: yarn discord epics ${{ github.repository }} + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + DISCORD_TOKEN: ${{ secrets.DISCORD_TOKEN }} + DISCORD_CHANNEL_ID: ${{ secrets.DISCORD_CHANNEL_ID }} + DISCORD_CHANNEL_ID_JA: ${{ secrets.DISCORD_CHANNEL_ID_JA }} + - name: Disord Notification LABO + run: yarn discord labo ${{ github.repository }} + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + DISCORD_TOKEN_LABO: ${{ secrets.DISCORD_TOKEN_LABO }} + LABO_SKEET_CHANNEL_ID: ${{ secrets.LABO_SKEET_CHANNEL_ID }} + - name: Post X package update notification + if: success() + run: | + curl --location ${{ secrets.TW_ENDPOINT }} \ + --header 'Content-Type: application/json' \ + --data '{"repo": "${{ github.repository }}", "hey": "${{ secrets.TW_ENDPOINT_SECRET }}"}' diff --git a/.github/workflows/runDiscordChangeLog.ts b/.github/workflows/runDiscordChangeLog.ts new file mode 100644 index 0000000..d3e9f47 --- /dev/null +++ b/.github/workflows/runDiscordChangeLog.ts @@ -0,0 +1,27 @@ +import dotenv from 'dotenv' +import { discordChangeLog } from '@skeet-framework/discord-utils' +dotenv.config() + +type ProjectType = 'labo' | 'epics' + +const run = async (project: ProjectType, repoName: string) => { + if (project === 'labo') { + console.log('labo') + const token = process.env.DISCORD_TOKEN_LABO || '' + const channelId = process.env.LABO_SKEET_CHANNEL_ID || '' + await discordChangeLog(token, repoName, [channelId]) + } else if (project === 'epics') { + console.log('epics') + const token = process.env.DISCORD_TOKEN || '' + const channelId = process.env.DISCORD_CHANNEL_ID || '' + const channelIdJA = process.env.DISCORD_CHANNEL_ID_JA || '' + await discordChangeLog(token, repoName, [channelId]) + await discordChangeLog(token, repoName, [channelIdJA], 'ja') + } else { + console.log('invalid project name') + } +} + +const project = process.argv[2] as ProjectType +const repoArg = process.argv[3] || '' +void run(project, repoArg) diff --git a/package.json b/package.json index c158c0a..5b2aee0 100644 --- a/package.json +++ b/package.json @@ -39,9 +39,10 @@ "discord.js": "14.13.0" }, "devDependencies": { - "dotenv": "16.3.1", + "@skeet-framework/discord-utils": "^0.2.2", "@types/jest": "29.5.6", "@types/node": "20.8.9", + "dotenv": "16.3.1", "esbuild": "0.19.5", "eslint": "8.52.0", "eslint-config-prettier": "9.0.0", @@ -55,4 +56,4 @@ "typedoc": "0.25.2", "typescript": "5.2.2" } -} \ No newline at end of file +} diff --git a/src/lib/discordChangeLog.ts b/src/lib/discordChangeLog.ts index 27713f7..f4bf0db 100644 --- a/src/lib/discordChangeLog.ts +++ b/src/lib/discordChangeLog.ts @@ -1,6 +1,8 @@ import { execSync } from 'child_process' import { messageChannel } from './messageChannel' +const DISCORD_BODY_LIMIT = 4000 + export const discordChangeLog = async ( discordToken: string, repoName: string, @@ -11,10 +13,12 @@ export const discordChangeLog = async ( const log = getReleaseInfoAsJson(repoName) const repo = repoName.split('/')[1] const headLine = lang === 'ja' ? 'をリリースしました' : 'Released' - const content = `## ${repo} ${log.tag} ${headLine} 🎉 + let content = `## ${repo} ${log.tag} ${headLine} 🎉 ${log.whatsChanged} ` + content = trimContent(content) + for (const channelId of channelIds) { const message = { content, @@ -30,6 +34,17 @@ ${log.whatsChanged} } } +function trimContent(content: string): string { + if (content.length > DISCORD_BODY_LIMIT) { + let lines = content.split('\n') + while (lines.join('\n').length > DISCORD_BODY_LIMIT) { + lines.pop() + } + return lines.join('\n') + } + return content +} + export type ReleaseInfo = { tag: string draft: boolean diff --git a/src/runDiscordChangeLog.ts b/src/runDiscordChangeLog.ts index 23faee6..9927a48 100644 --- a/src/runDiscordChangeLog.ts +++ b/src/runDiscordChangeLog.ts @@ -4,14 +4,23 @@ dotenv.config() const REPO_NAME = 'elsoul/skeet-discord-utils' -const run = async () => { - const token = process.env.DISCORD_TOKEN || '' - const channelId = process.env.DISCORD_CHANNEL_ID || '' - const channelIdJA = process.env.DISCORD_CHANNEL_ID_JA || '' - const laboSkeetChannelId = process.env.LABO_SKEET_CHANNEL_ID || '' - await discordChangeLog(token, REPO_NAME, [channelId]) - await discordChangeLog(token, REPO_NAME, [channelIdJA], 'ja') - await discordChangeLog(token, REPO_NAME, [laboSkeetChannelId]) +const run = async (project: 'labo' | 'epics') => { + if (project === 'labo') { + console.log('labo') + const token = process.env.DISCORD_TOKEN_LABO || '' + const channelId = process.env.LABO_SKEET_CHANNEL_ID || '' + await discordChangeLog(token, REPO_NAME, [channelId]) + } else if (project === 'epics') { + console.log('epics') + const token = process.env.DISCORD_TOKEN || '' + const channelId = process.env.DISCORD_CHANNEL_ID || '' + const channelIdJA = process.env.DISCORD_CHANNEL_ID_JA || '' + await discordChangeLog(token, REPO_NAME, [channelId]) + await discordChangeLog(token, REPO_NAME, [channelIdJA], 'ja') + } else { + console.log('invalid project name') + } } -run() +const project = process.argv[2] as 'labo' | 'epics' +run(project) diff --git a/yarn.lock b/yarn.lock index fc45a5e..24eeba0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -986,6 +986,14 @@ dependencies: "@sinonjs/commons" "^3.0.0" +"@skeet-framework/discord-utils@^0.2.2": + version "0.2.2" + resolved "https://registry.yarnpkg.com/@skeet-framework/discord-utils/-/discord-utils-0.2.2.tgz#866e46fa093ecb4ae5efcb925cb429afd1d3fbf8" + integrity sha512-kN6xmJ7IdS7kAZY3zhUX9+iyjtvwsNaqmzFe0GBtl8RhsuoWav8tzDMknIvizd93LwJkC+/0qt1IaU5o5C8g+A== + dependencies: + discord-interactions "3.4.0" + discord.js "14.13.0" + "@szmarczak/http-timer@^5.0.1": version "5.0.1" resolved "https://registry.npmjs.org/@szmarczak/http-timer/-/http-timer-5.0.1.tgz" @@ -1815,7 +1823,7 @@ dot-prop@^6.0.1: dependencies: is-obj "^2.0.0" -dotenv@^16.3.1: +dotenv@16.3.1: version "16.3.1" resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.3.1.tgz#369034de7d7e5b120972693352a3bf112172cc3e" integrity sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ==