From c49bea794c0f2ecba78383561d7678ae94c6f077 Mon Sep 17 00:00:00 2001 From: Avril Li Date: Tue, 27 Oct 2020 16:24:28 +0800 Subject: [PATCH] fix: support auto trigger for git push code & update test case --- __tests__/ci/ci.req.test.ts | 136 ++++++++++++++++++++++++++++++++++-- src/ci/actions.ts | 7 +- src/typings/ci.d.ts | 12 +++- 3 files changed, 147 insertions(+), 8 deletions(-) diff --git a/__tests__/ci/ci.req.test.ts b/__tests__/ci/ci.req.test.ts index 98a0fe6..2d28523 100644 --- a/__tests__/ci/ci.req.test.ts +++ b/__tests__/ci/ci.req.test.ts @@ -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), @@ -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), @@ -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), @@ -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), @@ -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, + }); + }); }); diff --git a/src/ci/actions.ts b/src/ci/actions.ts index ecae7d0..1f7242e 100644 --- a/src/ci/actions.ts +++ b/src/ci/actions.ts @@ -56,6 +56,7 @@ function createCodingCIJobReq({ useGit = false, gitBranch = 'master', warmUp = false, + autoTriggerRuleOptions, }: CreateCodingCIJobOptions): CreateCodingCIJobRequest { if (!pipeline) { pipeline = new Pipeline(); @@ -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; diff --git a/src/typings/ci.d.ts b/src/typings/ci.d.ts index 5f87488..f679b3d 100644 --- a/src/typings/ci.d.ts +++ b/src/typings/ci.d.ts @@ -289,6 +289,12 @@ export interface CIParseOptions { layerOptions?: LayerOptions; } +export interface TriggerRuleOptions { + hookType: string; + branchRegex?: string; + branchSelector?: string; +} + export interface CreateCodingCIJobOptions { // 构建计划名称 jobName: string; @@ -316,6 +322,8 @@ export interface CreateCodingCIJobOptions { needBuild?: boolean; // 是否需要预热云函数 warmUp?: boolean; + // 开启自动触发构建,定义触发构建的规则 + autoTriggerRuleOptions?: TriggerRuleOptions; } export interface CreateCodingCIJobRequest { @@ -386,12 +394,12 @@ export interface CreateCodingCIJobRequest { /** * hookType 为 DEFAULT 时须指定 */ - BranchSelector: string; + BranchSelector?: string; /** * hookType 为 CUSTOME 时须指定 */ - BranchRegex: string; + BranchRegex?: string; /** * JenkinsFileFromType 为 SCM 必填