|
| 1 | +module.exports = mutateGithubRepositories; |
| 2 | + |
| 3 | +const { resolve } = require("path"); |
| 4 | + |
| 5 | +const { Octokit: OctokitCore } = require("@octokit/core"); |
| 6 | +const { paginateRest } = require("@octokit/plugin-paginate-rest"); |
| 7 | +const { throttling } = require("@octokit/plugin-throttling"); |
| 8 | +const { retry } = require("@octokit/plugin-retry"); |
| 9 | + |
| 10 | +const { cache: octokitCachePlugin } = require("./lib/octokit-plugin-cache"); |
| 11 | +const { resolveRepositories } = require("./lib/resolve-repositories"); |
| 12 | +const { name, version } = require("./package.json"); |
| 13 | + |
| 14 | +const Octokit = OctokitCore.plugin(paginateRest, throttling, retry).defaults({ |
| 15 | + log: console, |
| 16 | + userAgent: [name, version].join("/"), |
| 17 | + throttle: { |
| 18 | + onAbuseLimit: (error, options) => { |
| 19 | + octokit.log.error("onAbuseLimit", error, options); |
| 20 | + }, |
| 21 | + onRateLimit: (error, options) => { |
| 22 | + octokit.log.error("onRateLimit", error, options); |
| 23 | + }, |
| 24 | + }, |
| 25 | +}); |
| 26 | + |
| 27 | +/** |
| 28 | + * Find all releases in a GitHub repository or organization after a specified date |
| 29 | + * |
| 30 | + * @param {object} options |
| 31 | + * @param {string} options.token Personal Access Token: Requires the "public_repo" scope for public repositories, "repo" scope for private repositories. |
| 32 | + * @param {string} options.script Path to script to run against a repository |
| 33 | + * @param {string} options.repos Array of repository names in the form of "repo-owner/repo-name". To match all repositories for an owner, pass "repo-owner/*" |
| 34 | + * @param {boolean} options.cache Cache responses for debugging |
| 35 | + */ |
| 36 | +async function mutateGithubRepositories( |
| 37 | + options = { |
| 38 | + cache: false, |
| 39 | + repos: [], |
| 40 | + } |
| 41 | +) { |
| 42 | + const { token, script, repos, cache, ...userOptions } = options; |
| 43 | + const MyOctokit = cache ? Octokit.plugin(octokitCachePlugin) : Octokit; |
| 44 | + const octokit = new MyOctokit({ |
| 45 | + auth: token, |
| 46 | + }); |
| 47 | + |
| 48 | + let userScript; |
| 49 | + try { |
| 50 | + userScript = require(resolve(process.cwd(), script)); |
| 51 | + } catch (error) { |
| 52 | + throw new Error( |
| 53 | + `[mutate-github-repositories] ${script} script could not be found` |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + if (repos.length === 0) { |
| 58 | + throw new Error("[mutate-github-repositories] No repositories provided"); |
| 59 | + } |
| 60 | + |
| 61 | + state = { |
| 62 | + log: console, |
| 63 | + octokit, |
| 64 | + }; |
| 65 | + |
| 66 | + const repositories = await resolveRepositories(state, repos); |
| 67 | + |
| 68 | + for (const repository of repositories) { |
| 69 | + console.log("Running %s on %s...", script, repository.full_name); |
| 70 | + await userScript(octokit, repository, userOptions); |
| 71 | + } |
| 72 | + |
| 73 | + console.log("\ndone."); |
| 74 | +} |
0 commit comments