Skip to content

Commit 4dda6da

Browse files
committed
feat: implement automation workflow
1 parent 9c17b42 commit 4dda6da

1 file changed

Lines changed: 83 additions & 84 deletions

File tree

.github/workflows/automation.yml

Lines changed: 83 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@ on:
1111
type: choice
1212
options:
1313
- sync
14-
- backup
1514
- report
1615
- all
1716

1817
env:
1918
ORG: emberlamp
20-
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2119

2220
jobs:
2321
sync:
@@ -27,69 +25,56 @@ jobs:
2725
- name: Checkout
2826
uses: actions/checkout@v4
2927

30-
- name: Install gh
31-
run: |
32-
type -p gh >/dev/null 2>&1 || npm install -g gh
33-
34-
- name: Fetch repos config
35-
run: |
36-
echo "Fetching repos from config..."
37-
CONFIG_URL="https://raw.githubusercontent.com/${{ env.ORG }}/config/main/repos.json"
38-
REPOS=$(curl -s $CONFIG_URL | jq -r '.repos[]')
39-
echo "Config repos: $REPOS"
40-
echo "repos=$REPOS" >> $GITHUB_OUTPUT
41-
42-
- name: Get GitHub repos
43-
run: |
44-
echo "Fetching GitHub repos..."
45-
GH_REPOS=$(gh api repos/${{ env.ORG }} --jq '.[].name' | sort | tr '\n' ',' | sed 's/,$//')
46-
echo "GitHub repos: $GH_REPOS"
47-
echo "github_repos=$GH_REPOS" >> $GITHUB_OUTPUT
28+
- name: Setup Python
29+
uses: actions/setup-python@v4
30+
with:
31+
python-version: '3.10'
32+
33+
- name: Install dependencies
34+
run: pip install requests pyyaml
4835

4936
- name: Compare repos
5037
run: |
51-
CONFIG="${{ steps.fetch-config.outputs.repos }}"
52-
GITHUB="${{ steps.fetch-github.outputs.github_repos }}"
38+
python3 << 'PYEOF'
39+
import requests
40+
import os
5341
54-
# Sort both
55-
CONFIG_SORTED=$(echo "$CONFIG" | tr ',' '\n' | sort | tr '\n' ',')
56-
GITHUB_SORTED=$(echo "$GITHUB" | tr ',' '\n' | grep -v '^$' | sort | tr '\n' ',')
42+
org = os.environ.get('ORG', 'emberlamp')
43+
token = os.environ.get('GH_TOKEN', '')
44+
headers = {'Authorization': f'token {token}'} if token else {}
5745
58-
if [ "$CONFIG_SORTED" = "$GITHUB_SORTED" ]; then
59-
echo "✅ Repos are in sync!"
60-
else
61-
echo "⚠️ Repos are out of sync!"
62-
echo "Config: $CONFIG_SORTED"
63-
echo "GitHub: $GITHUB_SORTED"
64-
fi
65-
66-
backup:
67-
runs-on: ubuntu-latest
68-
if: github.event.inputs.action == 'backup' || github.event.inputs.action == 'all'
69-
steps:
70-
- name: Checkout
71-
uses: actions/checkout@v4
72-
73-
- name: Install gh
74-
run: type -p gh >/dev/null 2>&1 || npm install -g gh
75-
76-
- name: Create backup
77-
run: |
78-
echo "Creating backup of all repos..."
79-
BACKUP_FILE="backup-$(date +%Y%m%d-%H%M%S).json"
46+
# Get config repos
47+
config_resp = requests.get(
48+
'https://raw.githubusercontent.com/' + org + '/config/main/repos.json',
49+
headers=headers
50+
)
51+
config_repos = sorted(config_resp.json().get('repos', []))
52+
53+
# Get GitHub repos
54+
gh_resp = requests.get(
55+
'https://api.github.com/orgs/' + org + '/repos',
56+
headers=headers
57+
)
58+
gh_repos = sorted([r['name'] for r in gh_resp.json()])
8059
81-
# Backup repos
82-
gh api repos/${{ env.ORG }} > $BACKUP_FILE
60+
print('Config repos ({0}): {1}'.format(len(config_repos), config_repos))
61+
print('GitHub repos ({0}): {1}'.format(len(gh_repos), gh_repos))
8362
84-
echo "Backup created: $BACKUP_FILE"
85-
echo "backup_file=$BACKUP_FILE" >> $GITHUB_OUTPUT
63+
if config_repos == gh_repos:
64+
print('REPOS_IN_SYNC=true')
65+
else:
66+
print('REPOS_IN_SYNC=false')
67+
missing = set(config_repos) - set(gh_repos)
68+
extra = set(gh_repos) - set(config_repos)
69+
if missing:
70+
print('Missing in GitHub: ' + str(missing))
71+
if extra:
72+
print('Extra in GitHub: ' + str(extra))
73+
PYEOF
8674
87-
- name: Upload backup
88-
uses: actions/upload-artifact@v4
89-
with:
90-
name: repo-backup
91-
path: backup-*.json
92-
retention-days: 30
75+
- name: Report status
76+
run: |
77+
echo "Sync check complete"
9378
9479
report:
9580
runs-on: ubuntu-latest
@@ -98,42 +83,56 @@ jobs:
9883
- name: Checkout
9984
uses: actions/checkout@v4
10085

101-
- name: Install gh
102-
run: type -p gh >/dev/null 2>&1 || npm install -g gh
86+
- name: Setup Python
87+
uses: actions/setup-python@v4
88+
with:
89+
python-version: '3.10'
90+
91+
- name: Install dependencies
92+
run: pip install requests
10393

10494
- name: Generate report
10595
run: |
106-
echo "Generating emberlamp organization report..."
96+
python3 << 'PYEOF'
97+
import requests
98+
import os
99+
from datetime import datetime
100+
101+
org = os.environ.get('ORG', 'emberlamp')
102+
token = os.environ.get('GH_TOKEN', '')
103+
headers = {'Authorization': 'token ' + token} if token else {}
107104
108-
# Get repo count
109-
REPO_COUNT=$(gh api repos/${{ env.ORG }} --jq 'length')
105+
# Get repos
106+
repos_resp = requests.get(
107+
'https://api.github.com/orgs/' + org + '/repos',
108+
headers=headers
109+
)
110+
repos = repos_resp.json()
110111
111-
# Get release count
112-
RELEASE_COUNT=0
113-
for repo in $(gh api repos/${{ env.ORG }} --jq '.[].name'); do
114-
COUNT=$(gh api repos/${{ env.ORG }}/$repo/releases --jq 'length' 2>/dev/null || echo 0)
115-
RELEASE_COUNT=$((RELEASE_COUNT + COUNT))
116-
done
112+
report = []
113+
report.append('# Emberlamp Organization Report')
114+
report.append('')
115+
report.append('Generated: ' + datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
116+
report.append('')
117+
report.append('## Summary')
118+
report.append('- Repositories: ' + str(len(repos)))
119+
report.append('')
120+
report.append('## Repositories')
117121
118-
# Get workflow count
119-
WORKFLOW_COUNT=0
120-
for repo in $(gh api repos/${{ env.ORG }} --jq '.[].name'); do
121-
COUNT=$(gh api repos/${{ env.ORG }}/$repo/actions/workflows --jq '.workflows | length' 2>/dev/null || echo 0)
122-
WORKFLOW_COUNT=$((WORKFLOW_COUNT + COUNT))
123-
done
122+
for repo in repos:
123+
name = repo['name']
124+
url = repo['html_url']
125+
report.append('- [' + name + '](' + url + ')')
124126
125-
echo "# Emberlamp Organization Report" > report.md
126-
echo "" >> report.md
127-
echo "Generated: $(date)" >> report.md
128-
echo "" >> report.md
129-
echo "## Summary" >> report.md
130-
echo "- Repositories: $REPO_COUNT" >> report.md
131-
echo "- Releases: $RELEASE_COUNT" >> report.md
132-
echo "- Workflows: $WORKFLOW_COUNT" >> report.md
127+
report_text = '\n'.join(report)
133128
134-
cat report.md
129+
with open('report.md', 'w') as f:
130+
f.write(report_text)
135131
136-
echo "report_content=$(cat report.md | base64)" >> $GITHUB_OUTPUT
132+
print(report_text)
133+
print('')
134+
print('Report saved to report.md')
135+
PYEOF
137136
138137
- name: Upload report
139138
uses: actions/upload-artifact@v4

0 commit comments

Comments
 (0)