From 879e247df40f1219e50b616899040efb59cbd399 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Wed, 25 Oct 2023 18:11:37 +0100 Subject: [PATCH] fix: support helia projecs in check-project Recognises the helia header image for readmes, etc --- src/check-project/check-build-files.js | 34 ++---------- src/check-project/check-monorepo-readme.js | 9 +++- src/check-project/check-readme.js | 9 +++- src/check-project/index.js | 63 +++++++++++----------- src/check-project/readme/header.js | 45 +++++++++++++--- 5 files changed, 87 insertions(+), 73 deletions(-) diff --git a/src/check-project/check-build-files.js b/src/check-project/check-build-files.js index 8763f358b..ddfde1dcf 100644 --- a/src/check-project/check-build-files.js +++ b/src/check-project/check-build-files.js @@ -6,9 +6,7 @@ import { ensureFileNotPresent } from './utils.js' -const managedRepos = 'https://raw.githubusercontent.com/protocol/.github/master/configs/js.json' -const ciFileUrl = 'https://raw.githubusercontent.com/protocol/.github/master/templates/.github/workflows/js-test-and-release.yml' -const mergeFileUrl = 'https://raw.githubusercontent.com/protocol/.github/master/templates/.github/workflows/automerge.yml' +const ciFileUrl = 'https://raw.githubusercontent.com/pl-strflt/uci/main/templates/.github/workflows/js-test-and-release.yml' /** * @param {string} url @@ -32,21 +30,6 @@ async function download (url) { }) } -/** - * @param {string} repoName - */ -async function isManagedRepo (repoName) { - const repos = JSON.parse(await download(managedRepos)).repositories - - for (const { target } of repos) { - if (target === repoName) { - return true - } - } - - return false -} - /** * @param {string} projectDir * @param {string} branchName @@ -58,20 +41,9 @@ export async function checkBuildFiles (projectDir, branchName, repoUrl) { await ensureFileNotPresent(projectDir, '.travis.yml') await ensureFileHasContents(projectDir, '.github/dependabot.yml') - // if this repo is managed by https://github.com/protocol/.github don't try to update the ci files - const isManaged = await isManagedRepo(repoUrl.replace('https://github.com/', '')) - - if (isManaged) { - console.info('CI files are managed by https://github.com/protocol/.github') - return - } - let defaultCiContent = await download(ciFileUrl) - defaultCiContent = defaultCiContent.replace(/\${{{ github.default_branch }}}/g, branchName) + defaultCiContent = defaultCiContent.replaceAll('${{{ github.default_branch }}}', branchName) // eslint-disable-line no-template-curly-in-string + defaultCiContent = defaultCiContent.replaceAll('${{{ .config.versions.uci // (.source.tag | sub("\\\\.[^.\\\\-\\\\+]+(?=\\\\-|\\\\+|$)"; "")) }}}', 'v0.0') // eslint-disable-line no-template-curly-in-string await ensureFileHasContents(projectDir, '.github/workflows/js-test-and-release.yml', defaultCiContent) - - const defaultMergeContent = await download(mergeFileUrl) - - await ensureFileHasContents(projectDir, '.github/workflows/automerge.yml', defaultMergeContent) } diff --git a/src/check-project/check-monorepo-readme.js b/src/check-project/check-monorepo-readme.js index e501c5093..e42151043 100644 --- a/src/check-project/check-monorepo-readme.js +++ b/src/check-project/check-monorepo-readme.js @@ -65,12 +65,17 @@ export async function checkMonorepoReadme (projectDir, repoUrl, defaultBranch, p file.children.forEach((child, index) => { const rendered = writeMarkdown(child).toLowerCase() - if (child.type === 'heading' && index === 0) { + if (rendered.includes('https://raw.githubusercontent.com/ipfs/helia/main/assets/helia.png')) { + // skip helia logo + return + } + + if (child.type === 'heading' && (index === 0 || rendered.includes(pkg.name))) { // skip heading return } - if (child.type === 'paragraph' && index === 1) { + if (child.type === 'paragraph' && (index === 1 || rendered.includes('![ci]'))) { // skip badges return } diff --git a/src/check-project/check-readme.js b/src/check-project/check-readme.js index 0fb946b0d..6974da93a 100644 --- a/src/check-project/check-readme.js +++ b/src/check-project/check-readme.js @@ -68,12 +68,17 @@ export async function checkReadme (projectDir, repoUrl, defaultBranch, ciFile, r file.children.forEach((child, index) => { const rendered = writeMarkdown(child).toLowerCase() - if (child.type === 'heading' && index === 0) { + if (rendered.includes('https://raw.githubusercontent.com/ipfs/helia/main/assets/helia.png')) { + // skip helia logo + return + } + + if (child.type === 'heading' && (index === 0 || rendered.includes(pkg.name))) { // skip heading return } - if (child.type === 'paragraph' && index === 1) { + if (child.type === 'paragraph' && (index === 1 || rendered.includes('![ci]'))) { // skip badges return } diff --git a/src/check-project/index.js b/src/check-project/index.js index 332e7f7a0..8958780eb 100755 --- a/src/check-project/index.js +++ b/src/check-project/index.js @@ -36,24 +36,39 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url)) * @param {string} projectDir */ async function getConfig (projectDir) { - if (process.env.CI) { - const branchName = await execa('git', ['symbolic-ref', '--short', 'refs/remotes/origin/HEAD'], { - cwd: projectDir + const branchName = await execa('git', ['symbolic-ref', '--short', 'refs/remotes/origin/HEAD'], { + cwd: projectDir + }) + .catch(async err => { + // if this repo was not clone from the origin, update the default + // origin/HEAD and try again + if (err.stderr.includes('ref refs/remotes/origin/HEAD is not a symbolic ref')) { + await execa('git', ['remote', 'set-head', 'origin', '-a'], { + cwd: projectDir + }) + + return await execa('git', ['symbolic-ref', '--short', 'refs/remotes/origin/HEAD'], { + cwd: projectDir + }) + } + + throw err + }) + .then(res => execa('basename', [res.stdout])) + .then(res => res.stdout) + .catch(() => { + return 'master' }) - .then(res => execa('basename', [res.stdout])) - .then(res => res.stdout) - .catch(() => { - return 'master' - }) - const repoUrl = await execa('git', ['remote', 'get-url', 'origin'], { - cwd: projectDir + const repoUrl = await execa('git', ['remote', 'get-url', 'origin'], { + cwd: projectDir + }) + .then(res => res.stdout.split(':')[1].split('.git')[0]) + .then(res => `https://github.com/${res}`) + .catch(() => { + return '' }) - .then(res => res.stdout.split(':')[1].split('.git')[0]) - .then(res => `https://github.com/${res}`) - .catch(() => { - return '' - }) + if (process.env.CI) { return { projectDir, branchName, @@ -66,24 +81,10 @@ async function getConfig (projectDir) { const res = await prompt.get({ properties: { branchName: { - default: await execa('git', ['symbolic-ref', '--short', 'refs/remotes/origin/HEAD'], { - cwd: projectDir - }) - .then(res => execa('basename', [res.stdout])) - .then(res => res.stdout) - .catch(() => { - return 'master' - }) + default: branchName }, repoUrl: { - default: await execa('git', ['remote', 'get-url', 'origin'], { - cwd: projectDir - }) - .then(res => res.stdout.split(':')[1].split('.git')[0]) - .then(res => `https://github.com/${res}`) - .catch(() => { - return '' - }) + default: repoUrl } } }) diff --git a/src/check-project/readme/header.js b/src/check-project/readme/header.js index 3159763c0..18f322e4e 100644 --- a/src/check-project/readme/header.js +++ b/src/check-project/readme/header.js @@ -26,14 +26,25 @@ const BADGES = { } /** - * @param {*} pkg - * @param {string} repoOwner - * @param {string} repoName - * @param {string} defaultBranch - * @param {string} ciFile + * @type {Record string>} */ -export const HEADER = (pkg, repoOwner, repoName, defaultBranch, ciFile) => { - return ` +const HEADERS = { + 'ipfs/helia(-.+)?$': (pkg, repoOwner, repoName, defaultBranch, ciFile) => ` +

+ + Helia logo + +

+ +# ${pkg.name} + +${(BADGES[repoOwner] ?? BADGES.default)(repoOwner, repoName, defaultBranch, ciFile).trim()} + +> ${pkg.description} + +## Table of contents +`, + default: (pkg, repoOwner, repoName, defaultBranch, ciFile) => ` # ${pkg.name} ${(BADGES[repoOwner] ?? BADGES.default)(repoOwner, repoName, defaultBranch, ciFile).trim()} @@ -43,3 +54,23 @@ ${(BADGES[repoOwner] ?? BADGES.default)(repoOwner, repoName, defaultBranch, ciFi ## Table of contents ` } + +/** + * @param {*} pkg + * @param {string} repoOwner + * @param {string} repoName + * @param {string} defaultBranch + * @param {string} ciFile + */ +export const HEADER = (pkg, repoOwner, repoName, defaultBranch, ciFile) => { + let generateHeader = HEADERS.default + + for (const [key, fn] of Object.entries(HEADERS)) { + if (new RegExp(key, 'm').test(`${repoOwner}/${repoName}`)) { + generateHeader = fn + break + } + } + + return generateHeader(pkg, repoOwner, repoName, defaultBranch, ciFile) +}