Skip to content

Commit

Permalink
feat: add fromTag option to override the latest tag fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
NGPixel committed Apr 5, 2023
1 parent e299d4a commit efcebcf
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 63 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ jobs:
| `noVersionBumpBehavior` | Whether to exit with an error *(default)*, a warning, the current version or silently when none of the commits result in a version bump. (Possible values: `error`, `warn`, `current` or `silent`) | :x: | `error` |
| `prefix` | A prefix that will be ignored when parsing tags (e.g. `foobar/`). Useful for monorepos. The prefix will be added back to the output values. | :x: | |
| `additionalCommits` | A list of additional commit messages to parse in order to calculate semver. | :x: | |
| `fromTag` | Override the tag to use when comparing against the branch in order to fetch the list of commits. | :x: | |

## Outputs

Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ inputs:
additionalCommits:
description: A list of additional commit messages to parse in order to calculate semver.
required: false
fromTag:
description: Override the tag to use when comparing against the branch in order to fetch the list of commits.
required: false
default: ''
outputs:
current:
description: Current version number / latest tag.
Expand Down
99 changes: 68 additions & 31 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31053,6 +31053,7 @@ async function main () {
const noVersionBumpBehavior = core.getInput('noVersionBumpBehavior')
const prefix = core.getInput('prefix') || ''
const additionalCommits = core.getInput('additionalCommits').split('\n').map(l => l.trim()).filter(l => l !== '')
const fromTag = core.getInput('fromTag')

const bumpTypes = {
major: core.getInput('majorList').split(',').map(p => p.trim()).filter(p => p),
Expand All @@ -31071,52 +31072,88 @@ async function main () {
core.setOutput('nextMajorStrict', `${prefix}${semver.major(version)}`)
}

// GET LATEST + PREVIOUS TAGS
let latestTag = null

if (!fromTag) {
// GET LATEST + PREVIOUS TAGS

const tagsRaw = await gh.graphql(`
query lastTags ($owner: String!, $repo: String!) {
repository (owner: $owner, name: $repo) {
refs(first: 10, refPrefix: "refs/tags/", orderBy: { field: TAG_COMMIT_DATE, direction: DESC }) {
nodes {
name
target {
oid
}
}
}
}
}
`, {
owner,
repo
})

const tagsList = _.get(tagsRaw, 'repository.refs.nodes', [])
if (tagsList.length < 1) {
return core.setFailed('Couldn\'t find the latest tag. Make sure you have at least one tag created first!')
}

let idx = 0
for (const tag of tagsList) {
if (prefix && tag.name.indexOf(prefix) === 0) {
tag.name = tag.name.replace(prefix, '')
}
if (semver.valid(tag.name)) {
latestTag = tag
break
} else if (idx === 0 && !skipInvalidTags) {
break
}
idx++
}

const tagsRaw = await gh.graphql(`
query lastTags ($owner: String!, $repo: String!) {
repository (owner: $owner, name: $repo) {
refs(first: 10, refPrefix: "refs/tags/", orderBy: { field: TAG_COMMIT_DATE, direction: DESC }) {
nodes {
if (!latestTag) {
return core.setFailed(skipInvalidTags ? 'None of the 10 latest tags are valid semver!' : 'Latest tag is invalid (does not conform to semver)!')
}

core.info(`Comparing against latest tag: ${prefix}${latestTag.name}`)
} else {
// GET SPECIFIC TAG

const tagRaw = await gh.graphql(`
query singleTag ($owner: String!, $repo: String!, $tag: String!) {
repository (owner: $owner, name: $repo) {
ref(qualifiedName: $tag) {
name
target {
oid
}
}
}
}
}
`, {
owner,
repo
})
`, {
owner,
repo,
tag: `refs/tags/${prefix}${fromTag}`
})

const tagsList = _.get(tagsRaw, 'repository.refs.nodes', [])
if (tagsList.length < 1) {
return core.setFailed('Couldn\'t find the latest tag. Make sure you have at least one tag created first!')
}
latestTag = _.get(tagsRaw, 'repository.ref')

let latestTag = null
let idx = 0
for (const tag of tagsList) {
if (prefix && tag.name.indexOf(prefix) === 0) {
tag.name = tag.name.replace(prefix, '')
if (!latestTag) {
return core.setFailed('Provided tag could not be found!')
}
if (semver.valid(tag.name)) {
latestTag = tag
break
} else if (idx === 0 && !skipInvalidTags) {
break
if (prefix && latestTag.name.indexOf(prefix) === 0) {
latestTag.name = latestTag.name.replace(prefix, '')
}
if (!semver.valid(latestTag.name)) {
return core.setFailed('Provided tag is invalid! (does not conform to semver)')
}
idx++
}

if (!latestTag) {
return core.setFailed(skipInvalidTags ? 'None of the 10 latest tags are valid semver!' : 'Latest tag is invalid (does not conform to semver)!')
core.info(`Comparing against provided tag: ${prefix}${latestTag.name}`)
}

core.info(`Comparing against latest tag: ${prefix}${latestTag.name}`)

// OUTPUT CURRENT VARS

core.exportVariable('current', `${prefix}${latestTag.name}`)
Expand Down
101 changes: 69 additions & 32 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ async function main () {
const noVersionBumpBehavior = core.getInput('noVersionBumpBehavior')
const prefix = core.getInput('prefix') || ''
const additionalCommits = core.getInput('additionalCommits').split('\n').map(l => l.trim()).filter(l => l !== '')
const fromTag = core.getInput('fromTag')

const bumpTypes = {
major: core.getInput('majorList').split(',').map(p => p.trim()).filter(p => p),
Expand All @@ -32,52 +33,88 @@ async function main () {
core.setOutput('nextMajorStrict', `${prefix}${semver.major(version)}`)
}

// GET LATEST + PREVIOUS TAGS
let latestTag = null

if (!fromTag) {
// GET LATEST + PREVIOUS TAGS

const tagsRaw = await gh.graphql(`
query lastTags ($owner: String!, $repo: String!) {
repository (owner: $owner, name: $repo) {
refs(first: 10, refPrefix: "refs/tags/", orderBy: { field: TAG_COMMIT_DATE, direction: DESC }) {
nodes {
name
target {
oid
}
}
}
}
}
`, {
owner,
repo
})

const tagsList = _.get(tagsRaw, 'repository.refs.nodes', [])
if (tagsList.length < 1) {
return core.setFailed('Couldn\'t find the latest tag. Make sure you have at least one tag created first!')
}

let idx = 0
for (const tag of tagsList) {
if (prefix && tag.name.indexOf(prefix) === 0) {
tag.name = tag.name.replace(prefix, '')
}
if (semver.valid(tag.name)) {
latestTag = tag
break
} else if (idx === 0 && !skipInvalidTags) {
break
}
idx++
}

const tagsRaw = await gh.graphql(`
query lastTags ($owner: String!, $repo: String!) {
repository (owner: $owner, name: $repo) {
refs(first: 10, refPrefix: "refs/tags/", orderBy: { field: TAG_COMMIT_DATE, direction: DESC }) {
nodes {
if (!latestTag) {
return core.setFailed(skipInvalidTags ? 'None of the 10 latest tags are valid semver!' : 'Latest tag is invalid (does not conform to semver)!')
}

core.info(`Comparing against latest tag: ${prefix}${latestTag.name}`)
} else {
// GET SPECIFIC TAG

const tagRaw = await gh.graphql(`
query singleTag ($owner: String!, $repo: String!, $tag: String!) {
repository (owner: $owner, name: $repo) {
ref(qualifiedName: $tag) {
name
target {
oid
}
}
}
}
}
`, {
owner,
repo
})

const tagsList = _.get(tagsRaw, 'repository.refs.nodes', [])
if (tagsList.length < 1) {
return core.setFailed('Couldn\'t find the latest tag. Make sure you have at least one tag created first!')
}
`, {
owner,
repo,
tag: `refs/tags/${prefix}${fromTag}`
})

let latestTag = null
let idx = 0
for (const tag of tagsList) {
if (prefix && tag.name.indexOf(prefix) === 0) {
tag.name = tag.name.replace(prefix, '')
latestTag = _.get(tagsRaw, 'repository.ref')

if (!latestTag) {
return core.setFailed('Provided tag could not be found!')
}
if (semver.valid(tag.name)) {
latestTag = tag
break
} else if (idx === 0 && !skipInvalidTags) {
break
if (prefix && latestTag.name.indexOf(prefix) === 0) {
latestTag.name = latestTag.name.replace(prefix, '')
}
if (!semver.valid(latestTag.name)) {
return core.setFailed('Provided tag is invalid! (does not conform to semver)')
}
idx++
}

if (!latestTag) {
return core.setFailed(skipInvalidTags ? 'None of the 10 latest tags are valid semver!' : 'Latest tag is invalid (does not conform to semver)!')
core.info(`Comparing against provided tag: ${prefix}${latestTag.name}`)
}

core.info(`Comparing against latest tag: ${prefix}${latestTag.name}`)

// OUTPUT CURRENT VARS

core.exportVariable('current', `${prefix}${latestTag.name}`)
Expand Down

0 comments on commit efcebcf

Please sign in to comment.