generated from MetaMask/metamask-module-template
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add function to load known MetaMask repos
If a user passes a bare identifier on the command line, like this: ``` yarn dlx @metamask/module-lint utils ``` then we assume that they want to lint the `MetaMask/utils` repository. However we have to double-check that the repo they want to lint actually exists. They shouldn't be able to do this, for instance: ``` yarn dlx @metamask/module-lint asdlsdfl ``` The way we do this is by pulling the list of repositories that sit under the MetaMask GitHub organization. We exclude forks as well as archived repos. This list is cached for an hour so that future runs of the tool do not cause the rate limit for the GitHub API to be exceeded.
- Loading branch information
Showing
5 changed files
with
163 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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,43 @@ | ||
import execa from 'execa'; | ||
|
||
import { ensureMetaMaskRepositoriesLoaded } from './ensure-metamask-repositories-loaded'; | ||
import type { PrimaryExecaFunction } from '../tests/helpers'; | ||
import { mockExeca } from '../tests/helpers'; | ||
|
||
jest.mock('execa'); | ||
|
||
const execaMock = jest.mocked<PrimaryExecaFunction>(execa); | ||
|
||
describe('ensureMetaMaskRepositoriesLoaded', () => { | ||
it('requests the repositories under the MetaMask GitHub organization, limiting the data to just a few fields', async () => { | ||
mockExeca(execaMock, [ | ||
{ | ||
args: [ | ||
'gh', | ||
['api', 'orgs/MetaMask/repos', '--cache', '1h', '--paginate'], | ||
], | ||
result: { | ||
stdout: JSON.stringify([ | ||
{ name: 'utils', fork: false, archived: false, extra: 'info' }, | ||
{ name: 'logo', fork: false, archived: false }, | ||
{ | ||
name: 'ethjs-util', | ||
fork: true, | ||
archived: false, | ||
something: 'else', | ||
}, | ||
{ name: 'test-snaps', fork: true, archived: true }, | ||
]), | ||
}, | ||
}, | ||
]); | ||
|
||
const gitHubRepositories = await ensureMetaMaskRepositoriesLoaded(); | ||
expect(gitHubRepositories).toStrictEqual([ | ||
{ name: 'utils', fork: false, archived: false }, | ||
{ name: 'logo', fork: false, archived: false }, | ||
{ name: 'ethjs-util', fork: true, archived: false }, | ||
{ name: 'test-snaps', fork: true, archived: true }, | ||
]); | ||
}); | ||
}); |
This file contains 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,42 @@ | ||
import execa from 'execa'; | ||
|
||
/** | ||
* The information about a GitHub repository that we care about. Primarily, | ||
* we want to know whether repos are forks or have been archived, because we | ||
* don't want to lint them. | ||
*/ | ||
type GitHubRepository = { | ||
name: string; | ||
fork: boolean; | ||
archived: boolean; | ||
}; | ||
|
||
/** | ||
* Requests data for the repositories listed under MetaMask's GitHub | ||
* organization via the GitHub API, or returns the results from a previous call. | ||
* The data is cached for an hour to prevent unnecessary calls to the GitHub | ||
* API. | ||
* | ||
* @returns The list of repositories (whether previously or newly cached). | ||
*/ | ||
export async function ensureMetaMaskRepositoriesLoaded(): Promise< | ||
GitHubRepository[] | ||
> { | ||
const { stdout } = await execa('gh', [ | ||
'api', | ||
'orgs/MetaMask/repos', | ||
'--cache', | ||
'1h', | ||
'--paginate', | ||
]); | ||
const fullGitHubRepositories = JSON.parse(stdout); | ||
return fullGitHubRepositories.map( | ||
(fullGitHubRepository: Record<string, unknown>) => { | ||
return { | ||
name: fullGitHubRepository.name, | ||
fork: fullGitHubRepository.fork, | ||
archived: fullGitHubRepository.archived, | ||
}; | ||
}, | ||
); | ||
} |
This file contains 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 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