Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rysiva committed Oct 13, 2024
1 parent 2e37cce commit 42334b9
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 60 deletions.
25 changes: 0 additions & 25 deletions __tests__/wait.test.ts

This file was deleted.

12 changes: 4 additions & 8 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
name: 'The name of your action here'
description: 'Provide a description here'
author: 'Your name or organization here'

# Add your action's branding here. This will appear on the GitHub Marketplace.
branding:
icon: 'heart'
color: 'red'
name: 'GitHub notice to Slack'
description:
'This workflow is triggered by the opening of a pull request and notifies
users assigned as reviewers via mention on Slack.'

# Define your inputs here.
inputs:
Expand Down
47 changes: 47 additions & 0 deletions src/create-context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { PullRequestEvent } from '@octokit/webhooks-types'
import { toSlackUser } from './to-slack-user'

interface ImageElement {
type: 'image'
image_url: string
alt_text: string
}

interface TextElement {
type: 'mrkdwn'
text: string
}

export interface Context {
type: 'context'
elements: (ImageElement | TextElement)[]
}

// createContextはFooterみたいなやつで、PR作成者とそのレポジトリ名が表示されるようになっている
export function createContext(payload: PullRequestEvent): Context {
const {
pull_request: { user },
repository: { full_name }
} = payload

const creator = toSlackUser(user.login)

const contextText = `${creator} - :github: ${full_name}`

const context: Context = {
type: 'context',
elements: [
{
type: 'image',
image_url: user.avatar_url,
alt_text: user.login
},
{
type: 'mrkdwn',
text: contextText
}
]
}

return context
}
31 changes: 31 additions & 0 deletions src/create-section.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { PullRequestEvent } from '@octokit/webhooks-types'
import { toSlackUsers } from './to-slack-users'

export interface Section {
type: 'section'
text: {
type: 'mrkdwn'
text: string
}
}

// createSectionでメッセージの本文を作成。レビューアーの一覧とPRタイトルのリンクが表示される。
// toSlackUsersは Record<GithubAccountName, SlackAccountName> をハードコードで管理していて、Githubのアカウント名からSlackのメンションを作成している
export function createSection(payload: PullRequestEvent): Section {
const {
pull_request: { title, html_url, number, requested_reviewers }
} = payload

const reviewers = toSlackUsers(requested_reviewers)

const sectionText = `${reviewers.join(' ')}\n*<${html_url}|#${number} - ${title}>*`
const section: Section = {
type: 'section',
text: {
type: 'mrkdwn',
text: sectionText
}
}

return section
}
26 changes: 26 additions & 0 deletions src/create-slack-message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import * as core from '@actions/core'
import * as github from '@actions/github'
import { PullRequestEvent } from '@octokit/webhooks-types'
import { createContext } from './create-context'
import { createSection } from './create-section'

export function createSlackMessage(): void {
const payload = github.context.payload as PullRequestEvent

if (payload.action !== 'opened') {
throw new Error(
'This action is supposed to be run on pull_request opened event'
)
}

if (!payload.pull_request) {
throw new Error('This action is supposed to be run on pull_request event')
}

const section = createSection(payload)
const context = createContext(payload)

const message = JSON.stringify({ blocks: [section, context] })

core.setOutput('message', message)
}
15 changes: 2 additions & 13 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
import * as core from '@actions/core'
import { wait } from './wait'
import { createSlackMessage } from './create-slack-message'

/**
* The main function for the action.
* @returns {Promise<void>} Resolves when the action is complete.
*/
export async function run(): Promise<void> {
try {
const ms: string = core.getInput('milliseconds')

// Debug logs are only output if the `ACTIONS_STEP_DEBUG` secret is true
core.debug(`Waiting ${ms} milliseconds ...`)

// Log the current timestamp, wait, then log the new timestamp
core.debug(new Date().toTimeString())
await wait(parseInt(ms, 10))
core.debug(new Date().toTimeString())

// Set outputs for other workflow steps to use
core.setOutput('time', new Date().toTimeString())
createSlackMessage()
} catch (error) {
// Fail the workflow run if an error occurs
if (error instanceof Error) core.setFailed(error.message)
Expand Down
8 changes: 8 additions & 0 deletions src/to-slack-user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function toSlackUser(githubUsername: string): string {
const userMappings: Record<string, string> = {
rysiva: 'Ryohei'
}

const slackUsername = userMappings[githubUsername]
return slackUsername ? `<@${slackUsername}>` : githubUsername
}
11 changes: 11 additions & 0 deletions src/to-slack-users.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { User, Team } from '@octokit/webhooks-types'
import { toSlackUser } from './to-slack-user'

export function toSlackUsers(requestedReviewers: (User | Team)[]): string[] {
return requestedReviewers.map(reviewer => {
if ('login' in reviewer) {
return toSlackUser(reviewer.login)
}
throw new Error('ログインプロパティが見つかりません')
})
}
14 changes: 0 additions & 14 deletions src/wait.ts

This file was deleted.

0 comments on commit 42334b9

Please sign in to comment.