Hey there, fellow developer! Ready to supercharge your workflow with the GitHub Issues API? This guide will walk you through creating a Python integration that'll have you managing issues like a pro. We'll keep things snappy and to the point, so you can get your hands dirty with code in no time.
Before we dive in, make sure you've got:
requests
library (pip install requests
)Got all that? Great! Let's roll.
First things first, let's get you authenticated:
repo
scopeNow, let's set up authentication in Python:
import requests headers = { 'Authorization': f'token YOUR_PERSONAL_ACCESS_TOKEN', 'Accept': 'application/vnd.github.v3+json', }
Time to make some requests! Here's how to list, create, and update issues:
# List issues response = requests.get('https://api.github.com/repos/OWNER/REPO/issues', headers=headers) issues = response.json() # Create an issue new_issue = { 'title': 'Found a bug', 'body': 'Everything is broken. Send help.' } response = requests.post('https://api.github.com/repos/OWNER/REPO/issues', json=new_issue, headers=headers) # Update an issue update_data = { 'state': 'closed' } response = requests.patch('https://api.github.com/repos/OWNER/REPO/issues/ISSUE_NUMBER', json=update_data, headers=headers)
GitHub's API uses pagination to keep things speedy. Here's how to handle it:
def get_all_issues(repo): issues = [] page = 1 while True: response = requests.get(f'https://api.github.com/repos/{repo}/issues?page={page}', headers=headers) if not response.json(): break issues.extend(response.json()) page += 1 return issues
Don't let errors catch you off guard. Wrap your requests like this:
try: response = requests.get('https://api.github.com/repos/OWNER/REPO/issues', headers=headers) response.raise_for_status() except requests.exceptions.RequestException as e: print(f"Oops! Something went wrong: {e}")
Want to level up? Try these:
# Filter issues params = {'state': 'open', 'labels': 'bug'} response = requests.get('https://api.github.com/repos/OWNER/REPO/issues', params=params, headers=headers) # Search issues query = 'is:open is:issue label:bug' response = requests.get(f'https://api.github.com/search/issues?q={query}', headers=headers) # Add a label label_data = {'labels': ['urgent']} response = requests.post('https://api.github.com/repos/OWNER/REPO/issues/ISSUE_NUMBER/labels', json=label_data, headers=headers)
Let's put it all together in a handy CLI tool:
import argparse def main(): parser = argparse.ArgumentParser(description='GitHub Issues CLI') parser.add_argument('action', choices=['list', 'create', 'update']) parser.add_argument('--repo', required=True) args = parser.parse_args() if args.action == 'list': issues = get_all_issues(args.repo) for issue in issues: print(f"{issue['number']}: {issue['title']}") elif args.action == 'create': # Implement create logic elif args.action == 'update': # Implement update logic if __name__ == '__main__': main()
Remember to:
And there you have it! You're now equipped to wrangle GitHub Issues like a champ. Remember, the official GitHub REST API docs are your best friend for diving deeper.
Now go forth and automate all the things! Happy coding! 🚀