Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
495c96b050 | ||
|
|
b1e0aeed50 | ||
|
|
d0ee813215 | ||
|
|
b2a51dd6b4 | ||
|
|
9450a454a7 | ||
|
|
a7221012c8 |
@@ -15,17 +15,30 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import {afterEach, beforeEach, describe, expect, it, jest} from '@jest/globals';
|
import {afterEach, beforeEach, describe, expect, it, jest} from '@jest/globals';
|
||||||
|
import * as fs from 'fs';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import * as io from '@actions/io';
|
import * as io from '@actions/io';
|
||||||
import osm = require('os');
|
import osm = require('os');
|
||||||
|
import * as rimraf from 'rimraf';
|
||||||
|
|
||||||
import {Docker} from '../../src/docker/docker';
|
import {Docker} from '../../src/docker/docker';
|
||||||
import {Exec} from '../../src/exec';
|
import {Exec} from '../../src/exec';
|
||||||
|
|
||||||
|
import {ConfigFile} from '../../src/types/docker';
|
||||||
|
|
||||||
|
const fixturesDir = path.join(__dirname, '..', 'fixtures');
|
||||||
|
|
||||||
|
// prettier-ignore
|
||||||
|
const tmpDir = path.join(process.env.TEMP || '/tmp', 'docker-jest');
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
jest.clearAllMocks();
|
jest.clearAllMocks();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
afterEach(function () {
|
||||||
|
rimraf.sync(tmpDir);
|
||||||
|
});
|
||||||
|
|
||||||
describe('configDir', () => {
|
describe('configDir', () => {
|
||||||
const originalEnv = process.env;
|
const originalEnv = process.env;
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
@@ -48,6 +61,45 @@ describe('configDir', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('configFile', () => {
|
||||||
|
const originalEnv = process.env;
|
||||||
|
beforeEach(() => {
|
||||||
|
jest.resetModules();
|
||||||
|
if (!fs.existsSync(tmpDir)) {
|
||||||
|
fs.mkdirSync(tmpDir, {recursive: true});
|
||||||
|
}
|
||||||
|
process.env = {
|
||||||
|
...originalEnv,
|
||||||
|
DOCKER_CONFIG: tmpDir
|
||||||
|
};
|
||||||
|
});
|
||||||
|
afterEach(() => {
|
||||||
|
process.env = originalEnv;
|
||||||
|
});
|
||||||
|
it('auths', async () => {
|
||||||
|
fs.copyFileSync(path.join(fixturesDir, 'docker-config-auths.json'), path.join(tmpDir, 'config.json'));
|
||||||
|
expect(Docker.configFile()).toEqual({
|
||||||
|
auths: {
|
||||||
|
'https://index.docker.io/v1/': {
|
||||||
|
auth: 'am9lam9lOmhlbGxv',
|
||||||
|
email: 'user@example.com'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} as unknown as ConfigFile);
|
||||||
|
});
|
||||||
|
it('proxies', async () => {
|
||||||
|
fs.copyFileSync(path.join(fixturesDir, 'docker-config-proxies.json'), path.join(tmpDir, 'config.json'));
|
||||||
|
expect(Docker.configFile()).toEqual({
|
||||||
|
proxies: {
|
||||||
|
default: {
|
||||||
|
httpProxy: 'http://127.0.0.1:3128',
|
||||||
|
httpsProxy: 'http://127.0.0.1:3128'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} as unknown as ConfigFile);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('isAvailable', () => {
|
describe('isAvailable', () => {
|
||||||
it('cli', async () => {
|
it('cli', async () => {
|
||||||
const ioWhichSpy = jest.spyOn(io, 'which');
|
const ioWhichSpy = jest.spyOn(io, 'which');
|
||||||
|
|||||||
8
__tests__/fixtures/docker-config-auths.json
Normal file
8
__tests__/fixtures/docker-config-auths.json
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"auths": {
|
||||||
|
"https://index.docker.io/v1/": {
|
||||||
|
"auth": "am9lam9lOmhlbGxv",
|
||||||
|
"email": "user@example.com"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
8
__tests__/fixtures/docker-config-proxies.json
Normal file
8
__tests__/fixtures/docker-config-proxies.json
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"proxies": {
|
||||||
|
"default": {
|
||||||
|
"httpProxy": "http://127.0.0.1:3128",
|
||||||
|
"httpsProxy": "http://127.0.0.1:3128"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -66,9 +66,12 @@ describe('isInsideWorkTree', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('remoteSha', () => {
|
describe('remoteSha', () => {
|
||||||
it('returns git remote sha', async () => {
|
it('returns sha using git ls-remote', async () => {
|
||||||
expect(await Git.remoteSha('https://github.com/docker/buildx.git', 'refs/pull/648/head')).toEqual('f11797113e5a9b86bd976329c5dbb8a8bfdfadfa');
|
expect(await Git.remoteSha('https://github.com/docker/buildx.git', 'refs/pull/648/head')).toEqual('f11797113e5a9b86bd976329c5dbb8a8bfdfadfa');
|
||||||
});
|
});
|
||||||
|
it('returns sha using github api', async () => {
|
||||||
|
expect(await Git.remoteSha('https://github.com/docker/buildx.git', 'refs/pull/648/head', process.env.GITHUB_TOKEN)).toEqual('f11797113e5a9b86bd976329c5dbb8a8bfdfadfa');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('remoteURL', () => {
|
describe('remoteURL', () => {
|
||||||
|
|||||||
@@ -73,6 +73,18 @@ describe('getInputList', () => {
|
|||||||
expect(res).toEqual(['user/app:cache', 'type=local,src=path/to/dir']);
|
expect(res).toEqual(['user/app:cache', 'type=local,src=path/to/dir']);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('multiline and ignoring comment correctly', async () => {
|
||||||
|
setInput('labels', 'foo=bar\nbar=qux#baz');
|
||||||
|
const res = Util.getInputList('labels');
|
||||||
|
expect(res).toEqual(['foo=bar', 'bar=qux#baz']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('multiline with comment', async () => {
|
||||||
|
setInput('labels', 'foo=bar\nbar=qux#baz');
|
||||||
|
const res = Util.getInputList('labels', {comment: '#'});
|
||||||
|
expect(res).toEqual(['foo=bar', 'bar=qux']);
|
||||||
|
});
|
||||||
|
|
||||||
it('different new lines and ignoring comma correctly', async () => {
|
it('different new lines and ignoring comma correctly', async () => {
|
||||||
setInput('cache-from', 'user/app:cache\r\ntype=local,src=path/to/dir');
|
setInput('cache-from', 'user/app:cache\r\ntype=local,src=path/to/dir');
|
||||||
const res = Util.getInputList('cache-from', {ignoreComma: true});
|
const res = Util.getInputList('cache-from', {ignoreComma: true});
|
||||||
|
|||||||
@@ -51,6 +51,7 @@
|
|||||||
"@actions/http-client": "^2.0.1",
|
"@actions/http-client": "^2.0.1",
|
||||||
"@actions/io": "^1.1.2",
|
"@actions/io": "^1.1.2",
|
||||||
"@actions/tool-cache": "^2.0.1",
|
"@actions/tool-cache": "^2.0.1",
|
||||||
|
"@octokit/plugin-rest-endpoint-methods": "^7.2.1",
|
||||||
"async-retry": "^1.3.3",
|
"async-retry": "^1.3.3",
|
||||||
"csv-parse": "^5.4.0",
|
"csv-parse": "^5.4.0",
|
||||||
"handlebars": "^4.7.7",
|
"handlebars": "^4.7.7",
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ export class Install {
|
|||||||
if (ref.match(/^[0-9a-fA-F]{40}$/)) {
|
if (ref.match(/^[0-9a-fA-F]{40}$/)) {
|
||||||
vspec = ref;
|
vspec = ref;
|
||||||
} else {
|
} else {
|
||||||
vspec = await Git.remoteSha(repo, ref);
|
vspec = await Git.remoteSha(repo, ref, process.env.GIT_AUTH_TOKEN);
|
||||||
}
|
}
|
||||||
core.debug(`Install.build: tool version spec ${vspec}`);
|
core.debug(`Install.build: tool version spec ${vspec}`);
|
||||||
|
|
||||||
|
|||||||
@@ -14,17 +14,29 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import fs from 'fs';
|
||||||
import os from 'os';
|
import os from 'os';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import * as core from '@actions/core';
|
import * as core from '@actions/core';
|
||||||
import * as io from '@actions/io';
|
import * as io from '@actions/io';
|
||||||
|
|
||||||
import {Exec} from '../exec';
|
import {Exec} from '../exec';
|
||||||
|
|
||||||
|
import {ConfigFile} from '../types/docker';
|
||||||
|
|
||||||
export class Docker {
|
export class Docker {
|
||||||
static get configDir(): string {
|
static get configDir(): string {
|
||||||
return process.env.DOCKER_CONFIG || path.join(os.homedir(), '.docker');
|
return process.env.DOCKER_CONFIG || path.join(os.homedir(), '.docker');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static configFile(): ConfigFile | undefined {
|
||||||
|
const f = path.join(Docker.configDir, 'config.json');
|
||||||
|
if (!fs.existsSync(f)) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
return <ConfigFile>JSON.parse(fs.readFileSync(f, {encoding: 'utf-8'}));
|
||||||
|
}
|
||||||
|
|
||||||
public static async isAvailable(): Promise<boolean> {
|
public static async isAvailable(): Promise<boolean> {
|
||||||
return await io
|
return await io
|
||||||
.which('docker', true)
|
.which('docker', true)
|
||||||
|
|||||||
30
src/git.ts
30
src/git.ts
@@ -14,8 +14,14 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import * as core from '@actions/core';
|
||||||
|
import {Octokit} from '@octokit/core';
|
||||||
|
import {restEndpointMethods} from '@octokit/plugin-rest-endpoint-methods';
|
||||||
|
|
||||||
import {Exec} from './exec';
|
import {Exec} from './exec';
|
||||||
|
import {GitHub} from './github';
|
||||||
import {Context} from '@actions/github/lib/context';
|
import {Context} from '@actions/github/lib/context';
|
||||||
|
|
||||||
import {Context as GitContext} from './types/git';
|
import {Context as GitContext} from './types/git';
|
||||||
|
|
||||||
export class Git {
|
export class Git {
|
||||||
@@ -36,7 +42,29 @@ export class Git {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async remoteSha(repo: string, ref: string): Promise<string> {
|
public static async remoteSha(repo: string, ref: string, token?: string): Promise<string> {
|
||||||
|
const repoMatch = repo.match(/github.com\/([^/]+)\/([^/]+?)(?:\.git)?(\/|$)/);
|
||||||
|
// if we have a token and this is a GitHub repo we can use the GitHub API
|
||||||
|
if (token && repoMatch) {
|
||||||
|
core.setSecret(token);
|
||||||
|
const octokit = new (Octokit.plugin(restEndpointMethods).defaults({
|
||||||
|
baseUrl: GitHub.apiURL
|
||||||
|
}))({auth: token});
|
||||||
|
const [owner, repoName] = repoMatch.slice(1, 3);
|
||||||
|
try {
|
||||||
|
return (
|
||||||
|
await octokit.rest.repos.listCommits({
|
||||||
|
owner: owner,
|
||||||
|
repo: repoName,
|
||||||
|
sha: ref,
|
||||||
|
per_page: 1
|
||||||
|
})
|
||||||
|
).data[0].sha;
|
||||||
|
} catch (e) {
|
||||||
|
throw new Error(`Cannot find remote ref for ${repo}#${ref}: ${e.message}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// otherwise we fall back to git ls-remote
|
||||||
return await Git.exec(['ls-remote', repo, ref]).then(out => {
|
return await Git.exec(['ls-remote', repo, ref]).then(out => {
|
||||||
const [rsha] = out.split(/[\s\t]/);
|
const [rsha] = out.split(/[\s\t]/);
|
||||||
if (rsha.length == 0) {
|
if (rsha.length == 0) {
|
||||||
|
|||||||
66
src/types/docker.ts
Normal file
66
src/types/docker.ts
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2023 actions-toolkit authors
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// https://github.com/docker/cli/blob/master/cli/config/configfile/file.go
|
||||||
|
export interface ConfigFile {
|
||||||
|
auths: Record<string, AuthConfig>;
|
||||||
|
HttpHeaders?: Record<string, string>;
|
||||||
|
psFormat?: string;
|
||||||
|
imagesFormat?: string;
|
||||||
|
networksFormat?: string;
|
||||||
|
pluginsFormat?: string;
|
||||||
|
volumesFormat?: string;
|
||||||
|
statsFormat?: string;
|
||||||
|
detachKeys?: string;
|
||||||
|
credsStore?: string;
|
||||||
|
credHelpers?: Record<string, string>;
|
||||||
|
serviceInspectFormat?: string;
|
||||||
|
servicesFormat?: string;
|
||||||
|
tasksFormat?: string;
|
||||||
|
secretFormat?: string;
|
||||||
|
configFormat?: string;
|
||||||
|
nodesFormat?: string;
|
||||||
|
pruneFilters?: string[];
|
||||||
|
proxies?: Record<string, ProxyConfig>;
|
||||||
|
experimental?: string;
|
||||||
|
stackOrchestrator?: string;
|
||||||
|
kubernetes?: KubernetesConfig;
|
||||||
|
currentContext?: string;
|
||||||
|
cliPluginsExtraDirs?: string[];
|
||||||
|
plugins?: Record<string, Record<string, string>>;
|
||||||
|
aliases?: Record<string, string>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ProxyConfig {
|
||||||
|
httpProxy?: string;
|
||||||
|
httpsProxy?: string;
|
||||||
|
noProxy?: string;
|
||||||
|
ftpProxy?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface KubernetesConfig {
|
||||||
|
allNamespaces?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface AuthConfig {
|
||||||
|
username?: string;
|
||||||
|
password?: string;
|
||||||
|
auth?: string;
|
||||||
|
email?: string;
|
||||||
|
serveraddress?: string;
|
||||||
|
identitytoken?: string;
|
||||||
|
registrytoken?: string;
|
||||||
|
}
|
||||||
@@ -21,6 +21,7 @@ import {parse} from 'csv-parse/sync';
|
|||||||
|
|
||||||
export interface InputListOpts {
|
export interface InputListOpts {
|
||||||
ignoreComma?: boolean;
|
ignoreComma?: boolean;
|
||||||
|
comment?: string;
|
||||||
quote?: string | boolean | Buffer | null;
|
quote?: string | boolean | Buffer | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,7 +37,7 @@ export class Util {
|
|||||||
const records = parse(items, {
|
const records = parse(items, {
|
||||||
columns: false,
|
columns: false,
|
||||||
relaxQuotes: true,
|
relaxQuotes: true,
|
||||||
comment: '#',
|
comment: opts?.comment,
|
||||||
relaxColumnCount: true,
|
relaxColumnCount: true,
|
||||||
skipEmptyLines: true,
|
skipEmptyLines: true,
|
||||||
quote: opts?.quote
|
quote: opts?.quote
|
||||||
|
|||||||
28
yarn.lock
28
yarn.lock
@@ -810,6 +810,7 @@ __metadata:
|
|||||||
"@actions/http-client": ^2.0.1
|
"@actions/http-client": ^2.0.1
|
||||||
"@actions/io": ^1.1.2
|
"@actions/io": ^1.1.2
|
||||||
"@actions/tool-cache": ^2.0.1
|
"@actions/tool-cache": ^2.0.1
|
||||||
|
"@octokit/plugin-rest-endpoint-methods": ^7.2.1
|
||||||
"@types/csv-parse": ^1.2.2
|
"@types/csv-parse": ^1.2.2
|
||||||
"@types/node": ^16.18.21
|
"@types/node": ^16.18.21
|
||||||
"@types/semver": ^7.5.0
|
"@types/semver": ^7.5.0
|
||||||
@@ -1348,6 +1349,13 @@ __metadata:
|
|||||||
languageName: node
|
languageName: node
|
||||||
linkType: hard
|
linkType: hard
|
||||||
|
|
||||||
|
"@octokit/openapi-types@npm:^18.0.0":
|
||||||
|
version: 18.0.0
|
||||||
|
resolution: "@octokit/openapi-types@npm:18.0.0"
|
||||||
|
checksum: d487d6c6c1965e583eee417d567e4fe3357a98953fc49bce1a88487e7908e9b5dbb3e98f60dfa340e23b1792725fbc006295aea071c5667a813b9c098185b56f
|
||||||
|
languageName: node
|
||||||
|
linkType: hard
|
||||||
|
|
||||||
"@octokit/openapi-types@npm:^7.0.0":
|
"@octokit/openapi-types@npm:^7.0.0":
|
||||||
version: 7.0.0
|
version: 7.0.0
|
||||||
resolution: "@octokit/openapi-types@npm:7.0.0"
|
resolution: "@octokit/openapi-types@npm:7.0.0"
|
||||||
@@ -1378,6 +1386,17 @@ __metadata:
|
|||||||
languageName: node
|
languageName: node
|
||||||
linkType: hard
|
linkType: hard
|
||||||
|
|
||||||
|
"@octokit/plugin-rest-endpoint-methods@npm:^7.2.1":
|
||||||
|
version: 7.2.1
|
||||||
|
resolution: "@octokit/plugin-rest-endpoint-methods@npm:7.2.1"
|
||||||
|
dependencies:
|
||||||
|
"@octokit/types": ^9.3.1
|
||||||
|
peerDependencies:
|
||||||
|
"@octokit/core": ">=3"
|
||||||
|
checksum: 069e52305f9d2e85fb83819a80860e526b9da2e0936640975f749a2c63020674053e6f4b5af771651a93320172579b0779bf3d663c8adcd090f152aac29f4ad4
|
||||||
|
languageName: node
|
||||||
|
linkType: hard
|
||||||
|
|
||||||
"@octokit/request-error@npm:^2.0.0":
|
"@octokit/request-error@npm:^2.0.0":
|
||||||
version: 2.0.2
|
version: 2.0.2
|
||||||
resolution: "@octokit/request-error@npm:2.0.2"
|
resolution: "@octokit/request-error@npm:2.0.2"
|
||||||
@@ -1468,6 +1487,15 @@ __metadata:
|
|||||||
languageName: node
|
languageName: node
|
||||||
linkType: hard
|
linkType: hard
|
||||||
|
|
||||||
|
"@octokit/types@npm:^9.3.1":
|
||||||
|
version: 9.3.1
|
||||||
|
resolution: "@octokit/types@npm:9.3.1"
|
||||||
|
dependencies:
|
||||||
|
"@octokit/openapi-types": ^18.0.0
|
||||||
|
checksum: 56fce104114730553c79175261f288a263055af4a6de848130fa964940460ee4fe8fa610f33dd0862c2c178d7d97f703e44a799898f3b52583e7ce5ae595f8ff
|
||||||
|
languageName: node
|
||||||
|
linkType: hard
|
||||||
|
|
||||||
"@sinclair/typebox@npm:^0.25.16":
|
"@sinclair/typebox@npm:^0.25.16":
|
||||||
version: 0.25.24
|
version: 0.25.24
|
||||||
resolution: "@sinclair/typebox@npm:0.25.24"
|
resolution: "@sinclair/typebox@npm:0.25.24"
|
||||||
|
|||||||
Reference in New Issue
Block a user