Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support helia projecs in check-project #1394

Merged
merged 1 commit into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
})
}

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

Check warning on line 46 in src/check-project/check-build-files.js

View check run for this annotation

Codecov / codecov/patch

src/check-project/check-build-files.js#L45-L46

Added lines #L45 - L46 were not covered by tests

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 @@
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))) {

Check warning on line 73 in src/check-project/check-monorepo-readme.js

View check run for this annotation

Codecov / codecov/patch

src/check-project/check-monorepo-readme.js#L68-L73

Added lines #L68 - L73 were not covered by tests
// skip heading
return
}

if (child.type === 'paragraph' && index === 1) {
if (child.type === 'paragraph' && (index === 1 || rendered.includes('![ci]'))) {

Check warning on line 78 in src/check-project/check-monorepo-readme.js

View check run for this annotation

Codecov / codecov/patch

src/check-project/check-monorepo-readme.js#L78

Added line #L78 was not covered by tests
// 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 @@
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))) {

Check warning on line 76 in src/check-project/check-readme.js

View check run for this annotation

Codecov / codecov/patch

src/check-project/check-readme.js#L71-L76

Added lines #L71 - L76 were not covered by tests
// skip heading
return
}

if (child.type === 'paragraph' && index === 1) {
if (child.type === 'paragraph' && (index === 1 || rendered.includes('![ci]'))) {

Check warning on line 81 in src/check-project/check-readme.js

View check run for this annotation

Codecov / codecov/patch

src/check-project/check-readme.js#L81

Added line #L81 was not covered by tests
// 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 @@
* @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'

Check warning on line 60 in src/check-project/index.js

View check run for this annotation

Codecov / codecov/patch

src/check-project/index.js#L39-L60

Added lines #L39 - L60 were not covered by tests
})
.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 ''

Check warning on line 68 in src/check-project/index.js

View check run for this annotation

Codecov / codecov/patch

src/check-project/index.js#L62-L68

Added lines #L62 - L68 were not covered by tests
})
.then(res => res.stdout.split(':')[1].split('.git')[0])
.then(res => `https://github.com/${res}`)
.catch(() => {
return ''
})

if (process.env.CI) {

Check warning on line 71 in src/check-project/index.js

View check run for this annotation

Codecov / codecov/patch

src/check-project/index.js#L71

Added line #L71 was not covered by tests
return {
projectDir,
branchName,
Expand All @@ -66,24 +81,10 @@
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

Check warning on line 84 in src/check-project/index.js

View check run for this annotation

Codecov / codecov/patch

src/check-project/index.js#L84

Added line #L84 was not covered by tests
},
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

Check warning on line 87 in src/check-project/index.js

View check run for this annotation

Codecov / codecov/patch

src/check-project/index.js#L87

Added line #L87 was not covered by tests
}
}
})
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 @@
}

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

Check warning on line 45 in src/check-project/readme/header.js

View check run for this annotation

Codecov / codecov/patch

src/check-project/readme/header.js#L33-L45

Added lines #L33 - L45 were not covered by tests
`,
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 @@
## 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)
}

Check warning on line 76 in src/check-project/readme/header.js

View check run for this annotation

Codecov / codecov/patch

src/check-project/readme/header.js#L66-L76

Added lines #L66 - L76 were not covered by tests
Loading