Skip to content

Commit 6b1096f

Browse files
committed
Update scripts
1 parent 7a7c0a6 commit 6b1096f

File tree

3 files changed

+149
-39
lines changed

3 files changed

+149
-39
lines changed

.github/scripts/check_snippets.py

Lines changed: 77 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -210,20 +210,12 @@ def compare_code_snippets(repo_references, repo_dependencies):
210210
start_line, end_line = None, None
211211

212212
# Fetch both versions of the code
213-
print(f"Fetching code snippets for {repo_name}...")
214-
215-
# Print url with line numbers
216-
print(f"Current URL: {current_url}")
217213
current_code = fetch_code_snippet(raw_current_url, current_version, start_line, end_line)
218-
print(current_code)
219-
220-
print(f"Latest: {latest_url}")
221214
latest_code = fetch_code_snippet(raw_latest_url, latest_version, start_line, end_line)
222-
print(latest_code)
223215

224216
# Check if they match
225217
match = (current_code == latest_code) if current_code and latest_code else False
226-
218+
227219
results.append({
228220
'file': ref['file'],
229221
'line_number': ref['line_number'],
@@ -239,6 +231,32 @@ def compare_code_snippets(repo_references, repo_dependencies):
239231

240232
return results
241233

234+
def convert_raw_to_regular_url(raw_url, line_start=None):
235+
"""
236+
Convert a raw.githubusercontent.com URL back to a regular GitHub URL.
237+
238+
Args:
239+
raw_url: Raw GitHub URL to convert
240+
line_start: Starting line number (optional)
241+
242+
Returns:
243+
Regular GitHub URL with line number if provided
244+
"""
245+
# Pattern: https://raw.githubusercontent.com/{owner}/{repo}/{branch}/{path}
246+
pattern = r'https://raw\.githubusercontent\.com/([^/]+)/([^/]+)/([^/]+)/(.+)'
247+
match = re.match(pattern, raw_url)
248+
249+
if match:
250+
owner, repo, branch, path = match.groups()
251+
github_url = f"https://github.com/{owner}/{repo}/blob/{branch}/{path}"
252+
253+
# Add line number if provided
254+
if line_start:
255+
github_url += f"#L{line_start}"
256+
257+
return github_url
258+
return raw_url
259+
242260
def check_outdated_snippets(json_file_path, codebase_root_dir):
243261
"""
244262
Complete workflow to check for outdated code snippets in the documentation codebase.
@@ -260,7 +278,6 @@ def check_outdated_snippets(json_file_path, codebase_root_dir):
260278

261279
# Step 3: Compare code snippets between current and latest versions
262280
comparison_results = compare_code_snippets(repo_references, repo_dependencies)
263-
#print(comparison_results)
264281

265282
# Filter for outdated snippets (where match is False)
266283
outdated_snippets = [result for result in comparison_results if not result['match']]
@@ -276,12 +293,60 @@ def check_outdated_snippets(json_file_path, codebase_root_dir):
276293
repo_name = snippet['repo_name']
277294
if repo_name not in repo_to_outdated:
278295
repo_to_outdated[repo_name] = []
296+
297+
# Convert raw URLs to regular GitHub URLs for better clickability
298+
current_url = snippet['current_url']
299+
latest_url = snippet['latest_url']
300+
301+
# Extract line numbers from the URLs if present
302+
current_line = None
303+
latest_line = None
304+
305+
# Check if URLs are in raw format and extract line numbers
306+
if "raw.githubusercontent.com" in current_url:
307+
base_url, start_line, _ = extract_line_range(current_url)
308+
if not start_line and ":" in current_url:
309+
# Handle the case where line numbers are in the URL format
310+
parts = current_url.split(":")
311+
if len(parts) >= 2 and parts[-2].isdigit():
312+
start_line = int(parts[-2])
313+
current_line = start_line
314+
current_url = base_url
315+
else:
316+
# Extract line number from regular GitHub URL
317+
line_match = re.search(r'#L(\d+)', current_url)
318+
if line_match:
319+
current_line = int(line_match.group(1))
320+
321+
if "raw.githubusercontent.com" in latest_url:
322+
base_url, start_line, _ = extract_line_range(latest_url)
323+
if not start_line and ":" in latest_url:
324+
# Handle the case where line numbers are in the URL format
325+
parts = latest_url.split(":")
326+
if len(parts) >= 2 and parts[-2].isdigit():
327+
start_line = int(parts[-2])
328+
latest_line = start_line
329+
latest_url = base_url
330+
else:
331+
# Extract line number from regular GitHub URL
332+
line_match = re.search(r'#L(\d+)', latest_url)
333+
if line_match:
334+
latest_line = int(line_match.group(1))
335+
336+
# Convert raw URLs to regular GitHub URLs
337+
if "raw.githubusercontent.com" in current_url:
338+
current_url = convert_raw_to_regular_url(current_url, current_line)
339+
340+
if "raw.githubusercontent.com" in latest_url:
341+
latest_url = convert_raw_to_regular_url(latest_url, latest_line)
279342

280343
repo_to_outdated[repo_name].append({
281344
'file': snippet['file'],
282345
'line_number': snippet['line_number'],
283-
'current_url': snippet['current_url'],
284-
'latest_url': snippet['latest_url']
346+
'current_url': current_url,
347+
'latest_url': latest_url,
348+
'current_code': snippet['current_code'],
349+
'latest_code': snippet['latest_code']
285350
})
286351

287352
# Add outdated_snippets field to each repository in the JSON

.github/scripts/create_issues.py

Lines changed: 65 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,41 @@
44
import sys
55

66
GITHUB_TOKEN = os.environ.get("GITHUB_TOKEN")
7+
REPO_API_URL = "https://api.github.com/repos/polkadot-developers/polkadot-docs/issues"
78

8-
def issue_exists(title):
9-
"""Check if an issue with the same title already exists."""
9+
def get_existing_issues():
10+
"""Retrieve all existing issues from GitHub and return their titles."""
1011
try:
11-
url = "https://api.github.com/repos/polkadot-developers/polkadot-docs/issues"
1212
headers = {
1313
"Authorization": f"token {GITHUB_TOKEN}",
1414
"Accept": "application/vnd.github.v3+json",
1515
}
16-
response = requests.get(url, headers=headers)
16+
api_url_with_query = f"{REPO_API_URL}?per_page=100"
17+
response = requests.get(api_url_with_query, headers=headers)
1718
response.raise_for_status()
1819

1920
issues = response.json()
20-
for issue in issues:
21-
if issue["title"] == title:
22-
return True
23-
return False
21+
return {issue["title"] for issue in issues}
2422
except Exception as e:
25-
print(f"Error checking for existing issues: {e}")
23+
print(f"Error retrieving existing issues: {e}")
2624
sys.exit(1)
2725

28-
def create_github_issue(title, body):
29-
"""Create a GitHub issue using the GitHub API."""
30-
try:
31-
if issue_exists(title):
32-
print(f"Issue '{title}' already exists. Skipping creation.")
33-
return
26+
def create_github_issue(title, body, existing_issues):
27+
"""Create a GitHub issue using the GitHub API if it does not already exist."""
28+
if title in existing_issues:
29+
print(f"Issue '{title}' already exists. Skipping creation.")
30+
return
3431

35-
url = "https://api.github.com/repos/polkadot-developers/polkadot-docs/issues"
32+
try:
3633
headers = {
3734
"Authorization": f"token {GITHUB_TOKEN}",
3835
"Accept": "application/vnd.github.v3+json",
3936
}
4037
data = {"title": title, "body": body}
41-
response = requests.post(url, headers=headers, json=data)
38+
response = requests.post(REPO_API_URL, headers=headers, json=data)
4239

4340
if response.status_code == 201:
4441
print(f"Successfully created issue '{title}'")
45-
return
4642
else:
4743
print(f"Failed to create issue '{title}'. Status code: {response.status_code}")
4844
print(response.text)
@@ -51,6 +47,24 @@ def create_github_issue(title, body):
5147
print(f"Error creating issue: {e}")
5248
sys.exit(1)
5349

50+
def format_code_diff(current_code, latest_code):
51+
"""Format the code difference for GitHub markdown."""
52+
diff_md = "```diff\n"
53+
54+
# Split the code into lines
55+
current_lines = current_code.splitlines() if current_code else []
56+
latest_lines = latest_code.splitlines() if latest_code else []
57+
58+
# Simple diff: prefix removed lines with - and added lines with +
59+
for line in current_lines:
60+
diff_md += f"- {line}\n"
61+
62+
for line in latest_lines:
63+
diff_md += f"+ {line}\n"
64+
65+
diff_md += "```\n"
66+
return diff_md
67+
5468
def main():
5569
if not GITHUB_TOKEN:
5670
print("Error: GITHUB_TOKEN environment variable is not set.")
@@ -63,19 +77,45 @@ def main():
6377
print(f"Error reading JSON file: {e}")
6478
sys.exit(1)
6579

80+
existing_issues = get_existing_issues()
81+
6682
for dep in data.get("outdated_dependencies", []):
6783
title = f"Update needed: {dep['name']} ({dep['current_version']} -> {dep['latest_version']})"
6884
body = f"""A new release has been detected for {dep['name']}.
6985
70-
Category: {dep['category']}
71-
Current version: {dep['current_version']}
72-
Latest version: {dep['latest_version']}
86+
**Category:** {dep['category']}
87+
**Current version:** {dep['current_version']}
88+
**Latest version:** {dep['latest_version']}
7389
74-
Latest release: [View here]({dep['latest_release_url']})
90+
Please review the [changelog]({dep['latest_release_url']}) and update the documentation accordingly.
91+
"""
7592

76-
Please review the change log and update the documentation accordingly."""
93+
# Add outdated snippets information if available
94+
if 'outdated_snippets' in dep and dep['outdated_snippets']:
95+
body += "\n## Outdated Code Snippets\n\n"
96+
body += "The following code snippets in the documentation need to be updated:\n\n"
97+
98+
for i, snippet in enumerate(dep['outdated_snippets'], 1):
99+
file_path = snippet['file']
100+
line_number = snippet['line_number']
101+
current_url = snippet['current_url']
102+
latest_url = snippet['latest_url']
103+
104+
# Include file path and line number as a link to the repository file if possible
105+
repo_file_path = f"https://github.com/polkadot-developers/polkadot-docs/blob/master/{file_path.replace('./', '')}?plain=1#L{line_number}"
106+
body += f"### {i}. [{file_path}:{line_number}]({repo_file_path})\n\n"
107+
108+
# Add URLs for reference
109+
body += f"**Current URL:** {current_url}\n\n"
110+
body += f"**Latest URL:** {latest_url}\n\n"
111+
112+
# If the comparison results include the actual code snippets, show the diff
113+
if 'current_code' in snippet and 'latest_code' in snippet:
114+
body += "**Code Difference:**\n\n"
115+
body += format_code_diff(snippet['current_code'], snippet['latest_code'])
116+
body += "\n"
77117

78-
create_github_issue(title, body)
118+
create_github_issue(title, body, existing_issues)
79119

80120
if __name__ == "__main__":
81-
main()
121+
main()

.github/workflows/check-dependencies.yaml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,17 @@ jobs:
2424
python -m pip install --upgrade pip
2525
pip install requests pyyaml
2626
27-
- name: Check for Outdated Dependencies
27+
- name: Check for outdated dependencies
2828
id: check_dependencies
2929
run: |
3030
python .github/scripts/check_dependencies.py
3131
32-
- name: Create GitHub Issues for Outdated Dependencies
32+
- name: Check for snippets that need updating
33+
id: check_snippets
34+
run: |
35+
python .github/scripts/check_snippets.py ./outdated_dependencies.json .
36+
37+
- name: Create gitHub issues for outdated dependencies
3338
env:
3439
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3540
run: |

0 commit comments

Comments
 (0)