-
-
Notifications
You must be signed in to change notification settings - Fork 55
218 lines (186 loc) · 8.49 KB
/
create-discussions.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
name: Create GitHub Discussions
on:
pull_request:
types: [opened]
paths:
- 'src/content/apps/**'
workflow_dispatch:
inputs:
createForExisting:
description: 'Create discussions for existing apps'
required: true
default: 'true'
type: boolean
jobs:
create-discussion:
runs-on: ubuntu-latest
permissions:
discussions: write
pull-requests: read
contents: read
steps:
- uses: actions/checkout@v4
- name: Create Discussion for PR
if: github.event_name == 'pull_request'
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const path = require('path');
async function ensureDiscussionCategory() {
// Get all discussion categories
const { data: categories } = await github.rest.discussions.listRepoCategories({
owner: context.repo.owner,
repo: context.repo.repo
});
// Look for "App Voting" category
let category = categories.find(c => c.name === "App Voting");
// Create category if it doesn't exist
if (!category) {
const { data: newCategory } = await github.rest.discussions.createRepoCategory({
owner: context.repo.owner,
repo: context.repo.repo,
name: "App Voting",
description: "Vote for community apps",
format: "discussion"
});
category = newCategory;
}
return category.id;
}
async function createDiscussion(content) {
// Parse the frontmatter manually
const frontmatterMatch = content.match(/---\n([\s\S]*?)\n---/);
if (!frontmatterMatch) return;
const frontmatter = frontmatterMatch[1];
// Parse the simple YAML format manually
const appData = {};
frontmatter.split('\n').forEach(line => {
const match = line.match(/^(\w+):\s*"?([^"]*)"?$/);
if (match) {
appData[match[1]] = match[2];
}
});
if (!appData.name || !appData.author) {
console.log('Missing required fields in frontmatter');
return;
}
// Check if discussion already exists - FIXED API CALL
const { data: existingDiscussions } = await github.rest.search.discussionsQuery({
owner: context.repo.owner,
repo: context.repo.repo,
query: `repo:${context.repo.owner}/${context.repo.repo} "${appData.name} by ${appData.author}" in:title`
});
const existingDiscussion = existingDiscussions.items?.find(d =>
d.title === `Vote: ${appData.name} by ${appData.author}`
);
const categoryId = await ensureDiscussionCategory();
if (!existingDiscussion) {
// Create a discussion - FIXED API CALL
await github.rest.discussions.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: `Vote: ${appData.name} by ${appData.author}`,
body: `🗳️ **Vote for this app by giving it a 👍 reaction!**\n\n${appData.description}`,
category_id: categoryId
});
console.log(`Created discussion for ${appData.name}`);
} else {
console.log(`Discussion already exists for ${appData.name}`);
}
}
// Get the changed files from the PR
const { data: files } = await github.rest.pulls.listFiles({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number
});
// Find new app submissions
const appFiles = files.filter(file =>
file.status === 'added' &&
file.filename.startsWith('src/content/apps/') &&
!file.filename.endsWith('_template.md')
);
for (const file of appFiles) {
const content = Buffer.from((await github.rest.repos.getContent({
owner: context.repo.owner,
repo: context.repo.repo,
path: file.filename,
ref: context.payload.pull_request.head.sha
})).data.content, 'base64').toString();
await createDiscussion(content);
}
- name: Create Discussions for Existing Apps
if: github.event_name == 'workflow_dispatch' && inputs.createForExisting
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const path = require('path');
async function ensureDiscussionCategory() {
const { data: categories } = await github.rest.discussions.listRepoCategories({
owner: context.repo.owner,
repo: context.repo.repo
});
let category = categories.find(c => c.name === "App Voting");
if (!category) {
const { data: newCategory } = await github.rest.discussions.createRepoCategory({
owner: context.repo.owner,
repo: context.repo.repo,
name: "App Voting",
description: "Vote for community apps",
format: "discussion"
});
category = newCategory;
}
return category.id;
}
async function createDiscussion(content) {
// Parse the frontmatter manually
const frontmatterMatch = content.match(/---\n([\s\S]*?)\n---/);
if (!frontmatterMatch) return;
const frontmatter = frontmatterMatch[1];
// Parse the simple YAML format manually
const appData = {};
frontmatter.split('\n').forEach(line => {
const match = line.match(/^(\w+):\s*"?([^"]*)"?$/);
if (match) {
appData[match[1]] = match[2];
}
});
if (!appData.name || !appData.author) {
console.log('Missing required fields in frontmatter');
return;
}
// Check if discussion already exists - FIXED API CALL
const { data: existingDiscussions } = await github.rest.search.discussionsQuery({
owner: context.repo.owner,
repo: context.repo.repo,
query: `repo:${context.repo.owner}/${context.repo.repo} "${appData.name} by ${appData.author}" in:title`
});
const existingDiscussion = existingDiscussions.items?.find(d =>
d.title === `Vote: ${appData.name} by ${appData.author}`
);
const categoryId = await ensureDiscussionCategory();
if (!existingDiscussion) {
// Create a discussion - FIXED API CALL
await github.rest.discussions.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: `Vote: ${appData.name} by ${appData.author}`,
body: `🗳️ **Vote for this app by giving it a 👍 reaction!**\n\n${appData.description}`,
category_id: categoryId
});
console.log(`Created discussion for ${appData.name}`);
} else {
console.log(`Discussion already exists for ${appData.name}`);
}
}
// Get all existing app files
const appsDir = 'src/content/apps';
const files = fs.readdirSync(appsDir)
.filter(file => file.endsWith('.md') && file !== '_template.md');
for (const file of files) {
const content = fs.readFileSync(path.join(appsDir, file), 'utf8');
await createDiscussion(content);
}