diff --git a/packages/core/README.md b/packages/core/README.md index a26abeb..641d45f 100644 --- a/packages/core/README.md +++ b/packages/core/README.md @@ -120,6 +120,7 @@ await new Releaser({ | Step | Description | | --- | --- | +| setUser | Set git user configuration. | | checkout | Checkout the desired branch. | | bump | Bump the version of the project and generate changelog. | | commit | Commit the changes with the new version. | diff --git a/packages/core/src/releaser.spec.ts b/packages/core/src/releaser.spec.ts index 0ea34f0..cc870ed 100644 --- a/packages/core/src/releaser.spec.ts +++ b/packages/core/src/releaser.spec.ts @@ -75,6 +75,26 @@ describe('core', () => { expect(project.versionUpdates).toEqual([]) }) + it('should set git user configuration', async () => { + const { cwd } = await forkProject('set-user', packageJsonProject()) + const project = new PackageJsonProject({ + path: join(cwd, 'package.json') + }) + const releaser = new Releaser({ + project, + silent: true + }) + .setUser({ + username: 'Release Bot', + email: 'release-bot@example.com' + }) + + await releaser.run() + + expect(await project.gitClient.getConfig('user.name')).toBe('Release Bot') + expect(await project.gitClient.getConfig('user.email')).toBe('release-bot@example.com') + }) + it('should create maintenance branches', async () => { const { cwd } = await forkProject('maintenance-branch', packageJsonProject({ version: '3.0.0' diff --git a/packages/core/src/releaser.ts b/packages/core/src/releaser.ts index 9c12e49..515834c 100644 --- a/packages/core/src/releaser.ts +++ b/packages/core/src/releaser.ts @@ -7,6 +7,7 @@ import type { ReleaserOptions, ReleaserCommitOptions, ReleaserMaintenanceBranchOptions, + ReleaserSetUserOptions, ReleaserTagOptions, ReleaserPushOptions, ReleaserStepsOptions @@ -106,6 +107,51 @@ export class Releaser< return this } + /** + * Enqueue a task to set git user configuration. + * @param options + * @returns Project releaser instance for chaining. + */ + setUser(options?: ReleaserSetUserOptions) { + return this.enqueue(async () => { + const { + logger, + gitClient + } = this + const { dryRun } = this.options + const { + username, + email + } = { + ...this.stepsOptions.setUser, + ...options + } + + logger.info('set-user', 'Setting git user...') + + if (!username && !email) { + logger.info('set-user', 'No git user configuration provided.') + return + } + + if (username) { + logger.verbose('set-user', `Setting git user.name to "${username}".`) + + if (!dryRun) { + await gitClient.setConfig('user.name', username) + } + } + + if (email) { + logger.verbose('set-user', `Setting git user.email to "${email}".`) + + if (!dryRun) { + await gitClient.setConfig('user.email', email) + } + } + }) + } + /** * Enqueue a task to checkout a branch. * @param branch - The branch to checkout, defaults to `'simple-release'`. @@ -121,8 +167,6 @@ export class Releaser< const { dryRun } = this.options const { branch: headBranch = 'simple-release', - username, - email, fetch, force } = { @@ -140,22 +184,6 @@ export class Releaser< this.state.headBranch = headBranch this.state.baseBranch = await this.getBaseBranch() - if (username) { - logger.verbose('checkout', `Setting git user.name to "${username}".`) - - if (!dryRun) { - await gitClient.setConfig('user.name', username) - } - } - - if (email) { - logger.verbose('checkout', `Setting git user.email to "${email}".`) - - if (!dryRun) { - await gitClient.setConfig('user.email', email) - } - } - if (fetch) { logger.verbose('checkout', `Fetching all commits and tags from the remote repository...`) diff --git a/packages/core/src/releaser.types.ts b/packages/core/src/releaser.types.ts index e1852fe..46ee08c 100644 --- a/packages/core/src/releaser.types.ts +++ b/packages/core/src/releaser.types.ts @@ -42,6 +42,9 @@ export interface ReleaserCheckoutOptions { * Force fetch all commits and tags from the remote repository. */ fetch?: boolean +} + +export interface ReleaserSetUserOptions { /** * Set git user name configuration. */ @@ -77,6 +80,7 @@ export interface ReleaserStepsOptions< checkout?: { branch?: string } & ReleaserCheckoutOptions + setUser?: ReleaserSetUserOptions bump?: PickOverridableOptions commit?: ReleaserCommitOptions maintenanceBranch?: ReleaserMaintenanceBranchOptions diff --git a/packages/github-action/README.md b/packages/github-action/README.md index 60ff534..f522a37 100644 --- a/packages/github-action/README.md +++ b/packages/github-action/README.md @@ -147,6 +147,7 @@ To fetch and parse comments you should use `fetchOptions` step after `checkout` ```js action + .setUser() .checkout() .fetchOptions() .bump() @@ -160,6 +161,7 @@ action ```js action + .setUser() .maintenanceBranch() .tag() .push() diff --git a/packages/github-action/src/releaser.ts b/packages/github-action/src/releaser.ts index 67ffaf6..d7a97d0 100644 --- a/packages/github-action/src/releaser.ts +++ b/packages/github-action/src/releaser.ts @@ -3,6 +3,7 @@ import { type PickOverridableOptions, type Project, type ReleaserOptions, + type ReleaserSetUserOptions, Releaser } from '@simple-release/core' import { @@ -36,10 +37,16 @@ export class ReleaserGithubAction

extends Releaser< }) } - override checkout(branch?: string) { - return super.checkout(branch, { + override setUser(options?: ReleaserSetUserOptions) { + return super.setUser({ username: 'github-actions[bot]', email: 'github-actions[bot]@users.noreply.github.com', + ...options + }) + } + + override checkout(branch?: string) { + return super.checkout(branch, { fetch: true, force: true }) @@ -122,6 +129,7 @@ export class ReleaserGithubAction

extends Releaser< */ async runPullRequestAction() { await this + .setUser() .checkout() .fetchOptions() .bump() @@ -137,6 +145,7 @@ export class ReleaserGithubAction

extends Releaser< */ async runReleaseAction(check = true) { await this + .setUser() .maintenanceBranch() .tag() .push()