Skip to content

Commit

Permalink
Merge branch 'main' into releases/v1
Browse files Browse the repository at this point in the history
  • Loading branch information
xt0rted committed Apr 27, 2021
2 parents bdb057c + 3df2d14 commit acaf255
Show file tree
Hide file tree
Showing 11 changed files with 202 additions and 105 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ jobs:

strategy:
matrix:
node: [12, 14]
node: [12, 14, 16]

steps:
- name: Checkout repo
uses: actions/[email protected]

- name: Install node
uses: actions/[email protected].4
uses: actions/[email protected].5
with:
node-version: ${{ matrix.node }}

Expand Down
5 changes: 2 additions & 3 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ on: pull_request

jobs:
message-check:
name: Block Autosquash Commits
name: Block on fixup commits

runs-on: ubuntu-latest

steps:
- name: Block Autosquash Commits
uses: xt0rted/[email protected]
- uses: xt0rted/[email protected]
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Changelog

## Unreleased

## Version 1.2.0

- Bumped `@actions/core` from 1.2.6 to 1.2.7
- Bumped `@actions/io` from 1.0.2 to 1.1.0
- Updated the `repo-token` input to be optional and defaulted it to `GITHUB_TOKEN`. If you're already using this value, or not using the `only-changed-files` option, you can remove this setting from your workflow.
- Updated the output target from `es6` to `es2019`
- Added a new `version` input to allow picking the cli version of `dotnet-format` to use. Currently this defaults to `3` and is the only version supported. A future update will add support for versions 4 and 5.

## Version 1.1.0

- Bumped `@actions/core` from 1.2.3 to 1.2.6
Expand Down
48 changes: 40 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![CI Workflow Status](https://github.com/xt0rted/dotnet-format/workflows/CI/badge.svg)](https://github.com/xt0rted/dotnet-format/actions?query=workflow%3ACI)

Run [dotnet-format](https://github.com/dotnet/format) as part of your workflow to report formatting errors or auto fix violations as part of your pull request workflow.
Run [dotnet-format](https://github.com/dotnet/format) v3 as part of your workflow to report formatting errors or auto fix violations as part of your pull request workflow.

## Usage

Expand All @@ -26,8 +26,6 @@ jobs:

- name: Run dotnet format
uses: xt0rted/dotnet-format@v1
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
```
Running on `pull_request`.
Expand All @@ -51,7 +49,6 @@ jobs:
- name: Run dotnet format
uses: xt0rted/dotnet-format@v1
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
only-changed-files: "true"
```

Expand All @@ -74,7 +71,6 @@ jobs:
uses: xt0rted/slash-command-action@v1
continue-on-error: true
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
command: dotnet
reaction-type: "eyes"
Expand All @@ -101,7 +97,6 @@ jobs:
id: format
uses: xt0rted/dotnet-format@v1
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
action: "fix"
only-changed-files: true
Expand All @@ -122,19 +117,56 @@ jobs:
github_token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
```

## Control Permissions

If your repository is using [control permissions](https://github.blog/changelog/2021-04-20-github-actions-control-permissions-for-github_token/), and you want to check only changed files, you'll need to set `pull-request: read` on either the workflow or the job.

### Workflow Config

```yml
on: pull_request
permissions:
pull-requests: read
jobs:
dotnet-format:
runs-on: ubuntu-latest
steps:
- name: Run dotnet format
uses: xt0rted/dotnet-format@v1
with:
only-changed-files: "true"
```

### Job Config

```yml
on: pull_request
jobs:
dotnet-format:
runs-on: ubuntu-latest
permissions:
pull-requests: read
steps:
- name: Run dotnet format
uses: xt0rted/dotnet-format@v1
with:
only-changed-files: "true"
```

## Options

### Required

Name | Allowed values | Description
-- | -- | --
`repo-token` | `GITHUB_TOKEN` or a custom value | The token used to call the GitHub api.
`repo-token` | `GITHUB_TOKEN` (default) or PAT | `GITHUB_TOKEN` token or a repo scoped PAT.
`version` | `3` (default) | Version of `dotnet-format` to use.
`action` | `check` (default), `fix` | Primary action `dotnet-format` should perform.

### Optional

Name | Allowed values | Description
-- | -- | --
`action` | `check` (default), `fix` | The primary action dotnet-format should perform.
`only-changed-files` | `true`, `false` (default) | Only changed files in the current pull request should be formatted.
`fail-fast` | `true` (default), `false` | The job should fail if there's a formatting error. Only used with the `check` action.

Expand Down
12 changes: 9 additions & 3 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@ branding:

inputs:
repo-token:
description: "The GITHUB_TOKEN secret"
description: "GITHUB_TOKEN token or a repo scoped PAT"
required: true
default: ${{ github.token }}

version:
description: "Version of dotnet-format to use"
required: true
default: "3"

action:
description: "The primary action dotnet-format should perform (check for errors or apply fixes)"
required: false
description: "Primary action dotnet-format should perform (check for errors or apply fixes)"
required: true
default: "check"

only-changed-files:
Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@
"author": "xt0rted",
"license": "MIT",
"dependencies": {
"@actions/core": "^1.2.6",
"@actions/core": "^1.2.7",
"@actions/exec": "^1.0.4",
"@actions/github": "^4.0.0",
"@actions/io": "^1.0.2"
"@actions/io": "^1.1.0"
},
"devDependencies": {
"@types/node": "^14.14.31",
"@typescript-eslint/eslint-plugin": "^4.15.2",
"@typescript-eslint/parser": "^4.15.2",
"@vercel/ncc": "^0.27.0",
"eslint": "^7.20.0",
"typescript": "^4.2.2"
"@types/node": "^15.0.0",
"@typescript-eslint/eslint-plugin": "^4.22.0",
"@typescript-eslint/parser": "^4.22.0",
"@vercel/ncc": "^0.28.3",
"eslint": "^7.25.0",
"typescript": "^4.2.4"
}
}
11 changes: 9 additions & 2 deletions src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ import {
} from "@actions/core";

import { format } from "./dotnet";
import { checkVersion } from "./version";

export async function check(): Promise<void> {
const onlyChangedFiles = getInput("only-changed-files") === "true";
const failFast = getInput("fail-fast") === "true";
const version = getInput("version", { required: true });

const result = await format({
const dotnetFormatVersion = checkVersion(version);

const result = await format(dotnetFormatVersion)({
dryRun: true,
onlyChangedFiles,
});
Expand All @@ -24,8 +28,11 @@ export async function check(): Promise<void> {

export async function fix(): Promise<void> {
const onlyChangedFiles = getInput("only-changed-files") === "true";
const version = getInput("version", { required: true });

const dotnetFormatVersion = checkVersion(version);

const result = await format({
const result = await format(dotnetFormatVersion)({
dryRun: false,
onlyChangedFiles,
});
Expand Down
16 changes: 15 additions & 1 deletion src/dotnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import { getPullRequestFiles } from "./files";

import type { ExecOptions } from "@actions/exec/lib/interfaces";

import type { DotNetFormatVersion } from "./version";

export type FormatFunction = (options: FormatOptions) => Promise<boolean>;

export interface FormatOptions {
dryRun: boolean;
onlyChangedFiles: boolean;
Expand All @@ -30,7 +34,7 @@ function formatOnlyChangedFiles(onlyChangedFiles: boolean): boolean {
return false;
}

export async function format(options: FormatOptions): Promise<boolean> {
async function formatVersion3(options: FormatOptions): Promise<boolean> {
const execOptions: ExecOptions = { ignoreReturnCode: true };

const dotnetFormatOptions = ["format", "--check"];
Expand Down Expand Up @@ -58,3 +62,13 @@ export async function format(options: FormatOptions): Promise<boolean> {

return !!dotnetResult;
}

export function format(version: DotNetFormatVersion): FormatFunction {
switch (version || "") {
case "3":
return formatVersion3;

default:
throw Error(`dotnet-format version "${version}" is unsupported`);
}
}
18 changes: 18 additions & 0 deletions src/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export type DotNetFormatVersion =
| "3"
;

const supportedVersions: DotNetFormatVersion[] = [
"3",
];

export function checkVersion(version: string): DotNetFormatVersion {
for (let i = 0; i < supportedVersions.length; i++) {
const ver = supportedVersions[i];
if (ver === version) {
return version;
}
}

throw Error(`dotnet-format version "${version}" is unsupported`);
}
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
"target": "es2019", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
Expand Down Expand Up @@ -38,7 +38,7 @@
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */

/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
Expand Down
Loading

0 comments on commit acaf255

Please sign in to comment.