1- const { File } = require ( './file' ) ;
21const { Repository } = require ( './github' ) ;
3- const { Ubuntu } = require ( './ubuntu ' ) ;
2+ const handlers = require ( './handlers ' ) ;
43
5- // load environment variables from .env file if not running in production
6- if ( process . env . NODE_ENV !== 'production' ) {
7- require ( 'dotenv' ) . config ( ) ;
8- }
9-
10- // Check if environment variable exists, otherwise set default
4+ /**
5+ * Raw configuration.
6+ */
117const RAW_CONFIG = {
128 TYPE : process . env . TYPE || null ,
139 FILE : process . env . FILE || null ,
14- KEYS : process . env . KEYS || null ,
1510 BRANCH_NAME : process . env . BRANCH_NAME || process . env . TYPE ,
1611 BRANCH_PREFIX : process . env . BRANCH_PREFIX || 'update' ,
1712 REPOSITORY : process . env . REPOSITORY || process . env . GITHUB_REPOSITORY ,
@@ -30,45 +25,22 @@ const CONFIG = {};
3025const processConfig = ( ) => {
3126 for ( const [ key , value ] of Object . entries ( RAW_CONFIG ) ) {
3227 if ( ! value ) {
33- if ( value === 'USERNAME' || value === 'PASSWORD' ) continue ;
28+ if ( key === 'USERNAME' || key === 'PASSWORD' ) {
29+ throw new Error ( `Invalid USERNAME or PASSWORD!` ) ;
30+ } ;
3431 throw new Error (
3532 `Invalid configuration value: ${ value } for ${ key } .` ,
3633 ) ;
3734 }
38- switch ( key ) {
39- case 'KEYS' :
40- CONFIG [ key ] = value . split ( ',' ) ;
41- break ;
42- default :
43- CONFIG [ key ] = value ;
44- }
35+ CONFIG [ key ] = value ;
4536 }
4637} ;
4738
48- const handleUbuntu = async ( ) => {
49- const filterValues = {
50- cloud : process . env . CLOUD || null ,
51- zone : process . env . ZONE || null ,
52- version : process . env . VERSION || null ,
53- architecture : process . env . ARCHITECTURE || null ,
54- instanceType : process . env . INSTANCE_TYPE || null ,
55- release : process . env . RELEASE || null ,
56- } ;
57-
58- const filter = { } ;
59- Object . keys ( filterValues ) . forEach ( ( value ) => {
60- if ( filterValues [ value ] ) filter [ value ] = filterValues [ value ] ;
61- } ) ;
62- const ubuntu = new Ubuntu ( filter ) ;
63- const latestUbuntu = await ubuntu . latest ;
64- const keyValuePairs = { } ;
65- CONFIG . KEYS . forEach ( ( key ) => {
66- keyValuePairs [ key ] = latestUbuntu . id ;
67- } ) ;
68-
69- return keyValuePairs ;
70- } ;
71-
39+ /**
40+ * Clone repository and checkout the feature branch
41+ * @param {string } branchName - feature branch name
42+ * @return {object }
43+ */
7244const initializeRepo = async ( branchName ) => {
7345 const repository = new Repository ( {
7446 repository : CONFIG . REPOSITORY ,
@@ -81,57 +53,37 @@ const initializeRepo = async (branchName) => {
8153 return repository ;
8254} ;
8355
84- const throwError = ( resolved ) => {
85- resolved . forEach ( ( process ) => {
86- if ( process . status === 'rejected' ) {
87- console . error ( 'An error has occurred.' ) ;
88- throw new Error ( JSON . stringify ( process . reason ) ) ;
89- }
90- } ) ;
91- } ;
92-
93- const TYPE_PROCESSORS = {
94- ubuntu : handleUbuntu ,
56+ /**
57+ * Define handle types
58+ */
59+ const PROCESSOR_TYPES = {
60+ ubuntu : handlers . handleUbuntu ,
61+ helmfile : handlers . handleHelmfile ,
9562} ;
9663
97- const main = async ( ) => {
64+ /**
65+ * Main function
66+ */
67+ ( async ( ) => {
9868 try {
9969 processConfig ( ) ;
10070 } catch ( err ) {
10171 console . error ( 'Config processor has failed!' , err ) ;
10272 process . exit ( 1 ) ;
10373 }
104- const processor = await Promise . allSettled ( [
105- TYPE_PROCESSORS [ CONFIG . TYPE ] ( ) ,
106- // TODO: option for supplying custom branch name and custom prefix
107- initializeRepo ( `${ CONFIG . BRANCH_PREFIX } /${ CONFIG . BRANCH_NAME } ` ) ,
108- ] ) ;
109- throwError ( processor ) ;
110- console . log ( `Successfully executed processor ${ CONFIG . TYPE } .` ) ;
111-
112- const repository = processor [ 1 ] . value ;
113- const filePath = `${ repository . path } /${ CONFIG . FILE } ` ;
114- const keyValuePairs = processor [ 0 ] . value ;
115-
116- try {
117- const file = new File ( { filePath, keyValuePairs } ) ;
118- await file . run ( ) ;
119- } catch ( err ) {
120- console . error ( 'Updating file has failed!' , err ) ;
121- process . exit ( 1 ) ;
122- }
123- console . log ( `File "${ filePath } " has been successfully updated.` ) ;
12474
12575 try {
76+ const repository = await initializeRepo (
77+ `${ CONFIG . BRANCH_PREFIX } /${ CONFIG . BRANCH_NAME } ` ,
78+ ) ;
79+ const fullFilePath = `${ repository . path } /${ CONFIG . FILE } ` ;
80+ await PROCESSOR_TYPES [ CONFIG . TYPE ] ( fullFilePath ) ;
81+ console . log ( `Successfully executed processor ${ CONFIG . TYPE } .` ) ;
12682 await repository . push ( `update ${ CONFIG . TYPE } ` ) ;
12783 await repository . createPullRequest ( ) ;
84+ console . log ( 'Pull-request created.' ) ;
12885 } catch ( err ) {
129- console . error ( `Opening pull request with changes has failed! ` , err ) ;
86+ console . error ( `${ CONFIG . TYPE } processor failed. ` , err ) ;
13087 process . exit ( 1 ) ;
13188 }
132- console . log (
133- `Successfully pushed branch "${ repository . branchName } " to remote.` ,
134- ) ;
135- } ;
136-
137- main ( ) ;
89+ } ) ( ) ;
0 commit comments