Skip to content

Commit dfe6c8d

Browse files
authored
Merge pull request #827 from crazy-max/cosign-version
cosign version
2 parents 6b0ff67 + 0f9a1c9 commit dfe6c8d

File tree

3 files changed

+157
-0
lines changed

3 files changed

+157
-0
lines changed

__tests__/cosign/cosign.test.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* Copyright 2025 actions-toolkit authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import {describe, expect, it, jest, test} from '@jest/globals';
18+
import * as semver from 'semver';
19+
20+
import {Exec} from '../../src/exec';
21+
import {Cosign} from '../../src/cosign/cosign';
22+
23+
describe('isAvailable', () => {
24+
it('checks Cosign is available', async () => {
25+
const execSpy = jest.spyOn(Exec, 'getExecOutput');
26+
const cosign = new Cosign();
27+
await cosign.isAvailable();
28+
// eslint-disable-next-line jest/no-standalone-expect
29+
expect(execSpy).toHaveBeenCalledWith(`cosign`, [], {
30+
silent: true,
31+
ignoreReturnCode: true
32+
});
33+
});
34+
});
35+
36+
describe('printVersion', () => {
37+
it('prints Cosign version', async () => {
38+
const execSpy = jest.spyOn(Exec, 'exec');
39+
const cosign = new Cosign();
40+
await cosign.printVersion();
41+
expect(execSpy).toHaveBeenCalledWith(`cosign`, ['version', '--json'], {
42+
failOnStdErr: false
43+
});
44+
});
45+
});
46+
47+
describe('version', () => {
48+
it('valid', async () => {
49+
const cosign = new Cosign();
50+
expect(semver.valid(await cosign.version())).not.toBeUndefined();
51+
});
52+
});
53+
54+
describe('versionSatisfies', () => {
55+
test.each([
56+
['v0.4.1', '>=0.3.2', true],
57+
['v0.8.0', '>0.6.0', true],
58+
['v0.8.0', '<0.3.0', false]
59+
])('given %p', async (version, range, expected) => {
60+
const cosign = new Cosign();
61+
expect(await cosign.versionSatisfies(range, version)).toBe(expected);
62+
});
63+
});

dev.Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ ARG BUILDX_VERSION=0.29.1
2020
ARG COMPOSE_VERSION=2.39.1
2121
ARG UNDOCK_VERSION=0.10.0
2222
ARG REGCTL_VERSION=v0.8.2
23+
ARG COSIGN_VERSION=v3.0.2
2324

2425
FROM node:${NODE_VERSION}-alpine AS base
2526
RUN apk add --no-cache cpio findutils git
@@ -81,6 +82,7 @@ FROM docker/buildx-bin:${BUILDX_VERSION} AS buildx
8182
FROM docker/compose-bin:v${COMPOSE_VERSION} AS compose
8283
FROM crazymax/undock:${UNDOCK_VERSION} AS undock
8384
FROM ghcr.io/regclient/regctl:${REGCTL_VERSION} AS regctl
85+
FROM ghcr.io/sigstore/cosign/cosign:${COSIGN_VERSION} AS cosign
8486

8587
FROM deps AS test
8688
RUN --mount=type=bind,target=.,rw \
@@ -93,6 +95,7 @@ RUN --mount=type=bind,target=.,rw \
9395
--mount=type=bind,from=compose,source=/docker-compose,target=/usr/bin/compose \
9496
--mount=type=bind,from=undock,source=/usr/local/bin/undock,target=/usr/bin/undock \
9597
--mount=type=bind,from=regctl,source=/regctl,target=/usr/bin/regctl \
98+
--mount=type=bind,from=cosign,source=/ko-app/cosign,target=/usr/bin/cosign \
9699
--mount=type=secret,id=GITHUB_TOKEN \
97100
GITHUB_TOKEN=$(cat /run/secrets/GITHUB_TOKEN) yarn run test:coverage --coverageDirectory=/tmp/coverage
98101

src/cosign/cosign.ts

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* Copyright 2025 actions-toolkit authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import * as core from '@actions/core';
18+
19+
import {Exec} from '../exec';
20+
import * as semver from 'semver';
21+
22+
export interface CosignOpts {
23+
binPath?: string;
24+
}
25+
26+
export class Cosign {
27+
private readonly binPath: string;
28+
private _version: string;
29+
private _versionOnce: boolean;
30+
31+
constructor(opts?: CosignOpts) {
32+
this.binPath = opts?.binPath || 'cosign';
33+
this._version = '';
34+
this._versionOnce = false;
35+
}
36+
37+
public async isAvailable(): Promise<boolean> {
38+
const ok: boolean = await Exec.getExecOutput(this.binPath, [], {
39+
ignoreReturnCode: true,
40+
silent: true
41+
})
42+
.then(res => {
43+
if (res.stderr.length > 0 && res.exitCode != 0) {
44+
core.debug(`Cosign.isAvailable cmd err: ${res.stderr.trim()}`);
45+
return false;
46+
}
47+
return res.exitCode == 0;
48+
})
49+
.catch(error => {
50+
core.debug(`Cosign.isAvailable error: ${error}`);
51+
return false;
52+
});
53+
54+
core.debug(`Cosign.isAvailable: ${ok}`);
55+
return ok;
56+
}
57+
58+
public async version(): Promise<string> {
59+
if (this._versionOnce) {
60+
return this._version;
61+
}
62+
this._versionOnce = true;
63+
this._version = await Exec.getExecOutput(this.binPath, ['version', '--json'], {
64+
ignoreReturnCode: true,
65+
silent: true
66+
}).then(res => {
67+
if (res.stderr.length > 0 && res.exitCode != 0) {
68+
throw new Error(res.stderr.trim());
69+
}
70+
return JSON.parse(res.stdout.trim()).gitVersion;
71+
});
72+
return this._version;
73+
}
74+
75+
public async printVersion() {
76+
await Exec.exec(this.binPath, ['version', '--json'], {
77+
failOnStdErr: false
78+
});
79+
}
80+
81+
public async versionSatisfies(range: string, version?: string): Promise<boolean> {
82+
const ver = version ?? (await this.version());
83+
if (!ver) {
84+
core.debug(`Cosign.versionSatisfies false: undefined version`);
85+
return false;
86+
}
87+
const res = semver.satisfies(ver, range) || /^[0-9a-f]{7}$/.exec(ver) !== null;
88+
core.debug(`Cosign.versionSatisfies ${ver} statisfies ${range}: ${res}`);
89+
return res;
90+
}
91+
}

0 commit comments

Comments
 (0)