Created
July 17, 2020 09:09
-
-
Save rosinghal/995425a5014c9527b517d2d664c2a40e to your computer and use it in GitHub Desktop.
Export GitHub issues to CSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Need to work on implementation if repositories or issues per repository are more 100.