GitHub
deleteRepos
Code
const axios = require('axios');
const path = require('path');
const { readPropertiesFile, requireHttpsUrl, resolveSecureUrl } = 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.");
}
requireHttpsUrl(config.repospecificurl, 'repospecificurl');
try {
const deleteRequests = repos.map(async repo => {
const replacements = { organization: org, repository: repo };
return await axios.delete(resolveSecureUrl(config.repospecificurl, replacements, 'repospecificurl'), {
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;
Usage
/*
@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);
Explanation
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:
- Parameters:
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.
- Logic:
- Reads the repository-specific URL from a properties file.
- Checks the endpoint uses https, and throws before the token is sent if it does not.
- Constructs a delete request for each repository using the GitHub API.
- Executes all delete requests concurrently using
Promise.all. - Handles and logs any errors encountered during the process.
- Output:
- An array of responses for each delete request, where each response contains the result of the deletion.