-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
指定した GitHub Discussion ID にコメントを追加する reusable workflow #60
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: 全体的に YAML のインデントは 2 でお願いしますw There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. まじごめんなさいwww 🙏🏻 🙇🏻 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# @usage | ||
# | ||
# on: | ||
# workflow_dispatch: | ||
# | ||
# jobs: | ||
# add_discussion_comment: | ||
# uses: route06/actions/.github/workflows/add_discussion_comment.yml@v2 | ||
# with: | ||
# discussion_id: {discussion_id} | ||
# comment_seed_path: _templates/weekly_meeting_discussion/seed.jsonl | ||
# | ||
# 補足: | ||
# comment_seed_pathに指定するファイルフォーマットは以下の通り | ||
# {"template_file_path": "xxx/yyy/zzz.md", "replace_values": {"title": "たいとる", "assign": "たなか", "foo": "こめんと"}} | ||
# template_file_pathにはコメントのもとになるテンプレートファイルを指定 | ||
# replace_valuesはテンプレートファイルの中身に記述された{{ title }}などの変数を置換するためのオブジェクトを指定 | ||
|
||
name: Create discussion | ||
|
||
on: | ||
workflow_call: | ||
inputs: | ||
discussion_id: | ||
description: コメントを追加するDiscussion IDを指定してください。 | ||
required: true | ||
type: string | ||
comment_seed_path: | ||
description: コメントを生成するためのシードデータファイルを指定してください。 | ||
required: true | ||
type: string | ||
|
||
jobs: | ||
add_discussion_comment: | ||
runs-on: ubuntu-latest | ||
timeout-minutes: 10 | ||
env: | ||
TZ: "Asia/Tokyo" | ||
Comment on lines
+37
to
+38
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. may: 要らないかも? |
||
permissions: | ||
contents: read | ||
discussions: write | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
|
||
- name: Run script | ||
env: | ||
DISCUSSION_ID: ${{ inputs.discussion_id }} | ||
COMMENT_SEED_PATH: ${{ inputs.comment_seed_path }} | ||
uses: actions/github-script@v7 | ||
with: | ||
script: | | ||
const fs = require("fs"); | ||
|
||
async function createDisussionComment(github, discussionId, body) { | ||
const result = await github.graphql(`mutation($body:String!, $discussion_id:ID!) { | ||
addDiscussionComment(input: {discussionId: $discussion_id, body: $body}) { | ||
comment { | ||
id | ||
} | ||
} | ||
}`, { | ||
body, | ||
discussion_id: discussionId, | ||
}); | ||
|
||
return result.addDiscussionComment.comment.id; | ||
} | ||
|
||
const { DISCUSSION_ID, COMMENT_SEED_PATH } = process.env; | ||
|
||
const commentSeeds = fs.readFileSync(COMMENT_SEED_PATH, "utf8") | ||
.trim() | ||
.split("\n") | ||
.map(JSON.parse); | ||
|
||
for await (const seed of commentSeeds) { | ||
// seedのイメージ | ||
// {"template_file_path": "xxx/yyy/zzz.md", "replace_values": {"title": "たいとる", "assign": "たなか", "foo": "こめんと"}} | ||
// seedファイルに指定されているファイルをテンプレートとしてコメントを作成 | ||
const template = fs.readFileSync(seed['template_file_path'], "utf8") | ||
Comment on lines
+78
to
+81
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. must: あと、この comment_seed_path の中身は、ユースケースごとに違いそうなので、どう需要に応えるか/応えないかが今回の肝かと思いました。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. そうなんですよね、ここ悩みどころでした。少し考えてみます 💭 |
||
let body = template; | ||
|
||
const replaceValues = seed['replace_values']; | ||
|
||
Object.entries(replaceValues).forEach(([key, value], index) => { | ||
const regex = new RegExp(`{{\\s*${key}\\s*}}`, 'g'); | ||
body = body.replace(regex, value); | ||
}); | ||
|
||
await createDisussionComment(github, DISCUSSION_ID, body); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
imo:
ファイル名の案:
gh
は付けたほうが良い