add language recognizer

Signed-off-by: Luca Stocchi <lstocchi@redhat.com>
This commit is contained in:
Luca Stocchi 2020-11-13 12:38:29 +01:00
parent 652179bd8c
commit 3043388d5b
No known key found for this signature in database
GPG key ID: 930BB00F3FCF30A4
12 changed files with 6828 additions and 106 deletions

3
.gitignore vendored
View file

@ -1 +1,2 @@
node_modules/ node_modules/
out/

View file

@ -20,6 +20,12 @@ inputs:
port: port:
description: 'Port' description: 'Port'
required: false required: false
working-dir:
description: 'Working directory'
required: false
envs:
description: 'envs'
required: false
runs: runs:
using: 'node12' using: 'node12'
main: 'dist/index.js' main: 'dist/index.js'

2
dist/index.js vendored

File diff suppressed because one or more lines are too long

2
dist/index.js.map vendored

File diff suppressed because one or more lines are too long

6622
dist/languages.yaml vendored Normal file

File diff suppressed because it is too large Load diff

3
language-image.json Normal file
View file

@ -0,0 +1,3 @@
{
"java": "docker.io/fabric8/java-alpine-openjdk11-jre"
}

View file

@ -18,22 +18,25 @@ class BuildahCli {
} }
from(baseImage) { from(baseImage) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
if (!baseImage) {
// find correct baseImage based on language project
}
return yield this.execute(['from', baseImage]); return yield this.execute(['from', baseImage]);
}); });
} }
copy(container, content, path) { copy(container, contentToCopy, path) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
core.debug('copy'); core.debug('copy');
core.debug(container); core.debug(container);
core.debug(content); let result;
const args = ["copy", container, content]; for (const content of contentToCopy) {
if (path) { const args = ["copy", container, content];
args.push(path); if (path) {
args.push(path);
}
result = yield this.execute(args);
if (result.succeeded === false) {
return result;
}
} }
return yield this.execute(args); return result;
}); });
} }
config(container, settings) { config(container, settings) {
@ -49,6 +52,12 @@ class BuildahCli {
args.push('--port'); args.push('--port');
args.push(settings.port); args.push(settings.port);
} }
if (settings.envs) {
settings.envs.forEach((env) => {
args.push('--env');
args.push(env);
});
}
args.push(container); args.push(container);
return yield this.execute(args); return yield this.execute(args);
}); });

View file

@ -9,45 +9,60 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
}); });
}; };
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.getInputList = exports.run = void 0; exports.run = void 0;
const core = require("@actions/core"); const core = require("@actions/core");
const io = require("@actions/io"); const io = require("@actions/io");
const buildah_1 = require("./buildah"); const buildah_1 = require("./buildah");
/** const recognizer = require("language-recognizer");
* 1. Buildah only works on Linux as the docker action const fs_1 = require("fs");
* 2. Does this action also need to setup buildah? const path = require("path");
* 3. Does this action also have the ability to push to a registry?
*
*/
function run() { function run() {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
const baseImage = core.getInput('base-image'); let baseImage = core.getInput('base-image');
const content = core.getInput('content'); const content = getInputList('content');
const entrypoint = yield getInputList('entrypoint');
const port = core.getInput('port');
const newImageName = core.getInput('new-image-name'); const newImageName = core.getInput('new-image-name');
const runnerOS = process.env.RUNNER_OS; const entrypoint = getInputList('entrypoint');
if (runnerOS !== 'Linux') { const port = core.getInput('port');
throw new Error(`Only supported on linux platform`); const workingDir = core.getInput('working-dir');
const envs = getInputList('envs');
if (process.env.RUNNER_OS !== 'Linux') {
return Promise.reject(new Error('Only linux platform is supported at this time.'));
} }
// get buildah cli // get buildah cli
const buildahPath = yield io.which('buildah', true); const buildahPath = yield io.which('buildah', true);
// create image // if base-image is not specified by the user we need to pick one automatically
const cli = new buildah_1.BuildahCli(buildahPath); if (!baseImage) {
const creationResult = yield cli.from(baseImage); const workspace = process.env['GITHUB_WORKSPACE'];
if (creationResult.succeeded === false) { if (workspace) {
return Promise.reject(new Error(creationResult.reason)); // check language/framework used and pick base-image automatically
const languages = yield recognizer.detectLanguages(workspace);
baseImage = yield getSuggestedBaseImage(languages);
if (!baseImage) {
return Promise.reject(new Error('No base image found to create a new container'));
}
}
else {
return Promise.reject(new Error('No base image found to create a new container'));
}
} }
const containerId = creationResult.output.replace('\n', ''); // create the new image
const cli = new buildah_1.BuildahCli(buildahPath);
const container = yield cli.from(baseImage);
if (container.succeeded === false) {
return Promise.reject(new Error(container.reason));
}
const containerId = container.output.replace('\n', '');
const copyResult = yield cli.copy(containerId, content); const copyResult = yield cli.copy(containerId, content);
if (copyResult.succeeded === false) { if (copyResult.succeeded === false) {
return Promise.reject(new Error(copyResult.reason)); return Promise.reject(new Error(copyResult.reason));
} }
const configuration = { const newImageConfig = {
entrypoint: entrypoint, entrypoint: entrypoint,
port: port port: port,
workingdir: workingDir,
envs: envs
}; };
const configResult = yield cli.config(containerId, configuration); const configResult = yield cli.config(containerId, newImageConfig);
if (configResult.succeeded === false) { if (configResult.succeeded === false) {
return Promise.reject(new Error(configResult.reason)); return Promise.reject(new Error(configResult.reason));
} }
@ -55,21 +70,39 @@ function run() {
if (commit.succeeded === false) { if (commit.succeeded === false) {
return Promise.reject(new Error(commit.reason)); return Promise.reject(new Error(commit.reason));
} }
core.setOutput('image', newImageName);
}); });
} }
exports.run = run; exports.run = run;
function getInputList(name, ignoreComma) { function getInputList(name) {
const items = core.getInput(name);
if (!items) {
return [];
}
return items
.split(/\r?\n/)
.filter(x => x)
.reduce((acc, line) => acc.concat(line).map(pat => pat.trim()), []);
}
function getSuggestedBaseImage(languages) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
const items = core.getInput(name); if (!languages || languages.length === 0) {
if (items == '') { return undefined;
return [];
} }
return items for (const language of languages) {
.split(/\r?\n/) const baseImage = yield getBaseImageByLanguage(language);
.filter(x => x) if (baseImage) {
.reduce((acc, line) => acc.concat(!ignoreComma ? line.split(',').filter(x => x) : line).map(pat => pat.trim()), []); return baseImage;
}
}
return undefined;
});
}
function getBaseImageByLanguage(language) {
return __awaiter(this, void 0, void 0, function* () {
// eslint-disable-next-line no-undef
const rawData = yield fs_1.promises.readFile(path.join(__dirname, 'language-image.json'), 'utf-8');
const languageImageJSON = JSON.parse(rawData);
return languageImageJSON[language.name];
}); });
} }
exports.getInputList = getInputList;
run().catch(core.setFailed); run().catch(core.setFailed);

View file

@ -15,7 +15,8 @@
"dependencies": { "dependencies": {
"@actions/core": "^1.2.6", "@actions/core": "^1.2.6",
"@actions/exec": "^1.0.4", "@actions/exec": "^1.0.4",
"@actions/io": "^1.0.2" "@actions/io": "^1.0.2",
"language-recognizer": "0.0.1"
}, },
"devDependencies": { "devDependencies": {
"@types/node": "^12.12.7", "@types/node": "^12.12.7",

View file

@ -1,58 +1,51 @@
import * as core from "@actions/core"; import * as core from "@actions/core";
import * as exec from "@actions/exec"; import * as exec from "@actions/exec";
import { CommandResult } from "./types";
interface Buildah { interface Buildah {
from(baseImage?: string): Promise<CommandResult>; from(baseImage: string): Promise<CommandResult>;
copy(container: string, content: string): Promise<CommandResult>; copy(container: string, contentToCopy: string[]): Promise<CommandResult>;
config(container: string, setting: {}): Promise<CommandResult>; config(container: string, setting: {}): Promise<CommandResult>;
commit(container: string, newImageName: string, flags?: string[]): Promise<CommandResult>; commit(container: string, newImageName: string, flags?: string[]): Promise<CommandResult>;
} }
export interface BuildahConfigSettings { export interface BuildahConfigSettings {
author?: string;
annotation?: string;
arch?: string;
created_by?: string;
entrypoint?: string[]; entrypoint?: string[];
labels?: string[];
envs?: string[]; envs?: string[];
port?: string; port?: string;
workingdir?: string;
} }
export interface CommandSucceeeded {
readonly succeeded: true;
readonly output?: string;
}
export interface CommandFailed {
readonly succeeded: false;
readonly reason?: string;
}
export type CommandResult = CommandFailed | CommandSucceeeded;
export class BuildahCli implements Buildah { export class BuildahCli implements Buildah {
private executable: string; private executable: string;
constructor(executable: string) { constructor(executable: string) {
this.executable = executable; this.executable = executable;
} }
async from(baseImage?: string): Promise<CommandResult> {
if (!baseImage) {
// find correct baseImage based on language project
}
async from(baseImage: string): Promise<CommandResult> {
return await this.execute(['from', baseImage]); return await this.execute(['from', baseImage]);
} }
async copy(container: string, content: string, path?: string): Promise<CommandResult> {
async copy(container: string, contentToCopy: string[], path?: string): Promise<CommandResult> {
core.debug('copy'); core.debug('copy');
core.debug(container); core.debug(container);
core.debug(content); let result: CommandResult;
const args: string[] = ["copy", container, content]; for (const content of contentToCopy) {
if (path) { const args: string[] = ["copy", container, content];
args.push(path); if (path) {
args.push(path);
}
result = await this.execute(args);
if (result.succeeded === false) {
return result;
}
} }
return await this.execute(args);
return result;
} }
async config(container: string, settings: BuildahConfigSettings): Promise<CommandResult> { async config(container: string, settings: BuildahConfigSettings): Promise<CommandResult> {
core.debug('config'); core.debug('config');
core.debug(container); core.debug(container);
@ -65,9 +58,16 @@ export class BuildahCli implements Buildah {
args.push('--port'); args.push('--port');
args.push(settings.port); args.push(settings.port);
} }
if (settings.envs) {
settings.envs.forEach((env) => {
args.push('--env');
args.push(env);
});
}
args.push(container); args.push(container);
return await this.execute(args); return await this.execute(args);
} }
async commit(container: string, newImageName: string, flags: string[] = []): Promise<CommandResult> { async commit(container: string, newImageName: string, flags: string[] = []): Promise<CommandResult> {
core.debug('commit'); core.debug('commit');
core.debug(container); core.debug(container);

View file

@ -1,45 +1,61 @@
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 { BuildahCli, BuildahConfigSettings } from './buildah'; import { BuildahCli, BuildahConfigSettings } from './buildah';
import * as recognizer from 'language-recognizer';
import {promises as fs} from 'fs';
import * as path from 'path';
import { Language } from 'language-recognizer/lib/types';
/**
* 1. Buildah only works on Linux as the docker action
* 2. Does this action also need to setup buildah?
* 3. Does this action also have the ability to push to a registry?
*
*/
export async function run(): Promise<void> { export async function run(): Promise<void> {
const baseImage = core.getInput('base-image'); let baseImage = core.getInput('base-image');
const content = core.getInput('content'); const content = getInputList('content');
const entrypoint = await getInputList('entrypoint');
const port = core.getInput('port');
const newImageName = core.getInput('new-image-name'); const newImageName = core.getInput('new-image-name');
const runnerOS = process.env.RUNNER_OS; const entrypoint = getInputList('entrypoint');
const port = core.getInput('port');
const workingDir = core.getInput('working-dir');
const envs = getInputList('envs');
if (runnerOS !== 'Linux') { if (process.env.RUNNER_OS !== 'Linux') {
throw new Error(`Only supported on linux platform`); return Promise.reject(new Error('Only linux platform is supported at this time.'));
} }
// get buildah cli // get buildah cli
const buildahPath = await io.which('buildah', true); const buildahPath = await io.which('buildah', true);
// create image // if base-image is not specified by the user we need to pick one automatically
const cli: BuildahCli = new BuildahCli(buildahPath); if (!baseImage) {
const creationResult = await cli.from(baseImage); const workspace = process.env['GITHUB_WORKSPACE'];
if (creationResult.succeeded === false) { if (workspace) {
return Promise.reject(new Error(creationResult.reason)); // check language/framework used and pick base-image automatically
const languages = await recognizer.detectLanguages(workspace);
baseImage = await getSuggestedBaseImage(languages);
if (!baseImage) {
return Promise.reject(new Error('No base image found to create a new container'));
}
} else {
return Promise.reject(new Error('No base image found to create a new container'));
}
} }
const containerId = creationResult.output.replace('\n', '');
// create the new image
const cli: BuildahCli = new BuildahCli(buildahPath);
const container = await cli.from(baseImage);
if (container.succeeded === false) {
return Promise.reject(new Error(container.reason));
}
const containerId = container.output.replace('\n', '');
const copyResult = await cli.copy(containerId, content); const copyResult = await cli.copy(containerId, content);
if (copyResult.succeeded === false) { if (copyResult.succeeded === false) {
return Promise.reject(new Error(copyResult.reason)); return Promise.reject(new Error(copyResult.reason));
} }
const configuration: BuildahConfigSettings = { const newImageConfig: BuildahConfigSettings = {
entrypoint: entrypoint, entrypoint: entrypoint,
port: port port: port,
} workingdir: workingDir,
const configResult = await cli.config(containerId, configuration); envs: envs
};
const configResult = await cli.config(containerId, newImageConfig);
if (configResult.succeeded === false) { if (configResult.succeeded === false) {
return Promise.reject(new Error(configResult.reason)); return Promise.reject(new Error(configResult.reason));
} }
@ -48,22 +64,42 @@ export async function run(): Promise<void> {
if (commit.succeeded === false) { if (commit.succeeded === false) {
return Promise.reject(new Error(commit.reason)); return Promise.reject(new Error(commit.reason));
} }
core.setOutput('image', newImageName);
} }
export async function getInputList(name: string, ignoreComma?: boolean): Promise<string[]> { function getInputList(name: string): string[] {
const items = core.getInput(name); const items = core.getInput(name);
if (items == '') { if (!items) {
return []; return [];
} }
return items return items
.split(/\r?\n/) .split(/\r?\n/)
.filter(x => x) .filter(x => x)
.reduce<string[]>( .reduce<string[]>(
(acc, line) => acc.concat(!ignoreComma ? line.split(',').filter(x => x) : line).map(pat => pat.trim()), (acc, line) => acc.concat(line).map(pat => pat.trim()),
[] []
); );
} }
async function getSuggestedBaseImage(languages: Language[]): Promise<string> {
if (!languages || languages.length === 0) {
return undefined;
}
for (const language of languages) {
const baseImage = await getBaseImageByLanguage(language);
if (baseImage) {
return baseImage;
}
}
return undefined;
}
async function getBaseImageByLanguage(language: Language): Promise<string> {
// eslint-disable-next-line no-undef
const rawData = await fs.readFile(path.join(__dirname, 'language-image.json'), 'utf-8');
const languageImageJSON = JSON.parse(rawData);
return languageImageJSON[language.name];
}
run().catch(core.setFailed); run().catch(core.setFailed);

11
src/types.ts Normal file
View file

@ -0,0 +1,11 @@
export interface CommandSucceeeded {
readonly succeeded: true;
readonly output?: string;
}
export interface CommandFailed {
readonly succeeded: false;
readonly reason?: string;
}
export type CommandResult = CommandFailed | CommandSucceeeded;