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: Set correct branch when it's not specified in the config #5844

Merged
merged 72 commits into from Apr 3, 2024

Conversation

bytrangle
Copy link
Collaborator

fix #5617

Summary
Currently, the default branch in Github, Gitlab and Bitbucket is set to be main instead of master.

However, when people use Netlify CMS and don't specify the branch name to push content in the config file, Netlify CMS will automatically push to branch master.

Since this branch may not exist, users will get an error on the frontend: "API_ERROR: Branch not found".

Test plan

  1. In the admin/config.yml file, specify a valid Github repo path to push content to.
  2. Run yarn start.
  3. Go to Neltify CMS dashboard, create a new post and hit Publish. You shouldn't get an error. Checking back on the repo that you specified in the config, you should see the new post.

Checklist

Please add a x inside each checkbox:

  • I have read the contribution guidelines.
  • Code is formatted via running yarn format.
  • Tests are passing via running yarn test.
  • The status checks are successful (continuous integration). Those can be seen below.

@bytrangle bytrangle requested a review from a team September 27, 2021 09:27
@erezrokah erezrokah added the type: bug code to address defects in shipped code label Sep 29, 2021
@bytrangle
Copy link
Collaborator Author

Please hold off on reviewing until I've pushed the unit tests.

Copy link
Contributor

@erezrokah erezrokah left a comment

Choose a reason for hiding this comment

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

Thanks @bytrangle, great work as always.

I've added some comments, please let me know your thoughts.
Also, are we planning to do this for other backends too?

if (!masterBranch) {
// Get default branch of the repo
const defaultBranch = await this.api!.newGetDefaultBranch();
this.branch = defaultBranch || '';
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
this.branch = defaultBranch || '';
this.branch = defaultBranch || 'master';

Should we default to master to ensure the property is set (this will avoid calling setDefaultBranch again if authenticate is re-invoked).

Copy link
Collaborator Author

@bytrangle bytrangle Oct 1, 2021

Choose a reason for hiding this comment

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

Two things:

  1. I didn't consider the scenario that authenticate can be revoked. I will change it as requested.Good point.

  2. I was going to apply the same approach to other backend, but then I realized that would mean writing some repetitive code. That will open up more room for errors and make it harder to track changes in the future. So what do you think about adding a helper in Netlify Utils to get a default branch and call that in each respective backend?

Copy link
Contributor

Choose a reason for hiding this comment

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

2. Netlify Utils to get a default branch and call that in each respective backend?

Sounds good! How about adding the helpers to https://github.com/netlify/netlify-cms/blob/master/packages/netlify-cms-lib-util/src/backendUtil.ts?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Should it be backendUtils or API.ts is the place?
And when do you need these changes? I normally don't work on open source on weekend, but if you need it soon, I can work on this issue on Sunday.

Copy link
Contributor

@erezrokah erezrokah Oct 1, 2021

Choose a reason for hiding this comment

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

Either backendUtils or API.ts work (or both) depending on the common logic you extract.

I normally don't work on open source on weekend, but if you need it soon, I can work on this issue on Sunday.

I definitely don't need it soon, and in any case no need to change your work schedule for us. We are the ones who should adjust our schedule to contributors.

packages/netlify-cms-backend-github/src/implementation.tsx Outdated Show resolved Hide resolved
@@ -1188,6 +1189,16 @@ export default class API {
return result;
}

async newGetDefaultBranch() {
try {
Copy link
Contributor

Choose a reason for hiding this comment

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

How about we drop the try/catch and let the calling function handle any errors (it already has a try/catch)?

This requestConfig object will be passed to a helper for making API request
Include switch clause to construct API urls for different backends
…ured

The property is needed so that we'll only set default branch when branch prop is missing in config
…config

This is needed so that this property won't be empty when authorization is revoked.
Reason: Requesting information from a public repo doesn't require token
@bytrangle
Copy link
Collaborator Author

Sorry that it took such a long time to update this PR. Writing a helper that can accommodate all the major git hosts turn out to be more complex than I had expected.

@bytrangle
Copy link
Collaborator Author

Hello @erezrokah . Let me ask a noob question: How do I rerun the Github action workflow? Or is it only possible for team members?

@erezrokah
Copy link
Contributor

No noob questions - do you have this button when you open the action?
image

@erezrokah
Copy link
Contributor

Regardless, I invited you to collaborate on the repo, long overdue ❤️

@bytrangle
Copy link
Collaborator Author

bytrangle commented Apr 13, 2022

@erezrokah Thank you for the reformat work. I was doing it but you're so fast.

Copy link
Contributor

@erezrokah erezrokah left a comment

Choose a reason for hiding this comment

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

Thanks for following up with these changes @bytrangle, I added some comments.

Also, in order for this change to be backwards compatible we would need to default to master if exists (default or not), and only if it doesn't exist use the default branch.

Please let me know what you think

token: this.token,
});
if (defaultBranchName) {
this.branch = defaultBranchName;
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't setting this.branch here is too late? We already passed this.branch to the API ctor a few lines back https://github.com/netlify/netlify-cms/pull/5844/files#diff-09a7e7be36a8f9bbf96a0407a90bee6df174fef8477a6d68de3472067a884643R199

Copy link
Collaborator Author

@bytrangle bytrangle May 16, 2022

Choose a reason for hiding this comment

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

Indeed, I kept wrestling with the question of when to set this.branch. I put it after the check for user having write access because I thought there was no point setting branch when user is unauthenticated or doesn't have write access.

The restoreUser seems to be the earliest function called in the implementation module, followed by authenticate. Should I call getDefaultBranchName before setting up API in authenticate, or do you recommend another location?

token: this.token,
});
if (defaultBranchName) {
this.branch = defaultBranchName;
Copy link
Contributor

Choose a reason for hiding this comment

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

See my previous comment about setting this.branch too late

token: this.token,
});
if (defaultBranchName) {
this.branch = defaultBranchName;
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment as other backends

}
}

export async function getDefaultBranchName(configs: {
Copy link
Contributor

Choose a reason for hiding this comment

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

What do you think about having a getDefaultBranchName per backend API. The logic seems to be very specific for each API, and that way we won't need to duplicate the error handling, response parsing, etc. (as each API already handles it).

I know we discussed this here, but having the various APIs paths duplicated in the common lib is not a pattern we'd like to follow.

The common logic in this PR is the one that checks if a branch is configured and then fetches the default one if needed. I think it's quite small, so I don't see a reason to de-duplicate it.

What are your thoughts on this?

Copy link
Collaborator Author

@bytrangle bytrangle May 16, 2022

Choose a reason for hiding this comment

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

Yes, that's a good point. I think I was doing that (duplicate getDefaultBranchName per backend) in the draft. Then I thought maybe I would make it a little more efficient by putting it in the lib. But it wasn't.

@stale
Copy link

stale bot commented Apr 26, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@martinjagodic
Copy link
Member

@bytrangle are you still interested in moving this forward?

@paulRbr
Copy link

paulRbr commented Feb 28, 2024

I think this PR is interesting to be merged upstream. I was going to suggest a much more simple change (changing the default value of the branch to main) but I think this PR is better.

@martinjagodic
Copy link
Member

@paulRbr yes, we would like to have this merged, but we need to solve those merge conflicts first.

@bytrangle
Copy link
Collaborator Author

@martinjagodic Hi, I'm interested in working on this again. However, I may have to ask you about resolving merging conflicts later :).

@martinjagodic
Copy link
Member

@bytrangle awesome! The conflicts emerged when we renamed the packages from netlify-cms-* to decap-cms-*. If a rename does not suffice, I am happy to answer questions/help out.

@bytrangle bytrangle requested a review from a team as a code owner March 18, 2024 09:55
@martinjagodic martinjagodic requested review from demshy and removed request for a team March 18, 2024 13:00
@bytrangle
Copy link
Collaborator Author

Hi @demshy . Is there anything else I need to do?

@demshy
Copy link
Member

demshy commented Apr 2, 2024

Sorry @bytrangle I somehow overlooked this one. I added a missing import that made the unit tests fail. I will gladly merge this one into a release this week

@demshy demshy merged commit c91a70f into decaporg:main Apr 3, 2024
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug code to address defects in shipped code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Default to branch name main rather than master
5 participants