Skip to content

Commit c287447

Browse files
authored
Merge pull request #72 from route06/add-discussion-comment-workflow
指定した GitHub Discussion ID に固定コメントを追加する reusable workflow を作成
2 parents 74d6dca + 16646f9 commit c287447

1 file changed

Lines changed: 135 additions & 0 deletions

File tree

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
# ## Summary
2+
#
3+
# Add GitHub Discussion Comment from the given arguments.
4+
5+
# ## Usage
6+
#
7+
# name: Add GitHub Discussion Comment
8+
#
9+
# on:
10+
# schedule:
11+
# # 毎週水曜 13:00 JST に実行
12+
# - cron: "0 4 * * wed"
13+
#
14+
# jobs:
15+
# get_next_meeting_date:
16+
# uses: route06/actions/.github/workflows/calc_next_date.yml@v2
17+
# with:
18+
# interval: weekly
19+
# target_day: wednesday # NOTE: MTG開催曜日に合わせて変更してください
20+
#
21+
# create_discussion:
22+
# needs: get_next_meeting_date
23+
# uses: route06/actions/.github/workflows/create_gh_discussion.yml@v2
24+
# with:
25+
# # NOTE: 作成するDiscussionのタイトルを指定
26+
# title: ${{ needs.get_next_meeting_date.outputs.next_date }} Meeting Title
27+
# # NOTE: category_slugについては補足参照
28+
# category_slug: ideas
29+
# # NOTE: 作成するDiscussionで使用するテンプレートファイルのパスを指定
30+
# description_template_path: _templates/weekly_meeting_discussion/test.md
31+
#
32+
# # 作成した Discussion にコメントを追加する
33+
# add_comment:
34+
# needs: create_discussion
35+
# uses: route06/actions/.github/workflows/add_discussion_comment.yml@v2
36+
# permissions:
37+
# contents: read
38+
# discussions: write
39+
# with:
40+
# discussion_id: ${{ needs.create_discussion.outputs.discussion_id }}
41+
# comment_template_path: _templates/weekly_meeting_discussion/comment1.md
42+
#
43+
# # 追加したコメントに返信する
44+
# reply_to_comment:
45+
# needs: add_comment
46+
# uses: route06/actions/.github/workflows/add_discussion_comment.yml@v2
47+
# permissions:
48+
# contents: read
49+
# discussions: write
50+
# with:
51+
# discussion_id: ${{ needs.add_comment.outputs.discussion_id }}
52+
# comment_template_path: _templates/weekly_meeting_discussion/comment2.md
53+
# reply_to_comment_id: ${{ needs.add_comment.outputs.comment_id }}
54+
55+
name: Add GitHub Discussion Comment
56+
57+
on:
58+
workflow_call:
59+
inputs:
60+
discussion_id:
61+
description: コメントするDiscussionIDを設定してください。
62+
required: true
63+
type: string
64+
comment_template_path:
65+
description: コメントの内容が書かれたテンプレートファイルのパスを設定してください。
66+
required: true
67+
type: string
68+
reply_to_comment_id:
69+
description: 返信先のCommentIDを設定してください。
70+
type: string
71+
outputs:
72+
discussion_id:
73+
description: コメントしたDiscussionIDを返します。
74+
value: ${{ jobs.add_comment.outputs.discussion_id }}
75+
comment_id:
76+
description: 追加したCommentIDを返します。
77+
value: ${{ jobs.add_comment.outputs.comment_id }}
78+
79+
jobs:
80+
add_comment:
81+
runs-on: ubuntu-latest
82+
timeout-minutes: 5
83+
steps:
84+
- uses: actions/checkout@v4
85+
- name: Add Comment
86+
id: add_comment
87+
env:
88+
DISCUSSION_ID: ${{ inputs.discussion_id }}
89+
COMMENT_TEMPLATE_PATH: ${{ inputs.comment_template_path }}
90+
REPLY_TO_COMMENT_ID: ${{ inputs.reply_to_comment_id }}
91+
uses: actions/github-script@v7
92+
with:
93+
script: |
94+
const fs = require("fs");
95+
const { DISCUSSION_ID, COMMENT_TEMPLATE_PATH, REPLY_TO_COMMENT_ID } = process.env;
96+
97+
async function addDisussionComment(github, discussionId, body, replyToId) {
98+
const result = await github.graphql(`mutation($body:String!, $discussion_id:ID!, $reply_to_id:ID) {
99+
addDiscussionComment(input: {discussionId: $discussion_id, body: $body, replyToId: $reply_to_id}) {
100+
comment {
101+
id
102+
}
103+
}
104+
}`, {
105+
discussion_id: discussionId,
106+
body: body,
107+
reply_to_id: replyToId === '' ? null : replyToId,
108+
});
109+
110+
return result.addDiscussionComment.comment.id;
111+
}
112+
113+
const comment = (() => {
114+
const DEFAULT_COMMENT = '<!-- Write comment here -->';
115+
try {
116+
const desc = fs.readFileSync(COMMENT_TEMPLATE_PATH, "utf8");
117+
return desc.trim() === '' ? DEFAULT_COMMENT : desc;
118+
} catch (error) {
119+
if (error.code === 'ENOENT') {
120+
console.error("Error reading comment template file:", error);
121+
return DEFAULT_COMMENT;
122+
}
123+
else {
124+
throw error;
125+
}
126+
}
127+
})();
128+
129+
const comment_id = await addDisussionComment(github, DISCUSSION_ID, comment, REPLY_TO_COMMENT_ID);
130+
131+
core.setOutput("discussion_id", DISCUSSION_ID);
132+
core.setOutput("comment_id", comment_id);
133+
outputs:
134+
discussion_id: ${{ steps.add_comment.outputs.discussion_id }}
135+
comment_id: ${{ steps.add_comment.outputs.comment_id }}

0 commit comments

Comments
 (0)