Skip to content

Commit

Permalink
GH batch overwrite files
Browse files Browse the repository at this point in the history
Force creation of a new tree for all subfolders
  • Loading branch information
JbIPS committed Mar 25, 2018
1 parent d5af2f7 commit 1cf7d8e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
18 changes: 14 additions & 4 deletions lib/unifile-github.js
Expand Up @@ -629,6 +629,7 @@ class GitHubConnector {
actionsChain = actionsChain.then(() => this[transformTree](session, splitPath[0], (treeRes, done) => {
const newTrees = {};
const blobsWaiting = [];

for(const currentAction of fileActions) {
const path = getPathTokens(currentAction.path).slice(2).join('/');
switch (currentAction.name.toLowerCase()) {
Expand Down Expand Up @@ -658,9 +659,18 @@ class GitHubConnector {
return new Promise.reject(new UnifileError(
UnifileError.EINVAL,
'WriteFile actions should have a content'));
// Get the longest path matching this file parent
const closestParent = Object.keys(newTrees).filter((p) => path.includes(p)).sort().pop();
if(closestParent) {
if(path.includes('/')) {
// We'll need a subtree
// Check existing ones with the longest path matching this file parent
let closestParent = Object.keys(newTrees).filter((p) => path.includes(p)).sort().pop();
if(!closestParent) {
// If none, create one
closestParent = path.split('/').slice(0, -1).join('/');
newTrees[closestParent] = {
tree: [],
blobs: []
};
}
newTrees[closestParent].blobs.push(this[createBlob](session, splitPath[0], currentAction.content));
newTrees[closestParent].tree.push({
path: path.replace(closestParent + '/', ''),
Expand All @@ -670,7 +680,7 @@ class GitHubConnector {
} else {
treeRes.tree = treeRes.tree.filter((node) => node.path !== path);
const newNode = {
path: path.replace(closestParent + '/', ''),
path: path,
mode: '100644',
type: 'blob'
};
Expand Down
4 changes: 2 additions & 2 deletions test/index.js
Expand Up @@ -41,9 +41,9 @@ describe('Unifile class', function() {
file != 'index.js' && file != 'error.js' && file.endsWith('.js') && !file.startsWith('.'));
// Get all the methods of Unifile
const methods = Object.getOwnPropertyNames(Object.getPrototypeOf(unifile))
// Filter out the one that should be implemented
// Filter out the one that should be implemented
.filter((method) => !['callMethod', 'listConnectors', 'use'].includes(method))
// Sort them alphabetically
// Sort them alphabetically
.sort();
for(const connectorName of connectors) {
describe(connectorName, function() {
Expand Down

0 comments on commit 1cf7d8e

Please sign in to comment.