Skip to content

Commit

Permalink
Merge pull request #22 from leonhartX/v2.0
Browse files Browse the repository at this point in the history
support file rename/delete
  • Loading branch information
leonhartX authored Jun 13, 2017
2 parents 6a53e44 + fecf6fc commit 60f548f
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 40 deletions.
8 changes: 1 addition & 7 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.0.1",
"version": "2.0.0",
"manifest_version": 2,
"default_locale": "en",
"name": "__MSG_appName__",
Expand All @@ -11,12 +11,6 @@
"48": "icon/icon.png",
"128": "icon/icon.png"
},
"oauth2" : {
"client_id" : "971735641612-f7v88rtfkcogntklsh5hitv6k7pd8teh.apps.googleusercontent.com",
"scopes": [
"https://www.googleapis.com/auth/drive"
]
},
"background": {
"scripts": [
"background/background.js"
Expand Down
47 changes: 43 additions & 4 deletions src/gas-api.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"use strict";

function pull(code) {
const promises = $('.diff-file:checked').toArray().map((elem) => {
const file = elem.value;
const changed = $('.diff-file:checked').toArray().map(e => e.value);
const update_promises = changed.filter(f => code.github[f])
.map((file) => {
const match = file.match(/(.*?)\.(gs|html)$/);
if (!match || !match[1] || !match[2]) {
showAlert('Unknow Error', LEVEL_ERROR);
Expand All @@ -20,14 +21,29 @@ function pull(code) {
return gasUpdateFile(name, code.github[file]);
}
});
if (promises.length === 0) {

const delete_promises = changed.filter(f => !code.github[f])
.map((file) => {
const match = file.match(/(.*?)\.(gs|html)$/);
if (!match || !match[1] || !match[2]) {
showAlert('Unknow Error', LEVEL_ERROR);
return;
}
const name = match[1];
return gasDeleteFile(name);
});

if (update_promises.length === 0 && delete_promises.length === 0) {
showAlert("Nothing to do", LEVEL_WARN);
return;
}

getGasContext()
.then(() => {
return Promise.all(promises)
return Promise.all(update_promises)
.then(() => {
return Promise.all(delete_promises);
})
})
.then(() => {
showAlert("Successfully pulled from github");
Expand Down Expand Up @@ -167,4 +183,27 @@ function gasUpdateFile(file, code) {
reject(new Error('Update file failed'));
});
});
}

function gasDeleteFile(file) {
const payload = `7|1|4|${getBaseUrl()}\|${context.gasToken}|_|deleteFile|1|2|3|4|0|`;
let headers = context.gasHeaders;
Object.assign(headers, { 'file-id': context.fileIds[file]});
return new Promise((resolve, reject) => {
$.ajax({
url: context.gasUrl,
headers: headers,
method: 'POST',
crossDomain: true,
data: payload,
dataType: 'text'
})
.then((response) => {
if (!response.startsWith('//OK')) reject(new Error('Delete file failed'));
resolve();
})
.fail((err) => {
reject(new Error('Update file failed'));
});
});
}
9 changes: 5 additions & 4 deletions src/gas-hub.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,17 +261,18 @@ function showDiff(code, type) {
const newCode = type === "push" ? code.gas : code.github;
const gasFiles = Object.keys(code.gas);
const githubFiles = Object.keys(code.github);
let diff = gasFiles.concat(githubFiles.filter((e) => {
let diff = githubFiles.filter((e) => {
return gasFiles.indexOf(e) < 0;
}))
})
.concat(gasFiles)
.reduce((diff, file) => {
let mode = null;
if (!oldCode[file]) {
mode = 'new file mode 100644';
} else if (!newCode[file]) {
return diff;
mode = 'deleted file mode 100644';
}
let fileDiff = JsDiff.createPatch(file, oldCode[file] || "", newCode[file]);
let fileDiff = JsDiff.createPatch(file, oldCode[file] || "", newCode[file] || "");
if (fileDiff.indexOf('@@') < 0) return diff; //no diff
let diffArr = fileDiff.split('\n');
diffArr.splice(0, 2, `diff --git a/${file} b/${file}`);
Expand Down
64 changes: 39 additions & 25 deletions src/github-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ function push(code){
}

function pushToRepo(code) {
const promises = $('.diff-file:checked').toArray().map((elem) => {
const file = elem.value;
const changed = $('.diff-file:checked').toArray().map(elem => elem.value);
const unchanged = Object.keys(code.gas).filter((f) => changed.indexOf(f) < 0 );
const promises = changed.filter(f => code.gas[f]).map((file) => {
const payload = {
content: code.gas[file],
encoding: "utf-8"
Expand All @@ -27,7 +28,7 @@ function pushToRepo(code) {
return {file: file, blob: response};
})
});
if (promises.length === 0) {
if (changed.lengthun === 0) {
showAlert("Nothing to do", LEVEL_WARN);
return;
}
Expand All @@ -40,28 +41,41 @@ function pushToRepo(code) {
)
])
.then((responses) => {
const tree = responses[0].map((data) => {
return {
path: data.file,
mode: "100644",
type: "blob",
sha: data.blob.sha
return $.getJSON(
responses[1].commit.commit.tree.url,
{
recursive: 1,
access_token: accessToken
}
});
const payload = {
base_tree: responses[1].commit.commit.tree.sha,
tree: tree
};
return $.ajax({
url: `${baseUrl}/repos/${context.repo.fullName}/git/trees`,
headers: {
"Authorization": `token ${accessToken}`
},
method: 'POST',
crossDomain: true,
dataType: 'json',
contentType: 'application/json',
data: JSON.stringify(payload)
)
.then((baseTree) => {
const tree = responses[0].map((data) => {
return {
path: data.file,
mode: "100644",
type: "blob",
sha: data.blob.sha
}
})
.concat(baseTree.tree.filter((t) => {
return (t.type != 'tree') && (!/.(gs|html)$/.test(t.path) || unchanged.indexOf(t.path) >= 0);
}));
return {
tree: tree
};
})
.then((payload) => {
return $.ajax({
url: `${baseUrl}/repos/${context.repo.fullName}/git/trees`,
headers: {
"Authorization": `token ${accessToken}`
},
method: 'POST',
crossDomain: true,
dataType: 'json',
contentType: 'application/json',
data: JSON.stringify(payload)
});
})
.then((response) => {
return Object.assign(response, { parent: responses[1].commit.sha })
Expand Down Expand Up @@ -91,7 +105,7 @@ function pushToRepo(code) {
});
})
.then((response) => {
const payload = {
const payload = {
force: true,
sha: response.sha
};
Expand Down

0 comments on commit 60f548f

Please sign in to comment.