-
Notifications
You must be signed in to change notification settings - Fork 2
指定した GitHub Discussion ID に固定コメントを追加する reusable workflow を作成 #72
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,135 @@ | ||
| # ## 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 | ||
| with: | ||
| script: | | ||
| const fs = require("fs"); | ||
| const { DISCUSSION_ID, COMMENT_TEMPLATE_PATH, REPLY_TO_COMMENT_ID } = process.env; | ||
|
|
||
| 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 }} | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
📝 GitHub製のアクションはv7指定で問題ない