Skip to content

Commit

Permalink
fix: support helia projecs in check-project (#1394)
Browse files Browse the repository at this point in the history
Recognises the helia header image for readmes, etc
  • Loading branch information
achingbrain authored Oct 25, 2023
1 parent bf2acb5 commit 6088b24
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 73 deletions.
34 changes: 3 additions & 31 deletions src/check-project/check-build-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
}
9 changes: 7 additions & 2 deletions src/check-project/check-monorepo-readme.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
9 changes: 7 additions & 2 deletions src/check-project/check-readme.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
63 changes: 32 additions & 31 deletions src/check-project/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}
}
})
Expand Down
45 changes: 38 additions & 7 deletions src/check-project/readme/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,25 @@ const BADGES = {
}

/**
* @param {*} pkg
* @param {string} repoOwner
* @param {string} repoName
* @param {string} defaultBranch
* @param {string} ciFile
* @type {Record<string, (pkg: *, repoOwner: string, repoName: string, defaultBranch: string, ciFile: string) => string>}
*/
export const HEADER = (pkg, repoOwner, repoName, defaultBranch, ciFile) => {
return `
const HEADERS = {
'ipfs/helia(-.+)?$': (pkg, repoOwner, repoName, defaultBranch, ciFile) => `
<p align="center">
<a href="https://github.com/ipfs/helia" title="Helia">
<img src="https://raw.githubusercontent.com/ipfs/helia/main/assets/helia.png" alt="Helia logo" width="300" />
</a>
</p>
# ${pkg.name} <!-- omit in toc -->
${(BADGES[repoOwner] ?? BADGES.default)(repoOwner, repoName, defaultBranch, ciFile).trim()}
> ${pkg.description}
## Table of contents <!-- omit in toc -->
`,
default: (pkg, repoOwner, repoName, defaultBranch, ciFile) => `
# ${pkg.name} <!-- omit in toc -->
${(BADGES[repoOwner] ?? BADGES.default)(repoOwner, repoName, defaultBranch, ciFile).trim()}
Expand All @@ -43,3 +54,23 @@ ${(BADGES[repoOwner] ?? BADGES.default)(repoOwner, repoName, defaultBranch, ciFi
## Table of contents <!-- omit in toc -->
`
}

/**
* @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)
}

0 comments on commit 6088b24

Please sign in to comment.