Skip to content

Commit 45b1598

Browse files
committed
Add branch name max length option to GitHub backend (closes #526)
1 parent 11ee874 commit 45b1598

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

src/backends/git-gateway/implementation.js

+2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ export default class GitGateway extends GitHubBackend {
7070
branch: this.branch,
7171
tokenPromise: this.tokenPromise,
7272
commitAuthor: pick(userData, ["name", "email"]),
73+
// this.config is set by the GitHubBackend constructor
74+
branchNameMaxLength: this.config.getIn(["backend", "branch_name_max_length"]),
7375
});
7476
return userData;
7577
} else {

src/backends/github/API.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export default class API {
1515
this.branch = config.branch || "master";
1616
this.repo = config.repo || "";
1717
this.repoURL = `/repos/${ this.repo }`;
18+
this.branchNameMaxLength = config.branchNameMaxLength || 40;
1819
}
1920

2021
user() {
@@ -85,8 +86,14 @@ export default class API {
8586
});
8687
}
8788

89+
truncateSlugForBranchName(slug) {
90+
const slugMaxLength = this.branchNameMaxLength - CMS_BRANCH_PREFIX.length;
91+
return slug.substring(0, slugMaxLength);
92+
}
93+
8894
generateBranchName(basename) {
89-
return `${CMS_BRANCH_PREFIX}${basename}`;
95+
const truncatedBasename = this.truncateSlugForBranchName(basename);
96+
return `${ CMS_BRANCH_PREFIX }${ truncatedBasename }`;
9097
}
9198

9299
checkMetadataRef() {
@@ -111,7 +118,10 @@ export default class API {
111118
});
112119
}
113120

114-
storeMetadata(key, data) {
121+
storeMetadata(contentKey, data) {
122+
// metadata filenames need to match branch names - see
123+
// unpublishedEntries in src/backends/github/implementation.js
124+
const key = this.truncateSlugForBranchName(contentKey);
115125
return this.checkMetadataRef()
116126
.then((branchData) => {
117127
const fileTree = {
@@ -135,7 +145,10 @@ export default class API {
135145
});
136146
}
137147

138-
retrieveMetadata(key) {
148+
retrieveMetadata(contentKey) {
149+
// metadata filenames need to match branch names - see
150+
// unpublishedEntries in src/backends/github/implementation.js
151+
const key = this.truncateSlugForBranchName(contentKey);
139152
const cache = LocalForage.getItem(`gh.meta.${ key }`);
140153
return cache.then((cached) => {
141154
if (cached && cached.expires > Date.now()) { return cached.data; }

src/backends/github/implementation.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@ export default class GitHub {
3030

3131
authenticate(state) {
3232
this.token = state.token;
33-
this.api = new API({ token: this.token, branch: this.branch, repo: this.repo, api_root: this.api_root });
33+
this.api = new API({
34+
token: this.token,
35+
branch: this.branch,
36+
repo: this.repo,
37+
api_root: this.api_root,
38+
branchNameMaxLength: this.config.getIn(["backend", "branch_name_max_length"]),
39+
});
3440
return this.api.user().then(user =>
3541
this.api.hasWriteAccess().then((isCollab) => {
3642
// Unauthorized user

0 commit comments

Comments
 (0)