move checking of git checkout out of parse logic

This commit is contained in:
Conor Sloan
2024-04-15 15:43:26 +01:00
parent 881fd1c540
commit 18cf56a126
6 changed files with 31 additions and 14 deletions
+9 -3
View File
@@ -249,19 +249,25 @@ describe('ensureCorrectShaCheckedOut', () => {
it('does not throw an error if the correct SHA is checked out', async () => {
await expect(
fsHelper.ensureCorrectShaCheckedOut(`refs/tags/${tag2}`, commit2, dir)
fsHelper.ensureTagAndRefCheckedOut(`refs/tags/${tag2}`, commit2, dir)
).resolves.toBeUndefined()
})
it('throws an error if the correct SHA is not checked out', async () => {
await expect(
fsHelper.ensureCorrectShaCheckedOut(`refs/tags/${tag1}`, commit1, dir)
fsHelper.ensureTagAndRefCheckedOut(`refs/tags/${tag1}`, commit1, dir)
).rejects.toThrow()
})
it('throws an error if the sha of the tag does not match expected sha', async () => {
await expect(async () =>
fsHelper.ensureCorrectShaCheckedOut(`refs/tags/${tag1}`, commit2, dir)
fsHelper.ensureTagAndRefCheckedOut(`refs/tags/${tag1}`, commit2, dir)
).rejects.toThrow()
})
it('throws if the provided ref is not a tag ref', async () => {
await expect(async () =>
fsHelper.ensureTagAndRefCheckedOut(`refs/heads/main`, commit2, dir)
).rejects.toThrow()
})
})
+1 -1
View File
@@ -51,7 +51,7 @@ describe('run', () => {
.spyOn(fsHelper, 'stageActionFiles')
.mockImplementation()
ensureCorrectShaCheckedOutMock = jest
.spyOn(fsHelper, 'ensureCorrectShaCheckedOut')
.spyOn(fsHelper, 'ensureTagAndRefCheckedOut')
.mockImplementation()
// GHCR Client mocks
+1 -1
View File
@@ -1 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="116" height="20" role="img" aria-label="Coverage: 96.78%"><title>Coverage: 96.78%</title><linearGradient id="s" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="r"><rect width="116" height="20" rx="3" fill="#fff"/></clipPath><g clip-path="url(#r)"><rect width="63" height="20" fill="#555"/><rect x="63" width="53" height="20" fill="#4c1"/><rect width="116" height="20" fill="url(#s)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="325" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="530">Coverage</text><text x="325" y="140" transform="scale(.1)" fill="#fff" textLength="530">Coverage</text><text aria-hidden="true" x="885" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="430">96.78%</text><text x="885" y="140" transform="scale(.1)" fill="#fff" textLength="430">96.78%</text></g></svg>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="116" height="20" role="img" aria-label="Coverage: 96.81%"><title>Coverage: 96.81%</title><linearGradient id="s" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="r"><rect width="116" height="20" rx="3" fill="#fff"/></clipPath><g clip-path="url(#r)"><rect width="63" height="20" fill="#555"/><rect x="63" width="53" height="20" fill="#4c1"/><rect width="116" height="20" fill="url(#s)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="325" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="530">Coverage</text><text x="325" y="140" transform="scale(.1)" fill="#fff" textLength="530">Coverage</text><text aria-hidden="true" x="885" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="430">96.81%</text><text x="885" y="140" transform="scale(.1)" fill="#fff" textLength="430">96.81%</text></g></svg>

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB

Generated Vendored
+8 -5
View File
@@ -104543,7 +104543,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.ensureCorrectShaCheckedOut = exports.stageActionFiles = exports.readFileContents = exports.isDirectory = exports.createArchives = exports.createTempDir = void 0;
exports.ensureTagAndRefCheckedOut = exports.stageActionFiles = exports.readFileContents = exports.isDirectory = exports.createArchives = exports.createTempDir = void 0;
const fs = __importStar(__nccwpck_require__(57147));
const fs_extra_1 = __importDefault(__nccwpck_require__(5630));
const path = __importStar(__nccwpck_require__(71017));
@@ -104635,7 +104635,10 @@ exports.stageActionFiles = stageActionFiles;
// Ensure the correct SHA is checked out for the tag by inspecting the git metadata in the workspace
// and comparing it to the information actions provided us.
// Provided ref should be in format refs/tags/<tagname>.
async function ensureCorrectShaCheckedOut(tagRef, expectedSha, gitDir) {
async function ensureTagAndRefCheckedOut(tagRef, expectedSha, gitDir) {
if (!tagRef.startsWith('refs/tags/')) {
throw new Error(`Tag ref provided is not in expected format.`);
}
const git = simpleGit.simpleGit(gitDir);
const tagCommitSha = await git.raw(['rev-parse', '--verify', tagRef]);
if (tagCommitSha.trim() !== expectedSha) {
@@ -104646,7 +104649,7 @@ async function ensureCorrectShaCheckedOut(tagRef, expectedSha, gitDir) {
throw new Error(`The expected commit associated with the tag ${tagRef} is not checked out.`);
}
}
exports.ensureCorrectShaCheckedOut = ensureCorrectShaCheckedOut;
exports.ensureTagAndRefCheckedOut = ensureTagAndRefCheckedOut;
// Converts a file path to a filemetadata object by querying the fs for relevant metadata.
async function fileMetadata(filePath) {
const stats = fs.statSync(filePath);
@@ -104874,6 +104877,8 @@ async function run() {
core.info(`Publishing action package version with options:`);
core.info(cfg.serializeOptions(options));
const semverTag = await parseSemverTagFromRef(options);
// Ensure the correct SHA is checked out for the tag we're parsing, otherwise the bundled content will be incorrect.
await fsHelper.ensureTagAndRefCheckedOut(options.ref, options.sha, options.workspaceDir);
const stagedActionFilesDir = fsHelper.createTempDir(options.runnerTempDir, 'staging');
fsHelper.stageActionFiles(options.workspaceDir, stagedActionFilesDir);
const archiveDir = fsHelper.createTempDir(options.runnerTempDir, 'archives');
@@ -104911,8 +104916,6 @@ async function parseSemverTagFromRef(opts) {
if (!semverTag) {
throw new Error(`${rawTag} is not a valid semantic version tag, and so cannot be uploaded to the action package.`);
}
// Ensure the correct SHA is checked out for the tag we're parsing, otherwise the bundled content will be incorrect.
await fsHelper.ensureCorrectShaCheckedOut(ref, opts.sha, opts.workspaceDir);
return semverTag;
}
// Generate an attestation using the actions toolkit
+5 -1
View File
@@ -118,11 +118,15 @@ export function stageActionFiles(actionDir: string, targetDir: string): void {
// Ensure the correct SHA is checked out for the tag by inspecting the git metadata in the workspace
// and comparing it to the information actions provided us.
// Provided ref should be in format refs/tags/<tagname>.
export async function ensureCorrectShaCheckedOut(
export async function ensureTagAndRefCheckedOut(
tagRef: string,
expectedSha: string,
gitDir: string
): Promise<void> {
if (!tagRef.startsWith('refs/tags/')) {
throw new Error(`Tag ref provided is not in expected format.`)
}
const git: simpleGit.SimpleGit = simpleGit.simpleGit(gitDir)
const tagCommitSha = await git.raw(['rev-parse', '--verify', tagRef])
+7 -3
View File
@@ -20,6 +20,13 @@ export async function run(): Promise<void> {
const semverTag: semver.SemVer = await parseSemverTagFromRef(options)
// Ensure the correct SHA is checked out for the tag we're parsing, otherwise the bundled content will be incorrect.
await fsHelper.ensureTagAndRefCheckedOut(
options.ref,
options.sha,
options.workspaceDir
)
const stagedActionFilesDir = fsHelper.createTempDir(
options.runnerTempDir,
'staging'
@@ -94,9 +101,6 @@ async function parseSemverTagFromRef(
)
}
// Ensure the correct SHA is checked out for the tag we're parsing, otherwise the bundled content will be incorrect.
await fsHelper.ensureCorrectShaCheckedOut(ref, opts.sha, opts.workspaceDir)
return semverTag
}