Repository navigation
141 lines (117 loc) · 6 KB
/
Copy pathprocess-ics-issue.yml
File metadata and controls
141 lines (117 loc) · 6 KB
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
name: Process Calendar Submission
on:
issues:
types: [opened]
jobs:
process-calendar:
if: contains(github.event.issue.labels.*.name, 'calendar-submission')
runs-on: ubuntu-latest
permissions:
contents: write
issues: write
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: "24"
- name: Process Issue Data
uses: actions/github-script@v8
with:
script: |
const fs = require('fs');
const path = require('path');
const issueBody = context.payload.issue.body || '';
function getSection(label) {
const sections = issueBody.split(/^###\s+/m);
const match = sections.find(s => s.toLowerCase().startsWith(label.toLowerCase()));
if (!match) return '';
const lines = match.split('\n');
lines.shift();
return lines.join('\n').trim();
}
// Allowed categories whitelist
const ALLOWED_CATEGORIES = [
"Tech",
"Holidays",
"Sports",
"TV & Movies",
"Gaming",
"Music",
"Finance",
"Education",
"Health & Fitness",
"Entertainment",
"Science",
"Community",
"Other"
];
const title = getSection('Calendar Title') || context.payload.issue.title.replace(/^New Calendar:\s*/i, '').trim();
let rawCategory = getSection('Category');
// Normalize category or fallback to 'Other'
let category = ALLOWED_CATEGORIES.find(
c => c.toLowerCase() === rawCategory.toLowerCase()
) || 'Other';
const description = getSection('Description') || 'No description provided.';
let rawIcs = getSection('ICS Content');
rawIcs = rawIcs.replace(/^```[a-z]*\r?\n?/i, '').replace(/\r?\n?```$/i, '').trim();
if (!rawIcs || !rawIcs.includes('BEGIN:VCALENDAR')) {
core.setFailed('Invalid ICS content provided in issue body.');
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.issue.number,
body: '❌ **Submission Failed:** Valid `.ics` calendar content (`BEGIN:VCALENDAR`) was not found in the issue body.'
});
return;
}
const slug = title.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/(^-|-$)/g, '') || `calendar-${context.payload.issue.number}`;
const categoryDir = category.toLowerCase().replace(/[^a-z0-9]+/g, '') || 'other';
const targetFolder = path.join(process.cwd(), 'calendars', categoryDir);
const targetIcsPath = path.join(targetFolder, `${slug}.ics`);
const relativeFilePath = `calendars/${categoryDir}/${slug}.ics`;
fs.mkdirSync(targetFolder, { recursive: true });
fs.writeFileSync(targetIcsPath, rawIcs, 'utf8');
const jsonPath = path.join(process.cwd(), 'calendars.json');
let db = [];
if (fs.existsSync(jsonPath)) {
try {
db = JSON.parse(fs.readFileSync(jsonPath, 'utf8'));
} catch (e) {
db = [];
}
}
const existingIndex = db.findIndex(item => item.id === slug);
const entry = {
id: slug,
title: title,
category: category,
description: description,
file: relativeFilePath
};
if (existingIndex >= 0) {
db[existingIndex] = entry;
} else {
db.push(entry);
}
fs.writeFileSync(jsonPath, JSON.stringify(db, null, 2), 'utf8');
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.issue.number,
body: `🎉 **Calendar Published Successfully!**\n\n- **Title:** ${title}\n- **Category:** ${category}\n- **Path:** \`${relativeFilePath}\`\n\nYour submission is now live in the directory!`
});
await github.rest.issues.update({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.issue.number,
state: 'closed'
});
- name: Commit & Push Changes
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git add calendars/ calendars.json
git commit -m "feat: auto-add calendar from Issue #${{ github.event.issue.number }}" || echo "No changes to commit"
git push