-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoGenerateKnowledge.py
More file actions
271 lines (204 loc) · 6.13 KB
/
Copy pathAutoGenerateKnowledge.py
File metadata and controls
271 lines (204 loc) · 6.13 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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
🇹🇭 Python: Auto Generate Knowledge
ถ้าจะใช้ Python แทน TypeScript ผมแนะนำให้แยกเป็น reusable script แล้วให้ GitHub Actions เรียกใช้ เพื่อสร้าง Knowledge จาก PR / Issue / CI event
Structure
scripts/
└── knowledge/
├── __init__.py
├── generator.py
├── github.py
└── models.py
docs/
└── knowledge/
├── prs/
├── issues/
├── ci-cd/
├── dependencies/
├── security/
└── decisions/
scripts/knowledge/models.py
from dataclasses import dataclass, field
@dataclass
class Knowledge:
title: str
knowledge_type: str
source: str
summary: str
status: str = "active"
tags: list[str] = field(default_factory=list)
metadata: dict[str, str] = field(default_factory=dict)
scripts/knowledge/generator.py
from __future__ import annotations
import os
import re
from datetime import datetime, timezone
from pathlib import Path
from models import Knowledge
ROOT = Path("docs/knowledge")
def slugify(value: str) -> str:
value = value.lower().strip()
value = re.sub(r"[^a-z0-9]+", "-", value)
return value.strip("-")
def render(item: Knowledge) -> str:
tags = ", ".join(f"`{tag}`" for tag in item.tags)
metadata = "\n".join(
f"- {key}: `{value}`"
for key, value in item.metadata.items()
)
return f"""# {item.title}
## Metadata
- Type: `{item.knowledge_type}`
- Status: `{item.status}`
- Source: {item.source}
- Generated: `{datetime.now(timezone.utc).isoformat()}`
- Tags: {tags}
{metadata}
## Summary
{item.summary}
## Automation
This document was generated automatically from GitHub repository activity.
## Source
{item.source}
"""
def generate(item: Knowledge) -> Path:
category = slugify(item.knowledge_type)
directory = ROOT / category
directory.mkdir(parents=True, exist_ok=True)
filename = f"{slugify(item.title)}.md"
output = directory / filename
output.write_text(
render(item),
encoding="utf-8",
)
return output
def main() -> None:
item = Knowledge(
title=os.getenv(
"KNOWLEDGE_TITLE",
"CrystalCastle Repository Activity",
),
knowledge_type=os.getenv(
"KNOWLEDGE_TYPE",
"ci-cd",
),
source=os.getenv(
"KNOWLEDGE_SOURCE",
"https://github.com/ZyntroAI/new-crystalcastle",
),
summary=os.getenv(
"KNOWLEDGE_SUMMARY",
"Automated knowledge generated from GitHub activity.",
),
tags=os.getenv(
"KNOWLEDGE_TAGS",
"github,automation,ci-cd",
).split(","),
metadata={
"Event": os.getenv("GITHUB_EVENT_NAME", "local"),
"Repository": os.getenv(
"GITHUB_REPOSITORY",
"ZyntroAI/new-crystalcastle",
),
"Ref": os.getenv("GITHUB_REF", "local"),
"SHA": os.getenv("GITHUB_SHA", "local"),
},
)
output = generate(item)
print(f"Knowledge generated: {output}")
if __name__ == "__main__":
main()
requirements.txt
ไม่จำเป็นต้องลง package เพิ่มสำหรับ generator ตัวนี้ เพราะใช้ Python standard library:
# No external dependencies
GitHub Actions
.github/workflows/knowledge-auto-generate.yml
name: Knowledge Auto Generate
on:
pull_request:
types:
- opened
- closed
- synchronize
issues:
types:
- opened
- closed
workflow_run:
workflows:
- CrystalCastle CodeRabbit + Tests
- Dependency Review
types:
- completed
workflow_dispatch:
permissions:
contents: write
pull-requests: read
issues: read
actions: read
concurrency:
group: knowledge-${{ github.ref }}
cancel-in-progress: true
jobs:
generate:
name: Generate Knowledge
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Generate knowledge
env:
KNOWLEDGE_TITLE: "CrystalCastle Repository Activity"
KNOWLEDGE_TYPE: "ci-cd"
KNOWLEDGE_SOURCE: "https://github.com/${{ github.repository }}"
KNOWLEDGE_SUMMARY: |
Automated knowledge generated from GitHub activity.
Event: ${{ github.event_name }}
Ref: ${{ github.ref }}
SHA: ${{ github.sha }}
KNOWLEDGE_TAGS: "github,automation,ci-cd,knowledge"
run: |
PYTHONPATH=scripts/knowledge \
python scripts/knowledge/generator.py
- name: Commit knowledge
run: |
git config user.name "github-actions[bot]"
git config user.email \
"41898282+github-actions[bot]@users.noreply.github.com"
git add docs/knowledge/
if git diff --cached --quiet; then
echo "No knowledge changes."
exit 0
fi
git commit \
-m "docs(knowledge): auto-generate repository knowledge"
git push
🇬🇧 Knowledge Pipeline
GitHub
│
├── Pull Request
├── Issue
├── CI
├── Dependency Review
└── CodeRabbit
│
▼
Python Generator
│
┌────┼────┐
▼ ▼ ▼
PR CI Security
│ │ │
└────┼─────┘
▼
Markdown Knowledge
│
▼
docs/knowledge/
สำหรับ new-crystalcastle ผมแนะนำให้ Python เป็น knowledge engine และให้ GitHub Actions เป็น event/orchestration layer ส่วน Slack CLI เป็น notification layer:
GitHub → Python Knowledge → Markdown → CI → Slack CLI
I can also add GitHub API extraction so Python automatically generates knowledge from actual PR #59, Issues, reviews, and workflow results.