generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
129 additions
and
60 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('ログインプロパティが見つかりません') | ||
}) | ||
} |
This file was deleted.
Oops, something went wrong.