From efcebcf8e8fb178c61ec8826c57b5151e4f0dc73 Mon Sep 17 00:00:00 2001 From: NGPixel Date: Wed, 5 Apr 2023 01:23:22 -0400 Subject: [PATCH] feat: add fromTag option to override the latest tag fetch --- README.md | 1 + action.yml | 4 ++ dist/index.js | 99 +++++++++++++++++++++++++++++++++---------------- index.js | 101 ++++++++++++++++++++++++++++++++++---------------- 4 files changed, 142 insertions(+), 63 deletions(-) diff --git a/README.md b/README.md index 5b57b77..1be543c 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/action.yml b/action.yml index 1715884..a2b7323 100644 --- a/action.yml +++ b/action.yml @@ -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. diff --git a/dist/index.js b/dist/index.js index 4f27674..7e6ee61 100644 --- a/dist/index.js +++ b/dist/index.js @@ -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), @@ -31071,13 +31072,60 @@ 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 @@ -31085,38 +31133,27 @@ async function main () { } } } - } - `, { - 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}`) diff --git a/index.js b/index.js index 63cbf12..a06ed54 100644 --- a/index.js +++ b/index.js @@ -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), @@ -32,13 +33,60 @@ 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 @@ -46,38 +94,27 @@ async function main () { } } } - } - `, { - 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}`)