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 ❤️

@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
yuri-g-taboola referenced this pull request in taboola/decap-cms May 22, 2024
* 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 <[email protected]>
Co-authored-by: Martin Jagodic <[email protected]>
Co-authored-by: Anze Demsar <[email protected]>
if (token) {
baseHeaders['Authorization'] = `token ${token}`
baseHeaders['Authorization'] = `token ${token}`;
Copy link
Contributor

@floscher floscher Jun 28, 2024

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:

Suggested change
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

Copy link
Collaborator Author

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.

Copy link
Contributor

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.

Copy link

@b-xb b-xb Jul 1, 2024

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.

martinjagodic pushed a commit to jmfiaschi/decap-cms that referenced this pull request Aug 1, 2024
…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)
martinjagodic pushed a commit that referenced this pull request Aug 5, 2024
…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)
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
7 participants