-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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 ❤️ |
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 |
* feat: add helper for fetching default branch from Github * feat: add method for setting default branch * fix: set default branch after user has authenticated successfully * fix: format code * feat: add unit test for getting default branch name * feat: add helpers for parsing API responses * feat(lib-util): add helper for constructing request headers * feat(lib-util): add helper for constructing full URL for API request * feat(lib-util): store base URLs for each backend * feat(lib-util): add type annotation for the request config This requestConfig object will be passed to a helper for making API request * feat(lib-util): add helper for handle API request error * feat(lib-util): add config for making api request * feat(lib-util): add api request generator * feat(lib-util): add helper for getting default branch name Include switch clause to construct API urls for different backends * feat(lib-util): export method for getting default branch name * feat(gh-backend): add a boolean property to check if branch is configured The property is needed so that we'll only set default branch when branch prop is missing in config * feat(gh-backend): set prop `branch` as `master` when it's missing in config This is needed so that this property won't be empty when authorization is revoked. * feat(gh-backend): set branch name when it's missing in config * feat(gitlab-backend): set branch when it's not in the config * feat(bitbucket-backend): set branch when it's not specified in config * feat(lib-util): allow token type to be undefined Reason: Requesting information from a public repo doesn't require token * fix: format codes * feat(github): removed setDefaultBranch function Reason: Default branch is already set when calling `authenticate` function * feat(github): remove function for getting default branch * fix (github): removed GithubRepo object because it was never used * fix (gitlab test): Repeat response for getting project info 2 times Reason: The endpoint for getting Gitlab project info is called twice. Need to specify the number of times to repeat the same response as 2, or Nock will throw an error. * fix(gitlab test): add property `default_branch` to project response REASON: Getting a single project through `/projects/:id` returns an object which contains `default_branch` property, among many other props. The mock response needs to convey that. * fix(gitlab test): reformat codes * feat(lib util api): change function name Change from `constructUrl` to `constructUrlWithParams` to indicate that the returned url may contain query string * feat(lib-util api): Change variable name for storing API roots Change from `rootApi` to `apiRoots` to indicate that the varible contains multiple values * feat(lib-util api): Add varialbe for storing endpoint constants * feat(lib-util api): Change the returned value for `getDefaultBranchName` Reason: There's no `null` value for default_branch * feat(api test): Import Nock module for mocking API requests * feat(api test): Add default values for mocking API Default values include: default branch name, mock tokens and mock repo slug * feat(api test): Add mock response to getting a single repo * feat(api test): Add function for itnercepting request to get single repo * feat(api test): Add test for gettingDefaultBranchName * feat(lib-util): reformat codes * feat(jest config): add moduleNameMapper for GitHub and BitBucket Required for the test that checks setDefaultBranchName is called in lib-util to work * feat(lib-util test): return some modules from backend core for testing * feat(lib-util test): add owner login value for Github's repo response The authenticate method of Github API wrapper extracts owner.login from repo resp * feat(lib-util test): change access level for Gitlab to 30 Reason: If access level is under 30, Gitlab package will throw error * feat(lib-util test): add mock response for getting a user The authenticate method of each backend requires this * feat(lib-util test): add default config for backend field * feat(lib-util test): rewrite function for mocking API * feat(lib-util test): rewrite function for mocking request for repo * test(lib-util): rewrite test for the function getDefaultBranchName * test(lib-util): add function for resolving backend * test(lib-util): import 'set' module from Lodash * test(lib-util): add helper for constructing url path for each backend * test(lib-util): add function for intercepting API request to authenticate * test(lib-util): import each backend module * test(lib-util): add tests that check each backend calls getDefaultBranchName * style: format files * fix: query branch name before setting Github API service * fix: reformat implementation module of Github backend * fix: remove importing of getDefaultBranchName from lib * fix: removed test for getDefaultBranchName in lib packages * fix: removed unused vars in api test for lib package * feat: retrieve default branch before creating Bitbucket AI instance * fix: reformat codes in Bitbucket implementation module * fix: add missing import --------- Co-authored-by: Erez Rokah <erezrokah@users.noreply.github.com> Co-authored-by: Martin Jagodic <jagodicmartin1@gmail.com> Co-authored-by: Anze Demsar <anze.demsar@p-m.si>
if (token) { | ||
baseHeaders['Authorization'] = `token ${token}` | ||
baseHeaders['Authorization'] = `token ${token}`; |
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.
Hi @bytrangle, do you by any chance remember, why this is using token
instead of Bearer
here?
This seems to be causing an issue with Gitlab logins, since this PR was merged in April: #7172 (comment)
I'm not sure about Github/Bitbucket, but it seems at least for Gitlab this line should be like this:
baseHeaders['Authorization'] = `token ${token}`; | |
baseHeaders['Authorization'] = `Bearer ${token}`; |
For reference: This line was originally introduced in 3edc3ce, this comment is on a2add4a, which was later squashed to c91a70f
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.
I don't remember. This corresponds to Gitlab docs 13.0, which is no longer available. Please feel free to make a new PR to correct it.
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 anyway, I now created #7242 for this change.
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.
Nice work @floscher
I cloned the decapCMS repo locally yesterday to double check the fix.
After adding the fix and rebuilding decapCMS, I no longer got the previous error message.
However, I then still got an "API_ERROR: Branch not found" message, implying that the code this patch was meant to fix doesn't do it's job on gitlab anyway..
Only after hard-coding "branch: main" into my config.yml, was I then able to get on with things.
…Bearer" (decaporg#7242) The authorization type "Bearer" is more widely recognized than "token". E.g. Gitlab requires that you use "Bearer". This should fix decaporg#7172 where apparently login with Github was broken by this line: decaporg#7172 (comment) See also: decaporg#5844 (comment)
…Bearer" (#7242) The authorization type "Bearer" is more widely recognized than "token". E.g. Gitlab requires that you use "Bearer". This should fix #7172 where apparently login with Github was broken by this line: #7172 (comment) See also: #5844 (comment)
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
.