-
Notifications
You must be signed in to change notification settings - Fork 2
154 lines (138 loc) · 5.46 KB
/
create_gh_discussion.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# ## Summary
#
# Create GitHub Discussion from the given arguments.
# ## Usage
#
# name: Create GitHub Discussion
#
# 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
# ## Note
#
# category_slug は各カテゴリを選択した時の、URL 末尾の文字列です。以下は例です。
# カテゴリ `Q&A` の slug は `q-a`:
# https://github.com/<org>/<repo>/discussions/categories/q-a
# カテゴリ `Show and tell` の slug は `show-and-tell`:
# https://github.com/<org>/<repo>/discussions/categories/show-and-tell
name: Create GitHub Discussion
on:
workflow_call:
inputs:
title:
description: 作成するDiscussionのタイトルを設定してください。
required: true
type: string
category_slug:
description: 作成するDiscussionのカテゴリslugを設定してください。
default: general
type: string
description_template_path:
description: 作成するDiscussionの説明文に利用するテンプレートファイルのパスを設定してください。
required: true
type: string
outputs:
discussion_id:
description: 作成したDiscussionのIDを返します。
value: ${{ jobs.create_discussion.outputs.discussion_id }}
jobs:
create_discussion:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Run script
id: create_discussion
env:
REPOSITORY: ${{ github.repository }}
CATEGORY_SLUG: ${{ inputs.category_slug }}
TITLE: ${{ inputs.title }}
DESCRIPTION_TEMPLATE_PATH: ${{ inputs.description_template_path }}
uses: actions/github-script@v7
with:
script: |
const fs = require("fs");
async function featchRepoIdAndDiscussionCategoryId(github, owner, repositoryName, discussionCategorySlug) {
const result = await github.graphql(`query($owner:String!, $repositoryName:String!, $discussionCategorySlug:String!) {
repository(owner: $owner, name: $repositoryName) {
id
discussionCategory(slug: $discussionCategorySlug) {
id
}
}
}`, {
owner: owner,
repositoryName: repositoryName,
discussionCategorySlug: discussionCategorySlug,
});
return {repositoryId: result.repository.id, categoryId: result.repository.discussionCategory.id};
}
async function createDiscussion(github, repositoryId, categoryId, title, description) {
const result = await github.graphql(`mutation($repositoryId:ID!, $categoryId:ID!, $title:String!, $body:String!) {
createDiscussion(input: {repositoryId: $repositoryId, categoryId: $categoryId, title: $title, body: $body}) {
discussion {
id
}
}
}`, {
repositoryId: repositoryId,
categoryId: categoryId,
title: title,
body: description
});
return result.createDiscussion.discussion.id;
}
const { REPOSITORY, CATEGORY_SLUG, TITLE, DESCRIPTION_TEMPLATE_PATH } = process.env;
const [owner, repositoryName] = REPOSITORY.split('/');
const repositoryIdAndCategoryId = await featchRepoIdAndDiscussionCategoryId(
github,
owner,
repositoryName,
CATEGORY_SLUG,
)
const discussionTitle = TITLE;
const description = (() => {
const DEFAULT_DESCRIPTION = '<!-- Write description here -->';
try {
const desc = fs.readFileSync(DESCRIPTION_TEMPLATE_PATH, "utf8");
return desc.trim() === '' ? DEFAULT_DESCRIPTION : desc;
} catch (error) {
if (error.code === 'ENOENT') {
console.error("Error reading description template file:", error);
return DEFAULT_DESCRIPTION;
}
else {
throw error;
}
}
})();
const discussionId = await createDiscussion(
github,
repositoryIdAndCategoryId.repositoryId,
repositoryIdAndCategoryId.categoryId,
discussionTitle,
description,
);
core.setOutput("discussion_id", discussionId);
outputs:
discussion_id: ${{ steps.create_discussion.outputs.discussion_id }}