mirror of
https://github.com/docker/metadata-action.git
synced 2025-06-24 03:37:59 +02:00
feat: allows to specify context to fetch git data
Signed-off-by: Emilien Escalle <emilien.escalle@escemi.com>
This commit is contained in:
@ -1,6 +1,9 @@
|
||||
import {beforeEach, describe, expect, test} from '@jest/globals';
|
||||
|
||||
import * as context from '../src/context';
|
||||
import {Context} from '@actions/github/lib/context';
|
||||
import {beforeEach, describe, expect, test, it, jest} from '@jest/globals';
|
||||
import * as dotenv from 'dotenv';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import {ContextSource, getInputs, Inputs} from '../src/context';
|
||||
|
||||
describe('getInputs', () => {
|
||||
beforeEach(() => {
|
||||
@ -20,6 +23,7 @@ describe('getInputs', () => {
|
||||
['images', 'moby/buildkit\nghcr.io/moby/mbuildkit'],
|
||||
]),
|
||||
{
|
||||
context: ContextSource.workflow,
|
||||
bakeTarget: 'docker-metadata-action',
|
||||
flavor: [],
|
||||
githubToken: '',
|
||||
@ -28,7 +32,7 @@ describe('getInputs', () => {
|
||||
sepLabels: '\n',
|
||||
sepTags: '\n',
|
||||
tags: [],
|
||||
} as context.Inputs
|
||||
} as Inputs
|
||||
],
|
||||
[
|
||||
1,
|
||||
@ -39,6 +43,7 @@ describe('getInputs', () => {
|
||||
['sep-tags', ','],
|
||||
]),
|
||||
{
|
||||
context: ContextSource.workflow,
|
||||
bakeTarget: 'metadata',
|
||||
flavor: [],
|
||||
githubToken: '',
|
||||
@ -47,19 +52,54 @@ describe('getInputs', () => {
|
||||
sepLabels: ',',
|
||||
sepTags: ',',
|
||||
tags: [],
|
||||
} as context.Inputs
|
||||
} as Inputs
|
||||
]
|
||||
])(
|
||||
'[%d] given %p as inputs, returns %p',
|
||||
async (num: number, inputs: Map<string, string>, expected: context.Inputs) => {
|
||||
async (num: number, inputs: Map<string, string>, expected: Inputs) => {
|
||||
inputs.forEach((value: string, name: string) => {
|
||||
setInput(name, value);
|
||||
});
|
||||
expect(await context.getInputs()).toEqual(expected);
|
||||
expect(await getInputs()).toEqual(expected);
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
describe('getContext', () => {
|
||||
it('get context with workflow', async () => {
|
||||
process.env = dotenv.parse(fs.readFileSync(path.join(__dirname, 'fixtures/event_create_branch.env')));
|
||||
|
||||
jest.resetModules();
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
const {getContext} = require('../src/context');
|
||||
const contextResult = await getContext(ContextSource.workflow);
|
||||
|
||||
expect(contextResult.ref).toEqual('refs/heads/dev');
|
||||
expect(contextResult.sha).toEqual('5f3331d7f7044c18ca9f12c77d961c4d7cf3276a');
|
||||
});
|
||||
|
||||
it('get context with git', async () => {
|
||||
jest.resetModules();
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
const git = require('@docker/actions-toolkit/lib/git');
|
||||
jest.spyOn(git.Git, 'context').mockImplementation((): Promise<Context> => {
|
||||
return Promise.resolve({
|
||||
ref: 'refs/heads/git-test',
|
||||
sha: 'git-test-sha'
|
||||
} as Context);
|
||||
});
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
const {getContext} = require('../src/context');
|
||||
|
||||
const contextResult = await getContext(ContextSource.git);
|
||||
|
||||
expect(contextResult.ref).toEqual('refs/heads/git-test');
|
||||
expect(contextResult.sha).toEqual('git-test-sha');
|
||||
});
|
||||
});
|
||||
|
||||
// See: https://github.com/actions/toolkit/blob/master/packages/core/src/core.ts#L67
|
||||
function getInputName(name: string): string {
|
||||
return `INPUT_${name.replace(/ /g, '_').toUpperCase()}`;
|
||||
|
@ -2,12 +2,10 @@ import {beforeEach, describe, expect, jest, test} from '@jest/globals';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import * as dotenv from 'dotenv';
|
||||
import {Context} from '@actions/github/lib/context';
|
||||
import {GitHub} from '@docker/actions-toolkit/lib/github';
|
||||
import {Toolkit} from '@docker/actions-toolkit/lib/toolkit';
|
||||
import {GitHubRepo} from '@docker/actions-toolkit/lib/types/github';
|
||||
|
||||
import {getInputs, Inputs} from '../src/context';
|
||||
import {ContextSource, getInputs, Inputs} from '../src/context';
|
||||
import {Meta, Version} from '../src/meta';
|
||||
|
||||
import repoFixture from './fixtures/repo.json';
|
||||
@ -23,6 +21,17 @@ jest.mock('moment-timezone', () => {
|
||||
return () => (jest.requireActual('moment-timezone') as typeof import('moment-timezone'))('2020-01-10T00:30:00.000Z');
|
||||
});
|
||||
|
||||
/**
|
||||
* Get a workflow context based on the current environment variables.
|
||||
*/
|
||||
const getFreshWorkflowContext = async () => {
|
||||
jest.resetModules();
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
const {getContext} = require('../src/context');
|
||||
const context = await getContext(ContextSource.workflow);
|
||||
return context;
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
Object.keys(process.env).forEach(function (key) {
|
||||
@ -49,7 +58,8 @@ const tagsLabelsTest = async (name: string, envFile: string, inputs: Inputs, exV
|
||||
process.env = dotenv.parse(fs.readFileSync(path.join(__dirname, 'fixtures', envFile)));
|
||||
const toolkit = new Toolkit();
|
||||
const repo = await toolkit.github.repoData();
|
||||
const meta = new Meta({...getInputs(), ...inputs}, new Context(), repo);
|
||||
const context = await getFreshWorkflowContext();
|
||||
const meta = new Meta({...getInputs(), ...inputs}, context, repo);
|
||||
|
||||
const version = meta.version;
|
||||
expect(version).toEqual(exVersion);
|
||||
@ -2766,7 +2776,8 @@ describe('pr-head-sha', () => {
|
||||
|
||||
const toolkit = new Toolkit();
|
||||
const repo = await toolkit.github.repoData();
|
||||
const meta = new Meta({...getInputs(), ...inputs}, new Context(), repo);
|
||||
const context = await getFreshWorkflowContext();
|
||||
const meta = new Meta({...getInputs(), ...inputs}, context, repo);
|
||||
|
||||
const version = meta.version;
|
||||
expect(version).toEqual(exVersion);
|
||||
@ -3708,7 +3719,8 @@ describe('json', () => {
|
||||
|
||||
const toolkit = new Toolkit();
|
||||
const repo = await toolkit.github.repoData();
|
||||
const meta = new Meta({...getInputs(), ...inputs}, new Context(), repo);
|
||||
const context = await getFreshWorkflowContext();
|
||||
const meta = new Meta({...getInputs(), ...inputs}, context, repo);
|
||||
|
||||
const jsonOutput = meta.getJSON();
|
||||
expect(jsonOutput).toEqual(exJSON);
|
||||
@ -4014,7 +4026,8 @@ describe('bake', () => {
|
||||
|
||||
const toolkit = new Toolkit();
|
||||
const repo = await toolkit.github.repoData();
|
||||
const meta = new Meta({...getInputs(), ...inputs}, new Context(), repo);
|
||||
const context = await getFreshWorkflowContext();
|
||||
const meta = new Meta({...getInputs(), ...inputs}, context, repo);
|
||||
|
||||
const bakeFile = meta.getBakeFile();
|
||||
expect(JSON.parse(fs.readFileSync(bakeFile, 'utf8'))).toEqual(exBakeDefinition);
|
||||
@ -4056,11 +4069,13 @@ describe('sepTags', () => {
|
||||
"user/app:dev,user/app:my,user/app:custom,user/app:tags"
|
||||
]
|
||||
])('given %p with %p event', async (name: string, envFile: string, inputs: Inputs, expTags: string) => {
|
||||
|
||||
process.env = dotenv.parse(fs.readFileSync(path.join(__dirname, 'fixtures', envFile)));
|
||||
|
||||
|
||||
const toolkit = new Toolkit();
|
||||
const repo = await toolkit.github.repoData();
|
||||
const meta = new Meta({...getInputs(), ...inputs}, new Context(), repo);
|
||||
const context = await getFreshWorkflowContext();
|
||||
const meta = new Meta({...getInputs(), ...inputs}, context, repo);
|
||||
|
||||
expect(meta.getTags().join(inputs.sepTags)).toEqual(expTags);
|
||||
});
|
||||
|
Reference in New Issue
Block a user