Skip to content

Commit

Permalink
fix(open_issue): wrong definition position + refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Wabri committed Mar 4, 2024
1 parent e59d54e commit 45aa4a5
Showing 1 changed file with 38 additions and 35 deletions.
73 changes: 38 additions & 35 deletions workflows/check_outdate_deps/open_issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,47 @@
parser.add_argument("latest", type=str, help='The latest version of the package')
args = parser.parse_args()

# Environment variable
repo = os.environ.get("GITHUB_REPOSITORY")
requirement_file = os.environ.get("REQUIREMENT_FILE")

# Define the title
issue_title = "Dependency outdated in " + requirement_file + ": " + args.package + ":" + args.version + " -> " + args.latest

# The double quote on the issue_title is necessary to avoid duplicate issues
query = f"repo:{repo} type:issue in:title \"{issue_title}\""
def get_description(requirement_file, package, latest):
return f"""The package {package} is outdated in {requirement_file}. The latest version is {latest}. Please update the package to the latest version.
# Send the query
response = requests.get("https://api.github.com/search/issues", params={"q": query})
data = response.json()
Check the package [here](https://pypi.org/project/{package}/{latest}/) for more information.
"""

# There is this error that we somehow try to avoid
# {'message': "API rate limit exceeded for 93.45.31.205. (But here's the good news: Authenticated requests get a higher rate limit. Check
# out the documentation for more details.)", 'documentation_url': 'https://docs.github.com/rest/overview/resources-in-the-rest-api#rat
# e-limiting'}
if data["total_count"] > 0:
print("There is already an issue with this title!")
else:
issue_description = get_description_from_package(args.package, args.latest)
token = os.environ.get("GITHUB_TOKEN")
issue = {"title": issue_title, "body": issue_description}
headers = {"Authorization": f"token {token}"}
def get_title(requirement_file, package_name, current_version, latest_version):
return f"Dependency outdated in {requirement_file}: {package_name}:{current_version} -> {latest_version}"

response = requests.post(f"https://api.github.com/repos/{repo}/issues", headers=headers, data=json.dumps(issue))
if __name__ == '__main__':
# Environment variable
repo = os.environ.get("GITHUB_REPOSITORY")
requirement_file = str(os.environ.get("REQUIREMENT_FILE"))

# Define the title
issue_title = get_title(requirement_file, args.package, args.version, args.latest)

# The double quote on the issue_title is necessary to avoid duplicate issues
query = f"repo:{repo} type:issue in:title \"{issue_title}\""

# Send the query
response = requests.get("https://api.github.com/search/issues", params={"q": query})
data = response.json()

# Check the response
if response.status_code == 201:
print("Issue created successfully.")
# There is this error that we somehow try to avoid
# {'message': "API rate limit exceeded for 93.45.31.205. (But here's the good news: Authenticated requests get a higher rate limit. Check
# out the documentation for more details.)", 'documentation_url': 'https://docs.github.com/rest/overview/resources-in-the-rest-api#rat
# e-limiting'}
if data["total_count"] > 0:
print("There is already an issue with this title!")
else:
print(f"Failed to create issue. Status code: {response.status_code}.")


def get_description_from_package(package, latest):
return f"""The package {package} is outdated in {requirement_file}. The latest version is {latest}. Please update the package to the latest version.
Check the package [here](https://pypi.org/project/{package}/{latest}/) for more information.
"""
issue_description = get_description(requirement_file, args.package, args.latest)
token = os.environ.get("GITHUB_TOKEN")
issue = {"title": issue_title, "body": issue_description}
headers = {"Authorization": f"token {token}"}

response = requests.post(f"https://api.github.com/repos/{repo}/issues", headers=headers, data=json.dumps(issue))

# Check the response
if response.status_code == 201:
print("Issue created successfully.")
else:
print(f"Failed to create issue. Status code: {response.status_code}.")

0 comments on commit 45aa4a5

Please sign in to comment.