refactor: extract tag object creation and improve annotation handling

Co-authored-by: jimeh <39563+jimeh@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2025-10-27 22:17:36 +00:00
parent 937c3e6f65
commit eb98d277e4
4 changed files with 75 additions and 20 deletions

View File

@@ -721,6 +721,33 @@ describe('run', () => {
expect(core.setOutput).toHaveBeenCalledWith('created', ['v1.0.0'])
})
it('creates lightweight tags when annotation is only whitespace', async () => {
setupInputs({
tags: 'v1.0.0',
ref: 'abc123',
github_token: 'test-token',
when_exists: 'update',
annotation: ' '
})
setupCommitResolver('sha-abc123')
setupTagDoesNotExist()
await run()
// Should NOT create tag object for whitespace-only annotation
expect(github.mockOctokit.rest.git.createTag).not.toHaveBeenCalled()
// Should create reference pointing directly to commit
expect(github.mockOctokit.rest.git.createRef).toHaveBeenCalledWith({
owner: 'test-owner',
repo: 'test-repo',
ref: 'refs/tags/v1.0.0',
sha: 'sha-abc123'
})
expect(core.setOutput).toHaveBeenCalledWith('created', ['v1.0.0'])
})
it('updates tags with annotation', async () => {
setupInputs({
tags: 'v1',

2
dist/index.js generated vendored

File diff suppressed because one or more lines are too long

2
dist/index.js.map generated vendored

File diff suppressed because one or more lines are too long

View File

@@ -170,6 +170,36 @@ export async function processTag(
}
}
/**
* Create a tag object for an annotated tag.
*
* @param tagName - Name of the tag
* @param sha - Commit SHA to tag
* @param annotation - Annotation message
* @param owner - Repository owner
* @param repo - Repository name
* @param octokit - GitHub API client
* @returns SHA of the created tag object
*/
async function createTagObject(
tagName: string,
sha: string,
annotation: string,
owner: string,
repo: string,
octokit: ReturnType<typeof github.getOctokit>
): Promise<string> {
const tagObject = await octokit.rest.git.createTag({
owner,
repo,
tag: tagName,
message: annotation,
object: sha,
type: 'commit'
})
return tagObject.data.sha
}
/**
* Create a tag (annotated or lightweight based on annotation parameter).
*
@@ -190,17 +220,16 @@ async function createTag(
): Promise<void> {
let refSha = sha
// If annotation is provided, create an annotated tag object first
if (annotation) {
const tagObject = await octokit.rest.git.createTag({
// If annotation is provided and non-empty, create an annotated tag object first
if (annotation && annotation.trim()) {
refSha = await createTagObject(
tagName,
sha,
annotation,
owner,
repo,
tag: tagName,
message: annotation,
object: sha,
type: 'commit'
})
refSha = tagObject.data.sha
octokit
)
}
// Create the reference pointing to the tag object (or commit for lightweight)
@@ -232,17 +261,16 @@ async function updateTag(
): Promise<void> {
let refSha = sha
// If annotation is provided, create an annotated tag object first
if (annotation) {
const tagObject = await octokit.rest.git.createTag({
// If annotation is provided and non-empty, create an annotated tag object first
if (annotation && annotation.trim()) {
refSha = await createTagObject(
tagName,
sha,
annotation,
owner,
repo,
tag: tagName,
message: annotation,
object: sha,
type: 'commit'
})
refSha = tagObject.data.sha
octokit
)
}
// Update the reference