improvement: allow custom workflow branch name prefix (#1494)

This commit is contained in:
Andreas Richter 2018-09-04 12:57:53 -04:00 committed by Shawn Erquhart
parent 62b2f9c06b
commit da0f520f0d
3 changed files with 33 additions and 6 deletions

View File

@ -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 = {}) {

View File

@ -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)

View File

@ -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}}
```