Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add branch name max length option to GitHub backend (closes #526) #862

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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