copy-repo-to
Advanced tools
Comparing version 0.6.0 to 0.7.0
53
lib.js
const { Octokit } = require('octokit'); | ||
const path = require('path'); | ||
@@ -41,3 +42,2 @@ /** | ||
description: description ?? repoDescription, | ||
homepage: html_url, | ||
include_all_branches: false, | ||
@@ -50,3 +50,30 @@ private: false, | ||
await octokit.request(`POST /repos/${owner}/${templateRepo}/pages`, { | ||
const waitForBranch = () => new Promise((resolve, reject) => { | ||
const BRANCH_CHECK_TIMER = 1000; | ||
let TIMES_LEFT = 10; | ||
const checkForBranch = async () => { | ||
if (TIMES_LEFT <= 0) { | ||
reject('Branch creation timeout'); | ||
} | ||
TIMES_LEFT--; | ||
const { data } = await octokit.request(`GET /repos/${owner}/${templateRepo}/branches`, { | ||
owner, | ||
repo: templateRepo, | ||
headers: { | ||
'X-GitHub-Api-Version': '2022-11-28' | ||
} | ||
}); | ||
if (data.find(({name})=>(name === 'main')) !== undefined) { | ||
resolve('branch found'); | ||
} else { | ||
startCheckBranchTimer(); | ||
} | ||
}; | ||
const startCheckBranchTimer = () => new setTimeout(checkForBranch, BRANCH_CHECK_TIMER); | ||
checkForBranch(); | ||
}); | ||
await waitForBranch(); | ||
const {data} = await octokit.request(`POST /repos/${owner}/${templateRepo}/pages`, { | ||
owner, | ||
@@ -63,3 +90,3 @@ repo: templateRepo, | ||
const { html_url: newRepoURL } = await octokit.request(`GET /repos/${owner}/${templateRepo}/pages`, { | ||
const { data: { html_url: GitHubPagesBaseURL}} = await octokit.request(`GET /repos/${owner}/${templateRepo}/pages`, { | ||
owner, | ||
@@ -70,4 +97,22 @@ repo: templateRepo, | ||
} | ||
}) | ||
}); | ||
const {data: filesList} = await octokit.request(`GET /repos/${owner}/${templateRepo}/contents/`, { | ||
owner, | ||
repo: templateRepo, | ||
path: '/', | ||
headers: { | ||
'X-GitHub-Api-Version': '2022-11-28' | ||
} | ||
}); | ||
const fileNamesList = filesList | ||
.filter(({type}) => (type === 'file')) | ||
.filter(({path: filePath}) => path.extname(filePath) === '.html') | ||
.map(({name}) => name); | ||
const hasHTML = fileNamesList.length > 0; | ||
const hasSingleHTML = fileNamesList.length === 1; | ||
const hasIndex = fileNamesList.find((name)=>(name === 'index.html')) !== undefined; | ||
const GitHubPagesURL = (hasHTML && !hasIndex && hasSingleHTML) ? `${GitHubPagesBaseURL}${fileNamesList[0]}`: GitHubPagesBaseURL; | ||
await octokit.request(`PATCH /repos/${owner}/${templateRepo}`, { | ||
@@ -74,0 +119,0 @@ owner, |
{ | ||
"name": "copy-repo-to", | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"description": "Copy repo marked as a template to new owner", | ||
@@ -5,0 +5,0 @@ "main": "lib.js", |
41179
149