-
Notifications
You must be signed in to change notification settings - Fork 185
Add git commit hash in output comments for Community Contracts #627
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
Changes from 19 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
b65e088
Use git commit hash in package dependencies
ericglau b809904
Add compatible Community Contracts version
ericglau 406cd6c
Reorganize print library versions
ericglau 9094a99
Inject commit hash for comment and import links
ericglau a216151
Extract git commit from package json
ericglau 7e03cf6
Simplify inject hyperlinks
ericglau 7872cfd
Improve inject hyperlinks logic
ericglau 96044c4
Revert prepare
ericglau 7739d3d
Update error msg
ericglau 4fd2179
Merge remote-tracking branch 'upstream/master' into communityhash
ericglau 83d74d9
Use regex source
ericglau c7f468f
Catch errors at runtime
ericglau cc37716
Update packages/ui/src/solidity/inject-hyperlinks.ts
ericglau 5bca3cd
Add changeset
ericglau 22e6639
Add stricter commit hash format check
ericglau cdf618d
Address review comments
ericglau a2ef81f
Add unit tests
ericglau 4bd1aa7
Use single line for compatible comment
ericglau 1afcd4f
Update snapshots
ericglau f7dcaf0
Clarify comment
ericglau 11ec9af
Merge branch 'master' into communityhash
CoveMB 615ebbe
Change comment link color
ericglau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@openzeppelin/wizard': patch | ||
| --- | ||
|
|
||
| Add compatible git commit in comments when importing OpenZeppelin Community Contracts |
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
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
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
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
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
Binary file not shown.
115 changes: 115 additions & 0 deletions
115
packages/core/solidity/src/utils/community-contracts-git-commit.test.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| import test from 'ava'; | ||
|
|
||
| import { extractGitCommitHash } from './community-contracts-git-commit'; | ||
|
|
||
| // Valid cases | ||
|
|
||
| test('extractGitCommitHash - lowercase 40-char hash', t => { | ||
| const hash = extractGitCommitHash( | ||
| '@openzeppelin/community-contracts', | ||
| 'git+https://github.com/OpenZeppelin/openzeppelin-community-contracts.git#0123456789abcdef0123456789abcdef01234567', | ||
| ); | ||
| t.is(hash, '0123456789abcdef0123456789abcdef01234567'); | ||
| }); | ||
|
|
||
| test('extractGitCommitHash - uppercase 7-char hash returns lowercase', t => { | ||
| const hash = extractGitCommitHash( | ||
| '@openzeppelin/community-contracts', | ||
| 'git+ssh://[email protected]/OpenZeppelin/openzeppelin-community-contracts.git#ABCDEF1', | ||
| ); | ||
| t.is(hash, 'abcdef1'); | ||
| }); | ||
|
|
||
| // Invalid format cases | ||
|
|
||
| test('extractGitCommitHash - missing git+ prefix', t => { | ||
| const err = t.throws(() => | ||
| extractGitCommitHash( | ||
| '@openzeppelin/community-contracts', | ||
| 'https://github.com/OpenZeppelin/openzeppelin-community-contracts.git#abcdef1', | ||
| ), | ||
| ); | ||
| t.true( | ||
| err instanceof Error && | ||
| err.message.includes( | ||
| 'Expected package dependency for @openzeppelin/community-contracts in format git+<url>#<commit-hash>,', | ||
| ), | ||
| ); | ||
| }); | ||
|
|
||
| test('extractGitCommitHash - missing #', t => { | ||
| const err = t.throws(() => | ||
| extractGitCommitHash( | ||
| '@openzeppelin/community-contracts', | ||
| 'git+https://github.com/OpenZeppelin/openzeppelin-community-contracts.git', | ||
| ), | ||
| ); | ||
| t.true( | ||
| err instanceof Error && | ||
| err.message.includes( | ||
| 'Expected package dependency for @openzeppelin/community-contracts in format git+<url>#<commit-hash>,', | ||
| ), | ||
| ); | ||
| }); | ||
|
|
||
| test('extractGitCommitHash - multiple # parts', t => { | ||
| const err = t.throws(() => | ||
| extractGitCommitHash( | ||
| '@openzeppelin/community-contracts', | ||
| 'git+https://github.com/OpenZeppelin/openzeppelin-community-contracts.git#abcdef1#extra', | ||
| ), | ||
| ); | ||
| t.true( | ||
| err instanceof Error && | ||
| err.message.includes( | ||
| 'Expected package dependency for @openzeppelin/community-contracts in format git+<url>#<commit-hash>,', | ||
| ), | ||
| ); | ||
| }); | ||
|
|
||
| // Invalid hash content cases | ||
|
|
||
| test('extractGitCommitHash - too short hash', t => { | ||
| const err = t.throws(() => | ||
| extractGitCommitHash( | ||
| '@openzeppelin/community-contracts', | ||
| 'git+https://github.com/OpenZeppelin/openzeppelin-community-contracts.git#abcde', | ||
| ), | ||
| ); | ||
| t.true( | ||
| err instanceof Error && | ||
| err.message.includes( | ||
| 'Expected git commit hash for package dependency @openzeppelin/community-contracts to have between 7 and 40 hex chars', | ||
| ), | ||
| ); | ||
| }); | ||
|
|
||
| test('extractGitCommitHash - too long hash', t => { | ||
| const err = t.throws(() => | ||
| extractGitCommitHash( | ||
| '@openzeppelin/community-contracts', | ||
| 'git+https://github.com/OpenZeppelin/openzeppelin-community-contracts.git#0123456789abcdef0123456789abcdef012345678', | ||
| ), | ||
| ); | ||
| t.true( | ||
| err instanceof Error && | ||
| err.message.includes( | ||
| 'Expected git commit hash for package dependency @openzeppelin/community-contracts to have between 7 and 40 hex chars', | ||
| ), | ||
| ); | ||
| }); | ||
|
|
||
| test('extractGitCommitHash - non-hex characters', t => { | ||
| const err = t.throws(() => | ||
| extractGitCommitHash( | ||
| '@openzeppelin/community-contracts', | ||
| 'git+https://github.com/OpenZeppelin/openzeppelin-community-contracts.git#abcdefg', | ||
| ), | ||
| ); | ||
| t.true( | ||
| err instanceof Error && | ||
| err.message.includes( | ||
| 'Expected git commit hash for package dependency @openzeppelin/community-contracts to have between 7 and 40 hex chars', | ||
| ), | ||
| ); | ||
| }); |
38 changes: 38 additions & 0 deletions
38
packages/core/solidity/src/utils/community-contracts-git-commit.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import { devDependencies } from '../../package.json'; | ||
|
|
||
| /** | ||
| * @returns The git commit hash of the @openzeppelin/community-contracts package dependency. | ||
| * @throws Error if the @openzeppelin/community-contracts package dependency is not found in devDependencies. | ||
| */ | ||
| export function getCommunityContractsGitCommit(): string { | ||
| const communityContractsVersion = devDependencies['@openzeppelin/community-contracts']; | ||
| if (!communityContractsVersion) { | ||
| throw new Error('@openzeppelin/community-contracts not found in devDependencies'); | ||
| } | ||
| return extractGitCommitHash('@openzeppelin/community-contracts', communityContractsVersion); | ||
| } | ||
|
|
||
| /** | ||
| * Extracts the git commit hash from a package dependency version string. | ||
| * The expected format is `git+<url>#<commit-hash>`. | ||
| * | ||
| * @param dependencyName The name of the package dependency. | ||
| * @param dependencyVersion The version string of the package dependency. | ||
| * @returns The git commit hash. | ||
| * @throws Error if the version string or commit hash is not in the expected format. | ||
| */ | ||
| export function extractGitCommitHash(dependencyName: string, dependencyVersion: string): string { | ||
| const split = dependencyVersion.split('#'); | ||
| if (!dependencyVersion.startsWith('git+') || split.length !== 2) { | ||
| throw new Error( | ||
| `Expected package dependency for ${dependencyName} in format git+<url>#<commit-hash>, but got ${dependencyVersion}`, | ||
| ); | ||
| } | ||
| const hash = split[1]!; | ||
| if (!/^[a-fA-F0-9]{7,40}$/.test(hash)) { | ||
| throw new Error( | ||
| `Expected git commit hash for package dependency ${dependencyName} to have between 7 and 40 hex chars, but got ${hash}`, | ||
| ); | ||
| } | ||
| return hash.toLowerCase(); | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import type { Contract } from '../contract'; | ||
|
|
||
| export function importsCommunityContracts(contract: Contract) { | ||
| return contract.imports.some(i => i.path.startsWith('@openzeppelin/community-contracts/')); | ||
| } | ||
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
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
Oops, something went wrong.
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.
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.
nits: as it returns a boolean maybe could be named doesImportsCommunityContracts
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.
The s in imports implies boolean