Skip to content

Commit

Permalink
Expand contributor docs (#993)
Browse files Browse the repository at this point in the history
The monorepo works differently than other repos, and we want to make
sure that contributors are able to use it on their own:

* The release process is different than other repos because it makes use
of `create-release-branch` rather than the `create-release-pr` action.
* Performing tasks (running tests, etc.) across the monorepo is
different than with other repos because it takes advantage of
workspaces.
* Testing changes made to the monorepo in other projects is different
because of the use of preview builds.

This commit moves the existing contributor documentation which was
formerly present in the README to a separate file and adds some more
sections.

Co-authored-by: Maarten Zuidhoorn <[email protected]>
Co-authored-by: Mark Stacey <[email protected]>
  • Loading branch information
3 people authored Dec 1, 2022
1 parent 6777d2c commit 8e5039a
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 48 deletions.
8 changes: 4 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ package-lock.json
.eslintcache

.DS_STORE
coverage
dist
docs
packages/*/coverage
packages/*/dist
packages/*/docs

# yarn v3 (w/o zero-install)
# See: https://yarnpkg.com/getting-started/qa#which-files-should-be-gitignored
Expand All @@ -26,4 +26,4 @@ docs
!.yarn/versions

# typescript
*.tsbuildinfo
packages/*/*.tsbuildinfo
50 changes: 6 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ A collection of platform-agnostic modules for creating secure data models for cr

## Modules

This is a monorepo that houses the following packages. Please refer to the READMEs for these packages for installation and usage instructions:
This repository houses the following packages:

- [`@metamask/address-book-controller`](packages/address-book-controller)
- [`@metamask/announcement-controller`](packages/announcement-controller)
Expand All @@ -26,52 +26,14 @@ This is a monorepo that houses the following packages. Please refer to the READM
- [`@metamask/subject-metadata-controller`](packages/subject-metadata-controller)
- [`@metamask/transaction-controller`](packages/transaction-controller)

Here is a graph that shows the dependencies among all packages:
Or, in graph form [^fn1]:

![Dependency graph](assets/dependency-graph.png)

> **Note**
> To regenerate this graph, run `yarn generate-dependency-graph`.
Refer to individual packages for usage instructions.

## Contributing
## Learn more

### Setup
For instructions on performing common development-related tasks, see [contributing to the monorepo](./docs/contributing.md).

- Install [Node.js](https://nodejs.org) version 14.
- If you are using [nvm](https://github.com/creationix/nvm#installation) (recommended) running `nvm use` will automatically choose the right node version for you.
- Install [Yarn v3](https://yarnpkg.com/getting-started/install).
- Run `yarn install` to install dependencies and run any required post-install scripts.
- Run `yarn simple-git-hooks` to add a [Git hook](https://github.com/toplenboren/simple-git-hooks#what-is-a-git-hook) which will ensure that all files pass the linter before you push a branch.

### Testing and Linting

Run `yarn test` to run tests for all packages. Run `yarn workspace <package-name> run test` to run tests for a single package.

Run `yarn lint` to lint all files and show possible violations, or run `yarn lint:fix` to fix any automatically fixable violations.

### Release & Publishing

This project follows a unique release process. The [`create-release-branch`](https://github.com/MetaMask/create-release-branch) tool and [`action-publish-release`](https://github.com/MetaMask/action-publish-release) GitHub action are used to automate the release process; see those repositories for more information about how they work.

1. To begin the release process, run `create-release-branch`, specifying the packages you want to release. This tool will bump versions and update changelogs across the monorepo automatically, then create a new branch for you.

2. Once you have a new release branch, review the set of package changelogs that the tool has updated. For each changelog, update it to move each change entry into the appropriate change category ([see here](https://keepachangelog.com/en/1.0.0/#types) for the full list of change categories and the correct ordering), and edit them to be more easily understood by users of the package.

- Generally any changes that don't affect consumers of the package (e.g. lockfile changes or development environment changes) are omitted. Exceptions may be made for changes that might be of interest despite not having an effect upon the published package (e.g. major test improvements, security improvements, improved documentation, etc.).
- Try to explain each change in terms that users of the package would understand (e.g. avoid referencing internal variables/concepts).
- Consolidate related changes into one change entry if it makes it easier to explain.
- Run `yarn auto-changelog validate --rc` to check that the changelog is correctly formatted.

3. Submit a pull request for the release branch, so that it can be reviewed and tested.

- If changes are made to the base branch, the release branch will need to be updated with these changes and review/QA will need to restart again. As such, it's probably best to avoid merging other PRs into the base branch while review is underway.

4. Squash & Merge the release.

- This should trigger the [`action-publish-release`](https://github.com/MetaMask/action-publish-release) workflow to tag the final release commit and publish the release on GitHub.

5. Publish the release on npm.

- Wait for the `publish-release` GitHub Action workflow to finish. This should trigger a second job (`publish-npm`), which will wait for a run approval by the [`npm publishers`](https://github.com/orgs/MetaMask/teams/npm-publishers) team.
- Approve the `publish-npm` job (or ask somebody on the npm publishers team to approve it for you).
- Once the `publish-npm` job has finished, check npm to verify that it has been published.
[^fn1]: To regenerate this graph, run `yarn generate-dependency-graph`.
116 changes: 116 additions & 0 deletions docs/contributing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# Contributing to the monorepo

## Getting started

- Install [Node.js](https://nodejs.org) version 14.
- If you're using [NVM](https://github.com/creationix/nvm#installation) (recommended), `nvm use` will ensure that the right version is installed.
- Install [Yarn v3](https://yarnpkg.com/getting-started/install).
- Run `yarn install` to install dependencies and run any required post-install scripts.
- Run `yarn simple-git-hooks` to add a [Git hook](https://github.com/toplenboren/simple-git-hooks#what-is-a-git-hook) to your local development environment which will ensure that all files pass linting before you push a branch.

## Testing

- Run `yarn workspace <workspaceName> run test` to run all tests for a package.
- Run `yarn workspace <workspaceName> run jest --no-coverage <file>` to run a test file within the context of a package.
- Run `yarn test` to run tests for all packages.

> **Note**
> `workspaceName` in these commands is the `name` field within a package's `package.json`, e.g., `@metamask/address-book-controller`, not the directory where it is located, e.g., `packages/address-book-controller`.
## Using controllers in other projects during development/testing

When developing changes to packages within this repository that a different project depends upon, you may wish to load those changes into the project and test them locally or in CI before publishing proper releases of those packages. To solve that problem, this repository provides a mechanism to publish "preview" versions of packages to GitHub Package Registry. These versions can then be used in the project like any other version, provided the project is configured to use that registry.

### As a MetaMask contributor

If you're a MetaMask contributor, you can create these preview versions via draft pull requests:

1. Navigate to your settings within GitHub and [create a classic access token](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token#creating-a-personal-access-token-classic). Make sure to give this token the `packages:read` scope.
2. Switch to your project locally and add a `.npmrc` file with the following content, filling in the appropriate areas:
```
@metamask:registry=https://npm.pkg.github.com
//npm.pkg.github.com/:_authToken=<your personal access token>
```
Make sure not to commit this file.
3. Go to GitHub and open up a pull request for this repository, then post a comment on the PR with the text `@metamask-bot preview-build`. (This triggers the `publish-preview` GitHub action.)
4. After a few minutes, you will see a new comment indicating that all packages have been published with the format `<package name>-<commit id>`.
5. Switch back to your project locally and update `package.json` by replacing the versions for the packages you've changed in your PR using the new version format (e.g. `1.2.3-e2df9b4` instead of `~1.2.3`), then run `yarn install`.
6. Repeat steps 3-5 after pushing new changes to your PR to generate and use new preview versions.

### As an independent contributor

If you're a contributor and you've forked this repository, you can create preview versions for a branch via provided scripts:

1. Navigate to your settings within GitHub and [create a **classic** access token](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token#creating-a-personal-access-token-classic). Make sure to give this token the `packages:read` scope.
2. Switch to your project locally and add a `.npmrc` file with the following content, filling in the appropriate areas:
```
@<your GitHub username>:registry=https://npm.pkg.github.com
//npm.pkg.github.com/:_authToken=<your personal access token>
```
Make sure not to commit this file.
3. Open the `package.json` for each package that you want to publish and change the scope in the name from `@metamask` to `@<your GitHub username>`.
4. Switch to your fork of this repository locally and run `yarn prepare-preview-builds "$(git rev-parse --short HEAD)" && yarn build && yarn publish-previews` to generate preview versions for all packages based on the current branch and publish them to GitHub Package Registry. Take note of the version that is published; it should look like `1.2.3-e2df9b4` instead of `1.2.3`.
5. Switch back to your project and update `package.json` by replacing the versions for all packages you've changed using the version that was output in the previous step, then run `yarn install`.
6. If you make any new changes to your project, repeat steps 3-5 to generate and use new preview versions.
7. As changes will have been made to this repository (due to step 4), make sure to clear out those changes after you've completed testing.

## Linting

Run `yarn lint` to lint all files and show possible violations.

Run `yarn lint:fix` to fix any automatically fixable violations.

## Performing operations across the monorepo

This repository relies on Yarn's [workspaces feature](https://yarnpkg.com/features/workspaces) to provide a way to work with packages individually and collectively. Refer to the documentation for the following Yarn commands for usage instructions:

- [`yarn workspace`](https://yarnpkg.com/cli/workspace)
- [`yarn workspaces foreach`](https://yarnpkg.com/cli/workspaces/foreach)

> **Note**
>
> - `workspaceName` in the Yarn documentation is the `name` field within a package's `package.json`, e.g., `@metamask/address-book-controller`, not the directory where it is located, e.g., `packages/address-book-controller`.
> - `commandName` in the Yarn documentation is any sub-command that the `yarn` executable would usually take. Pay special attention to the difference between `run` vs `exec`. If you want to run a package script, you would use `run`, e.g., `yarn workspace @metamask/address-book-controller run changelog:validate`; but if you want to run _any_ shell command, you'd use `exec`, e.g. `yarn workspace @metamask/address-book-controller exec cat package.json | jq '.version'`.
## Releasing

The [`create-release-branch`](https://github.com/MetaMask/create-release-branch) tool and [`action-publish-release`](https://github.com/MetaMask/action-publish-release) GitHub action are used to automate the release process.

1. **Create a release branch.**

Run `yarn create-release-branch`. This tool generates a file and opens it in your editor, where you can specify which packages you want to include in the next release and which versions they should receive. Instructions are provided for you at the top; read them and update the file accordingly.

When you're ready to continue, save and close the file.

2. **Update changelogs for relevant packages.**

At this point you will be on a new release branch, and a new section will have been added to the changelog of each package you specified in the previous step.

For each changelog, review the new section and make the appropriate changes:

- Move each entry into the appropriate category (review the ["Keep a Changelog"](https://keepachangelog.com/en/1.0.0/#types) spec for the full list of categories and the correct ordering of all categories).
- Remove any changelog entries that don't affect consumers of the package (e.g. lockfile changes or development environment changes). Exceptions may be made for changes that might be of interest despite not having an effect upon the published package (e.g. major test improvements, security improvements, improved documentation, etc.).
- Reword changelog entries to explain changes in terms that users of the package will understand (e.g., avoid referencing internal variables/concepts).
- Consolidate related changes into one change entry if it makes it easier to comprehend.

Run `yarn changelog:validate` to check that all changelogs are correctly formatted.

Commit and push the branch.

3. **Submit a pull request for the release branch so that it can be reviewed and tested.**

Make sure the title of the pull request follows the pattern "Release \<new version\>".

If changes are made to the base branch, the release branch will need to be updated with these changes and review/QA will need to restart again. As such, it's probably best to avoid merging other PRs into the base branch while review is underway.

4. **"Squash & Merge" the release.**

This step triggers the [`publish-release` GitHub action](https://github.com/MetaMask/action-publish-release) workflow to tag the final release commit and publish the release on GitHub.

Pay attention to the box you see when you press the green button and ensure that the final name of the commit follows the pattern "Release \<new version\>".

5. **Publish the release on NPM.**

The `publish-release` GitHub Action workflow runs the `publish-npm` job, which publishes relevant packages to NPM. It requires approval from the [`npm-publishers`](https://github.com/orgs/MetaMask/teams/npm-publishers) team to complete. If you're not on the team, ask a member to approve it for you; otherwise, approve the job.

Once the `publish-npm` job has finished, [check NPM](https://npms.io/search?q=scope%3Ametamask) to verify that all relevant packages has been published.

0 comments on commit 8e5039a

Please sign in to comment.