Skip to content

Commit 2096030

Browse files
0xHieu01Copilot
andauthored
feat: auto-generate llms workflow(#2)
Co-authored-by: Copilot <[email protected]>
1 parent ea958db commit 2096030

File tree

3 files changed

+988
-0
lines changed

3 files changed

+988
-0
lines changed
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
name: Generate CLIX Flutter LLMS
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
llm_mode:
7+
description: 'Generation mode'
8+
type: choice
9+
options: [changed, all]
10+
default: changed
11+
base:
12+
description: 'Base ref/tag/sha for compare (optional)'
13+
type: string
14+
required: false
15+
head:
16+
description: 'Head ref/tag/sha for compare (optional)'
17+
type: string
18+
required: false
19+
release:
20+
types: [published]
21+
22+
permissions:
23+
contents: write
24+
pull-requests: write
25+
26+
jobs:
27+
generate:
28+
name: Generate LLMS
29+
runs-on: ubuntu-latest
30+
env:
31+
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
32+
steps:
33+
- name: Checkout
34+
uses: actions/checkout@v4
35+
with:
36+
fetch-depth: 0
37+
38+
- name: Ensure tools
39+
run: |
40+
sudo apt-get update
41+
sudo apt-get install -y jq
42+
chmod +x scripts/generate_llms.sh || true
43+
44+
- name: Compute compare range and inputs
45+
id: range
46+
shell: bash
47+
run: |
48+
set -euo pipefail
49+
# Defaults from inputs
50+
LLM_MODE="${{ inputs.llm_mode || 'changed' }}"
51+
echo "LLM_MODE=$LLM_MODE" >> "$GITHUB_ENV"
52+
53+
# Optionally honor explicit base/head
54+
if [[ -n "${{ inputs.base }}" && -n "${{ inputs.head }}" ]]; then
55+
echo "COMPARE=${{ inputs.base }}...${{ inputs.head }}" >> "$GITHUB_ENV"
56+
fi
57+
58+
# If release event, try to set COMPARE to previous tag...current tag
59+
if [[ "${{ github.event_name }}" == "release" ]]; then
60+
TAG="${{ github.event.release.tag_name }}"
61+
echo "HEAD_REF=$TAG" >> "$GITHUB_ENV"
62+
git fetch --tags --force --depth=1 origin "+refs/tags/*:refs/tags/*" || true
63+
BASE_TAG="$(git describe --tags --abbrev=0 "${TAG}^" 2>/dev/null || true)"
64+
if [[ -n "$BASE_TAG" ]]; then
65+
echo "COMPARE=${BASE_TAG}...${TAG}" >> "$GITHUB_ENV"
66+
fi
67+
fi
68+
69+
# Echo for debugging
70+
echo "LLM_MODE=${LLM_MODE}"
71+
echo "COMPARE=${COMPARE:-"(none)"}"
72+
73+
- name: Create update branch
74+
id: branch
75+
shell: bash
76+
run: |
77+
set -euo pipefail
78+
DEFAULT_BRANCH="${{ github.event.repository.default_branch }}"
79+
BRANCH="chore/llms-${{ github.run_id }}-${{ github.run_attempt }}"
80+
git checkout -B "$BRANCH" "origin/${DEFAULT_BRANCH}"
81+
echo "BRANCH=$BRANCH" >> "$GITHUB_ENV"
82+
83+
- name: Run generators
84+
shell: bash
85+
env:
86+
GH_TOKEN: ${{ github.token }} # used by scripts for GitHub compare API if needed
87+
run: |
88+
set -euo pipefail
89+
if [[ -z "${OPENAI_API_KEY:-}" ]]; then
90+
echo "OPENAI_API_KEY is not set. Add it in repository Secrets." >&2
91+
exit 1
92+
fi
93+
# Always fetch default branch for raw links
94+
git fetch origin "${{ github.event.repository.default_branch }}" --depth=1 || true
95+
96+
# Determine compare range (strict surgical mode)
97+
if [[ "${LLM_MODE}" == "all" ]]; then
98+
CMD='./scripts/generate_llms.sh --llm-all --slug "'"${{ github.repository }}"'" --branch "'"${{ github.event.repository.default_branch }}"'"'
99+
else
100+
# HEAD ref: inputs.head or default to origin/<default_branch>
101+
if [[ -n "${{ inputs.head }}" ]]; then
102+
HEAD_REF="${{ inputs.head }}"
103+
else
104+
HEAD_REF="${{ github.event.repository.default_branch }}"
105+
fi
106+
107+
if [[ -n "${COMPARE:-}" ]]; then
108+
COMPARE_RANGE="${COMPARE}"
109+
else
110+
# Read base SHA from existing file header
111+
BASE="$(sed -nE 's/^<!--[[:space:]]*commit:[[:space:]]*([0-9a-f]+).*/\1/p' llms.txt | head -1 || true)"
112+
[[ -z "${BASE}" ]] && BASE="${HEAD_REF}"
113+
COMPARE_RANGE="${BASE}...${HEAD_REF}"
114+
fi
115+
CMD='./scripts/generate_llms.sh --compare "'"${COMPARE_RANGE}"'" --llm --slug "'"${{ github.repository }}"'" --branch "'"${{ github.event.repository.default_branch }}"'"'
116+
fi
117+
118+
eval "${CMD}"
119+
120+
- name: Commit and push changes
121+
shell: bash
122+
run: |
123+
set -euo pipefail
124+
# Stage files if present
125+
git add llms.txt 2>/dev/null || true
126+
if git diff --cached --quiet; then
127+
echo "No changes to commit."
128+
echo "NO_CHANGES=true" >> "$GITHUB_ENV"
129+
exit 0
130+
fi
131+
git config user.name "github-actions[bot]"
132+
git config user.email "github-actions[bot]@users.noreply.github.com"
133+
git commit -m "chore: update llms index [skip ci]"
134+
git push origin "HEAD:${BRANCH}"
135+
136+
- name: Create pull request
137+
if: env.NO_CHANGES != 'true'
138+
uses: actions/github-script@v7
139+
with:
140+
script: |
141+
const defaultBranch = context.payload.repository.default_branch;
142+
const head = process.env.BRANCH;
143+
const title = 'chore: update CLIX Flutter LLMS index';
144+
const body = [
145+
'This PR updates the generated CLIX Flutter SDK LLMS index.',
146+
'',
147+
`- Triggered by: ${context.eventName}`,
148+
`- Range: ${process.env.COMPARE || '(none / full)'}`
149+
].join('\n');
150+
try {
151+
const { data: pr } = await github.rest.pulls.create({
152+
owner: context.repo.owner,
153+
repo: context.repo.repo,
154+
title,
155+
head,
156+
base: defaultBranch,
157+
body
158+
});
159+
core.info(`Created PR #${pr.number}`);
160+
} catch (e) {
161+
if (e.status === 422) {
162+
core.info('PR likely already exists or no changes to propose.');
163+
} else {
164+
throw e;
165+
}
166+
}

0 commit comments

Comments
 (0)