44import sys
55
66GITHUB_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+
5468def 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
80120if __name__ == "__main__" :
81- main ()
121+ main ()
0 commit comments