Skip to content

Commit

Permalink
Ready for test
Browse files Browse the repository at this point in the history
  • Loading branch information
miha42-github committed Jan 14, 2024
1 parent 928d743 commit 5526baa
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 126 deletions.
10 changes: 5 additions & 5 deletions cli/mrcli-company.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ if (myArgs.report) {
stat = foundObjects[1]
results = foundObjects[2]
} else if (myArgs.update) {
const lockResp = companyCtl.checkForLock()
const lockResp = await companyCtl.checkForLock()
if(lockResp[0]) {
console.log(`ERROR: ${lockResp[1].status_msg}`)
process.exit(-1)
Expand All @@ -233,7 +233,7 @@ if (myArgs.report) {
}
// TODO: Need to reimplement the below to account for GitHub
} else if (myArgs.delete) {
const lockResp = companyCtl.checkForLock()
const lockResp = await companyCtl.checkForLock()
if(lockResp[0]) {
console.log(`ERROR: ${lockResp[1].status_msg}`)
process.exit(-1)
Expand Down Expand Up @@ -261,7 +261,7 @@ if (myArgs.report) {
process.exit(-1)
}
} else if (myArgs.add_wizard) {
const lockResp = companyCtl.checkForLock()
const lockResp = await companyCtl.checkForLock()
if(lockResp[0]) {
console.log(`ERROR: ${lockResp[1].status_msg}`)
process.exit(-1)
Expand All @@ -270,10 +270,10 @@ if (myArgs.report) {
const newCompany = new AddCompany(myEnv, {github: gitHubCtl, interaction: interactionCtl, company: companyCtl, user: userCtl})
const result = await newCompany.wizard()
if(result[0]) {
console.log('SUCCESS: Created new company in the backend')
console.log(`SUCCESS: ${result[1].status_msg}`)
process.exit(0)
} else {
console.log(`ERROR: Failed to create company object with:, ${result[2]}`)
console.log(`ERROR: ${result[1].status_msg}`)
process.exit(-1)
}
} else if (myArgs.reset_by_type) {
Expand Down
12 changes: 6 additions & 6 deletions cli/mrcli-interaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ if (myArgs.report) {
stat = foundObjects[1]
results = foundObjects[2]
} else if (myArgs.update) {
const lockResp = interactionCtl.checkForLock()
const lockResp = await interactionCtl.checkForLock()
if(lockResp[0]) {
console.log(`ERROR: ${lockResp[1].status_msg}`)
process.exit(-1)
Expand All @@ -202,7 +202,7 @@ if (myArgs.report) {
process.exit(-1)
}
} else if (myArgs.delete) {
const lockResp = interactionCtl.checkForLock()
const lockResp = await interactionCtl.checkForLock()
if(lockResp[0]) {
console.log(`ERROR: ${lockResp[1].status_msg}`)
process.exit(-1)
Expand All @@ -226,22 +226,22 @@ if (myArgs.report) {
process.exit(-1)
}
} else if (myArgs.add_wizard) {
const lockResp = interactionCtl.checkForLock()
const lockResp = await interactionCtl.checkForLock()
if(lockResp[0]) {
console.log(`ERROR: ${lockResp[1].status_msg}`)
process.exit(-1)
}
const newInteraction = new AddInteraction(myEnv, {github: gitHubCtl, interaction: interactionCtl, company: companyCtl, user: userCtl})
const result = await newInteraction.wizard()
if(result[0]) {
console.log(`SUCCESS: Added new interaction object(s).`)
console.log(`SUCCESS: ${result[1].status_msg}`)
process.exit(0)
} else {
console.log(`ERROR: Unable to add interaction object due to [${result[1].status_msg}].`)
console.log(`ERROR: ${result[1].status_msg}.`)
process.exit(-1)
}
} else if (myArgs.reset_by_type) {
console.error('ERROR (%d): Reset by type not implemented.', -1)
console.log('ERROR: Reset by type not implemented.')
process.exit(-1)
const lockResp = interactionCtl.checkForLock()
if(lockResp[0]) {
Expand Down
4 changes: 2 additions & 2 deletions cli/mrcli-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,11 +420,11 @@ let spinner
// Save the companies to GitHub
spinner = ora(chalk.bold.blue('Saving companies to GitHub ... '))
spinner.start() // Start the spinner
const companyResp = await companyCtl.createObj2([owningCompany, firstCompany])
const companyResp = await companyCtl.createObj([owningCompany, firstCompany])
spinner.stop() // Stop the spinner
// If the company creation failed then exit
if(!companyResp[0]) {
console.log(chalk.red.bold(`FAILED: ${companyResp[1].status_msg}, you may need to clean up the repo.`))
console.log(chalk.red.bold(`FAILED: ${companyResp[1].status_msg}, you may need to clean up the repository.`))
process.exit(-1)
}
cliOutput.printLine()
Expand Down
2 changes: 1 addition & 1 deletion cli/mrcli.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import program from 'commander'

program
.name('mrcli')
.version('0.4.34')
.version('0.4.35')
.description('mediumroast.io command line interface')
.command('setup', 'setup the mediumroast.io system via the command line').alias('f')
.command('interaction', 'manage and report on mediumroast.io interaction objects').alias('i')
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mediumroast_js",
"version": "0.4.34",
"version": "0.4.35",
"description": "A Command Line Interface (CLI) and Javascript SDK to interact with mediumroast.io.",
"main": "cli/mrcli.js",
"scripts": {
Expand Down
27 changes: 21 additions & 6 deletions src/api/gitHubServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,24 @@ class baseObjects {
const allObjectsResp = await this.serverCtl.readObjects(this.objType)
allObjects = allObjectsResp[2].mrJson
}
// If the length of allObjects is 0 then return an error
// This will occur when there are no objects of the type in the backend
if(allObjects.length === 0) {
return [false, {status_code: 404, status_msg: `no ${this.objType} found`}, null]
}
for(const obj in allObjects) {
let currentObject
attribute == 'name' ? currentObject = allObjects[obj][attribute].toLowerCase() : null
attribute == 'name' ? currentObject = allObjects[obj][attribute].toLowerCase() : currentObject = allObjects[obj][attribute]
if(currentObject === value) {
myObjects.push(allObjects[obj])
}
}
return [true, `SUCCESS: found all objects where ${attribute} = ${value}`, myObjects]

if (myObjects.length === 0) {
return [false, {status_code: 404, status_msg: `no ${this.objType} found where ${attribute} = ${value}`}, null]
} else {
return [true, `SUCCESS: found all objects where ${attribute} = ${value}`, myObjects]
}
}

/**
Expand All @@ -107,11 +117,11 @@ class baseObjects {
* @param {Array} objs - the objects to create in the backend
* @returns {Array} the results from the called function mrRest class
*/
async createObj(objs) {
return await this.serverCtl.createObjects(this.objType, objs)
}
// async createObj1(objs) {
// return await this.serverCtl.createObjects(this.objType, objs)
// }

async createObj2(objs) {
async createObj(objs) {
// Create the repoMetadata object
let repoMetadata = {
containers: {
Expand Down Expand Up @@ -361,6 +371,7 @@ class Companies extends baseObjects {
// Return the response
return [true, {status_code: 200, status_msg: `deleted company [${objName}] and all linked interactions`}, null]
}

}


Expand Down Expand Up @@ -397,6 +408,10 @@ class Interactions extends baseObjects {
}
return await super.deleteObj(objName, source)
}

async findByHash(hash) {
return this.findByX('file_hash', hash)
}
}

// Export classes for consumers
Expand Down
110 changes: 55 additions & 55 deletions src/api/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -855,63 +855,63 @@ class GitHubFunctions {
}


/**
* @function createObjects
* @description Creates objects in a specified container using the GitHub API.
* @async
* @param {string} containerName - The name of the container to create the objects in.
* @param {object} objs - The objects to create in the container.
* @returns {Promise<string>} A promise that resolves to the decoded contents of the object.
* @throws {Error} If an error occurs while getting the content or parsing it.
* @memberof GitHubFunctions
*/
async createObjects(containerName, objs) {
// Call the method above to check for a lock
const lockExists = await this.checkForLock(containerName)
// If the lock exists return an error
if(lockExists[0]) { return [false, `ERROR: The container [${containerName}] is locked unable to create objects.`] }


// Lock the container
const locked = await this.lockContainer(containerName)
// Check to see if the container was locked and return the error if not
if(!locked[0]) { return [false, `ERROR: Unable to lock [${containerName}] cannot create new objects.`, locked] }
const lockSha = locked[2].data.content.sha
// /**
// * @function createObjects
// * @description Creates objects in a specified container using the GitHub API.
// * @async
// * @param {string} containerName - The name of the container to create the objects in.
// * @param {object} objs - The objects to create in the container.
// * @returns {Promise<string>} A promise that resolves to the decoded contents of the object.
// * @throws {Error} If an error occurs while getting the content or parsing it.
// * @memberof GitHubFunctions
// */
// async createObjects(containerName, objs) {
// // Call the method above to check for a lock
// const lockExists = await this.checkForLock(containerName)
// // If the lock exists return an error
// if(lockExists[0]) { return [false, `ERROR: The container [${containerName}] is locked unable to create objects.`] }


// // Lock the container
// const locked = await this.lockContainer(containerName)
// // Check to see if the container was locked and return the error if not
// if(!locked[0]) { return [false, `ERROR: Unable to lock [${containerName}] cannot create new objects.`, locked] }
// const lockSha = locked[2].data.content.sha

// Call the method above createBranchFromMain to create a new branch
const branchCreated = await this.createBranchFromMain()
// Check to see if the branch was created
if(!branchCreated[0]) { return [false, `ERROR: Unable to create new branch`, branchCreated] }
const branchSha = branchCreated[2].data.object.sha
const branchName = branchCreated[2].data.ref

// Call the method above to read the objects
const readResponse = await this.readObjects(containerName)
// Check to see if the read was successful
if(!readResponse[0]) { return [false, `ERROR: unable to read the source objects [${containerName}/${this.objectFiles[containerName]}].`, readResponse] }
const objectSha = readResponse[2].data.sha

// Merge the objects with the updated object which are an array of objects
let mergedObjects = [...readResponse[2].mrJson, ...objs]


// Write the objects to the new branch
const writeResponse = await this.writeObject(containerName, mergedObjects, branchName, objectSha)
// Check to see if the write was successful and return the error if not
if(!writeResponse[0]) { return [false,`ERROR: Unable to write the objects to [${containerName}/${this.objectFiles[containerName]}].`, writeResponse] }

// Merge the branch to main
const mergeResponse = await this.mergeBranchToMain(branchName, branchSha)
// Check to see if the merge was successful and return the error if not
if(!mergeResponse[0]) { return [false,`ERROR: Unable to merge the branch to main.`, mergeResponse] }

// Unlock the container
const unlocked = await this.unlockContainer(containerName, lockSha)
if(!unlocked[0]) { return [false, `ERROR: Unable to unlock the container, objects may have been written please check [${containerName}] for objects and the lock file.`, unlocked] }
// // Call the method above createBranchFromMain to create a new branch
// const branchCreated = await this.createBranchFromMain()
// // Check to see if the branch was created
// if(!branchCreated[0]) { return [false, `ERROR: Unable to create new branch`, branchCreated] }
// const branchSha = branchCreated[2].data.object.sha
// const branchName = branchCreated[2].data.ref

// // Call the method above to read the objects
// const readResponse = await this.readObjects(containerName)
// // Check to see if the read was successful
// if(!readResponse[0]) { return [false, `ERROR: unable to read the source objects [${containerName}/${this.objectFiles[containerName]}].`, readResponse] }
// const objectSha = readResponse[2].data.sha

// // Merge the objects with the updated object which are an array of objects
// let mergedObjects = [...readResponse[2].mrJson, ...objs]


// // Write the objects to the new branch
// const writeResponse = await this.writeObject(containerName, mergedObjects, branchName, objectSha)
// // Check to see if the write was successful and return the error if not
// if(!writeResponse[0]) { return [false,`ERROR: Unable to write the objects to [${containerName}/${this.objectFiles[containerName]}].`, writeResponse] }

// // Merge the branch to main
// const mergeResponse = await this.mergeBranchToMain(branchName, branchSha)
// // Check to see if the merge was successful and return the error if not
// if(!mergeResponse[0]) { return [false,`ERROR: Unable to merge the branch to main.`, mergeResponse] }

// // Unlock the container
// const unlocked = await this.unlockContainer(containerName, lockSha)
// if(!unlocked[0]) { return [false, `ERROR: Unable to unlock the container, objects may have been written please check [${containerName}] for objects and the lock file.`, unlocked] }

// Return success with number of objects written
return [true, `SUCCESS: [${objs.length}] object(s) written to [${containerName}/${this.objectFiles[containerName]}].`, null]
}
// // Return success with number of objects written
// return [true, `SUCCESS: [${objs.length}] object(s) written to [${containerName}/${this.objectFiles[containerName]}].`, null]
// }
}

export default GitHubFunctions
26 changes: 22 additions & 4 deletions src/cli/companyWizard.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import ora from "ora"
import mrRest from "../api/scaffold.js"
import WizardUtils from "./commonWizard.js"
import CLIOutput from "./output.js"
import crypto from "node:crypto"
import axios from "axios"


Expand Down Expand Up @@ -652,6 +651,8 @@ class AddCompany {
creator: {consoleString: "", value: myUser.login}, // Set the creator to the GitHub user
creator_id: {consoleString: "", value: myUser.id}, // Set the creator to the GitHub user
creator_name: {consoleString: "", value: myUser.name}, // Set the creator to the GitHub user
creation_date: {consoleString: "", value: new Date().toISOString()}, // Set the creation date to now
modification_date: {consoleString: "", value: new Date().toISOString()}, // Set the modification date to now
region: {consoleString: "region (AMER, EMEA or APAC)", value:this.defaultValue},
company_type: {consoleString: "company type (e.g. Public, Private, etc.)", value:this.defaultValue},
industry: {consoleString: "industry description", value:this.defaultValue},
Expand Down Expand Up @@ -717,6 +718,14 @@ class AddCompany {
myCompany.name = tmpCompany.name
companyPrototype.name.value = myCompany.name

// Check to see if the company name is already in the system using findByName
const companyExists = await this.apiController.findByName(myCompany.name)
if (companyExists[0]) {
// Return the companyExists object with an error message
return [false,{status_code: 400, status_msg: `company [${myCompany.name}] already exists, duplicates not allowed, exiting.`}, companyExists[2]]
}


// Define the company type
const tmpCompanyType = await this.wutils.doList(
"What type of company is this?",
Expand Down Expand Up @@ -782,9 +791,18 @@ class AddCompany {

// Either return the company object or create it
if (createObj) {
console.log(chalk.blue.bold(`Saving company ${myCompany.name} to mediumroast.io ...`))
this.output.printLine()
return await this.apiController.createObj2([myCompany])
const spinner = ora(chalk.bold.blue(`Saving ${myCompany.name} to GitHub ... `))
spinner.start() // Start the spinner
const myCompanyResp = await this.apiController.createObj([myCompany])
spinner.stop() // Stop the spinner
// Check to see if the company was created
if (myCompanyResp[0]) {
this.output.printLine()
return [true,{status_code: 200, status_msg: `created [${myCompany.name}]`}, myCompanyResp[2]]
} else {
this.output.printLine()
return [false,{status_code: 400, status_msg: `unable to create [${myCompany.name}]`}, myCompanyResp[2]]
}
} else {
this.output.printLine()
return [true,{status_code: 200, status_msg: `Returning object for ${myCompany.name}`}, myCompany]
Expand Down
Loading

0 comments on commit 5526baa

Please sign in to comment.