UserProfileViaGithubApi Tentacule23

This commit is contained in:
Tentacule23 committed 2021-10-25 23:12:13 -04:00
1 parent fd48fc006c
commit 99dd678173
2 files changed
+238

No files matched your search

+168
View File
@@ -862,6 +862,174 @@ exports.newHttpInterface = function newHttpInterface() {
break
}
case 'UserUpdate': {
try {
let commitMessage = unescape(requestPath[3])
const username = unescape(requestPath[4])
const token = unescape(requestPath[5])
const currentBranch = unescape(requestPath[6])
const contributionsBranch = unescape(requestPath[7])
let error
/* Unsaving # */
for (let i = 0; i < 10; i++) {
commitMessage = commitMessage.replace('_SLASH_', '/')
commitMessage = commitMessage.replace('_HASHTAG_', '#')
}
updateUser()
async function updateUser() {
await doGithub()
if (error !== undefined) {
let docs = {
project: 'Foundations',
category: 'Topic',
type: 'App Error - Contribution Not Sent',
anchor: undefined,
placeholder: {}
}
console.log('respond with docs ')
respondWithDocsObject(docs, error)
return
}
SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(global.DEFAULT_OK_RESPONSE), httpResponse)
}
async function doGithub() {
const { Octokit } = SA.nodeModules.octokit
const octokit = new Octokit({
auth: token,
userAgent: 'Superalgos Beta 12'
})
const repo = 'Superalgos'
const owner = 'Superalgos'
const head = username + ':' + contributionsBranch
const base = currentBranch
const title = 'Contribution: ' + commitMessage
const path = 'Projects/Governance/Plugins/User-Profiles/' + username + '.json' ;
const sha = await getSHA(path, octokit);
let file = await SA.projects.communityPlugins.utilities.plugins.getPluginFileContent(
'Governance',
'User-Profiles',
username + '.json'
)
let buff = new Buffer.from(file, 'utf-8');
let encodedFile = buff.toString('base64');
try {
await octokit.repos.createOrUpdateFileContents({
owner: username,
repo: "Superalgos",
path,
message: title,
content: encodedFile,
sha,
branch: base
});
} catch (err) {
if (err.stack.indexOf('Error User Commit') >= 0) {
return
} else {
console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> Method call produced an error.')
console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> err.stack = ' + err.stack)
console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> commitMessage = ' + commitMessage)
console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> username = ' + username)
console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> token starts with = ' + token.substring(0, 10) + '...')
console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> token ends with = ' + '...' + token.substring(token.length - 10))
console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> currentBranch = ' + currentBranch)
console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> contributionsBranch = ' + contributionsBranch)
error = err
}
}
try {
await octokit.pulls.create({
owner,
repo,
title,
head,
base,
});
} catch (err) {
if (err.stack.indexOf('A pull request already exists') >= 0) {
return
} else {
console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> Method call produced an error.')
console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> err.stack = ' + err.stack)
console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> commitMessage = ' + commitMessage)
console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> username = ' + username)
console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> token starts with = ' + token.substring(0, 10) + '...')
console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> token ends with = ' + '...' + token.substring(token.length - 10))
console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> currentBranch = ' + currentBranch)
console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> contributionsBranch = ' + contributionsBranch)
error = err
}
}
}
async function getSHA(path, octokit) {
try{
const result = await octokit.repos.getContent({
owner: username,
repo: "Superalgos",
path,
ref: currentBranch
});
const sha = result?.data?.sha
return sha
} catch (err) {
if (err.message = 'Not Found') {
console.log("[User Not Found] -> Creating new user")
const sha = ''
return sha
} else {
console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> Method call produced an error.')
console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> err.stack = ' + err.stack)
console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> commitMessage = ' + commitMessage)
console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> username = ' + username)
console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> token starts with = ' + token.substring(0, 10) + '...')
console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> token ends with = ' + '...' + token.substring(token.length - 10))
console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> currentBranch = ' + currentBranch)
console.log('[ERROR] httpInterface -> App -> Contribute -> doGithub -> contributionsBranch = ' + contributionsBranch)
const sha = ''
return sha
}
}
}
} catch (err) {
console.log('[ERROR] httpInterface -> App -> Contribute -> Method call produced an error.')
console.log('[ERROR] httpInterface -> App -> Contribute -> err.stack = ' + err.stack)
console.log('[ERROR] httpInterface -> App -> Contribute -> commitMessage = ' + commitMessage)
console.log('[ERROR] httpInterface -> App -> Contribute -> username = ' + username)
console.log('[ERROR] httpInterface -> App -> Contribute -> token starts with = ' + token.substring(0, 10) + '...')
console.log('[ERROR] httpInterface -> App -> Contribute -> token ends with = ' + '...' + token.substring(token.length - 10))
console.log('[ERROR] httpInterface -> App -> Contribute -> currentBranch = ' + currentBranch)
console.log('[ERROR] httpInterface -> App -> Contribute -> contributionsBranch = ' + contributionsBranch)
let error = {
result: 'Fail Because',
message: err.message,
stack: err.stack
}
SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(error), httpResponse)
}
break
}
case 'Checkout': {
try {
const currentBranch = unescape(requestPath[3])
@@ -26,6 +26,7 @@ function newFoundationsDocsCommmandInterface() {
function detectAppCommands() {
if (checkContributeCommand() === undefined) { return true }
if (checkUserUpdateCommand() === undefined) { return true }
if (checkUpdateCommand() === undefined) { return true }
if (fixAppSchema() === undefined) { return true }
@@ -98,6 +99,75 @@ function newFoundationsDocsCommmandInterface() {
}
}
function checkUserUpdateCommand() {
if (UI.projects.education.spaces.docsSpace.commandInterface.command.toLowerCase() === 'app.help app.userupdate') {
UI.projects.education.spaces.docsSpace.navigateTo('Foundations', 'Topic', 'App Userupdate Command')
return
}
if (UI.projects.education.spaces.docsSpace.commandInterface.command.indexOf('App.UserUpdate') !== 0 && UI.projects.education.spaces.docsSpace.commandInterface.command.indexOf('app.userupdate') !== 0) { return 'Not Contribute Commands' }
/* Set up the commit message */
let message = UI.projects.education.spaces.docsSpace.commandInterface.command.trim().substring(UI.projects.education.spaces.docsSpace.commandInterface.command.indexOf(' ') + 1, UI.projects.education.spaces.docsSpace.commandInterface.command.length)
if (message.toLowerCase() === 'app.UserUpdate') {
message = 'This is my UserUpdate to Superalgos'
}
/* Find the Username and Password */
let apisNode = UI.projects.foundations.spaces.designSpace.workspace.getHierarchyHeadByNodeType('APIs')
if (apisNode === undefined) {
UI.projects.education.spaces.docsSpace.navigateTo('Foundations', 'Topic', 'App Error - Github Credentials Missing', 'Anchor Github Credentials Missing')
return
}
if (apisNode.githubAPI === undefined) {
UI.projects.education.spaces.docsSpace.navigateTo('Foundations', 'Topic', 'App Error - Github Credentials Missing', 'Anchor Github Credentials Missing')
return
}
let config = JSON.parse(apisNode.githubAPI.config)
if (config.username === undefined || config.username === "") {
UI.projects.education.spaces.docsSpace.navigateTo('Foundations', 'Topic', 'App Error - Github Credentials Missing', 'Anchor Github Credentials Missing')
return
}
if (config.token === undefined || config.token === "") {
UI.projects.education.spaces.docsSpace.navigateTo('Foundations', 'Topic', 'App Error - Github Credentials Missing', 'Anchor Github Credentials Missing')
return
}
message = message.replaceAll('#', '_HASHTAG_')
message = message.replaceAll('/', '_SLASH_')
httpRequest(
undefined,
'App/UserUpdate/' +
message + '/' +
config.username + '/' +
config.token + '/' +
UI.projects.education.spaces.docsSpace.currentBranch + '/' +
UI.projects.education.spaces.docsSpace.contributionsBranch
, onResponse)
UI.projects.education.spaces.docsSpace.navigateTo('Foundations', 'Topic', 'App Message - UserUpdate Request')
return
function onResponse(err, data) {
/* Lets check the result of the call through the http interface */
data = JSON.parse(data)
if (err.result === GLOBAL.DEFAULT_OK_RESPONSE.result && data.result === GLOBAL.DEFAULT_OK_RESPONSE.result) {
UI.projects.education.spaces.docsSpace.navigateTo('Foundations', 'Topic', 'App Message - UserUpdate Done')
} else {
UI.projects.education.spaces.docsSpace.navigateTo(
data.docs.project,
data.docs.category,
data.docs.type,
data.docs.anchor,
undefined,
data.docs.placeholder
)
}
}
}
function checkUpdateCommand() {
if (UI.projects.education.spaces.docsSpace.commandInterface.command.toLowerCase() === 'app.help app.update') {
UI.projects.education.spaces.docsSpace.navigateTo('Foundations', 'Topic', 'App Update Command')