Skip to content

Instantly share code, notes, and snippets.

@rosinghal
Created July 17, 2020 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rosinghal/995425a5014c9527b517d2d664c2a40e to your computer and use it in GitHub Desktop.
Save rosinghal/995425a5014c9527b517d2d664c2a40e to your computer and use it in GitHub Desktop.
Export GitHub issues to CSV
const Axios = require("axios");
const { createObjectCsvWriter } = require("csv-writer");
const GITHUB_ACCESS_TOKEN = "<<GITHUB_ACCESS_TOKEN>>";
const ORG_NAME = "<<ORG_NAME>>";
const asyncForEach = async (array, callback) => {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
};
const githubInstance = Axios.create({
baseURL: "https://api.github.com",
headers: {
Authorization: `token ${GITHUB_ACCESS_TOKEN}`
}
});
const listReposForOrg = (org, params = {}) => {
return githubInstance.get(`/orgs/${org}/repos`, {
params
});
};
const listIssuesForRepo = (repo, params = {}) => {
return githubInstance.get(`/repos/${repo}/issues`, {
params
});
};
const createCSV = (name, rows) => {
const csvWriter = createObjectCsvWriter({
path: `${name}.csv`,
header: Object.keys(rows[0]).map(key => ({ id: key, title: key }))
});
csvWriter.writeRecords(rows);
};
const init = async () => {
try {
const csvRows = [];
const repos = (
await listReposForOrg(ORG_NAME, {
per_page: 100
})
).data;
await asyncForEach(repos, async repo => {
const issuesForRepo = (
await listIssuesForRepo(repo.full_name, {
per_page: 100
})
).data;
console.log(
`${repo.full_name} repo has ${issuesForRepo.length} issues`
);
issuesForRepo.forEach(issueForRepo => {
const labels = issueForRepo.labels.map(l => l.name);
const csvRow = {
name: issueForRepo.title,
labels: labels.join(","),
description: issueForRepo.body,
repo_name: repo.full_name
};
csvRows.push(csvRow);
});
});
createCSV("github-issues", csvRows);
} catch (error) {
console.error(error.message);
}
};
init();
@rosinghal
Copy link
Author

Need to work on implementation if repositories or issues per repository are more 100.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment