Skip to content

Commit

Permalink
Add branch name max length option to GitHub backend (closes #526)
Browse files Browse the repository at this point in the history
  • Loading branch information
Benaiah committed Dec 19, 2017
1 parent 11ee874 commit 45b1598
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/backends/git-gateway/implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ export default class GitGateway extends GitHubBackend {
branch: this.branch,
tokenPromise: this.tokenPromise,
commitAuthor: pick(userData, ["name", "email"]),
// this.config is set by the GitHubBackend constructor
branchNameMaxLength: this.config.getIn(["backend", "branch_name_max_length"]),
});
return userData;
} else {
Expand Down
19 changes: 16 additions & 3 deletions src/backends/github/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default class API {
this.branch = config.branch || "master";
this.repo = config.repo || "";
this.repoURL = `/repos/${ this.repo }`;
this.branchNameMaxLength = config.branchNameMaxLength || 40;
}

user() {
Expand Down Expand Up @@ -85,8 +86,14 @@ export default class API {
});
}

truncateSlugForBranchName(slug) {
const slugMaxLength = this.branchNameMaxLength - CMS_BRANCH_PREFIX.length;
return slug.substring(0, slugMaxLength);
}

generateBranchName(basename) {
return `${CMS_BRANCH_PREFIX}${basename}`;
const truncatedBasename = this.truncateSlugForBranchName(basename);
return `${ CMS_BRANCH_PREFIX }${ truncatedBasename }`;
}

checkMetadataRef() {
Expand All @@ -111,7 +118,10 @@ export default class API {
});
}

storeMetadata(key, data) {
storeMetadata(contentKey, data) {
// metadata filenames need to match branch names - see
// unpublishedEntries in src/backends/github/implementation.js
const key = this.truncateSlugForBranchName(contentKey);
return this.checkMetadataRef()
.then((branchData) => {
const fileTree = {
Expand All @@ -135,7 +145,10 @@ export default class API {
});
}

retrieveMetadata(key) {
retrieveMetadata(contentKey) {
// metadata filenames need to match branch names - see
// unpublishedEntries in src/backends/github/implementation.js
const key = this.truncateSlugForBranchName(contentKey);
const cache = LocalForage.getItem(`gh.meta.${ key }`);
return cache.then((cached) => {
if (cached && cached.expires > Date.now()) { return cached.data; }
Expand Down
8 changes: 7 additions & 1 deletion src/backends/github/implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ export default class GitHub {

authenticate(state) {
this.token = state.token;
this.api = new API({ token: this.token, branch: this.branch, repo: this.repo, api_root: this.api_root });
this.api = new API({
token: this.token,
branch: this.branch,
repo: this.repo,
api_root: this.api_root,
branchNameMaxLength: this.config.getIn(["backend", "branch_name_max_length"]),
});
return this.api.user().then(user =>
this.api.hasWriteAccess().then((isCollab) => {
// Unauthorized user
Expand Down

0 comments on commit 45b1598

Please sign in to comment.