Skip to content

Commit

Permalink
fix: support auto trigger for git push code & update test case
Browse files Browse the repository at this point in the history
  • Loading branch information
LinLzis authored and Yuga Sun committed Oct 27, 2020
1 parent bd7b59e commit c49bea7
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 8 deletions.
136 changes: 132 additions & 4 deletions __tests__/ci/ci.req.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('Coding CI', () => {
ProjectId: projectId,
Name: 'slsplus-cli-test',
ExecuteIn: 'CVM',
TriggerMethodList: ['REF_CHANGE', 'MR_CHANGE'],
TriggerMethodList: [],
HookType: 'DEFAULT',
JenkinsFileFromType: 'STATIC',
JenkinsFileStaticContent: expect.any(String),
Expand Down Expand Up @@ -93,7 +93,7 @@ describe('Coding CI', () => {
ProjectId: projectId,
Name: 'slsplus-cli-test',
ExecuteIn: 'CVM',
TriggerMethodList: ['REF_CHANGE', 'MR_CHANGE'],
TriggerMethodList: [],
HookType: 'DEFAULT',
JenkinsFileFromType: 'STATIC',
JenkinsFileStaticContent: expect.any(String),
Expand Down Expand Up @@ -137,7 +137,7 @@ describe('Coding CI', () => {
ProjectId: projectId,
Name: 'slsplus-cli-test',
ExecuteIn: 'CVM',
TriggerMethodList: ['REF_CHANGE', 'MR_CHANGE'],
TriggerMethodList: [],
HookType: 'DEFAULT',
JenkinsFileFromType: 'STATIC',
JenkinsFileStaticContent: expect.any(String),
Expand Down Expand Up @@ -211,7 +211,7 @@ describe('Coding CI', () => {
ProjectId: projectId,
Name: 'slsplus-cli-test-git',
ExecuteIn: 'CVM',
TriggerMethodList: ['REF_CHANGE', 'MR_CHANGE'],
TriggerMethodList: [],
HookType: 'DEFAULT',
JenkinsFileFromType: 'STATIC',
JenkinsFileStaticContent: expect.any(String),
Expand Down Expand Up @@ -248,4 +248,132 @@ describe('Coding CI', () => {
],
});
});

it('[createCodingCIJobReq using git with auto trigger by target branch] should get right request data', async () => {
const req = createCodingCIJobReq({
jobName: 'slsplus-cli-test-git',
projectId,
envs: credentialEnvs,
useGit: true,
gitBranch: 'dev',
autoTriggerRuleOptions: {
hookType: 'DEFAULT',
branchRegex: '',
branchSelector: 'test-branch',
},
});
expect(req).toEqual({
ProjectId: projectId,
Name: 'slsplus-cli-test-git',
ExecuteIn: 'CVM',
HookType: 'DEFAULT',
TriggerMethodList: ['REF_CHANGE', 'MR_CHANGE'],
BranchSelector: 'test-branch',
JenkinsFileFromType: 'STATIC',
JenkinsFileStaticContent: expect.any(String),
AutoCancelSameRevision: true,
AutoCancelSameMergeRequest: true,
TriggerRemind: 'ALWAYS',
JobFromType: 'SERVERLESS',
DepotType: 'NONE',
DepotId: 0,
EnvList: credentialEnvs,
});
});

it('[createCodingCIJobReq using git with auto trigger by all branch] should get right request data', async () => {
const req = createCodingCIJobReq({
jobName: 'slsplus-cli-test-git',
projectId,
envs: credentialEnvs,
useGit: true,
gitBranch: 'dev',
autoTriggerRuleOptions: {
hookType: 'BRANCH',
branchRegex: '^refs/heads/.+$',
branchSelector: '',
},
});
expect(req).toEqual({
ProjectId: projectId,
Name: 'slsplus-cli-test-git',
ExecuteIn: 'CVM',
HookType: 'BRANCH',
TriggerMethodList: ['REF_CHANGE', 'MR_CHANGE'],
BranchRegex: '^refs/heads/.+$',
JenkinsFileFromType: 'STATIC',
JenkinsFileStaticContent: expect.any(String),
AutoCancelSameRevision: true,
AutoCancelSameMergeRequest: true,
TriggerRemind: 'ALWAYS',
JobFromType: 'SERVERLESS',
DepotType: 'NONE',
DepotId: 0,
EnvList: credentialEnvs,
});
});

it('[createCodingCIJobReq using git with auto trigger by new tag] should get right request data', async () => {
const req = createCodingCIJobReq({
jobName: 'slsplus-cli-test-git',
projectId,
envs: credentialEnvs,
useGit: true,
gitBranch: 'dev',
autoTriggerRuleOptions: {
hookType: 'TAG',
branchRegex: '^refs/tags/.+$',
branchSelector: '',
},
});
expect(req).toEqual({
ProjectId: projectId,
Name: 'slsplus-cli-test-git',
ExecuteIn: 'CVM',
HookType: 'TAG',
TriggerMethodList: ['REF_CHANGE', 'MR_CHANGE'],
BranchRegex: '^refs/tags/.+$',
JenkinsFileFromType: 'STATIC',
JenkinsFileStaticContent: expect.any(String),
AutoCancelSameRevision: true,
AutoCancelSameMergeRequest: true,
TriggerRemind: 'ALWAYS',
JobFromType: 'SERVERLESS',
DepotType: 'NONE',
DepotId: 0,
EnvList: credentialEnvs,
});
});

it('[createCodingCIJobReq using git with auto trigger by custom regex rule] should get right request data', async () => {
const req = createCodingCIJobReq({
jobName: 'slsplus-cli-test-git',
projectId,
envs: credentialEnvs,
useGit: true,
gitBranch: 'dev',
autoTriggerRuleOptions: {
hookType: 'CUSTOM',
branchRegex: '^refs/heads/master$',
branchSelector: '',
},
});
expect(req).toEqual({
ProjectId: projectId,
Name: 'slsplus-cli-test-git',
ExecuteIn: 'CVM',
HookType: 'CUSTOM',
TriggerMethodList: ['REF_CHANGE', 'MR_CHANGE'],
BranchRegex: '^refs/heads/master$',
JenkinsFileFromType: 'STATIC',
JenkinsFileStaticContent: expect.any(String),
AutoCancelSameRevision: true,
AutoCancelSameMergeRequest: true,
TriggerRemind: 'ALWAYS',
JobFromType: 'SERVERLESS',
DepotType: 'NONE',
DepotId: 0,
EnvList: credentialEnvs,
});
});
});
7 changes: 5 additions & 2 deletions src/ci/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ function createCodingCIJobReq({
useGit = false,
gitBranch = 'master',
warmUp = false,
autoTriggerRuleOptions,
}: CreateCodingCIJobOptions): CreateCodingCIJobRequest {
if (!pipeline) {
pipeline = new Pipeline();
Expand Down Expand Up @@ -172,8 +173,10 @@ function createCodingCIJobReq({
req.ProjectId = projectId;
req.Name = jobName;
req.ExecuteIn = 'CVM';
req.TriggerMethodList = ['REF_CHANGE', 'MR_CHANGE'];
req.HookType = 'DEFAULT';
req.TriggerMethodList = autoTriggerRuleOptions ? ['REF_CHANGE', 'MR_CHANGE'] : [];
req.HookType = autoTriggerRuleOptions?.hookType || 'DEFAULT';
req.BranchSelector = autoTriggerRuleOptions?.branchSelector || undefined;
req.BranchRegex = autoTriggerRuleOptions?.branchRegex || undefined;
req.JenkinsFileFromType = 'STATIC';
req.JenkinsFileStaticContent = pipeline.toString();
req.AutoCancelSameRevision = true;
Expand Down
12 changes: 10 additions & 2 deletions src/typings/ci.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,12 @@ export interface CIParseOptions {
layerOptions?: LayerOptions;
}

export interface TriggerRuleOptions {
hookType: string;
branchRegex?: string;
branchSelector?: string;
}

export interface CreateCodingCIJobOptions {
// 构建计划名称
jobName: string;
Expand Down Expand Up @@ -316,6 +322,8 @@ export interface CreateCodingCIJobOptions {
needBuild?: boolean;
// 是否需要预热云函数
warmUp?: boolean;
// 开启自动触发构建,定义触发构建的规则
autoTriggerRuleOptions?: TriggerRuleOptions;
}

export interface CreateCodingCIJobRequest {
Expand Down Expand Up @@ -386,12 +394,12 @@ export interface CreateCodingCIJobRequest {
/**
* hookType 为 DEFAULT 时须指定
*/
BranchSelector: string;
BranchSelector?: string;

/**
* hookType 为 CUSTOME 时须指定
*/
BranchRegex: string;
BranchRegex?: string;

/**
* JenkinsFileFromType 为 SCM 必填
Expand Down

0 comments on commit c49bea7

Please sign in to comment.