-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate.py
More file actions
319 lines (257 loc) · 10.2 KB
/
validate.py
File metadata and controls
319 lines (257 loc) · 10.2 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#!/usr/bin/env python3
"""
SmartFormat v2.1 RSS feed validator.
Validates one or more RSS feeds against the SmartNews SmartFormat v2.1
specification: https://publishers.smartnews.com/hc/en-us/articles/360036526213
Hard failures cause a non-zero exit code (action fails).
Warnings are surfaced in the step summary but do not fail the action.
Environment variables (set by action.yml):
INPUT_FEED_URLS JSON array of feed URLs
INPUT_MIN_ITEM_COUNT Minimum items required per feed (default: 5)
INPUT_MAX_AGE_HOURS Max hours since lastBuildDate before warn (default: 48)
INPUT_REQUIRE_SNF_NAMESPACE Require snf: namespace declaration (default: true)
GITHUB_OUTPUT Path to GitHub Actions output file
GITHUB_STEP_SUMMARY Path to GitHub Actions step summary file
"""
import json
import os
import sys
import urllib.error
import urllib.parse
import urllib.request
import xml.etree.ElementTree as ET
from datetime import datetime, timedelta, timezone
from email.utils import parsedate_to_datetime
# ---------------------------------------------------------------------------
# Namespace URIs
# ---------------------------------------------------------------------------
NS_CONTENT = "http://purl.org/rss/1.0/modules/content/"
NS_MEDIA = "http://search.yahoo.com/mrss/"
NS_DC = "http://purl.org/dc/elements/1.1/"
NS_SNF = "http://www.smartnews.be/snf"
# Required channel-level tags (plain RSS 2.0, no namespace)
CHANNEL_REQUIRED = ["title", "link", "description"]
# Required item-level tags — (local_name, namespace_uri or None)
ITEM_REQUIRED = [
("title", None),
("link", None),
("guid", None),
("pubDate", None),
("encoded", NS_CONTENT), # content:encoded
("thumbnail", NS_MEDIA), # media:thumbnail
]
VALIDATOR_BASE_URL = "https://sf-validator.smartnews.com/?url="
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _tag(local, ns=None):
"""Return Clark-notation tag string."""
if ns:
return f"{{{ns}}}{local}"
return local
def _find_text(parent, local, ns=None):
el = parent.find(_tag(local, ns))
return el.text.strip() if el is not None and el.text else None
def _has_element(parent, local, ns=None):
return parent.find(_tag(local, ns)) is not None
def fetch_feed(url):
"""Fetch a URL and return the raw bytes, or raise on HTTP error."""
req = urllib.request.Request(
url, headers={"User-Agent": "smartformat-validate-action/1"}
)
with urllib.request.urlopen(req, timeout=30) as resp:
return resp.read()
def parse_date(date_str):
"""Parse an RFC 2822 date string; return datetime or None."""
if not date_str:
return None
try:
return parsedate_to_datetime(date_str)
except Exception:
return None
# ---------------------------------------------------------------------------
# Validation
# ---------------------------------------------------------------------------
def validate_feed(url, min_item_count, max_age_hours, require_snf_namespace):
"""
Validate a single feed URL.
Returns:
errors list[str] — hard failures
warnings list[str] — non-fatal issues
"""
errors = []
warnings = []
# 1. Fetch
try:
raw = fetch_feed(url)
except urllib.error.HTTPError as exc:
errors.append(f"HTTP {exc.code} fetching feed")
return errors, warnings
except Exception as exc:
errors.append(f"Failed to fetch feed: {exc}")
return errors, warnings
# 2. Parse XML
try:
root = ET.fromstring(raw)
except ET.ParseError as exc:
errors.append(f"XML parse error: {exc}")
return errors, warnings
# 3. RSS 2.0 root check
if root.tag != "rss":
errors.append(f"Root element is <{root.tag}>, expected <rss>")
return errors, warnings
# 4. snf: namespace declaration
if require_snf_namespace:
raw_str = raw.decode("utf-8", errors="replace")
if "http://www.smartnews.be/snf" not in raw_str:
errors.append(
"Missing SmartNews snf: namespace declaration "
'(xmlns:snf="http://www.smartnews.be/snf")'
)
# 5. Channel
channel = root.find("channel")
if channel is None:
errors.append("Missing <channel> element")
return errors, warnings
for field in CHANNEL_REQUIRED:
if not _has_element(channel, field):
errors.append(f"Channel missing required element <{field}>")
# 6. lastBuildDate freshness (warn-only)
if max_age_hours > 0:
lbd_text = _find_text(channel, "lastBuildDate")
if lbd_text:
lbd = parse_date(lbd_text)
if lbd:
age = datetime.now(timezone.utc) - lbd
if age > timedelta(hours=max_age_hours):
hours_old = int(age.total_seconds() / 3600)
warnings.append(
f"<lastBuildDate> is {hours_old}h old "
f"(threshold: {max_age_hours}h)"
)
else:
warnings.append(f"<lastBuildDate> could not be parsed: {lbd_text!r}")
else:
warnings.append("<lastBuildDate> is absent from <channel>")
# 7. snf:logo (warn-only)
if not _has_element(channel, "logo", NS_SNF):
warnings.append(
"Channel missing optional <snf:logo> (recommended for branding)"
)
# 8. Items
items = channel.findall("item")
if len(items) < min_item_count:
errors.append(
f"Feed contains {len(items)} item(s); minimum required is {min_item_count}"
)
# 9. Per-item required fields
for idx, item in enumerate(items, start=1):
item_title = _find_text(item, "title") or f"item[{idx}]"
for local, ns in ITEM_REQUIRED:
if not _has_element(item, local, ns):
ns_prefix = ""
if ns == NS_CONTENT:
ns_prefix = "content:"
elif ns == NS_MEDIA:
ns_prefix = "media:"
errors.append(
f"Item {idx} ({item_title!r}) missing <{ns_prefix}{local}>"
)
# description is optional per spec but recommended (warn-only)
if not _has_element(item, "description"):
warnings.append(
f"Item {idx} ({item_title!r}) missing optional <description>"
)
return errors, warnings
# ---------------------------------------------------------------------------
# Summary rendering
# ---------------------------------------------------------------------------
def build_summary(results, feed_urls):
"""Return a Markdown string for $GITHUB_STEP_SUMMARY."""
lines = ["# SmartFormat Feed Validation\n"]
all_passed = all(not errs for errs, _ in results.values())
badge = "**Result: PASS**" if all_passed else "**Result: FAIL**"
lines.append(f"{badge}\n")
for url in feed_urls:
errs, warns = results[url]
status = "PASS" if not errs else "FAIL"
icon = ":white_check_mark:" if not errs else ":x:"
encoded_url = urllib.parse.quote(url, safe="")
validator_link = (
f"[Open in SmartNews Validator]({VALIDATOR_BASE_URL}{encoded_url})"
)
lines.append(f"## {icon} {url}\n")
lines.append(f"Status: **{status}** | {validator_link}\n")
if errs:
lines.append("### Errors\n")
for e in errs:
lines.append(f"- {e}")
lines.append("")
if warns:
lines.append("### Warnings\n")
for w in warns:
lines.append(f"- {w}")
lines.append("")
if not errs and not warns:
lines.append("All checks passed with no warnings.\n")
return "\n".join(lines)
# ---------------------------------------------------------------------------
# Entry point
# ---------------------------------------------------------------------------
def main():
# Read inputs
feed_urls_raw = os.environ.get("INPUT_FEED_URLS", "").strip()
min_item_count = int(os.environ.get("INPUT_MIN_ITEM_COUNT", "5"))
max_age_hours = int(os.environ.get("INPUT_MAX_AGE_HOURS", "48"))
require_snf_namespace = (
os.environ.get("INPUT_REQUIRE_SNF_NAMESPACE", "true").lower() == "true"
)
github_output = os.environ.get("GITHUB_OUTPUT", "")
github_step_summary = os.environ.get("GITHUB_STEP_SUMMARY", "")
# Parse feed URLs
try:
feed_urls = json.loads(feed_urls_raw)
if not isinstance(feed_urls, list) or not feed_urls:
raise ValueError("feed_urls must be a non-empty JSON array")
except (json.JSONDecodeError, ValueError) as exc:
print(f"::error ::Invalid feed_urls input: {exc}", file=sys.stderr)
sys.exit(1)
# Validate each feed
results = {}
for url in feed_urls:
print(f"Validating: {url}")
errs, warns = validate_feed(
url, min_item_count, max_age_hours, require_snf_namespace
)
results[url] = (errs, warns)
for e in errs:
print(f" ERROR: {e}")
for w in warns:
print(f" WARN: {w}")
if not errs and not warns:
print(" OK")
# Aggregate
feeds_passed = sum(1 for errs, _ in results.values() if not errs)
feeds_failed = len(feed_urls) - feeds_passed
overall = "pass" if feeds_failed == 0 else "fail"
# Write step summary
if github_step_summary:
summary = build_summary(results, feed_urls)
with open(github_step_summary, "a", encoding="utf-8") as fh:
fh.write(summary)
# Write outputs
if github_output:
with open(github_output, "a", encoding="utf-8") as fh:
fh.write(f"result={overall}\n")
fh.write(f"feeds_passed={feeds_passed}\n")
fh.write(f"feeds_failed={feeds_failed}\n")
if overall == "fail":
print(
f"\n{feeds_failed}/{len(feed_urls)} feed(s) failed SmartFormat validation.",
file=sys.stderr,
)
sys.exit(1)
else:
print(f"\nAll {feeds_passed}/{len(feed_urls)} feed(s) passed.")
if __name__ == "__main__":
main()