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

Fix multi branch github deployes by appending branch name for cms pul… #1494

Merged
merged 5 commits into from
Sep 4, 2018

Conversation

richtera
Copy link
Contributor

Added support to name space the CMS_BRANCH_PREFIX by including the branch name for non-master branches. This allows multiple deployments to each user their own branch and their own pull requests.

@verythorough
Copy link
Contributor

verythorough commented Jul 10, 2018

Deploy preview for netlify-cms-www ready!

Built with commit 3691a3a

https://deploy-preview-1494--netlify-cms-www.netlify.com

@verythorough
Copy link
Contributor

verythorough commented Jul 10, 2018

Deploy preview for cms-demo ready!

Built with commit 3691a3a

https://deploy-preview-1494--cms-demo.netlify.com

@@ -138,7 +138,7 @@ export default class GitHub {
const promises = [];
branches.map((branch) => {
promises.push(new Promise((resolve, reject) => {
const slug = branch.ref.split("refs/heads/cms/").pop();
const slug = branch.ref.split(`refs/heads/${this.api.cms_branch_prefix}`).pop();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will break sites that don't use master and have existing editorial workflow entries. This kind of backwards compatibility bug is the primary concern with a change like this. We almost need a way to find and transition existing entries, or by some other means ensure that that the change is transparent for existing entries.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@erquhart Doesn't line 14 in API.js address this concern?

this.cms_branch_prefix = this.branch === 'master' ? 'cms/' : `cms-${this.branch}/`;

@richtera
Copy link
Contributor Author

richtera commented Aug 1, 2018

It should not affect people that don't use master. It will just keep changes for each main branch separately. Currently things will get mixed and if you have the CMS attached to two different sites each bound to a different branch then changes for one branch get committed to the last branch that commits the workflow. So it will move changes across branches and remove them from the branch they belong to in some cases.
We have been using this patch in production and it works well

@richtera
Copy link
Contributor Author

richtera commented Aug 1, 2018

Rebased.

@richtera
Copy link
Contributor Author

richtera commented Aug 1, 2018

I had to rebase which makes the original comment invalid in this thread. Basically the one side of the changes is for the write site into github causing it to make commits of cms- for master and cms-BRANCH- for other branchs. The second file is for reading so it reads the last cms- commit matching the branch it came from.

@erquhart
Copy link
Contributor

erquhart commented Aug 1, 2018

You're changing the unpublishedEntries method, which fetches unpublished entries.

Example of how this breaks things:

  1. a project has existing unpublished entries
  2. the project is using the develop branch
  3. existing unpublished entries are on branches prefixed with cms/
  4. with this change, the CMS will look for branches prefixed with cms-develop/
  5. no existing entries will show, existing unpublished entries will be inaccessible

@richtera
Copy link
Contributor Author

richtera commented Aug 2, 2018

@erquhart Yes, but that's the whole point of this patch. When you have two deployments one against master and one against dev with each the cms configured then making changing in dev or master will show up as unpublished entries on the other branch and will get committed on the wrong branch. We have the client work on a deployment in master and we ourselves work on a deployment in dev. When the client has items in their workflow they will see ours and visa versa. So if in dev I work on a certain post of make changes I will all of a sudden see modifications the client has made to the same post and when I merge my changes I will take theirs with it removing them from their change set and workflow.

@tech4him1
Copy link
Contributor

@richtera If I understand correctly, the problem @erquhart mentions would come when someone is using a non-default branch (production, for example), and that is their main deployment -- they are not using seperate CMS instances. We need to handle both the case you are addressing and this case.

@richtera
Copy link
Contributor Author

richtera commented Aug 2, 2018

Maybe we could then make a little config section in the back end that resolved specific CMS prefix names son only people that need it will get the separate bwhavior

@richtera
Copy link
Contributor Author

richtera commented Aug 3, 2018

I changed the implementation to use a custom_branches object to specify the cms prefix. So that way it will not change existing implementations but will allow anyone to configure a multi branch multi deployment setup.
Example:

backend:
  custom_branches:
    master: cms-master
    dev: cms-dev

@tech4him1 tech4him1 requested a review from erquhart August 3, 2018 14:57
@erquhart
Copy link
Contributor

erquhart commented Aug 4, 2018

That makes sense, but it's probably worth it to keep this implementation detail internalized. What we need is a better approach to handling metadata, one that allows us to apply these kinds of changes in a backwards compatible fashion.

For example, rather than just checking for branches, we should use reproducible metadata to determine which branches were created by Netlify CMS, which also gives us the ability to change how we're naming branches. Still haven't thought through some of the challenges of this approach, like the "reproducible" bit, meaning metadata can always be recreated from a repo if the metadata is deleted for some reason.

Note that "metadata" here is referring to one or more files in a branch or ref, which we use to keep track of things.

@richtera
Copy link
Contributor Author

richtera commented Aug 7, 2018

Rebased to current master.

@tech4him1
Copy link
Contributor

tech4him1 commented Aug 7, 2018

@richtera To be clear, we do really appreciate your work here, even through we're looking at other options as well!

@richtera
Copy link
Contributor Author

richtera commented Aug 7, 2018

Using metadata to assign destination branches would be great. I don't know enough about the internals to pull that off myself. Ideally then in the workflow pane you might be able to choose the working branch to peek at other changes and maybe pull them but it might add a lot of complications.

@richtera
Copy link
Contributor Author

richtera commented Aug 21, 2018

@tech4him1 Other options would be fine, but currently the new setup makes it pretty much impossible to roll out my patch (because of the lerna repo.) Essentially my requirement is that each netlify deployment (with cms) linked to a branch of a repo should have a standalone workflow. Not doing this was causing changes to be lost all the time because publishing on the wrong branch would move the pull request to the other branch. How about just configuring the cms branch prefix and not supporting multiple ones since each branch has it's own config.yml anyways? Would that be more acceptable?

@Benaiah
Copy link
Contributor

Benaiah commented Aug 21, 2018

Allowing the CMS branch prefix to be customized in the config.yml makes sense to me - we should allow implementors to work around branch name conflicts regardless of the underlying cause. Even with proper metadata in place like @erquhart suggests, we'll likely still want to use a cms-specific prefix for any branches created by the CMS, so it's worth allowing that branch name to be customized if, for whatever reason, it conflicts. I do think that backend.branch_prefix makes more sense than backend.cms_branch_prefix.

@tech4him1 @erquhart thoughts? Is there a reason we shouldn't make this configurable? We can't reasonably guarantee that cms/<anything> will never be a used branch name in git repos, especially those used for storing content.

Copy link
Contributor

@Benaiah Benaiah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach makes sense to me - @richtera I can write and push the fixes to my review comments to your branch if you'd prefer.

sem.leave();
resolve(null);
}));
}));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like something's been truncated here, since this is a syntax error and the function no longer returns anything, and the PR as a whole needs to be formatted by running yarn run format:prettier --write.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking. This was a merge conflict in the branch which I tried to resolve on github. Bad idea.

this.repoURL = `/repos/${this.repo}`;
this.merge_method = config.squash_merges ? 'squash' : 'merge';
this.branch = config.branch || "master";
if (config.cms_branch_prefix) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The config argument here is not the value of the config.yml, it's the argument passed in here: https://github.com/netlify/netlify-cms/blob/master/packages/netlify-cms-backend-github/src/implementation.js#L45-L52. Thus, this isn't actually going to work, though it's very close.

Additionally, I would suggest we use backend.branch_prefix instead of just cms_branch_prefix at the top level of the config.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I see. I need to fix that I had passed in something else. Fixing.
I like "branch_prefix" as the name. Changing.

cms_branch_prefix: cms-dev
```

The default for this value is "cms"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docs need to be cleaned up before merge.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can clean it up, but I am not sure what cleaning is required?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of the content of the docs here is in the heading, which should just be a short title such as "Custom Branch Name Prefix". The content in the heading should be moved to a short explanatory paragraph, and the 2nd-person voice ("You can configure...", "To enable this feature, you can set...") from earlier in the document should be maintained. A quick note of the reason you might use this (i.e., branch name collisions) would be good too. Thanks!

@erquhart
Copy link
Contributor

I'd like to find a way to use the PR base branch, just leaves the problem of determining which PRs were created by the CMS.

Sent with GitHawk

@richtera
Copy link
Contributor Author

@erquhart I think no matter what you'd need different branch prefixes for multiple source branches since branch names need to be unique and editing the same slug in both branches would still cause a conflict, but maybe there is a workaround there.

@erquhart
Copy link
Contributor

erquhart commented Aug 21, 2018

We could use some kind of shortened unique id. A timestamp could even work for our purposes. I'll give this a think tomorrow.

Sent with GitHawk

@erquhart
Copy link
Contributor

I think this is the next step: #1669

@erquhart
Copy link
Contributor

Gave this another look and it really is a simple enough change. @Benaiah I think I missed your comment on the first pass, agreed.

I do think branch/branch_prefix is going to cause confusion - they look related but they're not at all. We should do worflow_branch_prefix.

@richtera if you can push that change I'll get this merged.

@richtera
Copy link
Contributor Author

@erquhart I still says changes are requested but as far as I know those were changed. Not quite sure how to make sure all the right things have been committed. I simplified this to branch_prefix inside of backend

@erquhart
Copy link
Contributor

It needs to be workflow_branch_prefix, I explained a bit of the reasoning in my last comment.

Sent with GitHawk

@richtera
Copy link
Contributor Author

Ah perfect. I will do it tomorrow morning. The review links always refer to really old stuff for some reason :)

@erquhart
Copy link
Contributor

erquhart commented Aug 31, 2018

@Benaiah I pushed another commit, please sanity check and merge if good.

@richtera
Copy link
Contributor Author

richtera commented Sep 1, 2018

Ah cool. I overlooked the one change I like removing the / from the setting

@erquhart erquhart merged commit da0f520 into decaporg:master Sep 4, 2018
erquhart added a commit that referenced this pull request Sep 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants