Automate GitHub Issue Reporting with Python and REST APIs
Manually creating GitHub issues based on error logs or monitoring tools is time-consuming and error-prone. Fortunately, GitHub’s REST API combined with Python automation provides an elegant solution to streamline this task. In this post, we’ll build a Python script that analyzes log files and automatically creates GitHub issues when it detects relevant errors—great for internal tooling, DevOps monitoring, or CI pipelines.
1. Why Automate GitHub Issue Creation?
In engineering teams, maintaining consistency and speed in issue tracking is critical. Manual ticket creation can be tedious, inconsistent, or even overlooked entirely. Automating GitHub issue reporting:
- Reduces human error
- Ensures timely alerts
- Improves developer workflows
- Can be integrated with logs, CI systems, and monitoring tools
Let’s build a script that reads from an error log and files issues automatically using GitHub’s REST API. We’ll use the requests library for HTTP calls, and GitHub personal access tokens for authentication.
2. Setup: Authentication and Permissions
To use the GitHub REST API, you’ll need a GitHub personal access token:
- Go to GitHub Developer Settings
- Generate a classic token with repo scope for private repos
- Store it securely (e.g., in environment variables or secrets manager)
Install the dependencies:
pip install requests
Set up your environment variables:
export GITHUB_TOKEN='your_personal_access_token'
3. Parsing Logs or Monitoring Output
Our first step is to analyze log files or alerts to detect errors. Let’s assume we have a simple log format where each line represents either INFO or ERROR events.
2024-05-18 12:00:10 [INFO] Scheduled job started
2024-05-18 12:01:12 [ERROR] Failed to connect to database
2024-05-18 12:01:15 [INFO] Retrying connection
We’ll build a parser to extract error messages:
def extract_errors(log_file):
errors = []
with open(log_file, 'r') as f:
for line in f:
if '[ERROR]' in line:
errors.append(line.strip())
return errors
This function returns a list of error messages. You can extend this to include categories, timestamps, and other metadata for richer GitHub issues.
4. Creating GitHub Issues via API
Now, let’s use Python’s requests to communicate with GitHub.
import os
import requests
def create_github_issue(repo, title, body):
token = os.getenv('GITHUB_TOKEN')
url = f'https://api.github.com/repos/{repo}/issues'
headers = {
'Authorization': f'token {token}',
'Accept': 'application/vnd.github.v3+json'
}
data = {
'title': title,
'body': body,
'labels': ['automated-report']
}
response = requests.post(url, json=data, headers=headers)
if response.status_code == 201:
print(f"Issue created: {response.json()['html_url']}")
else:
print(f"Failed to create issue: {response.text}")
Replace repo with your-username/your-repo. This function takes error data and formats it into a GitHub issue. Optionally, you can assign assignees or link to files.
5. Integration: End-to-End Automation Script
Now let’s merge everything into a single script:
def automate_issue_reporting(log_path, repo):
errors = extract_errors(log_path)
for error in errors:
issue_title = error[:80] # truncate title to ~80 chars
issue_body = f"Detected error in logs:\n\n{error}\n\nAutomated by internal monitoring script."
create_github_issue(repo, issue_title, issue_body)
# Run automation
if __name__ == '__main__':
LOG_FILE_PATH = 'path/to/your.log'
REPO_NAME = 'your-org/your-repo'
automate_issue_reporting(LOG_FILE_PATH, REPO_NAME)
This function reads the log, detects errors, and automatically files structured GitHub issues.
6. Tips and Best Practices
- De-duplicate issues: Avoid creating the same issue multiple times by saving hashes of previously reported errors or querying existing issues.
- Rate limiting: GitHub’s REST API allows 5000 authenticated requests per hour. Make sure your script handles rate-limit responses (status 403).
- Use labels effectively: Add labels like
log-alertorCI-failureto categorize issues. - Secure tokens: Never hard-code personal access tokens in your codebase. Use environment variables or encrypted secrets.
7. Extensions and Real-World Use Cases
This script can be extended for:
- CI/CD Pipelines (e.g., file issues on build/deploy failures)
- Monitoring systems (e.g., Prometheus, Kibana)
- Alerting bots in Slack combined with GitHub issue creation
- Error aggregation from multiple microservices
To improve robustness, consider integrating with logging frameworks like Loguru, logging handlers for Sentry or Datadog, or formatting issues in markdown with stack traces.
Conclusion
Automating GitHub issue creation with Python and REST APIs is a powerful step toward reducing manual intervention in your developer workflows. Whether tracking CI failures, infrastructure issues, or runtime errors, this approach keeps your backlog updated and your team more responsive. Once connected to log streams or alerting systems, this tool becomes a lightweight but essential piece of your engineering toolkit.
Useful links:

