Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for gitlab #123

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"typescript": "^4.1.2"
},
"dependencies": {
"@gitbeaker/node": "^34.1.0",
"@octokit/core": "^3.2.4",
"@octokit/rest": "^18.0.12",
"js-yaml": "^4.1.0",
Expand Down
File renamed without changes.
10 changes: 10 additions & 0 deletions sample/config/config-gitlab.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
repo:
url: https://gitlab.com/codeleague/codecoach
pr: 40
token: 123qwe123qwe123qwe123
gitlabProjectId: "12"
buildLogFiles:
- type: tslint
path: /path/to/log.json
cwd: /repo/src
output: ./output.json
8 changes: 6 additions & 2 deletions src/Config/@types/configArgument.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { BuildLogFile } from './buildLogFile';

export type ConfigArgument = {
export interface ConfigArgument {
url: string;
pr: number;
buildLogFile: BuildLogFile[];
output: string;
token: string;
removeOldComment: boolean;
config: string;
};
}

export interface ConfigArgumentGitlab extends ConfigArgument {
gitlabProjectId: string | number;
}
22 changes: 14 additions & 8 deletions src/Config/@types/configYAML.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import { BuildLogFile } from './buildLogFile';

export interface RepoDetailsYAML {
url: string;
pr: number;
token: string;
userAgent: string;
timeZone: string;
removeOldComment: boolean;
}

export interface RepoDetailsGitlabYAML extends RepoDetailsYAML {
gitlabProjectId: string | number;
}

export type ConfigYAML = {
repo: {
url: string;
pr: number;
token: string;
userAgent: string;
timeZone: string;
removeOldComment: boolean;
};
repo: RepoDetailsGitlabYAML;
buildLogFiles: BuildLogFile[];
output: string;
};
1 change: 1 addition & 0 deletions src/Config/@types/providerConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export type ProviderConfig = {
repoUrl: string;
prId: number;
removeOldComment: boolean;
gitlabProjectId: string | number;
};
57 changes: 54 additions & 3 deletions src/Config/Config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,28 @@ const MOCK_ARGS = [
'-o=./tmp/out.json',
];

const MOCK_ARGS_W_CONFIG_YAML = [
const MOCK_ARGS_FOR_GITLAB = [
'/usr/local/Cellar/node/15.13.0/bin/node',
'/Users/codecoach/src/app.ts',
'--config=sample/config/config.yaml',
'--url=https://gitlab.com/codeleague/codecoach.git',
'--removeOldComment',
'--token=placeyourtokenhere',
'--pr=15',
'--gitlabProjectId=12',
'-f=dotnetbuild;./sample/dotnetbuild/build.content;/repo/src',
'-o=./tmp/out.json',
];

const MOCK_ARGS_W_CONFIG_YAML_FOR_GITHUB = [
'/usr/local/Cellar/node/15.13.0/bin/node',
'/Users/codecoach/src/app.ts',
'--config=sample/config/config-github.yaml',
];

const MOCK_ARGS_W_CONFIG_YAML_FOR_GITLAB = [
'/usr/local/Cellar/node/15.13.0/bin/node',
'/Users/codecoach/src/app.ts',
'--config=sample/config/config-gitlab.yaml',
];

export const EXPECTED_MOCK_ARGS = [
Expand All @@ -28,6 +46,17 @@ export const EXPECTED_MOCK_ARGS = [
'./tmp/out.json',
];

export const EXPECTED_MOCK_ARGS_FOR_GITLAB = [
'/usr/local/Cellar/node/15.13.0/bin/node',
'/Users/codecoach/src/app.ts',
'https://gitlab.com/codeleague/codecoach.git',
true,
'placeyourtokenhere',
15,
'12',
'dotnetbuild;./sample/dotnetbuild/build.content;/repo/src',
'./tmp/out.json',
];
describe('Config Test', () => {
let config: typeof Config;

Expand All @@ -44,9 +73,31 @@ describe('Config Test', () => {
});

it('Should able to use a config file without passing other args', async () => {
process.argv = MOCK_ARGS_W_CONFIG_YAML;
process.argv = MOCK_ARGS_W_CONFIG_YAML_FOR_GITHUB;
config = (await import('./Config')).Config;
const fullfillConfig = await config;
expect(fullfillConfig.app.buildLogFiles[0].type).toBe('tslint');
});

// gitlab args should pass when project id is present
it('Should able to parse this args and run without throwing error', async () => {
process.argv = MOCK_ARGS_FOR_GITLAB;
config = (await import('./Config')).Config;
const fullfillConfig = await config;
expect(fullfillConfig.provider.repoUrl).toBe(EXPECTED_MOCK_ARGS_FOR_GITLAB[2]);
expect(fullfillConfig.provider.removeOldComment).toBe(
EXPECTED_MOCK_ARGS_FOR_GITLAB[3],
);
expect(fullfillConfig.provider.gitlabProjectId).toBe(
EXPECTED_MOCK_ARGS_FOR_GITLAB[6],
);
});

it('Should able to use a config file without passing other args', async () => {
process.argv = MOCK_ARGS_W_CONFIG_YAML_FOR_GITLAB;
config = (await import('./Config')).Config;
const fullfillConfig = await config;
expect(fullfillConfig.app.buildLogFiles[0].type).toBe('tslint');
expect(fullfillConfig.provider.gitlabProjectId).toBe('12');
});
});
18 changes: 14 additions & 4 deletions src/Config/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { ProjectType } from './@enums';
import { BuildLogFile, ConfigArgument, ConfigObject } from './@types';
import { buildAppConfig, buildProviderConfig } from './configBuilder';
import { DEFAULT_OUTPUT_FILE } from './constants/defaults';
import { REQUIRED_ARGS } from './constants/required';
import { isGitlab } from '../Provider/utils/vcsType';

import { REQUIRED_ARGS_GITHUB, REQUIRED_ARGS_GITLAB } from './constants/required';
import { ConfigArgumentGitlab } from './@types/configArgument';

const projectTypes = Object.keys(ProjectType);

Expand All @@ -24,6 +27,10 @@ const args = yargs
describe: 'GitHub token',
type: 'string',
})
.option('gitlabProjectId', {
describe: 'Gitlab Project Id',
type: 'string',
})
.option('buildLogFile', {
alias: 'f',
describe: `Build log content files formatted in '<type>;<path>[;<cwd>]'
Expand Down Expand Up @@ -56,11 +63,14 @@ and <cwd> is build root directory (optional (Will use current context as cwd)).
.check((options) => {
// check required arguments
const useConfigArgs = options.config === undefined;
const validRequiredArgs = REQUIRED_ARGS.every(
const requiredArgs = isGitlab(options.url)
? REQUIRED_ARGS_GITLAB
: REQUIRED_ARGS_GITHUB;
const validRequiredArgs = requiredArgs.every(
(el) => options[el] != undefined || options[el] != null,
);
if (useConfigArgs && !validRequiredArgs)
throw `please fill all required fields ${REQUIRED_ARGS.join(', ')}`;
throw `please fill all required fields ${requiredArgs.join(', ')}`;
return true;
})
.check((options) => {
Expand All @@ -76,7 +86,7 @@ and <cwd> is build root directory (optional (Will use current context as cwd)).
})
.help()
.wrap(120)
.parse(process.argv.slice(1)) as ConfigArgument;
.parse(process.argv.slice(1)) as ConfigArgumentGitlab;

export const Config: Promise<ConfigObject> = (async () => {
return Object.freeze({
Expand Down
10 changes: 9 additions & 1 deletion src/Config/YML.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@ import { YML } from './YML';

describe('YML Test', () => {
it('Should able to validate yaml file correctly', async () => {
const config = await YML.parse('sample/config/config.yaml');
const config = await YML.parse('sample/config/config-github.yaml');
expect(config.output).toBe('./output.json');
expect(config.repo.pr).toBe(40);
expect(config.repo.removeOldComment).toBe(false);
});

it('Should able to validate yaml file correctly', async () => {
const config = await YML.parse('sample/config/config-gitlab.yaml');
expect(config.output).toBe('./output.json');
expect(config.repo.pr).toBe(40);
expect(config.repo.gitlabProjectId).toBe('12');
expect(config.repo.removeOldComment).toBe(false);
});
});
14 changes: 11 additions & 3 deletions src/Config/YML.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { File } from '../File';
import yaml from 'js-yaml';
import { REQUIRED_YAML_ARGS, REQUIRED_YAML_PROVIDER_ARGS } from './constants/required';
import {
REQUIRED_YAML_ARGS,
REQUIRED_GITHUB_YAML_PROVIDER_ARGS,
REQUIRED_GITLAB_YAML_PROVIDER_ARGS,
} from './constants/required';
import { ConfigYAML } from './@types/configYAML';
import { isGitlab } from '../Provider/utils/vcsType';

export class YML {
private static transform(config: ConfigYAML): ConfigYAML {
Expand All @@ -14,11 +19,14 @@ export class YML {
);
if (!validRequiredArgs)
throw `please fill all required fields ${REQUIRED_YAML_ARGS.join(', ')}`;
const validRequiredProviderArgs = REQUIRED_YAML_PROVIDER_ARGS.every(
const requiredYamlProviderArgs = isGitlab(config.repo.url)
? REQUIRED_GITLAB_YAML_PROVIDER_ARGS
: REQUIRED_GITHUB_YAML_PROVIDER_ARGS;
const validRequiredProviderArgs = requiredYamlProviderArgs.every(
(el) => config.repo[el] != undefined || config.repo[el] != null,
);
if (!validRequiredProviderArgs)
throw `please fill all required fields ${REQUIRED_YAML_PROVIDER_ARGS.join(', ')}`;
throw `please fill all required fields ${requiredYamlProviderArgs.join(', ')}`;

return {
...config,
Expand Down
4 changes: 3 additions & 1 deletion src/Config/configBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AppConfig, ConfigArgument, ProviderConfig } from './@types';
import { ConfigArgumentGitlab } from './@types/configArgument';
import { YML } from './YML';

const buildYMLConfig = async (args: ConfigArgument) => {
Expand All @@ -7,7 +8,7 @@ const buildYMLConfig = async (args: ConfigArgument) => {
};

export const buildProviderConfig = async (
arg: ConfigArgument,
arg: ConfigArgumentGitlab,
): Promise<ProviderConfig> => {
const configFile = await buildYMLConfig(arg);

Expand All @@ -16,6 +17,7 @@ export const buildProviderConfig = async (
repoUrl: configFile?.repo.url || arg.url,
prId: configFile?.repo.pr || arg.pr,
removeOldComment: configFile?.repo.removeOldComment || arg.removeOldComment,
gitlabProjectId: configFile?.repo.gitlabProjectId || arg.gitlabProjectId,
};
};

Expand Down
22 changes: 17 additions & 5 deletions src/Config/constants/required.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
import { ConfigArgument } from '..';
import { ConfigArgumentGitlab } from '../@types/configArgument';
import { ConfigYAML } from '../@types/configYAML';

type RequiredArgs = (keyof ConfigArgument)[];
export const REQUIRED_ARGS: RequiredArgs = ['url', 'pr', 'buildLogFile', 'token'];
export type RequiredArgs = (keyof ConfigArgumentGitlab)[];
export const REQUIRED_ARGS_GITHUB: RequiredArgs = ['url', 'pr', 'buildLogFile', 'token'];
export const REQUIRED_ARGS_GITLAB: RequiredArgs = [
'url',
'pr',
'buildLogFile',
'token',
'gitlabProjectId',
];

type RequiredYamlArgs = (keyof ConfigYAML)[];
export const REQUIRED_YAML_ARGS: RequiredYamlArgs = ['repo', 'buildLogFiles'];

type RequiredYamlProviderArgs = (keyof ConfigYAML['repo'])[];
export const REQUIRED_YAML_PROVIDER_ARGS: RequiredYamlProviderArgs = [
export type RequiredYamlProviderArgs = (keyof ConfigYAML['repo'])[];
export const REQUIRED_GITHUB_YAML_PROVIDER_ARGS: RequiredYamlProviderArgs = [
'url',
'pr',
'token',
];
export const REQUIRED_GITLAB_YAML_PROVIDER_ARGS: RequiredYamlProviderArgs = [
'url',
'pr',
'token',
Expand Down
Loading