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
Conversation
Please hold off on reviewing until I've pushed the unit tests. |
There was a problem hiding this 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 || ''; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two things:
-
I didn't consider the scenario that
authenticate
can be revoked. I will change it as requested.Good point. -
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
@@ -1188,6 +1189,16 @@ export default class API { | |||
return result; | |||
} | |||
|
|||
async newGetDefaultBranch() { | |||
try { |
There was a problem hiding this comment.
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
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. |
Hello @erezrokah . Let me ask a noob question: How do I rerun the Github action workflow? Or is it only possible for team members? |
Regardless, I invited you to collaborate on the repo, long overdue ❤️ |
@erezrokah Thank you for the reformat work. I was doing it but you're so fast. |
There was a problem hiding this 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; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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: { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
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. |
@bytrangle are you still interested in moving this forward? |
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 |
@paulRbr yes, we would like to have this merged, but we need to solve those merge conflicts first. |
@martinjagodic Hi, I'm interested in working on this again. However, I may have to ask you about resolving merging conflicts later :). |
@bytrangle awesome! The conflicts emerged when we renamed the packages from |
Hi @demshy . Is there anything else I need to do? |
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 |
fix #5617
Summary
Currently, the default branch in Github, Gitlab and Bitbucket is set to be
main
instead ofmaster
.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
admin/config.yml
file, specify a valid Github repo path to push content to.yarn start
.Checklist
Please add a
x
inside each checkbox:yarn format
.yarn test
.