Skip to content
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

build: add automated releases and pre-commit hooks #289

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .commitlintrc.json
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
{ "extends": ["@commitlint/config-conventional"] }
{
"extends": ["@commitlint/config-conventional"],
"rules": {
"body-max-line-length": [0]
Maytha8 marked this conversation as resolved.
Show resolved Hide resolved
}
}
64 changes: 41 additions & 23 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -1,31 +1,49 @@
name: 'tests'

on:
push:
branches: [master]
pull_request:
branches: [master]
on: [push, pull_request]
Maytha8 marked this conversation as resolved.
Show resolved Hide resolved

jobs:
tests:
runs-on: 'ubuntu-latest'

publish:
needs: ci
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
persist-credentials: false
- uses: actions/setup-node@v3
with:
node-version: latest
- run: npm install
- run: npx semantic-release
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
ci:
needs: ci-matrix
runs-on: ubuntu-latest
steps:
- run: exit 0
ci-matrix:
needs: get-supported-node-versions
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12.x, 14.x, 16.x]

node-version: ${{ fromJson(needs.get-supported-node-versions.outputs.versions) }}
steps:
- uses: 'actions/checkout@v3.0.2'

- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Use Node.js ${{ matrix.node-version }}
uses: 'actions/setup-node@v3.5.0'
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}

- name: 'Install Dependencies'
run: 'npm install'

- run: 'npm run lint:commit -- --to "${{ github.sha }}"'
- run: 'npm run lint:standard'
- run: 'npm run lint:ts-standard'
- run: 'npm run test'
- run: npm install
- run: npm run lint:commit -- --from "7b4219a67b4fe94b1a1487b7d545d0cc7742f770" --to "${{ github.sha }}"
Copy link
Member

Choose a reason for hiding this comment

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

Why adding this from flag with a hard-coded commit hash?

Copy link
Author

@Maytha8 Maytha8 Jun 12, 2023

Choose a reason for hiding this comment

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

This is because some old commits in the commit history don't follow the format and cause commitlint to error, as you can see below. (This log excludes the errors caused by dependabot's messages being too long, as body-max-line-len was disabled at the time of running this.)

$ npm run lint:commit -- --to "79b63eb"

> ts-standard@12.0.3 lint:commit
> commitlint --to 79b63eb

⧗   input: feat(package): Allow multiple projects to be used (#163)
✖   subject must not be sentence-case, start-case, pascal-case, upper-case [subject-case]

✖   found 1 problems, 0 warnings
ⓘ   Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint

⧗   input: fix(badges): Fix readme file badges
✖   subject must not be sentence-case, start-case, pascal-case, upper-case [subject-case]

✖   found 1 problems, 0 warnings
ⓘ   Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint

⧗   input: fix(typescript): Try to make typescript a peerDependency
✖   subject must not be sentence-case, start-case, pascal-case, upper-case [subject-case]

✖   found 1 problems, 0 warnings
ⓘ   Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint

⧗   input: Initial commit
✖   subject may not be empty [subject-empty]
✖   type may not be empty [type-empty]

✖   found 2 problems, 0 warnings
ⓘ   Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint

I've hardcoded it from the latest commit on standard/ts-standard as that's the last commit before my commits, and this is when I've setup a precommit hook for commitlint.

- run: npm run lint:standard
- run: npm run lint:ts-standard
- run: npm run test
get-supported-node-versions:
runs-on: ubuntu-latest
steps:
- id: get
uses: msimerson/node-lts-versions@v1
outputs:
versions: ${{ steps.get.outputs.active }}
4 changes: 4 additions & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npx --no -- commitlint --edit "$1"
6 changes: 6 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npm run test
npm run lint:standard -- --fix
npm run lint:ts-standard -- --fix
44 changes: 44 additions & 0 deletions .releaserc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
branches:
- master
plugins:
- - "@semantic-release/commit-analyzer"
- releaseRules:
- breaking: true
release: major
- revert: true
release: patch
- type: feat
release: minor
- type: fix
release: patch
- type: perf
release: patch
- type: docs
release: patch
- - "@semantic-release/release-notes-generator"
- preset: conventionalcommits
presetConfig:
types:
- type: build
section: Build system / dependencies
- type: ci
section: CI
- type: docs
section: Documentation
- type: feat
section: Features
- type: fix
section: Bug fixes
- type: perf
section: Performance
- type: refactor
section: Refactoring
- type: text
section: Testing
- "@semantic-release/changelog"
- "@semantic-release/github"
- "@semantic-release/npm"
- - "@semantic-release/git"
- assets:
- CHANGELOG.md
- package.json
Comment on lines +42 to +44
Copy link
Member

Choose a reason for hiding this comment

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

That might be best to not have any assets.
The changelog will be published with the GitHub Release, what do you think? 😄

Copy link
Author

@Maytha8 Maytha8 Jun 12, 2023

Choose a reason for hiding this comment

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

CHANGELOG.md
This file already exists in this repo, so I might as well setup semantic-release to update it. 😃
@semantic-release/changelog will update the changelog automatically for us.

package.json
@semantic-release/npm updates the package.json version, so might as well commit that.

Copy link
Author

Choose a reason for hiding this comment

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

See #289 (comment) regarding package.json.

2 changes: 0 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# Changelog

## 13.0.0 - Pending

## 12.0.2

- **Fix:** Allow absolute `--project` path [(#269)](https://github.com/standard/ts-standard/pull/269).
Expand Down
26 changes: 26 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Contributing

I welcome all pull requests. Please make sure you add appropriate test cases for any features
added.

> New to contributing? Check out [this How to Contribute guide](https://opensource.guide/how-to-contribute/).

## Setup

It is assumed that you already have a copy of this project.

To setup your development environment, run the following command inside the project folder:

```sh
npm install
```

This will install the required tools, dependencies, and git hooks.

## Committing

We follow the [Conventional Commits specification](https://www.conventionalcommits.org/en/v1.0.0/) and this is enforced by commitlint. Commits with incorrectly formatted messages will result in an error from git and the commit to be rejected.

When committing, tests and linters are also run. If any of them fail, git will throw an error and the commit will be rejected.

> Note that linters are run with the `--fix` argument, so any fixable issues will be fixed automatically.
13 changes: 3 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,7 @@ You can add an `ignore` property to your `package.json` `ts-standard` configurat
```json
{
"ts-standard": {
"ignore": [
"dist",
"src/**/*.js"
]
"ignore": ["dist", "src/**/*.js"]
}
}
```
Expand Down Expand Up @@ -119,10 +116,6 @@ Special thanks to [`standard`](https://github.com/standard/standard) for inspira
to [`eslint-config-standard-with-typescript`](https://github.com/standard/eslint-config-standard-with-typescript) for
creating a typescript specific standard.

## 📋 Contributing Guide
## 📋 Contributing

I welcome all pull requests. Please make sure you add appropriate test cases for any features
added. Before opening a PR please make sure to run the following scripts:

- `npm run lint:standard` checks for code errors and format according to [standard](https://github.com/standard/standard)
- `npm test` make sure all tests pass
See [CONTRIBUTING.md](./CONTRIBUTING.md)
13 changes: 12 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ts-standard",
"description": "TypeScript Standard Style based on StandardJS",
"version": "12.0.2",
"version": "12.0.3",
Copy link
Member

Choose a reason for hiding this comment

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

Now that we automate it, I think we should not increase the version ourselves.
I think we should use 0.0.0-development instead.

https://github.com/semantic-release/semantic-release/blob/18bd0c490483008230c57ae6a73d07a09dd00d0d/package.json#L4

Copy link
Author

@Maytha8 Maytha8 Jun 13, 2023

Choose a reason for hiding this comment

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

semantic-release will bump the version for you, and @semantic-release/git will commit and push the version bump to the git repo (This is why the accidental version bump seen here happened, as I was testing semantic-release). The version won't have to be increased manually. 😄

You can see this in action in a test repo I made; here's an example commit of what it will do:
https://github.com/Maytha8/ts-standard-test/commit/b00d5b3fefff87dac6bf0c4f0dccf3c11d116156#diff-7ae45ad102eab3b6d7e7896acd08c427a9b25b346470d7bc6507b6481575d519
Here you can see that it knew what the next version was and bumped it.

The only reason semantic-release have their own version set to 0.0.0-development is because they don't use @semantic-release/npm on their own project.

If you'd like this behavior of leaving the version at 0.0.0-development, then we could remove package.json from the assets property of @semantic-release/git in .releaserc.yml, thus not committing the package.json version bump.

"main": "./index.js",
"type": "module",
"bin": {
Expand All @@ -16,6 +16,10 @@
"author": "Todd Bluhm",
"license": "MIT",
"homepage": "https://github.com/standard/ts-standard",
"repository": {
"type": "git",
"url": "https://github.com/Maytha8/ts-standard.git"
Maytha8 marked this conversation as resolved.
Show resolved Hide resolved
},
"keywords": [
"TypeScript Standard Style",
"check",
Expand Down Expand Up @@ -47,6 +51,7 @@
]
},
"scripts": {
"prepare": "husky install",
"lint:standard": "standard",
"lint:ts-standard": "node cli.js",
"lint:commit": "commitlint",
Expand All @@ -72,8 +77,14 @@
"devDependencies": {
"@commitlint/cli": "^17.0.3",
"@commitlint/config-conventional": "^17.0.3",
"@semantic-release/changelog": "^6.0.3",
"@semantic-release/git": "^10.0.1",
"@semantic-release/github": "^8.0.7",
"@semantic-release/npm": "^10.0.3",
"@types/minimist": "^1.2.2",
"@types/node": "^18.0.6",
"husky": "^8.0.3",
"semantic-release": "^21.0.2",
"standard": "^17.0.0",
"tape": "^5.5.3"
}
Expand Down