From a053328720a877104591742986c68e233a087cc0 Mon Sep 17 00:00:00 2001 From: tomcky Date: Fri, 26 Jul 2024 19:17:40 +0900 Subject: [PATCH 1/2] draft update --- .github/workflows/add_discussion_comment.yml | 92 ++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 .github/workflows/add_discussion_comment.yml diff --git a/.github/workflows/add_discussion_comment.yml b/.github/workflows/add_discussion_comment.yml new file mode 100644 index 0000000..6155b04 --- /dev/null +++ b/.github/workflows/add_discussion_comment.yml @@ -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" + 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") + 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); + } \ No newline at end of file From dd39493796e030fb87abd04a84f9a1e4c2c8e6ac Mon Sep 17 00:00:00 2001 From: tomcky Date: Fri, 26 Jul 2024 19:26:54 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=E6=9C=AB=E5=B0=BE=E6=94=B9=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/add_discussion_comment.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/add_discussion_comment.yml b/.github/workflows/add_discussion_comment.yml index 6155b04..94f227c 100644 --- a/.github/workflows/add_discussion_comment.yml +++ b/.github/workflows/add_discussion_comment.yml @@ -89,4 +89,4 @@ jobs: }); await createDisussionComment(github, DISCUSSION_ID, body); - } \ No newline at end of file + }