8000 fix(provider): fix npm provider to update package-lock.json and npm-shrinkwrap.json if they exist by davidlday · Pull Request #813 · commitizen-tools/commitizen · GitHub
[go: up one dir, main page]

Skip to content

fix(provider): fix npm provider to update package-lock.json and npm-shrinkwrap.json if they exist #813

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

Merged
merged 7 commits into from
Aug 26, 2023

Conversation

davidlday
Copy link
Contributor

closes #804

Description

Added new version provider npm2 which uses jsonpath_ng to update version in

  • package.json: $.version
  • package-lock.json (if it exists): $.version, $.packages.''.version
  • npm-shrinkwrap.json (if it exists): $.version, $.packages.''.version

package-lock.json and npm-shrinkwrap.json may or may not exist.

Checklist

  • Add test cases to all the changes you introduce
  • Run ./scripts/format and ./scripts/test locally to ensure this change passes linter check and test
  • Test the changes on the local machine manually
  • Update the documentation for the changes

Expected behavior

When using npm2 as the version provider, the following files will have the project's version number updated appropriately:

  • package.json (required)
  • package-lock.json (optional)
  • npm-shrinkwrap.json (optional)

Steps to Test This Pull Request

Test cases added to tests/test_version_providers.py.

Additional context

@codecov
Copy link
codecov bot commented Aug 12, 2023

Codecov Report

Patch coverage: 100.00% and project coverage change: +0.09% 🎉

Comparison is base (120d514) 97.33% compared to head (d904094) 97.43%.
Report is 5 commits behind head on master.

❗ Current head d904094 differs from pull request most recent head cf0c20d. Consider uploading reports for the commit cf0c20d to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #813      +/-   ##
==========================================
+ Coverage   97.33%   97.43%   +0.09%     
==========================================
  Files          42       42              
  Lines        2104     2141      +37     
==========================================
+ Hits         2048     2086      +38     
+ Misses         56       55       -1     
Flag Coverage Δ
unittests 97.43% <100.00%> (+0.09%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Changed Coverage Δ
commitizen/providers.py 98.10% <100.00%> (+0.58%) ⬆️

... and 1 file with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member
@Lee-W Lee-W left a comment

Choose a reason for hiding this comment

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

Just left a few comments 🙂

@@ -1,6 +1,7 @@
from __future__ import annotations

import json
import jsonpath_ng # type: ignore
Copy link
Member

Choose a reason for hiding this comment

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

May I know why do we need #type: ignore here

Copy link
Member

Choose a reason for hiding this comment

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

Also, may I know why do we need to install this package?

Copy link
Member

Choose a reason for hiding this comment

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

I'd also like to know, is it not possible to update it accessing like a json?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The problem came from trying to access the correct element in "packages". Here's the sample JSON:

{
  "name": "whatever",
  "version": "0.1.0",
  "lockfileVersion": 2,
  "requires": true,
  "packages": {
    "": {
      "name": "whatever",
      "version": "0.1.0"
    },
    "someotherpackage": {
      "version": "0.1.0"
    }
  }
}

The json path expression is $.packages.''.version, but I couldn't get that to work via the json object directly.

And the # type: ignore is there b/c the library doesn't appear to have type hints, nor is there anything available for it on typeshed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay - so not sure why I was struggling with using the json.loads and updated the dict, but I have that working now. Will remove the dependency on jsonpath_ng.

@@ -166,6 +167,87 @@ class NpmProvider(JsonProvider):
filename = "package.json"


class Npm2Provider(VersionProvider):
Copy link
Member

Choose a reason for hiding this comment

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

As mentioned in the issue, I'd suggest we update the NpmProvider directly, but I'm not a JavaScript users. Would love to see how @woile thinks.

Copy link
Member

Choose a reason for hiding this comment

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

I think so as well, if package-lock.json and or npm-shrinkwrap.json exists they should be updated.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense. I'll go back and update the existing NpmProvider and take out the Npm2Provider.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should I change the PR from feat to fix then?

@davidlday davidlday changed the title feat(provider): add npm2 provider to update package.json, package-lock.json, and npm-shrinkwrap.json fix(provider):fix npm provider to update package-lock.json and npm-shrinkwrap.json if they exist Aug 22, 2023
@davidlday davidlday changed the title fix(provider):fix npm provider to update package-lock.json and npm-shrinkwrap.json if they exist fix(provider): fix npm provider to update package-lock.json and npm-shrinkwrap.json if they exist Aug 22, 2023
Copy link
Member
@Lee-W Lee-W left a comment

Choose a reason for hiding this comment

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

Hi @davidlday , thanks for your contribution! The overall implementation is great! Just a few minor thing needs to be fixed

Copy link
Member
@woile woile left a comment

Choose a reason for hiding this comment

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

LGTM, thank you so much! @Lee-W take a last look and if ready go ahead and merge 🙏🏻

Copy link
Member
@Lee-W Lee-W left a comment

Choose a reason for hiding this comment

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

@davidlday Awesome! Just left one minor suggestion. After rebasing, we're good to go

Comment on lines 281 to 288
@pytest.mark.parametrize(
"pkg_lock_content,pkg_lock_expected,pkg_shrinkwrap_content,pkg_shrinkwrap_expected",
(
(None, None, None, None),
(NPM_LOCKFILE_JSON, NPM_LOCKFILE_EXPECTED, None, None),
(None, None, NPM_LOCKFILE_JSON, NPM_LOCKFILE_EXPECTED),
(
NPM_LOCKFILE_JSON,
NPM_LOCKFILE_EXPECTED,
NPM_LOCKFILE_JSON,
NPM_LOCKFILE_EXPECTED,
),
),
)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
@pytest.mark.parametrize(
"pkg_lock_content,pkg_lock_expected,pkg_shrinkwrap_content,pkg_shrinkwrap_expected",
(
(None, None, None, None),
(NPM_LOCKFILE_JSON, NPM_LOCKFILE_EXPECTED, None, None),
(None, None, NPM_LOCKFILE_JSON, NPM_LOCKFILE_EXPECTED),
(
NPM_LOCKFILE_JSON,
NPM_LOCKFILE_EXPECTED,
NPM_LOCKFILE_JSON,
NPM_LOCKFILE_EXPECTED,
),
),
)
@pytest.mark.parametrize(
"pkg_shrinkwrap_content, pkg_shrinkwrap_expected",
(
(NPM_LOCKFILE_JSON, NPM_LOCKFILE_EXPECTED),
(None, None)
)
)
@pytest.mark.parametrize(
"pkg_lock_content, pkg_lock_expected",
(
(NPM_LOCKFILE_JSON, NPM_LOCKFILE_EXPECTED),
(None, None)
)
)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Lee-W - Applying suggestion now. Sorry, you made this suggestion earlier, and I wasn't getting it. Makes sense to me now.

@Lee-W Lee-W added pr-status: ready-to-merge almost ready to merge. just keep it for a few days for others to check and removed pr-status: wait-for-review labels Aug 24, 2023
@Lee-W
Copy link
Member
Lee-W commented Aug 26, 2023

Hi @davidlday , could you please rebase from the master branch so that we can merge it? You can do it through git rebase -i master Thanks!

@davidlday
Copy link
Contributor Author

@Lee-W - apologies, been awhile since I've done a rebase. Let me know if I messed anything up.

@Lee-W
Copy link
Member
Lee-W commented Aug 26, 2023

@Lee-W - apologies, been awhile since I've done a rebase. Let me know if I messed anything up.

No worry 🙂 Just want to fix a CI issue along with this PR.

@Lee-W Lee-W merged commit 37f1371 into commitizen-tools:master Aug 26, 2023
@davidlday davidlday deleted the feat/npm2 branch August 26, 2023 18:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr-status: ready-to-merge almost ready to merge. just keep it for a few days for others to check pr-status: wait-for-modification pr-status: wait-for-review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expected Version Provider Behavior - npm should update package.json, package-lock.json if present, and npm-shrinkwrap.json if present.
3 participants
0