|
| 1 | +import { getFileContent } from "./get-file-content.js"; |
| 2 | +import { createRepositoryFromFolder } from "./create-repository-from-folder.js"; |
| 3 | + |
| 4 | +/** |
| 5 | + * Updates branch protection based on a settings from a template repository |
| 6 | + * |
| 7 | + * @param {import('@octoherd/octokit').Octokit} octokit |
| 8 | + * @param {import('@octokit/openapi-types').components["schemas"]["repository"]} repository |
| 9 | + * @param {object} options |
| 10 | + * @param {string} [options.pathToFolders] |
| 11 | + */ |
| 12 | +export async function script(octokit, repository, { pathToFolders }) { |
| 13 | + if (!pathToFolders) { |
| 14 | + throw new Error(`--path-to-folders is required`); |
| 15 | + } |
| 16 | + |
| 17 | + const owner = repository.owner.login; |
| 18 | + const repo = repository.name; |
| 19 | + |
| 20 | + try { |
| 21 | + const { data } = await octokit.request( |
| 22 | + "GET /repos/{owner}/{repo}/contents/{path}", |
| 23 | + { |
| 24 | + owner, |
| 25 | + repo, |
| 26 | + path: pathToFolders, |
| 27 | + } |
| 28 | + ); |
| 29 | + |
| 30 | + if (!Array.isArray(data)) { |
| 31 | + octokit.info( |
| 32 | + "'%s' is not a folder in %s. It's a %s", |
| 33 | + pathToFolders, |
| 34 | + repository.full_name, |
| 35 | + data.type |
| 36 | + ); |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + let pkg; |
| 41 | + let pkgLock; |
| 42 | + |
| 43 | + for (const item of data) { |
| 44 | + if (item.type !== "dir") { |
| 45 | + octokit.info( |
| 46 | + "'%s/%s' is not a folder in %s. It's a %s", |
| 47 | + pathToFolders, |
| 48 | + item.name, |
| 49 | + repository.full_name, |
| 50 | + item.type |
| 51 | + ); |
| 52 | + continue; |
| 53 | + } |
| 54 | + |
| 55 | + pkg = pkg || (await getFileContent(octokit, owner, repo, "package.json")); |
| 56 | + pkgLock = |
| 57 | + pkgLock || |
| 58 | + (await getFileContent(octokit, owner, repo, "package-lock.json")); |
| 59 | + |
| 60 | + const readme = await getFileContent( |
| 61 | + octokit, |
| 62 | + owner, |
| 63 | + repo, |
| 64 | + item.path + "/README.md" |
| 65 | + ); |
| 66 | + |
| 67 | + await createRepositoryFromFolder(octokit, repository, item.path, { |
| 68 | + readme, |
| 69 | + package: pkg, |
| 70 | + packageLock: pkgLock, |
| 71 | + }); |
| 72 | + } |
| 73 | + } catch (error) { |
| 74 | + if (error.status === 404) { |
| 75 | + octokit.info( |
| 76 | + "'%s' does not exist in %s", |
| 77 | + pathToFolders, |
| 78 | + repository.full_name |
| 79 | + ); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + throw error; |
| 84 | + } |
| 85 | +} |
0 commit comments