Skip to content
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

Auto version bump #202

Merged
merged 40 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
1dbab8c
feat: auto version bump
sameer-coder Dec 9, 2022
93a41be
chore: action yml updated
sameer-coder Dec 9, 2022
648820b
chore: corrections in yaml
sameer-coder Dec 9, 2022
5b59f68
chore: correct action condition
sameer-coder Dec 9, 2022
82793fa
chore: minor changes
sameer-coder Dec 12, 2022
f2ae228
chore: query to get commits changed
sameer-coder Dec 12, 2022
6e426f5
chore: readme update
sameer-coder Dec 12, 2022
c52f849
chore: added tests
sameer-coder Dec 12, 2022
83b7707
chore: more tests added
sameer-coder Dec 12, 2022
d26d977
chore: coverage improved
sameer-coder Dec 12, 2022
83a1fa1
chore: lint fix
sameer-coder Dec 12, 2022
3dd744b
chore: bump logic triggered from action yaml
sameer-coder Dec 13, 2022
b594ed5
chore: readability changes
sameer-coder Dec 13, 2022
99c81ec
chore: fixed tests
sameer-coder Dec 14, 2022
188352f
chore: more tests
sameer-coder Dec 14, 2022
df340ee
chore: using conventional commits parser
sameer-coder Dec 15, 2022
8e6fdee
chore: removed unnecessary default params
sameer-coder Dec 15, 2022
2c3bace
chore: console.log removed
sameer-coder Dec 15, 2022
a35fd85
chore: lint fix
sameer-coder Dec 15, 2022
12be444
chore: using conventionalcommits npm package
sameer-coder Dec 19, 2022
f6ca3bd
chore: removed unused npm package
sameer-coder Dec 19, 2022
c9627d1
Merge branch 'main' of https://github.com/nearform/optic-release-auto…
sameer-coder Jan 3, 2023
75d68e8
chore: added support for base tag when auto bump
sameer-coder Jan 5, 2023
92ef817
chore: corrected fetch depth
sameer-coder Jan 5, 2023
edda212
chore: tests fixed
sameer-coder Jan 5, 2023
932c2c8
tests updated
sameer-coder Jan 6, 2023
70ee1c2
chore: fetch-depth test
sameer-coder Jan 6, 2023
563d2d4
chore: restoring fetch-depth
sameer-coder Jan 6, 2023
86bb339
chore: base-tag is no longer a required field
sameer-coder Jan 6, 2023
68874d7
chore: fix tests again
sameer-coder Jan 6, 2023
7eac288
chore: only fetch full commit history if auto bump
sameer-coder Jan 6, 2023
747e234
chore: logging
sameer-coder Jan 6, 2023
7e77ce0
chore: tag correction
sameer-coder Jan 6, 2023
6ca93d4
chore: sorting tags
sameer-coder Jan 6, 2023
56033fb
chore: fixed bug with fetching tags
sameer-coder Jan 6, 2023
370fdc6
chore: readme updated
sameer-coder Jan 9, 2023
ea1fbb4
chore: readme updated
sameer-coder Jan 10, 2023
29fe874
Merge branch 'main' of https://github.com/nearform/optic-release-auto…
sameer-coder Jan 12, 2023
95c79bc
chore: coverage 100
sameer-coder Jan 12, 2023
9cd8957
chore: pr updates
sameer-coder Jan 12, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ on:
default: 'patch'
type: choice
options:
- auto
- patch
- minor
- major
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ on:
default: "patch"
type: choice
options:
- auto
- patch
- minor
- major
Expand Down Expand Up @@ -84,6 +85,9 @@ When you merge this PR:

When you close the PR without merging it: nothing will happen.

## Using 'auto' bump version option
If you choose the "auto" option for semver version updates, the action will attempt to determine the new version number based on the commit messages. For this option to work, the repository must use the conventional commits standard.
sameer-coder marked this conversation as resolved.
Show resolved Hide resolved

## Using branches filter

In case you want to reduce the amount of the relase workflow runs, you can configure a workflow to run only for pull requests that target specific branches.
Expand Down
19 changes: 13 additions & 6 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,19 @@ runs:
shell: 'bash'

- name: Version bump
uses: actions/github-script@v6
id: version-bump
if: ${{ github.event_name == 'workflow_dispatch' }}
run: |
npm version --no-git-tag-version ${{ inputs.semver }}
echo "PACKAGE_VERSION=$(echo $(node -p "require('./package.json').version"))" >> $GITHUB_OUTPUT
shell: 'bash'
if: github.event_name == 'workflow_dispatch'
with:
github-token: ${{ inputs.github-token }}
script: |
let newVersion = '${{ inputs.semver }}'
if ('${{ inputs.semver }}' == 'auto') {
const script = require('${{ github.action_path }}/dist/index.js')
newVersion = await script.getBumpedVersion({ github, context })
}
await exec.exec(`npm version --no-git-tag-version ${newVersion}`)
return require('./package.json').version
sameer-coder marked this conversation as resolved.
Show resolved Hide resolved

- name: Build the package
if: ${{ inputs.build-command }}
Expand All @@ -105,7 +112,7 @@ runs:
github-token: ${{ inputs.github-token }}
script: |
const script = require('${{ github.action_path }}/dist/index.js')
await script({ github, context, inputs: ${{ toJSON(inputs) }}, packageVersion: "${{ steps.version-bump.outputs.PACKAGE_VERSION }}" })
await script({ github, context, inputs: ${{ toJSON(inputs) }}, packageVersion: ${{ steps.version-bump.outputs.result }} })

- name: Release the package
uses: actions/github-script@v6
Expand Down
174 changes: 174 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26847,6 +26847,7 @@ exports.APP_NAME = 'optic-release-automation[bot]'

const openPr = __nccwpck_require__(1515)
const release = __nccwpck_require__(2026)
const { getBumpedVersion } = __nccwpck_require__(7292)
const { logError } = __nccwpck_require__(653)

module.exports = async function ({ github, context, inputs, packageVersion }) {
Expand All @@ -26861,6 +26862,8 @@ module.exports = async function ({ github, context, inputs, packageVersion }) {
logError('Unsupported event')
}

module.exports.getBumpedVersion = getBumpedVersion


/***/ }),

Expand Down Expand Up @@ -27299,6 +27302,177 @@ const deriveFilename = (filePath, extension) => {
exports.attach = attach


/***/ }),

/***/ 7292:
/***/ ((__unused_webpack_module, exports, __nccwpck_require__) => {

"use strict";


const semver = __nccwpck_require__(1383)

async function getBumpedVersion({ github, context }) {
const { owner, repo } = context.repo

const {
latestReleaseCommitSha,
latestReleaseTagName,
latestReleaseCommitDate,
} = await getLatestRelease({ github, owner, repo })

if (
!latestReleaseCommitSha ||
!latestReleaseTagName ||
!latestReleaseCommitDate
) {
throw new Error(`Couldn't find latest release`)
}

const allCommits = await getCommitMessagesSinceLatestRelease({
github,
owner,
repo,
commitDate: latestReleaseCommitDate,
})

if (!allCommits.length) {
throw new Error(`No commits found since last release`)
}

const bumpedVersion = getVersionFromCommits(latestReleaseTagName, allCommits)

if (!semver.valid(bumpedVersion)) {
throw new Error(`Invalid bumped version ${bumpedVersion}`)
}
return bumpedVersion
}

function getVersionFromCommits(currentVersion, commits = []) {
// Define a regular expression to match Conventional Commits messages
const commitRegex = /^(feat|fix|BREAKING CHANGE)(\(.+\))?:(.+)$/

// Define a mapping of commit types to version bump types
var versionBumpMap = {
'BREAKING CHANGE': 'major',
feat: 'minor',
fix: 'patch',
}

let { major, minor, patch } = semver.parse(currentVersion)

if (
!Number.isInteger(major) ||
!Number.isInteger(minor) ||
!Number.isInteger(patch)
) {
throw new Error('Invalid major/minor/patch version found')
}

let isBreaking = false
let isMinor = false

for (const commit of commits) {
const match = commitRegex.exec(commit)
if (!match) continue

const type = match[1]

const bumpType = versionBumpMap[type]
if (!bumpType) continue

if (bumpType === 'major') {
isBreaking = true
break
} else if (bumpType === 'minor') {
isMinor = true
}
}

if (isBreaking) {
return `${++major}.0.0`
} else if (isMinor) {
return `${major}.${++minor}.0`
} else {
return `${major}.${minor}.${++patch}`
}
}

async function getLatestRelease({ github, owner, repo }) {
const data = await github.graphql(
`
query getLatestTagCommit($owner: String!, $repo: String!) {
repository(owner: $owner, name: $repo) {
latestRelease{
tagName
tagCommit {
oid
committedDate
}
}
}
}
`,
{
owner,
repo,
}
)

const latestReleaseCommitSha = data?.repository?.latestRelease?.tagCommit?.oid
const latestReleaseTagName = data?.repository?.latestRelease?.tagName
const latestReleaseCommitDate =
data?.repository?.latestRelease?.tagCommit?.committedDate

return {
latestReleaseCommitSha,
latestReleaseTagName,
latestReleaseCommitDate,
}
}

async function getCommitMessagesSinceLatestRelease({
github,
owner,
repo,
commitDate,
}) {
const parsedCommitDate = new Date(commitDate)
parsedCommitDate.setSeconds(parsedCommitDate.getSeconds() + 1)

const data = await github.graphql(
`
query getCommitsSinceLastRelease($owner: String!, $repo: String!, $since: GitTimestamp!) {
repository(owner: $owner, name: $repo) {
defaultBranchRef {
target {
... on Commit {
history(first: 100, since: $since) {
nodes {
message
}
}
}
}
}
}
}
`,
{
owner,
repo,
since: parsedCommitDate,
}
)

const commitsList =
data?.repository?.defaultBranchRef?.target?.history?.nodes || []
return commitsList.map(c => c.message)
}

exports.getBumpedVersion = getBumpedVersion


/***/ }),

/***/ 4235:
Expand Down
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const openPr = require('./openPr')
const release = require('./release')
const { getBumpedVersion } = require('./utils/bump')
const { logError } = require('./log')

module.exports = async function ({ github, context, inputs, packageVersion }) {
Expand All @@ -15,3 +16,5 @@ module.exports = async function ({ github, context, inputs, packageVersion }) {

logError('Unsupported event')
}

module.exports.getBumpedVersion = getBumpedVersion
sameer-coder marked this conversation as resolved.
Show resolved Hide resolved
Loading