@@ -45,56 +45,72 @@ jobs:
4545 # Install Python and packaging library if not already in runner image
4646 python -m pip install packaging
4747
48- python -c "
49- import os
50- import tempfile
51- from packaging.version import parse
52-
53- issue_version = os.environ['ISSUE_VERSION']
54- latest_version = os.environ['LATEST_VERSION']
55- issue_number = os.environ['ISSUE_NUMBER']
56- github_token = os.environ['GITHUB_TOKEN']
57- github_repository = os.environ.get('GITHUB_REPOSITORY')
58-
59- print(f'Issue version : {issue_version}')
60- print(f'Latest release version : {latest_version}')
61-
62- # Ensure the issue version is parseable (e.g., if user inputs garbage)
63- try :
64- parsed_issue_version = parse(issue_version)
65- except Exception as e :
66- print(f'Could not parse issue version : {issue_version}. Error: {e}. Skipping version comparison.')
67- exit(0) # Exit gracefully if version cannot be parsed
68-
69- try :
70- parsed_latest_version = parse(latest_version)
71- except Exception as e :
72- print(f'Could not parse latest release version : {latest_version}. Error: {e}. Skipping version comparison.')
73- exit(0) # Exit gracefully
74-
75- if parsed_issue_version < parsed_latest_version :
76- print('Version mismatch detected. Commenting on issue.')
77- update_instructions = f'''It looks like you are running an older version of `python-pirate-weather` (`{issue_version}`).
78-
79- **Please update to the latest stable version (`{latest_version}`) before reporting issues, as your issue might already be resolved.**
80-
81- You can update by running :
82- \`\`\`bash
83- pip install --upgrade python-pirate-weather
84- \`\`\`
85-
86- If the issue persists after updating, please provide the new version number in a comment.
87- ' ''
88- # Write the comment body to a temporary file
89- with tempfile.NamedTemporaryFile(mode=' w', delete=False, encoding='utf-8') as temp_file:
90- temp_file.write(update_instructions)
91- temp_file_path = temp_file.name
92-
93- # Use GitHub CLI to add a comment from the temporary file
94- # Ensure gh CLI is authenticated with the GITHUB_TOKEN
95- # GITHUB_TOKEN is automatically available to gh CLI in GitHub Actions
96- os.system(f'gh issue comment {issue_number} --repo {github_repository} --body-file \"{temp_file_path}\"')
97- os.remove(temp_file_path) # Clean up the temporary file
98- else :
99- print('User is running the latest version or a newer/unreleased version.')
100- "
48+ # Start of Python script
49+ import os
50+ import tempfile
51+ import subprocess # Import subprocess module
52+ from packaging.version import parse
53+
54+ issue_version = os.environ['ISSUE_VERSION']
55+ latest_version = os.environ['LATEST_VERSION']
56+ issue_number = os.environ['ISSUE_NUMBER']
57+ github_token = os.environ['GITHUB_TOKEN']
58+ github_repository = os.environ.get('GITHUB_REPOSITORY')
59+
60+ print(f'Issue version: {issue_version}')
61+ print(f'Latest release version: {latest_version}')
62+
63+ # Ensure the issue version is parseable (e.g., if user inputs garbage)
64+ try:
65+ parsed_issue_version = parse(issue_version)
66+ except Exception as e:
67+ print(f'Could not parse issue version: {issue_version}. Error: {e}. Skipping version comparison.')
68+ exit(0) # Exit gracefully if version cannot be parsed
69+
70+ try:
71+ parsed_latest_version = parse(latest_version)
72+ except Exception as e:
73+ print(f'Could not parse latest release version: {latest_version}. Error: {e}. Skipping version comparison.')
74+ exit(0) # Exit gracefully
75+
76+ if parsed_issue_version < parsed_latest_version:
77+ print('Version mismatch detected. Commenting on issue.')
78+ # Define update_instructions as a list of lines to avoid YAML parsing issues
79+ update_instructions_lines = [
80+ f'It looks like you are running an older version of `python-pirate-weather` (`{issue_version}`).',
81+ '',
82+ f'**Please update to the latest stable version (`{latest_version}`) before reporting issues, as your issue might already be resolved.**',
83+ '',
84+ 'You can update by running:',
85+ '\`\`\`bash',
86+ 'pip install --upgrade python-pirate-weather',
87+ '\`\`\`',
88+ '',
89+ 'If the issue persists after updating, please provide the new version number in a comment.'
90+ ]
91+ update_instructions = '\n'.join(update_instructions_lines)
92+
93+ # Write the comment body to a temporary file
94+ with tempfile.NamedTemporaryFile(mode='w', delete=False, encoding='utf-8') as temp_file:
95+ temp_file.write(update_instructions)
96+ temp_file_path = temp_file.name
97+
98+ # Use subprocess.run to execute the gh CLI command
99+ # This is more robust against quoting issues
100+ try:
101+ subprocess.run(
102+ ['gh', 'issue', 'comment', str(issue_number), '--repo', github_repository, '--body-file', temp_file_path],
103+ check=True, # Raise an exception for non-zero exit codes
104+ capture_output=True, # Capture stdout/stderr
105+ text=True # Decode stdout/stderr as text
106+ )
107+ print(f"Successfully commented on issue #{issue_number}")
108+ except subprocess.CalledProcessError as e:
109+ print(f"Error commenting on issue: {e}")
110+ print(f"Stdout: {e.stdout}")
111+ print(f"Stderr: {e.stderr}")
112+ finally:
113+ os.remove(temp_file_path) # Clean up the temporary file
114+ else:
115+ print('User is running the latest version or a newer/unreleased version.')
116+ shell : python # Explicitly set the shell to python
0 commit comments