Skip to content

Commit

Permalink
Merge pull request #60 from mediumroast/alpha_2_object_updates
Browse files Browse the repository at this point in the history
Alpha 2 object updates
  • Loading branch information
miha42-github authored Jan 2, 2024
2 parents 55ea8ac + 5e6a835 commit 479c4e8
Show file tree
Hide file tree
Showing 13 changed files with 160 additions and 291 deletions.
11 changes: 6 additions & 5 deletions cli/mrcli-company.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Environmentals from '../src/cli/env.js'
import CLIOutput from '../src/cli/output.js'
import FilesystemOperators from '../src/cli/filesystem.js'
import ArchivePackage from '../src/cli/archive.js'
import ora from 'ora'

// Related object type
const objectType = 'Companies'
Expand Down Expand Up @@ -197,17 +198,17 @@ if (myArgs.report) {
success = foundObjects[0]
stat = foundObjects[1]
results = foundObjects[2]
// TODO: Need to reimplment the below to account for GitHub
} else if (myArgs.update) {
console.error('ERROR (%d): Update not implemented.', -1)
process.exit(-1)
const myCLIObj = JSON.parse(myArgs.update)
const mySpinner = new ora(`Updating company [${myCLIObj.name}] object ...`)
mySpinner.start()
const [success, stat, resp] = await companyCtl.updateObj(myCLIObj)
mySpinner.stop()
if(success) {
console.log(`SUCCESS: processed update to company object.`)
console.log(`SUCCESS: ${stat.status_msg}`)
process.exit(0)
} else {
console.error('ERROR (%d): Unable to update company object.', -1)
console.log(`ERROR: ${stat.status_msg}`)
process.exit(-1)
}
// TODO: Need to reimplement the below to account for GitHub
Expand Down
2 changes: 1 addition & 1 deletion cli/mrcli-github.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import chalk from 'chalk'
import GitHubFunctions from '../src/api/github.js'
import * as progress from 'cli-progress'

// console.log(chalk.bold.yellow('NOTICE: This CLI is being deprecated and will be removed in a future release.'))
console.log(chalk.bold.yellow('NOTICE: This CLI is being deprecated and will be removed in a future release.'))

/*
-----------------------------------------------------------------------
Expand Down
10 changes: 6 additions & 4 deletions cli/mrcli-interaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import Environmentals from '../src/cli/env.js'
import CLIOutput from '../src/cli/output.js'
import FilesystemOperators from '../src/cli/filesystem.js'
import ArchivePackage from '../src/cli/archive.js'
import ora from 'ora'

// External modules
import chalk from 'chalk'
Expand Down Expand Up @@ -182,15 +183,16 @@ if (myArgs.report) {
stat = foundObjects[1]
results = foundObjects[2]
} else if (myArgs.update) {
console.error('ERROR (%d): Update not implemented.', -1)
process.exit(-1)
const myCLIObj = JSON.parse(myArgs.update)
const mySpinner = new ora(`Updating interaction [${myCLIObj.name}] object ...`)
mySpinner.start()
const [success, stat, resp] = await interactionCtl.updateObj(myCLIObj)
mySpinner.stop()
if(success) {
console.log(`SUCCESS: processed update to interaction object.`)
console.log(`SUCCESS: ${stat.status_msg}`)
process.exit(0)
} else {
console.error('ERROR (%d): Unable to update interaction object.', -1)
console.log(`ERROR: ${stat.status_msg}`)
process.exit(-1)
}
} else if (myArgs.delete) {
Expand Down
43 changes: 22 additions & 21 deletions cli/mrcli-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ let gitHubCtl = await confirmGitHubOrg(myConfig.GitHub.token, myEnv)

// Capture the GitHub organization name should we need it later
myConfig.GitHub.org = gitHubCtl.orgName
// TODO: Add the GitHub organization Identifier to the config file

cliOutput.printLine()
/* ------ End GitHub org confirmation ------ */
Expand Down Expand Up @@ -391,7 +392,7 @@ cliOutput.printLine()

// Create needed objects
let companies = []
let studies = []
// let studies = []

// Create the owning company
console.log(chalk.blue.bold('Creating your owning company'))
Expand Down Expand Up @@ -420,29 +421,29 @@ const firstCompany = firstCompanyResp[2]
// NOTE: For the first release studies aren't needed, therefore we're commenting out anything related to them
// const linkedCompanies = companyCtl.linkObj([owningCompany, firstCompany])

// Create a default study for interactions and companies to use
// process.stdout.write(chalk.blue.bold(`Creating default study ... `))
// let myStudy = {
// name: 'Default Study',
// description: 'A placeholder study to ensure that interactions are able to have something to link to',
// public: false,
// groups: 'default:default',
// document: {},
// linked_companies: linkedCompanies,
// linked_interactions: {}
// }
// console.log(chalk.bold.green('Ok'))

// Assign the study to the studies array
// studies = [myStudy]
// Create a default study for good housekeeping
process.stdout.write(chalk.blue.bold(`Creating default study ... `))
let myStudy = {
name: 'Default Study',
description: 'A placeholder study to ensure that interactions are able to have something to link to',
public: false,
groups: 'default:default',
document: {},
linked_companies: linkedCompanies,
linked_interactions: {}
}
console.log(chalk.bold.green('Ok'))

// NOTE: Since studies aren't needed in the alpha_2 series of releases we will comment things out related to them.
// Additionally, in alpha_3 we'll determine if we need to create a default study or not. So leaving this
// code in place for now.
// Obtain the link object for studies
// const linkedStudies = studyCtl.linkObj(studies)
const linkedStudies = {}
// const linkedStudies = studyCtl.linkObj([myStudy])
// const linkedStudies = {}

// Link the study to the companies
owningCompany.linked_studies = linkedStudies
firstCompany.linked_studies = linkedStudies
// owningCompany.linked_studies = linkedStudies
// firstCompany.linked_studies = linkedStudies
// companies = [owningCompany, firstCompany]

// Set up the spinner
Expand All @@ -464,7 +465,7 @@ if(!companyResp[0]) {
// Save the default study to GitHub
// spinner = ora(chalk.bold.blue('Saving study to GitHub ... '))
// spinner.start() // Start the spinner
// const studyResp = await studyCtl.createObj(studies)
// const studyResp = await studyCtl.createObj([myStudy])
// spinner.stop() // Stop the spinner
// // If the study creation failed then exit
// if(!studyResp[0]) {
Expand Down
5 changes: 2 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mediumroast_js",
"version": "0.4.16",
"version": "0.4.19",
"description": "A Command Line Interface (CLI) and Javascript SDK to interact with mediumroast.io.",
"main": "cli/mrcli.js",
"scripts": {
Expand Down Expand Up @@ -73,7 +73,6 @@
"node-geocoder": "^4.2.0",
"octokit": "^3.1.2",
"open": "^9.1.0",
"opn": "^6.0.0",
"ora": "^6.1.2",
"prompt-sync": "^4.2.0",
"terminal-link": "^3.0.0",
Expand Down
12 changes: 7 additions & 5 deletions src/api/authorize.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,13 @@ class GitHubAuth {
deviceCode = verifier.device_code
console.log(
chalk.blue.bold(
`Type this code into your browser [${verifier.user_code}] and follow the prompts to authorize this client.
\nIf you need to run the browser on a different machine, open this URL:
\n\t${verifier.verification_uri}
\n
\nOpening your browser ...`
`
If your platform supports this, opening your browser. Otherwise, navigate to the URL below in your browser.
\n\tAuthorization URL: ${verifier.verification_uri}
Type or paste the code below into your browser and follow the prompts to authorize this client.
\n\tDevice authorization code: ${verifier.user_code}
`

)
)
open(verifier.verification_uri)
Expand Down
44 changes: 31 additions & 13 deletions src/api/gitHubServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ class baseObjects {
* @param {String} endpoint - defaults to findbyx and is combined with credential and version info
* @returns {Array} the results from the called function mrRest class
*/
async updateObj(objName, key, value, dontWrite=false) {
return await this.serverCtl.updateObject(this.objType, objName, key, value, dontWrite)
async updateObj(objName, key, value, dontWrite, system, whiteList) {
return await this.serverCtl.updateObject(this.objType, objName, key, value, dontWrite, system, whiteList)
}

/**
Expand Down Expand Up @@ -206,6 +206,23 @@ class Companies extends baseObjects {
constructor (token, org, processName) {
super(token, org, processName, 'Companies')
}

async updateObj(objToUpdate, dontWrite=false, system=false) {
// Destructure objToUpdate
const { name, key, value } = objToUpdate
// Define the attributes that can be updated by the user
const whiteList = [
'description', 'company_type', 'url', 'role', 'wikipedia_url', 'status',

'region', 'country', 'city', 'state_province', 'zip_postal', 'street_address', 'latitude', 'longitude','phone',
'google_maps_url', 'google_news_url', 'google_finance_url','google_patents_url',

'cik', 'stock_symbol', 'stock_exchange', 'recent_10k_url', 'recent_10q_url', 'firmographic_url', 'filings_url', 'owner_tranasactions',

'industry', 'industry_code', 'industry_group_code', 'industry_group_description', 'major_group_code','major_group_description'
]
return await super.updateObj(name, key, value, dontWrite, system, whiteList)
}
}


Expand All @@ -221,17 +238,18 @@ class Interactions extends baseObjects {
super(token, org, processName, 'Interactions')
}

async createObj(objs) {
// NOTE: This is an interesting way to do this, but it may not be correct.
const linkedCompanies = this.linkObj(objs)
const linkedStudies = this.linkObj(objs)
const linkedInteractions = this.linkObj(objs)
const linkedObjs = {
linked_companies: linkedCompanies,
linked_studies: linkedStudies,
linked_interactions: linkedInteractions
}
return await this.serverCtl.createObjects(this.objType, objs, linkedObjs)
async updateObj(objToUpdate, dontWrite=false, system=false) {
// Destructure objToUpdate
const { name, key, value } = objToUpdate
// Define the attributes that can be updated by the user
const whiteList = [
'status', 'content_type', 'file_size', 'reading_time', 'word_count', 'page_count', 'description', 'abstract',

'region', 'country', 'city', 'state_province', 'zip_postal', 'street_address', 'latitude', 'longitude',

'public', 'groups'
]
return await super.updateObj(name, key, value, dontWrite, system, whiteList)
}
}

Expand Down
80 changes: 72 additions & 8 deletions src/api/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,11 +441,32 @@ class GitHubFunctions {
* @memberof GitHubFunctions
* @todo Update to catchContainer and releaseContainer
*/
async updateObject(containerName, objName, key, value, dontWrite=false) {
async updateObject(containerName, objName, key, value, dontWrite=false, system=false, whiteList=[]) {
// console.log(`Updating object [${objName}] in container [${containerName}] with key [${key}] and value [${value}]`)
// Check to see if this is a system call or not
if(!system) {
// Since this is not a system call check to see if the key is in the white list
if(!whiteList.includes(key)) {
return [
false,
{
status_code: 403,
status_msg: `Updating the key [${key}] is not supported.`
},
null
]
}
}
// Using the method above 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.`, readResponse] }
if(!readResponse[0]) {
return [
false,
{status_code: 500, status_msg: `Unable to read source objects from GitHub.`},
readResponse
]
}

// Catch the container if needed
let repoMetadata = {
Expand All @@ -457,26 +478,69 @@ class GitHubFunctions {
let caught = {}
if(!dontWrite) {
repoMetadata.containers[containerName] = {}
await this.catchContainer(repoMetadata)
caught = await this.catchContainer(repoMetadata)
}

// Loop through the objects, find and update the objects matching the name
for (const obj in readResponse[2].mrJson) {
if(readResponse[2].mrJson[obj].name === objName) {
readResponse[2].mrJson[obj][key] = value
// Update the modified date of the object
const now = new Date()
readResponse[2].mrJson[obj].modification_date = now.toISOString()
}
}


if (dontWrite) { return [true, `SUCCESS: merged objects with [${containerName}/${this.objectFiles[containerName]}]`, readResponse[2].mrJson] }
// If this flag is set merely return the modified object(s) to the caller
if (dontWrite) {
return [
true,
{
status_code: 200,
status_msg: `Merged updates object(s) with [${containerName}] objects.`
},
readResponse[2].mrJson
]
}

// Call the method above to write the object
const writeResponse = await this.writeObject(containerName, mergedObjects, caught.branch.name)
const writeResponse = await this.writeObject(
containerName,
readResponse[2].mrJson,
caught[2].branch.name,
caught[2].containers[containerName].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.`, writeResponse] }
if(!writeResponse[0]) {
return [
false,
{status_code: 503, status_msg: `Unable to write the objects.`},
writeResponse
]
}

// Release the container
const released = await this.releaseContainer(repoMetadata)
if(!released[0]) { return [false, `ERROR: Unable to release the container, objects may have been written please check [${containerName}] for objects and the lock file.`, released] }
const released = await this.releaseContainer(caught[2])
if(!released[0]) {
return [
false,
{
status_code: 503,
status_msg: `Cannot release the container please check [${containerName}] in GitHub.`
},
released
]
}

// Finally return success with the results of the release
return [
true,
{
status_code: 200,
status_msg: `Updated [${containerName}] object of the name [${objName}] with [${key} = ${value}].`
},
released
]
}

async catchContainer(repoMetadata) {
Expand Down
Loading

0 comments on commit 479c4e8

Please sign in to comment.