-
-
Notifications
You must be signed in to change notification settings - Fork 40
feat: add an input to run at a subdirectory of the repo root #232
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "changesets-gitlab": minor | ||
| --- | ||
|
|
||
| feat: add an input to run at a subdirectory of the repo root |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| import path from 'node:path' | ||
|
|
||
| import { ValidationError } from '@changesets/errors' | ||
| import type { | ||
| ComprehensiveRelease, | ||
|
|
@@ -21,7 +23,12 @@ import * as context from './context.js' | |
| import { env } from './env.js' | ||
| import { getChangedPackages } from './get-changed-packages.js' | ||
| import type { LooseString } from './types.js' | ||
| import { getUsername, HTTP_STATUS_NOT_FOUND, TRUTHY_VALUES } from './utils.js' | ||
| import { | ||
| getUsername, | ||
| HTTP_STATUS_NOT_FOUND, | ||
| getCwdInput, | ||
| TRUTHY_VALUES, | ||
| } from './utils.js' | ||
|
|
||
| const generatedByBotNote = 'Generated By Changesets GitLab Bot' | ||
|
|
||
|
|
@@ -220,13 +227,15 @@ async function getNoteInfo( | |
|
|
||
| const hasChangesetBeenAdded = async ( | ||
| changedFilesPromise: Promise<CommitDiffSchema[] | MergeRequestDiffSchema[]>, | ||
| changesetPrefix: string, | ||
| ) => { | ||
| const changedFiles = await changedFilesPromise | ||
| return changedFiles.some(file => { | ||
| return ( | ||
| file.new_file && | ||
| /^\.changeset\/.+\.md$/.test(file.new_path) && | ||
| file.new_path !== '.changeset/README.md' | ||
| file.new_path.startsWith(changesetPrefix + '/') && | ||
| file.new_path.endsWith('.md') && | ||
| file.new_path !== changesetPrefix + '/README.md' | ||
| ) | ||
| }) | ||
| } | ||
|
|
@@ -249,6 +258,10 @@ export const comment = async () => { | |
| return | ||
| } | ||
|
|
||
| const cwdRel = getCwdInput() | ||
| const changesetPrefix = cwdRel ? `${cwdRel}/.changeset` : '.changeset' | ||
| const absoluteCwd = path.resolve(process.cwd(), cwdRel || '.') | ||
|
|
||
| const api = createApi() | ||
|
|
||
| let errFromFetchingChangedFiles = '' | ||
|
|
@@ -273,15 +286,25 @@ export const comment = async () => { | |
| return changes | ||
| }) | ||
|
|
||
| const subdirPrefix = cwdRel ? `${cwdRel}/` : '' | ||
| const packageChangedFiles = changedFilesPromise.then(changedFiles => | ||
| changedFiles | ||
| .filter( | ||
| ({ new_path }) => !subdirPrefix || new_path.startsWith(subdirPrefix), | ||
| ) | ||
| .map(({ new_path }) => | ||
| subdirPrefix ? new_path.slice(subdirPrefix.length) : new_path, | ||
| ), | ||
| ) | ||
|
|
||
| const [noteInfo, hasChangeset, { changedPackages, releasePlan }] = | ||
| await Promise.all([ | ||
| getNoteInfo(api, mrIid, commentType), | ||
| hasChangesetBeenAdded(changedFilesPromise), | ||
| hasChangesetBeenAdded(changedFilesPromise, changesetPrefix), | ||
| getChangedPackages({ | ||
| changedFiles: changedFilesPromise.then(changedFiles => | ||
| changedFiles.map(({ new_path }) => new_path), | ||
| ), | ||
| changedFiles: packageChangedFiles, | ||
| api, | ||
| cwd: absoluteCwd, | ||
| }).catch((err: unknown) => { | ||
| if (err instanceof ValidationError) { | ||
| errFromFetchingChangedFiles = `<details><summary>💥 An error occurred when fetching the changed packages and changesets in this MR</summary>\n\n\`\`\`\n${err.message}\n\`\`\`\n\n</details>\n` | ||
|
|
@@ -295,7 +318,7 @@ export const comment = async () => { | |
| }), | ||
| ] as const) | ||
|
|
||
| const newChangesetFileName = `.changeset/${humanId({ | ||
| const newChangesetFileName = `${changesetPrefix}/${humanId({ | ||
| separator: '-', | ||
| capitalize: false, | ||
| })}.md` | ||
|
Comment on lines
+321
to
324
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -161,6 +161,18 @@ export const execSync = (command: string) => | |
|
|
||
| export const getOptionalInput = (name: string) => getInput(name) || undefined | ||
|
|
||
| export const getCwdInput = (): string => { | ||
| const input = getOptionalInput('cwd') | ||
| if (!input) { | ||
| return '' | ||
| } | ||
| const normalized = input.replace(/^\.\//, '').replace(/\/$/, '') | ||
| if (path.isAbsolute(normalized) || normalized.split('/').includes('..')) { | ||
| throw new Error(`Invalid cwd input: "${input}"`) | ||
| } | ||
| return normalized === '.' ? '' : normalized | ||
| } | ||
|
Comment on lines
+164
to
+174
|
||
|
|
||
| // eslint-disable-next-line sonarjs/function-return-type | ||
| export const getUsername = (api: Gitlab) => { | ||
| return ( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
cwdRelis set,packageChangedFileskeeps changes outside the subdirectory unchanged. BecausegetChangedPackagesnow operates relative toabsoluteCwd, leaving out-of-scope paths in the changed-files list can cause false positives (e.g., an outside path containingpackages/foowill match a workspace package dir ofpackages/foo). Consider filtering out files that don't start withsubdirPrefixwhencwdRelis provided, instead of keeping them.