const axios = require('axios');
const path = require('path');
const { readPropertiesFile, replacePlaceholders } = require('../essential');
async function deleteRepos(org, repos, token) {
const filePath = path.join(__dirname, 'properties', 'api.properties');
const config = readPropertiesFile(filePath);
if (!config.repospecificurl) {
throw new Error("Repository-specific URL is missing in the configuration.");
}
try {
const deleteRequests = repos.map(async repo => {
const replacements = { organization: org, repository: repo };
return await axios.delete(replacePlaceholders(config.repospecificurl, replacements), {
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
});
});
return await Promise.all(deleteRequests);
} catch (error) {
console.error('Error deleting GitHub repos:', error);
throw error;
}
}
module.exports = deleteRepos;
/*
@param org = String
@param repos = Array
@param token = String
*/
const { deleteRepos } = require('jwz/github');
const org = 'your-org-name';
const repos = ['your-repoA', 'your-repoB'];
const token = 'your-token';
const res = await deleteRepos(org, repos, token);
console.log(res);
The deleteRepos
function is a utility for programmatically deleting GitHub repositories within an organization. It reads configurations from an external properties file and uses Axios for API requests. Here's a breakdown:
org
: The name of the GitHub organization from which the repositories will be deleted.repos
: An array of repository names to be deleted.token
: A GitHub personal access token for authentication.Promise.all
.