Home > Article > Web Front-end > Ultimate Guide: Build a Complete Issue Tracker with the GitHub API
This guide will help you create an automated, efficient issue tracker integrated with the GitHub API. You'll learn how to set up issue creation, assignments, notifications, and more—step by step!
To access private repositories or avoid rate limits, you need a Personal Access Token (PAT).
How to Generate a Token:
This code lets you create an issue in any of your repositories via the GitHub API.
async function createIssue(owner, repo, title, body, token) { const url = `https://api.github.com/repos/${owner}/${repo}/issues`; const response = await fetch(url, { method: 'POST', headers: { Authorization: `token ${token}`, 'Content-Type': 'application/json', }, body: JSON.stringify({ title, body }), }); const issue = await response.json(); console.log(`Issue Created: ${issue.html_url}`); } createIssue('YourGitHubUsername', 'my-repo', 'Bug Report', 'Details about the bug.', 'your_token');
? How it works:
Ensure that every issue gets assigned to a team member automatically. This step can save time, ensuring accountability.
async function assignIssue(owner, repo, issueNumber, assignees, token) { const url = `https://api.github.com/repos/${owner}/${repo}/issues/${issueNumber}`; const response = await fetch(url, { method: 'PATCH', headers: { Authorization: `token ${token}`, 'Content-Type': 'application/json', }, body: JSON.stringify({ assignees }), }); const updatedIssue = await response.json(); console.log(`Issue Assigned: ${updatedIssue.html_url}`); } assignIssue('YourGitHubUsername', 'my-repo', 42, ['assignee_username'], 'your_token');
? What this does:
Tracking all open issues is essential for managing a project efficiently. Use this code to list all unresolved issues.
async function getOpenIssues(owner, repo, token) { const url = `https://api.github.com/repos/${owner}/${repo}/issues?state=open`; const response = await fetch(url, { headers: { Authorization: `token ${token}` }, }); const issues = await response.json(); console.log(`Total Open Issues: ${issues.length}`); issues.forEach(issue => console.log(`#${issue.number}: ${issue.title}`)); } getOpenIssues('YourGitHubUsername', 'my-repo', 'your_token');
? How it helps:
Create alerts for issues that remain unresolved for too long. Set a cron job to run this code periodically (e.g., every day) and send notifications through Slack or email.
async function checkStaleIssues(owner, repo, daysOld, token) { const url = `https://api.github.com/repos/${owner}/${repo}/issues?state=open`; const response = await fetch(url, { headers: { Authorization: `token ${token}` }, }); const issues = await response.json(); const today = new Date(); issues.forEach(issue => { const createdDate = new Date(issue.created_at); const ageInDays = (today - createdDate) / (1000 * 60 * 60 * 24); if (ageInDays > daysOld) { console.log(`Stale Issue: #${issue.number} - ${issue.title}`); // Send alert logic here (e.g., Slack or email notification) } }); } checkStaleIssues('YourGitHubUsername', 'my-repo', 7, 'your_token');
? What this does:
Automatically label issues based on their content using simple keyword matching. This can help categorize issues instantly.
async function createIssue(owner, repo, title, body, token) { const url = `https://api.github.com/repos/${owner}/${repo}/issues`; const response = await fetch(url, { method: 'POST', headers: { Authorization: `token ${token}`, 'Content-Type': 'application/json', }, body: JSON.stringify({ title, body }), }); const issue = await response.json(); console.log(`Issue Created: ${issue.html_url}`); } createIssue('YourGitHubUsername', 'my-repo', 'Bug Report', 'Details about the bug.', 'your_token');
? Usage:
Create a dashboard using JavaScript and the GitHub API to display all open issues on a web page. You can visualize issue statuses, assignments, and labels.
async function assignIssue(owner, repo, issueNumber, assignees, token) { const url = `https://api.github.com/repos/${owner}/${repo}/issues/${issueNumber}`; const response = await fetch(url, { method: 'PATCH', headers: { Authorization: `token ${token}`, 'Content-Type': 'application/json', }, body: JSON.stringify({ assignees }), }); const updatedIssue = await response.json(); console.log(`Issue Assigned: ${updatedIssue.html_url}`); } assignIssue('YourGitHubUsername', 'my-repo', 42, ['assignee_username'], 'your_token');
? How this works:
By building an issue tracker with the GitHub API, you automate project management, improve productivity, and ensure accountability. Whether you’re managing small projects or large open-source repositories, these automation tools can save time and keep your team on track.
The above is the detailed content of Ultimate Guide: Build a Complete Issue Tracker with the GitHub API. For more information, please follow other related articles on the PHP Chinese website!