Skip to content

Commit

Permalink
use full name of repo
Browse files Browse the repository at this point in the history
  • Loading branch information
leonhartX committed Sep 21, 2017
1 parent 012b884 commit 4051872
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 51 deletions.
16 changes: 7 additions & 9 deletions src/gas-hub.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,17 +185,15 @@ function initPageEvent() {
let label;
switch (type) {
case 'repo' :
if (context.repo && target.text() === context.repo.name) return;
if (context.repo && target.text() === context.repo.fullName) return;
//update context.repo with name and fullName
const name = target.text();
const fullName = target.attr('data');
content = {
name: name,
fullName : fullName,
gist: fullName === 'gist'
}
label = name;
context.gist = content.gist
label = fullName;
context.gist = content.gist;
break;
case 'branch' :
if (context[type] && target.text() === context[type]) return;
Expand Down Expand Up @@ -335,15 +333,15 @@ function showDiff(code, type) {

function updateRepo(repos) {
$('.repo-menu').empty().append('<div class="scm-new-repo scm-item goog-menuitem"><div class="goog-menuitem-content">Create new repo</div></div>');
$('.repo-menu').append('<div class="scm-use-gist scm-item goog-menuitem"><div class="goog-menuitem-content" scm-content="repo" data="gist">Using Gist</div></div>');
$('.repo-menu').append('<div class="scm-use-gist scm-item goog-menuitem"><div class="goog-menuitem-content" scm-content="repo" data="gist">gist</div></div>');

repos.forEach((repo) => {
let content = `<div class="scm-item goog-menuitem"><div class="goog-menuitem-content" scm-content="repo" data="${repo.fullName}">${repo.fullName}</div></div>`
let content = `<div class="scm-item goog-menuitem"><div class="goog-menuitem-content" scm-content="repo" data="${repo}">${repo}</div></div>`
$('.repo-menu').append(content);
});
if (context.repo) {
$('#scm-bind-repo').text(`Repo: ${context.repo.name}`);
return context.repo.name;
$('#scm-bind-repo').text(`Repo: ${context.repo.fullName}`);
return context.repo.fullName;
}
return null;
}
Expand Down
79 changes: 37 additions & 42 deletions src/scm/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Github {
contentType: 'application/json',
data: JSON.stringify(payload)
})
.then((response) => {
.then(response => {
return {file: file, blob: response};
})
});
Expand All @@ -47,15 +47,15 @@ class Github {
{ access_token: this.accessToken }
)
])
.then((responses) => {
.then(responses => {
return $.getJSON(
responses[1].commit.commit.tree.url,
{
recursive: 1,
access_token: this.accessToken
}
)
.then((baseTree) => {
.then(baseTree => {
const tree = responses[0].map((data) => {
return {
path: data.file,
Expand All @@ -71,7 +71,7 @@ class Github {
tree: tree
};
})
.then((payload) => {
.then(payload => {
return $.ajax({
url: `${this.baseUrl}/repos/${context.repo.fullName}/git/trees`,
headers: {
Expand All @@ -84,14 +84,14 @@ class Github {
data: JSON.stringify(payload)
});
})
.then((response) => {
.then(response => {
return Object.assign(response, { parent: responses[1].commit.sha })
})
.fail((err) => {
.fail(err => {
throw err;
})
});
})
.then((response) => {
.then(response => {
const payload = {
message: $('#commit-comment').val(),
tree: response.sha,
Expand All @@ -111,7 +111,7 @@ class Github {
data: JSON.stringify(payload)
});
})
.then((response) => {
.then(response => {
const payload = {
force: true,
sha: response.sha
Expand All @@ -129,9 +129,9 @@ class Github {
});
})
.then(() => {
showAlert(`Successfully push to ${context.branch} of ${context.repo.name}`);
showAlert(`Successfully push to ${context.branch} of ${context.repo.fullName}`);
})
.catch((err) => {
.catch(err => {
showAlert('Failed to push', LEVEL_ERROR);
});
}
Expand All @@ -145,18 +145,17 @@ class Github {
const payload = {
files: {}
};
files.forEach((file) => {
files.forEach(file => {
payload.files[file] = {
content: code.gas[file]
};
})
});
if (code.github['init_by_gas_hub.html']) {
payload.files['init_by_gas_hub.html'] = null;
}
if ($('#gist-desc').val() !== '') {
payload.description = $('#gist-desc').val();
}
console.log(payload);
return $.ajax({
url: `${this.baseUrl}/gists/${context.branch}`,
headers: {
Expand All @@ -171,17 +170,17 @@ class Github {
.then(() => {
showAlert(`Successfully update gist: ${context.branch}`);
})
.fail((err) => {
.fail(err => {
showAlert('Failed to update', LEVEL_ERROR);
});
}

getAllGists() {
return getAllItems(Promise.resolve({items: [], url: `${this.baseUrl}/users/${this.user}/gists?access_token=${this.accessToken}`}))
return getAllItems(Promise.resolve({ items: [], url: `${this.baseUrl}/users/${this.user}/gists?access_token=${this.accessToken}` }), 'github');
}

getAllBranches() {
return getAllItems(Promise.resolve({items: [], url: `${this.baseUrl}/repos/${context.repo.fullName}/branches?access_token=${this.accessToken}`}))
return getAllItems(Promise.resolve({ items: [], url: `${this.baseUrl}/repos/${context.repo.fullName}/branches?access_token=${this.accessToken}` }), 'github');
}

getCode() {
Expand All @@ -198,21 +197,21 @@ class Github {
.then(resolve)
.fail(reject);
})
.then((response) => {
.then(response => {
return $.getJSON(
`${this.baseUrl}/repos/${context.repo.fullName}/git/trees/${response.commit.commit.tree.sha}`,
{ recursive: 1, access_token: this.accessToken }
);
})
.then((response) => {
.then(response => {
const promises = response.tree.filter((tree) => {
return tree.type === 'blob' && /(\.gs|\.html)$/.test(tree.path);
})
.map((tree) => {
.map(tree => {
return new Promise((resolve, reject) => {
$.getJSON(tree.url, {access_token: this.accessToken })
$.getJSON(tree.url, { access_token: this.accessToken })
.then((content) => {
resolve({ file: tree.path, content: decodeURIComponent(escape(atob(content.content)))});
resolve({ file: tree.path, content: decodeURIComponent(escape(atob(content.content))) });
})
.fail(reject)
});
Expand Down Expand Up @@ -250,24 +249,21 @@ class Github {
}

getRepos() {
return getAllItems(Promise.resolve({items: [], url: `${this.baseUrl}/user/repos?access_token=${this.accessToken}`}))
.then((response) => {
const repos = response.map((repo) => {
return { name : repo.name, fullName : repo.full_name }
});
return getAllItems(Promise.resolve({ items: [], url: `${this.baseUrl}/user/repos?access_token=${this.accessToken}` }), 'github')
.then(response => {
const repos = response.map(repo => repo.full_name);
//if current bind still existed, use it
const repo = context.bindRepo[context.id];
if (repo && $.inArray(repo.name, repos.map(repo => repo.name)) >= 0 ) {
if (repo && $.inArray(repo.fullName, repos) >= 0) {
context.repo = repo;
} else if (context.gist) {
context.repo = {
name: 'Using Gist',
fullName: 'gist',
gist: true
}
}
return repos;
})
});
}

createRepo() {
Expand Down Expand Up @@ -296,9 +292,8 @@ class Github {
.then(resolve)
.fail(reject);
})
.then((response) => {
.then(response => {
const repo = {
name : response.name,
fullName : response.full_name
};
context.repo = repo;
Expand All @@ -309,7 +304,7 @@ class Github {
chrome.storage.sync.set({ bindRepo: context.bindRepo });
return response;
})
.then(getGithubRepos)
.then(this.getRepos.bind(this))
.then(updateRepo)
.then(updateBranch)
.then(() => {
Expand All @@ -318,7 +313,7 @@ class Github {
$('#new-repo-type').val('public');
showAlert(`Successfully create new repository ${repo}`);
})
.catch((err) => {
.catch(err => {
showAlert('Failed to create new repository.', LEVEL_ERROR);
});
}
Expand Down Expand Up @@ -351,7 +346,7 @@ class Github {
.then(resolve)
.fail(reject);
})
.then((response) => {
.then(response => {
const gist = response.id;
context.branch = gist;
Object.assign(context.bindBranch, { [context.id] : gist });
Expand All @@ -364,7 +359,7 @@ class Github {
$('#new-gist-public').val('public');
showAlert(`Successfully create new gist.`);
})
.catch((err) => {
.catch(err => {
showAlert('Failed to create new gist.', LEVEL_ERROR);
});
}
Expand All @@ -380,7 +375,7 @@ class Github {
.then(resolve)
.fail(reject)
})
.then((response) => {
.then(response => {
if (response.object) {
return response.object.sha;
}
Expand All @@ -389,12 +384,12 @@ class Github {
`${this.baseUrl}/repos/${context.repo.fullName}/git/refs/heads`,
{ access_token: this.accessToken }
)
.then((response) => {
.then(response => {
return response[0].object.sha;
})
}
})
.then((sha) => {
.then(sha => {
const payload = {
ref: `refs/heads/${branch}`,
sha: sha
Expand All @@ -411,18 +406,18 @@ class Github {
data: JSON.stringify(payload)
});
})
.then((response) => {
.then(response => {
context.branch = branch;
Object.assign(context.bindBranch, { [context.id] : branch });
chrome.storage.sync.set({ bindBranch: context.bindBranch });
return context.repo.name;
return context.repo.fullName;
})
.then(updateBranch)
.then(() => {
$('#new-branch-name').val('');
showAlert(`Successfully create new branch: ${branch}`);
})
.catch((err) => {
.catch(err => {
if (err.status === 409) {
showAlert('Cannot create branch in empty repository with API, try to create branch in Github.', LEVEL_ERROR);
} else {
Expand Down

0 comments on commit 4051872

Please sign in to comment.