Skip to content

Commit 88e4ca9

Browse files
committed
feat: Publish policy index
1 parent e057bfb commit 88e4ca9

File tree

6 files changed

+179
-0
lines changed

6 files changed

+179
-0
lines changed

.ci/scripts/docgen.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/usr/bin/env python3
2+
3+
import os
4+
import yaml
5+
from dataclasses import dataclass
6+
from typing import List
7+
from urllib.parse import urlparse, urlunparse
8+
9+
@dataclass
10+
class PolicyMetadata:
11+
dir: str
12+
version: str
13+
description: str
14+
path: str
15+
16+
def find_policy_yaml(start_dir=".") -> List[str]:
17+
"""Recursively search for files named 'Policy.yaml'."""
18+
matches = []
19+
for root, _, files in os.walk(start_dir):
20+
if "Policy.yaml" in files:
21+
matches.append(os.path.join(root, "Policy.yaml"))
22+
return matches
23+
24+
def load_policy_metadata(file_path: str) -> PolicyMetadata:
25+
"""Load a Policy.yaml file and unmarshal it into a PolicyMetadata object."""
26+
with open(file_path, "r", encoding="utf-8") as f:
27+
data = yaml.safe_load(f) or {}
28+
29+
dir_name = os.path.dirname(file_path)
30+
return PolicyMetadata(
31+
dir=data.get("dir", dir_name),
32+
version=data.get("version", ""),
33+
description=data.get("description", ""),
34+
path=file_path,
35+
)
36+
37+
def generate_markdown_table(policies: List[PolicyMetadata]) -> str:
38+
"""Generate a Markdown table from a list of PolicyMetadata objects."""
39+
header = "| URL | Version | Description | Link |\n"
40+
separator = "|------------|----------|-----------|---------| \n"
41+
rows = []
42+
for p in policies:
43+
description = p.description.replace("\n", " ").strip()
44+
ghcr_path = f"ghcr.io/updatecli/policies/{os.path.basename(p.path)}"
45+
readme_url = replace_filename_in_url(f"https://github.com/updatecli/policies/tree/main/{p.path}", "README.md")
46+
rows.append(f"| `{ghcr_path or '-'}` | {p.version or '-'} | {description or '-'} | {f"[link]({readme_url})" } |")
47+
return header + separator + "\n".join(rows)
48+
49+
def replace_filename_in_url(url: str, new_filename: str) -> str:
50+
# Parse the URL
51+
parsed = urlparse(url)
52+
53+
# Split the path and replace the last part with the new filename
54+
path_parts = parsed.path.split('/')
55+
path_parts[-1] = new_filename
56+
new_path = '/'.join(path_parts)
57+
58+
# Rebuild the URL with the new path
59+
new_url = urlunparse(parsed._replace(path=new_path))
60+
return new_url
61+
62+
# Example usage
63+
original_url = "https://github.com/updatecli/policies/blob/main/updatecli/policies/file/Policy.yaml"
64+
new_url = replace_filename_in_url(original_url, "Readme.md")
65+
66+
def main():
67+
policies = []
68+
for policy_file in find_policy_yaml("."):
69+
try:
70+
metadata = load_policy_metadata(policy_file)
71+
policies.append(metadata)
72+
except Exception as e:
73+
print(f"⚠️ Error parsing {policy_file}: {e}")
74+
75+
if not policies:
76+
print("No Policy.yaml files found.")
77+
return
78+
79+
markdown = generate_markdown_table(policies)
80+
81+
with open("POLICIES.md", "w", encoding="utf-8") as f:
82+
f.write(markdown)
83+
84+
if __name__ == "__main__":
85+
main()

.github/workflows/updatecli.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: updatecli
2+
on:
3+
workflow_dispatch:
4+
push:
5+
branches: [main]
6+
schedule:
7+
# Run every hour
8+
- cron: "0 * * * *"
9+
jobs:
10+
updatecli:
11+
runs-on: ubuntu-latest
12+
permissions:
13+
contents: read
14+
steps:
15+
- name: "Checkout"
16+
uses: "actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8" # v5.0.0
17+
- name: "Setup updatecli"
18+
uses: "updatecli/updatecli-action@144e241dd804cca91d5a5f362c98b71e7d3ea057" # v2
19+
- uses: actions/create-github-app-token@a8d616148505b5069dccd32f177bb87d7f39123b # v2.1.1
20+
id: generate_token
21+
if: github.ref == 'refs/heads/main'
22+
with:
23+
app-id: ${{ secrets.UPDATECLIBOT_APP_ID }}
24+
private-key: ${{ secrets.UPDATECLIBOT_APP_PRIVKEY }}
25+
- name: "Run updatecli"
26+
if: github.ref == 'refs/heads/main'
27+
run: "updatecli compose apply"
28+
env:
29+
UPDATECLI_GITHUB_USERNAME: ${{ secrets.UPDATECLI_BOT_GITHUB_ACTOR }}
30+
UPDATECLI_GITHUB_TOKEN: ${{ steps.generate_token.outputs.token }}

updatecli-compose.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
policies:
2+
- name: Local Updatecli policies
3+
config:
4+
- updatecli/updatecli.d
5+
values:
6+
- updatecli/values.d/scm.yaml
7+
8+
- name: Update githubactions
9+
policy: ghcr.io/updatecli/policies/autodiscovery/githubaction:0.2.1@sha256:cfddec11464cc09615135f0f1e069f00ad24d28edc7cc6a4e8224e04c3699008
10+
values:
11+
- updatecli/values.d/scm.yaml
12+
- updatecli/values.d/githubaction.yaml
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: Update POLICIES.md
2+
3+
actions:
4+
default:
5+
title: "deps: update POLICIES.md"
6+
kind: github/pullrequest
7+
scmid: default
8+
spec:
9+
automerge: true
10+
labels:
11+
- documentation
12+
13+
scms:
14+
default:
15+
kind: github
16+
spec:
17+
branch: "{{ .scm.branch }}"
18+
email: "{{ .scm.email }}"
19+
owner: "{{ .scm.owner }}"
20+
repository: "{{ .scm.repository }}"
21+
user: "{{ .scm.user }}"
22+
23+
conditions:
24+
python:
25+
name: Check if Python is installed
26+
kind: shell
27+
spec:
28+
command: "python3 --version"
29+
30+
targets:
31+
policies:
32+
name: update POLICIES.md
33+
kind: shell
34+
spec:
35+
command: .ci/scripts/docgen.py
36+
changedif:
37+
kind: file/checksum
38+
spec:
39+
files:
40+
- "POLICIES.md"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
spec:
2+
digest: true
3+
rootdir: '.github'

updatecli/values.d/scm.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
scm:
2+
commitusingapi: true
3+
enabled: true
4+
user: updatecli
5+
email: bot@updatecli.io
6+
owner: updatecli
7+
repository: policies
8+
username: "updatecli-bot"
9+
branch: main

0 commit comments

Comments
 (0)