chore: adopt yarn 4 and restore context input

This commit is contained in:
Shawn Anderson 2025-11-09 15:03:46 -05:00
parent 8d8c7c12f7
commit ff416072d6
15 changed files with 3732 additions and 8388 deletions

View file

@ -2,20 +2,12 @@ import {afterEach, beforeEach, describe, expect, test, it, jest} from '@jest/glo
import * as dotenv from 'dotenv';
import * as fs from 'fs';
import * as path from 'path';
import {Context} from '@actions/github/lib/context';
import {Git} from '@docker/actions-toolkit/lib/git';
import {GitHub} from '@docker/actions-toolkit/lib/github';
import {Toolkit} from '@docker/actions-toolkit/lib/toolkit';
import {ContextSource, getContext, getInputs, Inputs} from '../src/context';
const toolkit = new Toolkit({githubToken: 'fake-github-token'});
import * as gitModule from '../src/git';
beforeEach(() => {
jest.clearAllMocks();
jest.spyOn(GitHub, 'context', 'get').mockImplementation((): Context => {
return new Context();
});
});
describe('getInputs', () => {
@ -36,17 +28,21 @@ describe('getInputs', () => {
['images', 'moby/buildkit\nghcr.io/moby/mbuildkit'],
]),
{
context: ContextSource.workflow,
context: ContextSource.git,
bakeTarget: 'docker-metadata-action',
flavor: [],
githubToken: '',
images: ['moby/buildkit', 'ghcr.io/moby/mbuildkit'],
labels: [],
annotations: [],
sepLabels: '\n',
sepTags: '\n',
sepAnnotations: '\n',
tags: [],
tags: [
'type=schedule',
'type=ref,event=branch',
'type=ref,event=tag',
'type=ref,event=pr'
],
} as Inputs
],
[
@ -59,17 +55,21 @@ describe('getInputs', () => {
['sep-annotations', ',']
]),
{
context: ContextSource.workflow,
context: ContextSource.git,
bakeTarget: 'metadata',
flavor: [],
githubToken: '',
images: ['moby/buildkit'],
labels: [],
annotations: [],
sepLabels: ',',
sepTags: ',',
sepAnnotations: ',',
tags: [],
tags: [
'type=schedule',
'type=ref,event=branch',
'type=ref,event=tag',
'type=ref,event=pr'
],
} as Inputs
],
[
@ -78,17 +78,21 @@ describe('getInputs', () => {
['images', 'moby/buildkit\n#comment\nghcr.io/moby/mbuildkit'],
]),
{
context: ContextSource.workflow,
context: ContextSource.git,
bakeTarget: 'docker-metadata-action',
flavor: [],
githubToken: '',
images: ['moby/buildkit', 'ghcr.io/moby/mbuildkit'],
labels: [],
annotations: [],
sepLabels: '\n',
sepTags: '\n',
sepAnnotations: '\n',
tags: [],
tags: [
'type=schedule',
'type=ref,event=branch',
'type=ref,event=tag',
'type=ref,event=pr'
],
} as Inputs
],
])(
@ -113,29 +117,43 @@ describe('getContext', () => {
});
afterEach(() => {
process.env = originalEnv;
jest.restoreAllMocks();
});
it('workflow', async () => {
const context = await getContext(ContextSource.workflow, toolkit);
it('should return git context', async () => {
jest.spyOn(gitModule, 'getGitContext').mockImplementation(async () => {
return {
sha: '5f3331d7f7044c18ca9f12c77d961c4d7cf3276a',
ref: process.env.GITHUB_REF || 'refs/heads/dev',
commitDate: new Date('2024-11-13T13:42:28.000Z'),
remoteUrl: 'https://github.com/test/repo.git',
defaultBranch: 'master'
};
});
const context = await getContext(ContextSource.git);
expect(context.ref).toEqual('refs/heads/dev');
expect(context.sha).toEqual('5f3331d7f7044c18ca9f12c77d961c4d7cf3276a');
expect(context.commitDate).toEqual(new Date('2024-11-13T13:42:28.000Z'));
});
it('git', async () => {
jest.spyOn(Git, 'context').mockImplementation((): Promise<Context> => {
return Promise.resolve({
ref: 'refs/heads/git-test',
sha: 'git-test-sha'
} as Context);
it('should fall back to git commit date when workflow payload missing', async () => {
jest.spyOn(gitModule, 'getGitContext').mockImplementation(async () => {
return {
sha: 'payload-sha',
ref: 'refs/heads/workflow-branch',
commitDate: new Date('2022-01-01T00:00:00.000Z'),
remoteUrl: 'https://github.com/test/repo.git',
defaultBranch: 'main'
};
});
jest.spyOn(Git, 'commitDate').mockImplementation(async (): Promise<Date> => {
return new Date('2023-01-01T13:42:28.000Z');
});
const context = await getContext(ContextSource.git, toolkit);
expect(context.ref).toEqual('refs/heads/git-test');
expect(context.sha).toEqual('git-test-sha');
expect(context.commitDate).toEqual(new Date('2023-01-01T13:42:28.000Z'));
process.env.GITHUB_SHA = 'payload-sha';
process.env.GITHUB_REF = 'refs/heads/workflow-branch';
delete process.env.GITHUB_EVENT_PATH;
const context = await getContext(ContextSource.workflow);
expect(context.ref).toEqual('refs/heads/workflow-branch');
expect(context.sha).toEqual('payload-sha');
expect(context.commitDate).toEqual(new Date('2022-01-01T00:00:00.000Z'));
});
});