forked from Superalgos/Superalgos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic Implementation of Github Storage save file function
- Loading branch information
1 parent
071ed72
commit e26dae7
Showing
3 changed files
with
134 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
exports.newOpenStorageUtilitiesGithubStorage = function () { | ||
|
||
let thisObject = { | ||
saveData: saveData, | ||
fetchData: fetchData | ||
} | ||
|
||
return thisObject | ||
|
||
async function saveData(fileName, filePath, fileContent, sotrageContainer) { | ||
|
||
const token = 'GET THE TOKEN FROM THE SECRETS FILE' | ||
const { Octokit } = SA.nodeModules.octokit | ||
const octokit = new Octokit({ | ||
auth: token, | ||
userAgent: 'Superalgos ' + SA.version | ||
}) | ||
const repo = sotrageContainer.config.repositoryName | ||
const owner = sotrageContainer.config.githubUserName | ||
const branch = 'main' | ||
const message = 'Open Storage: New File.' | ||
const completePath = filePath + '/' + fileName + '.json' | ||
const { graphql } = SA.nodeModules.graphql | ||
const { repository } = await graphql( | ||
'{ ' + | ||
' repository(name: "SuperAlgos", owner: "' + owner + '") {' + | ||
' object(expression: "' + branch + ':' + completePath + '") {' + | ||
' ... on Blob {' + | ||
' oid' + | ||
' }' + | ||
' }' + | ||
' name' + | ||
' }' + | ||
'}', | ||
{ | ||
headers: { | ||
authorization: 'token ' + token | ||
} | ||
} | ||
) | ||
|
||
if ( | ||
repository.name === undefined || | ||
repository.object === null | ||
) { | ||
console.log('[ERROR] Github Storage -> Save Data -> SHA graphql failed.') | ||
return | ||
} | ||
|
||
const sha = repository.object.oid | ||
|
||
if ( | ||
sha === undefined | ||
) { | ||
console.log('[ERROR] Github Storage -> Save Data -> SHA calculation failed.') | ||
return | ||
} | ||
|
||
const buff = new Buffer.from(fileContent, 'utf-8'); | ||
const content = buff.toString('base64'); | ||
|
||
await octokit.repos.createOrUpdateFileContents({ | ||
owner: owner, | ||
repo: repo, | ||
filePath: completePath, | ||
message: message, | ||
content: content, | ||
sha: sha, | ||
branch: branch | ||
}) | ||
} | ||
|
||
async function fetchData(fileName, sotrageContainer) { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters