Read and set a package's top-level owner - #65
Merged
Merged
Conversation
`ParsePackwerk::Package` had no way to read or change the top-level `owner` key, even though `write_package_yml!` already orders it among the canonical keys: `package.owner` raised NoMethodError, and `package.with(owner: "x")` raised ArgumentError, since T::Struct#with only accepts declared props. The only way in was editing `package.config['owner']` by hand. `owner` is a reader over `config['owner']` rather than a new prop, and `with(owner:)` sets that key, or removes it when given nil. With a separate prop, a package loaded from disk would hold its owner in two places, and the prop would silently override any edit made through `config`, which is the workaround in use today. Keeping `config` as the only store leaves every path that works on main byte-identical. A value that isn't a String reads as nil and is still written back unchanged. `metadata.owner` is a separate, older convention and isn't read into `owner`. Fixes #49. Also bumps the version to 0.28.0.
`with(owner:)` passed the whole config back through `T::Struct#with`, which
stringifies every hash key it is given, so a nested key such as `1:` or
`true:` was written back as `'1':` or `'true':`. It now edits the copy that
`super` returns, which is already a deep copy of the receiver's config, so
only the owner line changes and the file matches what the
`config['owner'] = ...` workaround writes. This also stops
`with(owner:, 'config' => {...})` from discarding the given config.
`with(owner:)` now raises TypeError for a value that isn't a String or nil,
rather than writing one that `owner` would then read as nil.
The comment on `owner` notes that it reads only the top-level key, unlike
`CodeOwnership.for_package`, which falls back to `metadata.owner`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #49. Thank you to @oskarpearson for the clear report.
Summary
ParsePackwerk::Packagehad no way to read or change the top-levelownerkey, even thoughwrite_package_yml!already orders it among the canonical keys.package.ownerraisedNoMethodError, andpackage.with(owner: "x")raisedArgumentError, becauseT::Struct#withonly accepts declared props. The only way in was editingpackage.config['owner']by hand.package.ownerreturns the top-levelowneras aString, ornil.package.with(owner: "Team")sets it, andpackage.with(owner: nil)removes it.write_package_yml!writes it with no other changes. Any other value raisesTypeError.metadata.owneris a separate, older convention and isn't read intoowner. Soownercan benilfor a pack thatCodeOwnership.for_packagestill resolves throughmetadata.owner.This bumps the version to 0.28.0, so merging publishes it to RubyGems through
cd.yml.Why
ownerreads fromconfiginstead of being a new propMy first version added a
const :ownerprop. Review caught that a package loaded from disk would then hold its owner in two places, and on write the prop would silently override any edit made throughconfig. That's the workaround in use today (package.config['owner'] = ...), and main honours it. Soowneris a reader overconfig['owner'], andwith(owner:)edits that key. With a single store, the two can't disagree, and every path that works on main behaves exactly as before.A value that isn't a
String(e.g.owner: 123) reads asniland is written back unchanged. A typed prop would have raisedTypeErrorinsideParsePackwerk.allon such a file.with(owner:)edits the copy ofconfigthatT::Struct#withreturns, rather than passingconfig:to it.T::Struct#withstringifies every key it's given, so passingconfig:would have rewritten a nested key such as1:as'1':.Package.newhas noowner:argument. To build a package with an owner, pass it inconfig, as callers such aspacksalready do, or use.with(owner:).Test plan
bundle exec rspec: 68 examples, 0 failures.srb tcandrubocopare clean.ownerorwith(owner:)fails against main'slibwithNoMethodErrororArgumentError.liband this branch'slib, withpreserve_key_orderboth on and off, and with a string owner,owner: 123, and ametadata-only owner. The scenarios were an unchanged round trip,config['owner'] = ...,with(config:)with the owner changed or removed, andwith(dependencies: []). The written files are byte-identical in all 30 runs. A broader harness (14 fixtures, 17 existing-usage scenarios,preserve_key_orderon and off: 476 runs) is also byte-identical to main, andwith(owner:)writes the same bytes as main'sconfig['owner'] = ...workaround in all 140 runs.liband the first version of this change. That version stored owner as a separate prop, and none of these gems passesowner:toPackage.neworwith, so the reworked version doesn't change what they exercise.