@@ -16,6 +16,7 @@ const INSTALLATION_DEFAULTS = {
1616 MAKE_DEFAULT_RE : false ,
1717 RUN_DEMO_PIPELINE : true ,
1818 DEMO_PIPELINE_NAME : 'Codefresh-Runner Demo' ,
19+ PROJECT_NAME : 'Runner' ,
1920 CF_CONTEXT_NAME : 'cf-runner' ,
2021} ;
2122
@@ -24,10 +25,40 @@ const DefaultLogFormatter = 'plain';
2425const defaultOpenIssueMessage = 'If you had any issues with this process please report them at: ' +
2526 `${ colors . blue ( 'https://github.com/codefresh-io/cli/issues/new' ) } ` ;
2627
27- async function createTestPipeline ( runtimeName , pipelineName , pipelineCommands , progressReporter ) {
28+ async function _createRunnerProjectIfNotExists ( ) {
29+ const [ err , projectExists ] = await to ( sdk . projects . getByName ( { name : INSTALLATION_DEFAULTS . PROJECT_NAME } ) ) ;
30+ if ( _ . get ( err , 'message' , '' ) . includes ( 'not found' ) || ! projectExists ) {
31+ await sdk . projects . create ( { projectName : INSTALLATION_DEFAULTS . PROJECT_NAME } ) ;
32+ console . log ( `Project "${ colors . cyan ( INSTALLATION_DEFAULTS . PROJECT_NAME ) } " was created` ) ;
33+ } else {
34+ console . log ( `Project "${ colors . cyan ( INSTALLATION_DEFAULTS . PROJECT_NAME ) } " already exists` ) ;
35+ }
36+ }
37+
38+ async function getTestPipelineLink ( pipelineName , pipeline ) {
2839 const url = _ . get ( sdk , 'config.context.url' , 'https://g.codefresh.io' ) ;
29- console . log ( `Creating test pipeline with the name: "${ colors . cyan ( pipelineName ) } "` ) ;
30- const pipeline = await sdk . pipelines . create ( { metadata : { name : pipelineName } } ) ;
40+ let _pipeline = pipeline ;
41+ if ( ! _pipeline ) {
42+ const pipelines = await sdk . pipelines . list ( { id : `${ INSTALLATION_DEFAULTS . PROJECT_NAME } /${ pipelineName } ` } ) ;
43+ if ( _ . get ( pipelines , 'docs.length' ) ) {
44+ [ _pipeline ] = pipelines . docs ;
45+ }
46+ }
47+
48+ if ( _pipeline ) {
49+ const cleanPipelineName = _pipeline . metadata . name . replace ( `${ INSTALLATION_DEFAULTS . PROJECT_NAME } /` , '' ) ; // remove the project prefix
50+ return `${ url } /pipelines/edit/workflow?id=${ _pipeline . metadata . id } &pipeline=${ encodeURI ( cleanPipelineName ) } ` +
51+ `&projects=${ encodeURI ( INSTALLATION_DEFAULTS . PROJECT_NAME ) } ` ;
52+ }
53+
54+ return '' ;
55+ }
56+
57+ async function createTestPipeline ( runtimeName , pipelineName , pipelineCommands , progressReporter ) {
58+ await _createRunnerProjectIfNotExists ( ) ;
59+ console . log ( `Creating test pipeline with the name: "${ colors . cyan ( pipelineName ) } " ` +
60+ `in project "${ colors . cyan ( INSTALLATION_DEFAULTS . PROJECT_NAME ) } "` ) ;
61+ const pipeline = await sdk . pipelines . create ( { metadata : { name : `${ INSTALLATION_DEFAULTS . PROJECT_NAME } /${ pipelineName } ` } } ) ;
3162
3263 pipeline . spec . runtimeEnvironment = {
3364 name : runtimeName ,
@@ -42,7 +73,7 @@ async function createTestPipeline(runtimeName, pipelineName, pipelineCommands, p
4273 } ;
4374
4475 await sdk . pipelines . replace (
45- { name : pipelineName } ,
76+ { name : ` ${ INSTALLATION_DEFAULTS . PROJECT_NAME } / ${ pipelineName } ` } ,
4677 {
4778 kind : pipeline . kind ,
4879 spec : pipeline . spec ,
@@ -55,28 +86,17 @@ async function createTestPipeline(runtimeName, pipelineName, pipelineCommands, p
5586 await to ( progressReporter . report ( PIPELINE_CREATED , STATUSES . SUCCESS ) ) ;
5687 }
5788
58- const pipelineLink = ` ${ url } /pipelines/edit/workflow?id= ${ pipeline . metadata . id } &pipeline= ${ encodeURI ( pipeline . metadata . name ) } ` ;
89+ const pipelineLink = await getTestPipelineLink ( undefined , pipeline ) ;
5990 console . log ( `Created test pipeline with the name "${ colors . cyan ( pipelineName ) } ". Watch it here: ${ colors . blue ( pipelineLink ) } ` ) ;
6091
6192 return pipeline ;
6293}
6394
64- async function getTestPipelineLink ( pipelineName ) {
65- const url = _ . get ( sdk , 'config.context.url' , 'https://g.codefresh.io' ) ;
66- const pipelines = await sdk . pipelines . list ( { id : pipelineName } ) ;
67- if ( _ . get ( pipelines , 'docs.length' ) ) {
68- const pipeline = pipelines . docs [ 0 ] ;
69- return `${ url } /pipelines/edit/workflow?id=${ pipeline . metadata . id } &pipeline=${ encodeURI ( pipeline . metadata . name ) } ` ;
70- }
71- return '' ;
72- }
73-
7495async function getTestPipeline ( pipelineName ) {
75- const url = _ . get ( sdk , 'config.context.url' , 'https://g.codefresh.io' ) ;
76- const pipelines = await sdk . pipelines . list ( { id : pipelineName } ) ;
96+ const pipelines = await sdk . pipelines . list ( { id : `${ INSTALLATION_DEFAULTS . PROJECT_NAME } /${ pipelineName } ` } ) ;
7797 if ( _ . get ( pipelines , 'docs.length' ) ) {
7898 const pipeline = pipelines . docs [ 0 ] ;
79- const pipelineLink = ` ${ url } /pipelines/edit/workflow?id= ${ pipeline . metadata . id } &pipeline= ${ encodeURI ( pipeline . metadata . name ) } ` ;
99+ const pipelineLink = await getTestPipelineLink ( undefined , pipeline ) ;
80100 console . log ( `Test pipeline with the name: "${ colors . cyan ( pipelineName ) } " already exists.` +
81101 ` Watch it here: ${ colors . blue ( pipelineLink ) } ` ) ;
82102 return pipeline ;
@@ -86,6 +106,24 @@ async function getTestPipeline(pipelineName) {
86106 return null ;
87107}
88108
109+ async function updateTestPipelineRuntime ( pipeline , runtimeName ) {
110+ // update pipeline runtime
111+ const _pipeline = pipeline ;
112+ _pipeline . spec . runtimeEnvironment = {
113+ name : runtimeName ,
114+ } ;
115+
116+ await sdk . pipelines . replace (
117+ { name : _pipeline . metadata . name } ,
118+ {
119+ kind : _pipeline . kind ,
120+ spec : _pipeline . spec ,
121+ metadata : _pipeline . metadata ,
122+ version : _pipeline . version ,
123+ } ,
124+ ) ;
125+ }
126+
89127async function executeTestPipeline ( runtimeName , pipeline , progressReporter ) {
90128 const url = _ . get ( sdk , 'config.context.url' , 'https://g.codefresh.io' ) ;
91129 const pipelineName = _ . get ( pipeline , 'metadata.name' ) ;
@@ -226,6 +264,7 @@ module.exports = {
226264 getRuntimeVersion,
227265 createTestPipeline,
228266 getTestPipeline,
267+ updateTestPipelineRuntime,
229268 executeTestPipeline,
230269 createProgressBar,
231270 getTestPipelineLink,
0 commit comments