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

Commit

Permalink
fix(changelog): simplified logic for generating changelog, fixed issu…
Browse files Browse the repository at this point in the history
…e with bigger repos
  • Loading branch information
sladg committed Nov 30, 2022
1 parent 54b4217 commit 71aac13
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 30 deletions.
4 changes: 3 additions & 1 deletion lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ program
.command('shipit')
.description('Get last tag, calculate bump version for all commits that happened and create release branch.')
.option('--failOnMissingCommit', 'In case commit has not happened since last tag (aka. we are on latest tag) fail.', Boolean, true)
.option('--failOnMissingTag', 'In case no tags exist that are in semver version format fail.', false)
.option('-f, --forceBump', 'In case no compatible commits found, use patch as fallback and ensure bump happens.', Boolean, true)
.option('-a, --autoPush', 'This will automatically create release branch and tag commit in master.', Boolean, true)
.option('-t, --tagPrefix <prefix>', 'Prefix version with string of your choice.', 'v')
Expand All @@ -71,13 +72,14 @@ program
.option('--changelog', 'Generate changelog.', false)
.action(async (options) => {
console.log('Our config is: ', options)
const { tagPrefix, failOnMissingCommit, releaseBranchPrefix, forceBump, gitUser, gitEmail, changelog } = options
const { tagPrefix, failOnMissingCommit, failOnMissingTag, releaseBranchPrefix, forceBump, gitUser, gitEmail, changelog } = options
wrapProcess(
shipitHandler({
tagPrefix,
gitEmail,
gitUser,
failOnMissingCommit,
failOnMissingTag,
forceBump,
releaseBranchPrefix,
generateChangelog: changelog,
Expand Down
12 changes: 8 additions & 4 deletions lib/cli/changelog.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { DefaultLogFields, simpleGit } from 'simple-git'
import { writeFileSync } from 'fs'
import packageJson from '../../package.json'
import { getCommitLink, getCompareLink, sortTagsDescending } from '../utils'

interface Props {
outputFile: string
gitBaseUrl?: string
nextTag?: string
}

const isGithub = !!process.env.GITHUB_REPOSITORY && !!process.env.GITHUB_SERVER_URL
Expand All @@ -20,7 +20,7 @@ const httpGitUrl = isGithub
? process.env.CI_PROJECT_URL
: null

export const changelogHandler = async ({ outputFile, gitBaseUrl }: Props) => {
export const changelogHandler = async ({ outputFile, gitBaseUrl, nextTag }: Props) => {
const git = simpleGit()

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

const tags = await git.tags()
const commits = await git.log()
const sortedTags = sortTagsDescending(tags.all)
const sortedNormalizedTags = nextTag ? [nextTag, ...sortedTags] : sortedTags

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

const log = await git.log({ from: lowerTag, to: tag })
const latestTag = sortedTags.includes(tag) ? tag : commits.latest?.hash

const log = await git.log({ from: lowerTag, to: latestTag })
const filteredLog = log.all
// Remove automatic commits (typically generated inside pipeline)
.filter((a) => !a.message.includes('[skip ci]'))
Expand Down
39 changes: 14 additions & 25 deletions lib/cli/shipit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ interface Props {
gitEmail: string
tagPrefix: string
failOnMissingCommit: boolean
failOnMissingTag: boolean
releaseBranchPrefix: string
forceBump: boolean
generateChangelog: boolean
Expand All @@ -19,12 +20,16 @@ export const shipitHandler = async ({
gitUser,
tagPrefix,
failOnMissingCommit,
failOnMissingTag,
forceBump,
releaseBranchPrefix,
generateChangelog,
changelogPath,
}: Props) => {
const git = simpleGit()
const gitWithoutAuthor = simpleGit()
const git = gitWithoutAuthor.addConfig('user.name', gitUser).addConfig('user.email', gitEmail)

// @TODO: Implement check so if no valid tag exists, it will tag first commit in repo.

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

// Commit changed files (versions) and create a release commit with skip ci flag.
await git
//
.add('./*')
.addConfig('user.name', gitUser)
.addConfig('user.email', gitEmail)
.raw('commit', '--message', `Release: ${nextTagWithPrefix} ${skipCiFlag}`)
.addTag(nextTagWithPrefix)

// If flag is passed, changelog is genrated and added after new tag is created.
// If flag is passed, changelog is generated and added.
if (generateChangelog) {
console.log('Generating changelog...')

await changelogHandler({ outputFile: changelogPath })
await git
//
.add(changelogPath)
.addConfig('user.name', gitUser)
.addConfig('user.email', gitEmail)
.raw('commit', '--amend', '--no-edit')
await changelogHandler({ outputFile: changelogPath, nextTag: nextTagWithPrefix })
}

// Commit changed files (versions) and create a release commit with skip ci flag.
await git
//
.addConfig('user.name', gitUser)
.addConfig('user.email', gitEmail)
.push(remote.name, branch.current)
.pushTags()
.add('./*')
.commit(`Release: ${nextTagWithPrefix} ${skipCiFlag}`)
.addTag(nextTagWithPrefix)

await git.push(remote.name, branch.current).pushTags()

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

Expand Down

0 comments on commit 71aac13

Please sign in to comment.