Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve vulnerable dependencies #77

Merged
merged 9 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 15 additions & 37 deletions controller/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const fs = require('fs');
const util = require('util');
const prompt = require('inquirer').prompt;
const mkdirp = require('mkdirp');
const fsJson = require('fs-json')();
const fsJson = require('../vendor/fs-json')();

const exec = require('child_process').exec;

Expand All @@ -14,18 +14,14 @@ const {
deleteGithubToken,
readGithubAuths,
checkGithubAuth,
createClient,
getClient,
getGithubID
} = require('./helpers');

const applicationDirectory = `${env.home()}/opspark`;
const authFilePath = `${applicationDirectory}/auth`;
const userFilePath = `${applicationDirectory}/user`;
let _auth;
let _user;
let _client;
let _opspark;

// TODO : consider the "module level" vars, like _client in this implementation, are they necessary.

Expand Down Expand Up @@ -66,6 +62,10 @@ function authorizeUser() {

module.exports.authorizeUser = authorizeUser;

/**
* Prompts the user for their GitHub username and personal access token (PAT).
* @returns {Promise<{username: string, token: string}>}
*/
function promptForUserInfo() {
return new Promise(function (res, rej) {
prompt(
Expand All @@ -84,6 +84,7 @@ function promptForUserInfo() {
conform: () => true
}
],
/** @param {{username: string, token: string}} user */
function (user) {
res(user);
}
Expand All @@ -97,7 +98,8 @@ module.exports.promptForUserInfo = promptForUserInfo;
*
* @param {object} auth
* @param {string} auth.token GitHub Personal Access Token
* @returns {Promise<{token: string}>}
* @param {string} auth.username GitHub username
* @returns {Promise<{token: string, username: string}>}
*/
function writeAuth(auth) {
deleteAuth();
Expand Down Expand Up @@ -131,6 +133,12 @@ function obtainAndWriteAuth({ username, password }) {

module.exports.obtainAndWriteAuth = obtainAndWriteAuth;

/**
* Writes the user to the file system. This function
* will overwrite any existing user file.
* @param {{ token: string, username: string}} auth
* @returns
*/
function writeUser(auth) {
deleteUser();
console.log(clc.yellow('Writing user. . .'));
Expand Down Expand Up @@ -161,37 +169,6 @@ function writeUser(auth) {

module.exports.writeUser = writeUser;

function obtainAndWriteUser(user) {
console.log(clc.yellow('Grabbing user. . .'));
return new Promise(function (res, rej) {
getOrCreateClient()
.then(function (client) {
getClient(client, user.username)
.catch(err => rej(err))
.then(writeUser)
.then(user => res(user));
})
.catch(err => console.error(err));
});
}

module.exports.obtainAndWriteUser = obtainAndWriteUser;

function getOrCreateClient() {
return new Promise(function (res, rej) {
if (_client) return res(_client);
getOrObtainAuth()
.then(function (auth) {
_client = createClient(auth.token);
// _opspark = _client.org('OperationSpark');
res(_client);
})
.catch(err => console.error(err));
});
}

module.exports.getOrCreateClient = getOrCreateClient;

function getOrObtainAuth() {
return new Promise(function (res) {
if (_auth) return res(_auth);
Expand Down Expand Up @@ -223,6 +200,7 @@ function hasAuthorization(token) {
exec(cmd, function (err, stdout, stderr) {
if (err) return rej(err);
if (typeof stdout === 'string') {
// eslint-disable-next-line no-param-reassign
stdout = JSON.parse(stdout);
}
res(stdout);
Expand Down
10 changes: 1 addition & 9 deletions controller/handshake.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const clc = require('cli-color');
const fs = require('fs');
const mkdirp = require('mkdirp');
const fsJson = require('fs-json')();
const fsJson = require('../vendor/fs-json')();
const rp = require('request-promise');
const greenlight = require('./greenlight');

Expand All @@ -11,14 +11,6 @@ const view = require('../view');
const filePath = `${env.home()}/opspark`;
const URI = greenlight.URI;

function readHandshake() {
const path = `${filePath}/handshake`;
if (!fs.existsSync(path)) {
return console.log('No handshake stored, run "os init-hs" to create');
}
return fsJson.loadSync(path);
}

// Checks if directory exists and creates if not
function checkForDirectory(path) {
if (!fs.existsSync(path)) {
Expand Down
14 changes: 0 additions & 14 deletions controller/helpers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const octonode = require('octonode');
const exec = require('child_process').exec;

module.exports.execAsync = function execAsync(cmd) {
Expand All @@ -10,19 +9,6 @@ module.exports.execAsync = function execAsync(cmd) {
});
};

module.exports.createClient = function (token) {
return octonode.client(token);
};

module.exports.getClient = function (client, username) {
return new Promise(function (res, rej) {
client.get(`/users/${username}`, {}, function (err, status, body) {
if (err) rej(err);
else res(body);
});
});
};

module.exports.createGithubToken = function (username, password, note) {
return `curl -u "${username}:${password}" -d '{"scopes":["public_repo", "repo", "gist"],"note":"${note}","note_url":"https://www.npmjs.com/package/opspark"}' https://api.github.com/authorizations`;
};
Expand Down
12 changes: 9 additions & 3 deletions controller/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const fs = require('fs');
const _ = require('lodash');
const mkdirp = require('mkdirp');
const rimraf = require('rimraf');
const fsJson = require('fs-json')();
const fsJson = require('../vendor/fs-json')();
harveysanders marked this conversation as resolved.
Show resolved Hide resolved
const program = require('commander');
const prompt = require('inquirer').prompt;
const changeCase = require('change-case');
Expand Down Expand Up @@ -34,10 +34,16 @@ const projectEntriesPath = `${rootDirectory}/projects/projects.json`;
const projectsDirectory = `${rootDirectory}/projects`;
const cancelOption = '[cancel]';

let action = null;
let action = '';

module.exports.action = () => action;
module.exports.action = action;

/**
*
* @param {{ session: *, projectAction: string}} param0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the * interpreted as any?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah

*
* @returns Promise<*>
*/
function selectProject({ session, projectAction }) {
action = projectAction;
const projects = listProjects(session, action);
Expand Down
2 changes: 1 addition & 1 deletion controller/shelve.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const sessions = require('./sessions');

module.exports = function () {
console.log(clc.blue('Beginning shelve process!'));
projects.action = () => 'shelve';
projects.action = 'shelve';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was this being invoked somewhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put it back. It was changed for a type error caused by this code. It's fixed now

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

word

github
.getCredentials()
.catch(janitor.error(clc.red('Failure getting credentials')))
Expand Down
Loading