Skip to content

Commit

Permalink
Simplify comments to use logging instead of the Github API (#5)
Browse files Browse the repository at this point in the history
* Change commenting to use core.error instead.

* Add `::error` annotation.

* Change to `info` logs.

* Cleanup.

* Remove `github_token` from `actions.yml`.

* Add column field and some more cleanup.

* Return raw output.

* Revert final print message.

* Experiment with returning an empty error message.

* Return an empty error.

* Change final log statement.

* Remove trailing whitespace.
  • Loading branch information
doriable authored Jul 21, 2021
1 parent cae2ff7 commit f0b4ff5
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 196 deletions.
5 changes: 0 additions & 5 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ inputs:
description: 'The input path.'
required: true
default: '.'
# The Github token MUST be referenced in the workflow file.
# https://docs.github.com/en/actions/reference/authentication-in-a-workflow
github_token:
description: 'The Github authentication token.'
required: true
buf_token:
description: 'The buf authentication token used for private inputs.'
runs:
Expand Down
15 changes: 3 additions & 12 deletions dist/main.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/main.js.map

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions src/buf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@ export interface LintResult {
fileAnnotations: FileAnnotation[];
}

// FileAnnotation conforms the buf FileAnnotation definition
// FileAnnotation is a subset of the buf FileAnnotation definition
// referenced from the following:
// https://github.com/bufbuild/buf/blob/8255257bd94c9f1b5faa27242211c5caad05be79/internal/buf/bufanalysis/bufanalysis.go#L102
export interface FileAnnotation {
type: string;
message: string;
path?: string;
start_line?: number;
end_line?: number;
start_column?: number;
}

// ExecException is a subset of the child.ExecException interface.
Expand Down Expand Up @@ -125,7 +124,6 @@ function parseLines(lines: string[]): FileAnnotation[] | Error {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function isFileAnnotation(o: any): o is FileAnnotation {
return (
'type' in o &&
'message' in o
);
}
Expand Down
127 changes: 0 additions & 127 deletions src/github.ts

This file was deleted.

61 changes: 16 additions & 45 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@
// limitations under the License.

import * as core from '@actions/core';
import * as github from '@actions/github'
import * as io from '@actions/io';
import * as child from 'child_process';
import * as fs from 'fs';
import * as path from 'path';
import * as semver from 'semver';
import { lint } from './buf';
import { lint, FileAnnotation } from './buf';
import { Error, isError } from './error';
import { postComments } from './github';

// minimumBufVersion is the minimum buf version required to
// run this action. At least this version is required because
Expand Down Expand Up @@ -59,30 +57,12 @@ export async function run(): Promise<void> {
// runLint runs the buf-lint action, and returns
// a non-empty error if it fails.
async function runLint(): Promise<null|Error> {
const authenticationToken = core.getInput('github_token');
if (authenticationToken === '') {
return {
message: 'a Github authentication token was not provided'
};
}
const input = core.getInput('input');
if (input === '') {
return {
message: 'an input was not provided'
};
}
const owner = github.context.repo.owner;
if (owner === '') {
return {
message: 'an owner was not provided'
};
}
const repository = github.context.repo.repo;
if (repository === '') {
return {
message: 'a repository was not provided'
};
}
const binaryPath = await io.which('buf', true);
if (binaryPath === '') {
return {
Expand Down Expand Up @@ -123,31 +103,22 @@ async function runLint(): Promise<null|Error> {
return null;
}

const pullRequestNumber = github.context.payload.pull_request?.number;
if (pullRequestNumber !== undefined) {
// If this action was configured for pull requests, we post the
// FileAnnotations as comments.
try {
await postComments(
authenticationToken,
owner,
repository,
pullRequestNumber,
result.fileAnnotations,
);
} catch (error) {
// Log the error, but continue so that we still write
// out the raw output to the user.
if (isError(error)) {
core.info(`Failed to write comments in-line: ${error.message}`);
} else {
core.info(`Failed to write comments in-line`);
}
// If this action was configured for pull requests, we post the
// FileAnnotations as comments.
result.fileAnnotations.forEach((fileAnnotation: FileAnnotation) => {
const { path, start_line, start_column, message } = fileAnnotation;
if (path === undefined || start_line === undefined || start_column === undefined) {
core.error(message);
return;
}
}

// Include the raw output so that the console includes sufficient context.
// This uses the `::error` message feature of Github Actions. It converts the message to
// an error log in the Github Actions console and creates an error annotation at the given
// file path and line. This is not currently supported with `core.error`.
// For more information, see the documentation:
// https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-error-message
core.info(`::error file=${path},line=${start_line},col=${start_column}::${message}`);
})
return {
message: `buf found ${result.fileAnnotations.length} lint failures.\n${result.raw}`
message: `buf found ${result.fileAnnotations.length} lint failures.`
};
}

0 comments on commit f0b4ff5

Please sign in to comment.