Skip to content
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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions .github/workflows/add_discussion_comment.yml
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imo:

ファイル名の案:

  • add_gh_discussion_comment.yml
    • 他 workflow と同じように gh は付けたほうが良い
  • add_comment_to_gh_discussion.yml
    • もう少し具体的な名前にする
  • append_comment_to_gh_discussion.yml
    • append で後ろに追加するニュアンスにする?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: 全体的に YAML のインデントは 2 でお願いしますw

Copy link
Member Author

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

must:
ここのイメージがうまく掴めませんでした🙏
実際にこの workflow を使うサンプルコードがあると分かりやすいと思います。

あと、この comment_seed_path の中身は、ユースケースごとに違いそうなので、どう需要に応えるか/応えないかが今回の肝かと思いました。

Copy link
Member Author

@TomckySan TomckySan Aug 1, 2024

Choose a reason for hiding this comment

The 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);
}