Skip to content
This repository was archived by the owner on Feb 1, 2025. It is now read-only.

Commit 71aac13

Browse files
committed
fix(changelog): simplified logic for generating changelog, fixed issue with bigger repos
1 parent 54b4217 commit 71aac13

File tree

3 files changed

+25
-30
lines changed

3 files changed

+25
-30
lines changed

Diff for: lib/cli.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ program
6262
.command('shipit')
6363
.description('Get last tag, calculate bump version for all commits that happened and create release branch.')
6464
.option('--failOnMissingCommit', 'In case commit has not happened since last tag (aka. we are on latest tag) fail.', Boolean, true)
65+
.option('--failOnMissingTag', 'In case no tags exist that are in semver version format fail.', false)
6566
.option('-f, --forceBump', 'In case no compatible commits found, use patch as fallback and ensure bump happens.', Boolean, true)
6667
.option('-a, --autoPush', 'This will automatically create release branch and tag commit in master.', Boolean, true)
6768
.option('-t, --tagPrefix <prefix>', 'Prefix version with string of your choice.', 'v')
@@ -71,13 +72,14 @@ program
7172
.option('--changelog', 'Generate changelog.', false)
7273
.action(async (options) => {
7374
console.log('Our config is: ', options)
74-
const { tagPrefix, failOnMissingCommit, releaseBranchPrefix, forceBump, gitUser, gitEmail, changelog } = options
75+
const { tagPrefix, failOnMissingCommit, failOnMissingTag, releaseBranchPrefix, forceBump, gitUser, gitEmail, changelog } = options
7576
wrapProcess(
7677
shipitHandler({
7778
tagPrefix,
7879
gitEmail,
7980
gitUser,
8081
failOnMissingCommit,
82+
failOnMissingTag,
8183
forceBump,
8284
releaseBranchPrefix,
8385
generateChangelog: changelog,

Diff for: lib/cli/changelog.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { DefaultLogFields, simpleGit } from 'simple-git'
22
import { writeFileSync } from 'fs'
3-
import packageJson from '../../package.json'
43
import { getCommitLink, getCompareLink, sortTagsDescending } from '../utils'
54

65
interface Props {
76
outputFile: string
87
gitBaseUrl?: string
8+
nextTag?: string
99
}
1010

1111
const isGithub = !!process.env.GITHUB_REPOSITORY && !!process.env.GITHUB_SERVER_URL
@@ -20,7 +20,7 @@ const httpGitUrl = isGithub
2020
? process.env.CI_PROJECT_URL
2121
: null
2222

23-
export const changelogHandler = async ({ outputFile, gitBaseUrl }: Props) => {
23+
export const changelogHandler = async ({ outputFile, gitBaseUrl, nextTag }: Props) => {
2424
const git = simpleGit()
2525

2626
const gitUrl = gitBaseUrl ?? httpGitUrl
@@ -31,14 +31,18 @@ export const changelogHandler = async ({ outputFile, gitBaseUrl }: Props) => {
3131
await git.fetch(['--tags'])
3232

3333
const tags = await git.tags()
34+
const commits = await git.log()
3435
const sortedTags = sortTagsDescending(tags.all)
36+
const sortedNormalizedTags = nextTag ? [nextTag, ...sortedTags] : sortedTags
3537

3638
// Sorted from newest to oldest (highest version to lowest).
37-
const tagsWithLog = sortedTags.map(async (tag, index, arr) => {
39+
const tagsWithLog = sortedNormalizedTags.map(async (tag, index, arr) => {
3840
const lowerTag = arr[index + 1]
3941
const higherTag = arr[index - 1]
4042

41-
const log = await git.log({ from: lowerTag, to: tag })
43+
const latestTag = sortedTags.includes(tag) ? tag : commits.latest?.hash
44+
45+
const log = await git.log({ from: lowerTag, to: latestTag })
4246
const filteredLog = log.all
4347
// Remove automatic commits (typically generated inside pipeline)
4448
.filter((a) => !a.message.includes('[skip ci]'))

Diff for: lib/cli/shipit.ts

+14-25
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ interface Props {
88
gitEmail: string
99
tagPrefix: string
1010
failOnMissingCommit: boolean
11+
failOnMissingTag: boolean
1112
releaseBranchPrefix: string
1213
forceBump: boolean
1314
generateChangelog: boolean
@@ -19,12 +20,16 @@ export const shipitHandler = async ({
1920
gitUser,
2021
tagPrefix,
2122
failOnMissingCommit,
23+
failOnMissingTag,
2224
forceBump,
2325
releaseBranchPrefix,
2426
generateChangelog,
2527
changelogPath,
2628
}: Props) => {
27-
const git = simpleGit()
29+
const gitWithoutAuthor = simpleGit()
30+
const git = gitWithoutAuthor.addConfig('user.name', gitUser).addConfig('user.email', gitEmail)
31+
32+
// @TODO: Implement check so if no valid tag exists, it will tag first commit in repo.
2833

2934
// Fetch tags to ensure we have the latest ones.
3035
const tags = await git.fetch(['--tags']).tags({ '--sort': '-creatordate' })
@@ -89,41 +94,25 @@ export const shipitHandler = async ({
8994
const replacementResults = replaceVersionInCommonFiles(currentTag, nextTag)
9095
console.log(`Replaced version in files.`, replacementResults)
9196

92-
// Commit changed files (versions) and create a release commit with skip ci flag.
93-
await git
94-
//
95-
.add('./*')
96-
.addConfig('user.name', gitUser)
97-
.addConfig('user.email', gitEmail)
98-
.raw('commit', '--message', `Release: ${nextTagWithPrefix} ${skipCiFlag}`)
99-
.addTag(nextTagWithPrefix)
100-
101-
// If flag is passed, changelog is genrated and added after new tag is created.
97+
// If flag is passed, changelog is generated and added.
10298
if (generateChangelog) {
10399
console.log('Generating changelog...')
104-
105-
await changelogHandler({ outputFile: changelogPath })
106-
await git
107-
//
108-
.add(changelogPath)
109-
.addConfig('user.name', gitUser)
110-
.addConfig('user.email', gitEmail)
111-
.raw('commit', '--amend', '--no-edit')
100+
await changelogHandler({ outputFile: changelogPath, nextTag: nextTagWithPrefix })
112101
}
113102

103+
// Commit changed files (versions) and create a release commit with skip ci flag.
114104
await git
115105
//
116-
.addConfig('user.name', gitUser)
117-
.addConfig('user.email', gitEmail)
118-
.push(remote.name, branch.current)
119-
.pushTags()
106+
.add('./*')
107+
.commit(`Release: ${nextTagWithPrefix} ${skipCiFlag}`)
108+
.addTag(nextTagWithPrefix)
109+
110+
await git.push(remote.name, branch.current).pushTags()
120111

121112
// As current branch commit includes skip ci flag, we want to ommit this flag for release branch so pipeline can run (omitting infinite loop).
122113
// So we are overwriting last commit message and pushing to release branch.
123114
await git
124115
//
125-
.addConfig('user.name', gitUser)
126-
.addConfig('user.email', gitEmail)
127116
.raw('commit', '--message', `Release: ${nextTagWithPrefix}`, '--amend')
128117
.push(remote.name, `${branch.current}:${releaseBranch}`)
129118

0 commit comments

Comments
 (0)