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 を作成 #72

Merged
merged 2 commits into from
Sep 13, 2024
Merged
Changes from 1 commit
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
139 changes: 139 additions & 0 deletions .github/workflows/add_discussion_comment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
# ## Summary
#
# Add GitHub Discussion Comment from the given arguments.

# ## Usage
#
# name: Add GitHub Discussion Comment
#
# on:
# schedule:
# # 毎週水曜 13:00 JST に実行
# - cron: "0 4 * * wed"
#
# jobs:
# get_next_meeting_date:
# uses: route06/actions/.github/workflows/calc_next_date.yml@v2
# with:
# interval: weekly
# target_day: wednesday # NOTE: MTG開催曜日に合わせて変更してください
#
# create_discussion:
# needs: get_next_meeting_date
# uses: route06/actions/.github/workflows/create_gh_discussion.yml@v2
# with:
# # NOTE: 作成するDiscussionのタイトルを指定
# title: ${{ needs.get_next_meeting_date.outputs.next_date }} Meeting Title
# # NOTE: category_slugについては補足参照
# category_slug: ideas
# # NOTE: 作成するDiscussionで使用するテンプレートファイルのパスを指定
# description_template_path: _templates/weekly_meeting_discussion/test.md
#
# # 作成した Discussion にコメントを追加する
# add_comment:
# needs: create_discussion
# uses: route06/actions/.github/workflows/add_discussion_comment.yml@v2
# permissions:
# contents: read
# discussions: write
# with:
# discussion_id: ${{ needs.create_discussion.outputs.discussion_id }}
# comment_template_path: _templates/weekly_meeting_discussion/comment1.md
#
# # 追加したコメントに返信する
# reply_to_comment:
# needs: add_comment
# uses: route06/actions/.github/workflows/add_discussion_comment.yml@v2
# permissions:
# contents: read
# discussions: write
# with:
# discussion_id: ${{ needs.add_comment.outputs.discussion_id }}
# comment_template_path: _templates/weekly_meeting_discussion/comment2.md
# reply_to_comment_id: ${{ needs.add_comment.outputs.comment_id }}

name: Add GitHub Discussion Comment

on:
workflow_call:
inputs:
discussion_id:
description: コメントするDiscussionIDを設定してください。
required: true
type: string
comment_template_path:
description: コメントの内容が書かれたテンプレートファイルのパスを設定してください。
required: true
type: string
reply_to_comment_id:
description: 返信先のCommentIDを設定してください。
type: string
outputs:
discussion_id:
description: コメントしたDiscussionIDを返します。
value: ${{ jobs.add_comment.outputs.discussion_id }}
comment_id:
description: 追加したCommentIDを返します。
value: ${{ jobs.add_comment.outputs.comment_id }}

jobs:
add_comment:
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- uses: actions/checkout@v4
- name: Add Comment
id: add_comment
env:
DISCUSSION_ID: ${{ inputs.discussion_id }}
COMMENT_TEMPLATE_PATH: ${{ inputs.comment_template_path }}
REPLY_TO_COMMENT_ID: ${{ inputs.reply_to_comment_id }}
uses: actions/github-script@v7
Copy link
Contributor

Choose a reason for hiding this comment

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

📝 GitHub製のアクションはv7指定で問題ない

with:
script: |
const fs = require("fs");
const { DISCUSSION_ID, COMMENT_TEMPLATE_PATH, REPLY_TO_COMMENT_ID } = process.env;

console.log('DISCUSSION_ID: %s', DISCUSSION_ID);
console.log('COMMENT_TEMPLATE_PATH: %s', COMMENT_TEMPLATE_PATH);
console.log('REPLY_TO_COMMENT_ID: %s', REPLY_TO_COMMENT_ID);
Copy link
Contributor

Choose a reason for hiding this comment

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

この辺りは消し忘れでしょうか?


nits: ログ出力を意図する場合、coreモジュールの core.info() などを使う方が適切そうでした。
https://github.com/actions/toolkit/tree/main/packages/core#logging

Copy link
Member Author

Choose a reason for hiding this comment

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

おっと! 消し忘れです! 16646f9 で削除しました。


async function addDisussionComment(github, discussionId, body, replyToId) {
const result = await github.graphql(`mutation($body:String!, $discussion_id:ID!, $reply_to_id:ID) {
addDiscussionComment(input: {discussionId: $discussion_id, body: $body, replyToId: $reply_to_id}) {
comment {
id
}
}
}`, {
discussion_id: discussionId,
body: body,
reply_to_id: replyToId === '' ? null : replyToId,
});

return result.addDiscussionComment.comment.id;
}

const comment = (() => {
const DEFAULT_COMMENT = '<!-- Write comment here -->';
try {
const desc = fs.readFileSync(COMMENT_TEMPLATE_PATH, "utf8");
return desc.trim() === '' ? DEFAULT_COMMENT : desc;
} catch (error) {
if (error.code === 'ENOENT') {
console.error("Error reading comment template file:", error);
return DEFAULT_COMMENT;
}
else {
throw error;
}
}
})();

const comment_id = await addDisussionComment(github, DISCUSSION_ID, comment, REPLY_TO_COMMENT_ID);

core.setOutput("discussion_id", DISCUSSION_ID);
core.setOutput("comment_id", comment_id);
outputs:
discussion_id: ${{ steps.add_comment.outputs.discussion_id }}
comment_id: ${{ steps.add_comment.outputs.comment_id }}