-
Notifications
You must be signed in to change notification settings - Fork 14
feat(create-cli): add CI/CD setup step #1266
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ee84cbb
feat(create-cli): add CI/CD setup step
hanna-skryl e5f9719
fix(create-cli): update ci provider options and their validation
hanna-skryl 60d9aeb
refactor(create-cli): improve CI yaml generation
hanna-skryl aae422e
refactor(ci): simplify GitLab config path
hanna-skryl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| import { select } from '@inquirer/prompts'; | ||
| import * as YAML from 'yaml'; | ||
| import { getGitDefaultBranch, logger } from '@code-pushup/utils'; | ||
| import { | ||
| CI_PROVIDERS, | ||
| type CiProvider, | ||
| type CliArgs, | ||
| type ConfigContext, | ||
| type Tree, | ||
| } from './types.js'; | ||
|
|
||
| const GITHUB_WORKFLOW_PATH = '.github/workflows/code-pushup.yml'; | ||
| const GITLAB_CONFIG_PATH = '.gitlab-ci.yml'; | ||
| const GITLAB_CONFIG_SEPARATE_PATH = '.gitlab/ci/code-pushup.gitlab-ci.yml'; | ||
|
|
||
| export async function promptCiProvider(cliArgs: CliArgs): Promise<CiProvider> { | ||
| if (isCiProvider(cliArgs.ci)) { | ||
| return cliArgs.ci; | ||
| } | ||
| if (cliArgs.yes) { | ||
| return 'none'; | ||
| } | ||
| return select<CiProvider>({ | ||
| message: 'CI/CD integration:', | ||
| choices: [ | ||
| { name: 'GitHub Actions', value: 'github' }, | ||
| { name: 'GitLab CI/CD', value: 'gitlab' }, | ||
| { name: 'none', value: 'none' }, | ||
| ], | ||
| default: 'none', | ||
| }); | ||
| } | ||
|
|
||
| export async function resolveCi( | ||
| tree: Tree, | ||
| provider: CiProvider, | ||
| context: ConfigContext, | ||
| ): Promise<void> { | ||
| switch (provider) { | ||
| case 'github': | ||
| await writeGitHubWorkflow(tree, context); | ||
| break; | ||
| case 'gitlab': | ||
| await writeGitLabConfig(tree); | ||
| break; | ||
| case 'none': | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| async function writeGitHubWorkflow( | ||
| tree: Tree, | ||
| context: ConfigContext, | ||
| ): Promise<void> { | ||
| await tree.write(GITHUB_WORKFLOW_PATH, await generateGitHubYaml(context)); | ||
| } | ||
|
|
||
| async function generateGitHubYaml({ | ||
| mode, | ||
| tool, | ||
| }: ConfigContext): Promise<string> { | ||
| const branch = await getGitDefaultBranch(); | ||
| const lines = [ | ||
| 'name: Code PushUp', | ||
| '', | ||
| 'on:', | ||
| ' push:', | ||
| ` branches: [${branch}]`, | ||
| ' pull_request:', | ||
| ` branches: [${branch}]`, | ||
| '', | ||
| 'permissions:', | ||
| ' contents: read', | ||
| ' actions: read', | ||
| ' pull-requests: write', | ||
| '', | ||
| 'jobs:', | ||
| ' code-pushup:', | ||
| ' runs-on: ubuntu-latest', | ||
| ' name: Code PushUp', | ||
| ' steps:', | ||
hanna-skryl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ' - name: Clone repository', | ||
| ' uses: actions/checkout@v5', | ||
| ' - name: Set up Node.js', | ||
| ' uses: actions/setup-node@v6', | ||
| ' - name: Install dependencies', | ||
| ' run: npm ci', | ||
| ' - name: Code PushUp', | ||
| ' uses: code-pushup/github-action@v0', | ||
| ...(mode === 'monorepo' && tool != null | ||
| ? [' with:', ` monorepo: ${tool}`] | ||
| : []), | ||
| ]; | ||
| return `${lines.join('\n')}\n`; | ||
| } | ||
|
|
||
| async function writeGitLabConfig(tree: Tree): Promise<void> { | ||
| const filePath = await resolveGitLabFilePath(tree); | ||
| await tree.write(filePath, generateGitLabYaml()); | ||
|
|
||
| if (filePath === GITLAB_CONFIG_SEPARATE_PATH) { | ||
| await patchRootGitLabConfig(tree); | ||
| } | ||
| } | ||
hanna-skryl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| function generateGitLabYaml(): string { | ||
| const lines = [ | ||
| 'workflow:', | ||
| ' rules:', | ||
| ' - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH', | ||
| " - if: $CI_PIPELINE_SOURCE == 'merge_request_event'", | ||
| '', | ||
| 'include:', | ||
| ' - https://gitlab.com/code-pushup/gitlab-pipelines-template/-/raw/latest/code-pushup.yml', | ||
| ]; | ||
| return `${lines.join('\n')}\n`; | ||
| } | ||
|
|
||
| async function patchRootGitLabConfig(tree: Tree): Promise<void> { | ||
| const content = await tree.read(GITLAB_CONFIG_PATH); | ||
| if (content == null) { | ||
| return; | ||
| } | ||
| const doc = YAML.parseDocument(content); | ||
| if (!YAML.isMap(doc.contents)) { | ||
| logger.warn( | ||
| `Could not update ${GITLAB_CONFIG_PATH}. Add an include entry for ${GITLAB_CONFIG_SEPARATE_PATH} to your config.`, | ||
| ); | ||
| return; | ||
| } | ||
| const entry = { local: GITLAB_CONFIG_SEPARATE_PATH }; | ||
| const include = doc.get('include', true); | ||
| if (include == null) { | ||
| doc.set('include', doc.createNode([entry])); | ||
| } else if (YAML.isSeq(include)) { | ||
| include.add(doc.createNode(entry)); | ||
| } else { | ||
| const existing = doc.get('include'); | ||
| doc.set('include', doc.createNode([existing, entry])); | ||
| } | ||
| await tree.write(GITLAB_CONFIG_PATH, doc.toString()); | ||
| } | ||
|
|
||
| async function resolveGitLabFilePath(tree: Tree): Promise<string> { | ||
| if (await tree.exists(GITLAB_CONFIG_PATH)) { | ||
| return GITLAB_CONFIG_SEPARATE_PATH; | ||
| } | ||
| return GITLAB_CONFIG_PATH; | ||
| } | ||
|
|
||
| function isCiProvider(value: string | undefined): value is CiProvider { | ||
| const validValues: readonly string[] = CI_PROVIDERS; | ||
| return value != null && validValues.includes(value); | ||
| } | ||
hanna-skryl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.