From da0f520f0d4f7af0e0ec8af16fbdd04bc39751b4 Mon Sep 17 00:00:00 2001 From: Andreas Richter Date: Tue, 4 Sep 2018 12:57:53 -0400 Subject: [PATCH] improvement: allow custom workflow branch name prefix (#1494) --- .../netlify-cms-backend-github/src/API.js | 7 +++--- .../src/implementation.js | 7 ++++-- website/content/docs/beta-features.md | 25 +++++++++++++++++++ 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/packages/netlify-cms-backend-github/src/API.js b/packages/netlify-cms-backend-github/src/API.js index 5fc084b6..602dd1dc 100644 --- a/packages/netlify-cms-backend-github/src/API.js +++ b/packages/netlify-cms-backend-github/src/API.js @@ -4,13 +4,12 @@ import { uniq, initial, last, get, find, hasIn, partial, result } from 'lodash'; import { filterPromises, resolvePromiseProperties } from 'netlify-cms-lib-util'; import { APIError, EditorialWorkflowError } from 'netlify-cms-lib-util'; -const CMS_BRANCH_PREFIX = 'cms/'; - export default class API { constructor(config) { this.api_root = config.api_root || 'https://api.github.com'; this.token = config.token || false; this.branch = config.branch || 'master'; + this.workflow_branch_prefix = config.workflow_branch_prefix; this.repo = config.repo || ''; this.repoURL = `/repos/${this.repo}`; this.merge_method = config.squash_merges ? 'squash' : 'merge'; @@ -91,7 +90,7 @@ export default class API { } generateBranchName(basename) { - return `${CMS_BRANCH_PREFIX}${basename}`; + return `${this.workflow_branch_prefix}/${basename}`; } checkMetadataRef() { @@ -650,7 +649,7 @@ export default class API { } assertCmsBranch(branchName) { - return branchName.startsWith(CMS_BRANCH_PREFIX); + return branchName.startsWith(`${this.workflow_branch_prefix}/`); } patchBranch(branchName, sha, opts = {}) { diff --git a/packages/netlify-cms-backend-github/src/implementation.js b/packages/netlify-cms-backend-github/src/implementation.js index ff0719ca..c0fecd89 100644 --- a/packages/netlify-cms-backend-github/src/implementation.js +++ b/packages/netlify-cms-backend-github/src/implementation.js @@ -1,4 +1,4 @@ -import trimStart from 'lodash/trimStart'; +import { trim, trimStart } from 'lodash'; import semaphore from 'semaphore'; import AuthenticationPage from './AuthenticationPage'; import API from './API'; @@ -22,6 +22,8 @@ export default class GitHub { this.repo = config.getIn(['backend', 'repo'], ''); this.branch = config.getIn(['backend', 'branch'], 'master').trim(); + this.workflow_branch_prefix = + trim(config.getIn(['backend', 'workflow_branch_prefix'], ''), '/ ') || 'cms'; this.api_root = config.getIn(['backend', 'api_root'], 'https://api.github.com'); this.token = ''; this.squash_merges = config.getIn(['backend', 'squash_merges']); @@ -40,6 +42,7 @@ export default class GitHub { this.api = new API({ token: this.token, branch: this.branch, + workflow_branch_prefix: this.workflow_branch_prefix, repo: this.repo, api_root: this.api_root, squash_merges: this.squash_merges, @@ -158,7 +161,7 @@ export default class GitHub { branches.map(branch => { promises.push( new Promise(resolve => { - const slug = branch.ref.split('refs/heads/cms/').pop(); + const slug = branch.ref.split(`refs/heads/${this.workflow_branch_prefix}/`).pop(); return sem.take(() => this.api .readUnpublishedBranchFile(slug) diff --git a/website/content/docs/beta-features.md b/website/content/docs/beta-features.md index e19932b7..c20b8e80 100644 --- a/website/content/docs/beta-features.md +++ b/website/content/docs/beta-features.md @@ -120,3 +120,28 @@ Template tags produce the following output: - `{{collection}}`: the name of the collection containing the entry changed - `{{path}}`: the full path to the file changed + +## Custom Editorial Workflow branch name prefixes + +When using [editorial workflow](), a branch is created for each entry. By default the branch name +follows this template: + +``` +cms/{{slug}} +``` + +You can customize the `cms` portion by setting `workflow_branch_prefix` under `backend` in your +`config.yml`: + +```yaml +backend: + name: some-backend + branch: dev + workflow_branch_prefix: cms-dev +``` + +The above configuration would change the branch name template to: + +``` +cms-dev/{{slug}} +``` \ No newline at end of file