Skip to content

Commit

Permalink
ci: fix automated NPM release (#131)
Browse files Browse the repository at this point in the history
Fixes #130

This PR ensures that eslint-plugin-oxlint's version in package.json gets
updated to match oxlint's version.

It does not automatically generate PRs for new oxlint versions, but it
should automatically release (without needing to manually create a tag)
once a manually generated `bump_oxlint.yml` PR is merged.

I've changed the package versions to reflect oxlint v0.6.1, so this can
correctly release v0.7.0. Here's how it should work:

1. A `bump_oxlint.yml` workflow is dispatched with the new oxlint
version (needs to be prefixed with `v`, i.e. `v0.7.0`)
2. The generate script bumps the version in the local `package.json` to
match oxlint
3. Prettier now runs _after_ the generate script rather than during, as
the API version was generating invalid json outputs
4. The update PR is generated, with the title `release: v0.7.0`
5. When this is merged, the `release.yml` workflow will start, and run
if the latest commit contains `release: v`
6. This workflow will extract the version from the commit and create a
corresponding tag
7. Everything else should work the same as before

If this gets merged, could a maintainer please delete the existing
GitHub release v0.7.0 (which did not generate an NPM release), and then
trigger a new `bump_oxlint.yml` workflow?
  • Loading branch information
lachlancollins authored Aug 10, 2024
1 parent bbd63e3 commit c0496a3
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 106 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/bump_oxlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
workflow_dispatch:
inputs:
version:
required: false
required: true
type: string

env:
Expand Down Expand Up @@ -35,11 +35,11 @@ jobs:
- uses: peter-evans/create-pull-request@v6
with:
token: ${{ secrets.GITHUB_TOKEN }}
commit-message: Release ${{ inputs.version }}
commit-message: 'release: ${{ inputs.version }}'
committer: Boshen <Boshen@users.noreply.github.com>
author: Boshen <Boshen@users.noreply.github.com>
branch: release
branch-suffix: timestamp
title: Release ${{ inputs.version }}
title: 'release: ${{ inputs.version }}'
assignees: Boshen
base: main
14 changes: 12 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ name: Release

on:
push:
tags:
- 'v*'
branches: [main]

permissions:
contents: write

jobs:
release:
if: "contains(github.event.head_commit.message, 'release: v')"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -25,6 +25,16 @@ jobs:
node-version-file: .node-version
cache: pnpm

- name: Extract version from commit message
run: |
VERSION=$(echo "${{ github.event.head_commit.message }}" | grep -oP 'release: \Kv[0-9]+\.[0-9]+\.[0-9]+')
echo "VERSION=$VERSION" >> $GITHUB_ENV
- name: Create and push tag
run: |
git tag ${{ env.VERSION }}
git push origin ${{ env.VERSION }}
- run: npx changelogithub
continue-on-error: true
env:
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-plugin-oxlint",
"version": "0.5.0",
"version": "0.6.1",
"description": "Turn off all rules already supported by oxlint",
"type": "module",
"packageManager": "pnpm@9.6.0",
Expand Down Expand Up @@ -36,7 +36,7 @@
},
"license": "MIT",
"scripts": {
"generate": "node --import @oxc-node/core/register ./scripts/generate.ts",
"generate": "node --import @oxc-node/core/register ./scripts/generate.ts && pnpm format",
"clone": "node --import @oxc-node/core/register ./scripts/sparse-clone.ts",
"build": "vite build",
"lint": "npx oxlint && npx eslint",
Expand Down Expand Up @@ -68,7 +68,7 @@
"husky": "^9.0.11",
"lint-staged": "^15.2.5",
"memfs": "^4.9.2",
"oxlint": "^0.7.0",
"oxlint": "^0.6.1",
"prettier": "^3.3.1",
"shelljs": "^0.8.5",
"typescript": "^5.4.5",
Expand Down
74 changes: 37 additions & 37 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 16 additions & 12 deletions scripts/__snapshots__/rules-generator.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,32 @@ exports[`RulesGenerator > RulesGenerator generates rules correctly > byCategory
"// These rules are automatically generated by scripts/generate-rules.ts
const styleRules = {
'rulename-with-mod': 'off',
};
'rulename-with-mod': "off"
}
const correctnessRules = {
'@typescript-eslint/rulename-without-mod': 'off',
};
'@typescript-eslint/rulename-without-mod': "off"
}
export { styleRules, correctnessRules };
"
export {
styleRules,
correctnessRules
}"
`;

exports[`RulesGenerator > RulesGenerator generates rules correctly > byScope 1`] = `
"// These rules are automatically generated by scripts/generate-rules.ts
const eslintRules = {
'rulename-with-mod': 'off',
};
'rulename-with-mod': "off"
}
const typescriptRules = {
'@typescript-eslint/rulename-without-mod': 'off',
};
'@typescript-eslint/rulename-without-mod': "off"
}
export { eslintRules, typescriptRules };
"
export {
eslintRules,
typescriptRules
}"
`;
26 changes: 25 additions & 1 deletion scripts/generate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { writeFileSync } from 'node:fs';
import { RulesGenerator, RulesGrouping } from './rules-generator.js';
import { traverseRules } from './traverse-rules.js';
import { getLatestVersionFromClonedRepo } from './oxlint-version.js';
import { TARGET_DIRECTORY, VERSION_PREFIX } from './constants.js';
import packageJson from '../package.json' with { type: 'json' };

const { successResultArray, failureResultArray } = await traverseRules();

Expand All @@ -9,9 +13,29 @@ if (failureResultArray.length > 0) {
);
}

const generator = new RulesGenerator(successResultArray);
const oxlintVersion = getLatestVersionFromClonedRepo(
TARGET_DIRECTORY,
VERSION_PREFIX
);

if (!oxlintVersion) {
throw new Error(
'Failed to get the latest version of oxlint, did you forget to run `pnpm clone`?'
);
}

const generator = new RulesGenerator(oxlintVersion, successResultArray);

generator.setRulesGrouping(RulesGrouping.SCOPE);
await generator.generateRules();
generator.setRulesGrouping(RulesGrouping.CATEGORY);
await generator.generateRules();

// Update package.json version
writeFileSync(
'../package.json',
JSON.stringify({
...packageJson,
version: oxlintVersion.replace(VERSION_PREFIX, '').split('-')[0],
})
);
1 change: 1 addition & 0 deletions scripts/rules-generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ suite('RulesGenerator', () => {

// Create an instance of RulesGenerator
const generator = new RulesGenerator(
'1.0.0',
successResultArray,
RulesGrouping.SCOPE
);
Expand Down
30 changes: 6 additions & 24 deletions scripts/rules-generator.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { writeFileSync } from 'node:fs';
import path from 'node:path';
import { getLatestVersionFromClonedRepo } from './oxlint-version.js';
import { TARGET_DIRECTORY, VERSION_PREFIX } from './constants.js';
import type { Rule } from './traverse-rules.js';
import prettier from 'prettier';

const __dirname = new URL('.', import.meta.url).pathname;

Expand All @@ -15,14 +12,17 @@ export enum RulesGrouping {
export type ResultMap = Map<string, string[]>;

export class RulesGenerator {
private oxlintVersion: string;
private rulesGrouping: RulesGrouping;
private rulesArray: Rule[];
constructor(
oxlintVersion: string,
rulesArray: Rule[] = [],
rulesGrouping: RulesGrouping = RulesGrouping.SCOPE
) {
this.rulesGrouping = rulesGrouping;
this.oxlintVersion = oxlintVersion;
this.rulesArray = rulesArray;
this.rulesGrouping = rulesGrouping;
}

public setRulesGrouping(rulesGrouping: RulesGrouping) {
Expand All @@ -45,19 +45,8 @@ export class RulesGenerator {
}

public async generateRulesCode() {
const oxlintVersion = getLatestVersionFromClonedRepo(
TARGET_DIRECTORY,
VERSION_PREFIX
);

if (!oxlintVersion) {
throw new Error(
'Failed to get the latest version of oxlint, did you forget to run `pnpm clone`?'
);
}

console.log(
`Generating rules for ${oxlintVersion}, grouped by ${this.rulesGrouping}`
`Generating rules for ${this.oxlintVersion}, grouped by ${this.rulesGrouping}`
);

const rulesGrouping = this.rulesGrouping;
Expand Down Expand Up @@ -93,14 +82,7 @@ export class RulesGenerator {
.join(',\n');
code += '\n}';

const prettierConfig = await prettier.resolveConfig('.', {
config: path.resolve(__dirname, '..', 'prettier.config.js'),
});

return await prettier.format(code, {
...prettierConfig,
parser: 'typescript',
});
return code;
}

public async generateRules() {
Expand Down
Loading

0 comments on commit c0496a3

Please sign in to comment.