Skip to content
This repository has been archived by the owner on Dec 9, 2020. It is now read-only.

Commit

Permalink
feat: project references, run path, token issues
Browse files Browse the repository at this point in the history
fix #77
fix #84
fix #93
  • Loading branch information
iCrawl committed Dec 21, 2019
1 parent 8652260 commit 20c7522
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 421 deletions.
17 changes: 17 additions & 0 deletions .github/tsc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"problemMatcher": [
{
"owner": "tsc",
"pattern": [
{
"regexp": "^(?:\\s+\\d+\\>)?([^\\s].*)\\((\\d+|\\d+,\\d+|\\d+,\\d+,\\d+,\\d+)\\)\\s*:\\s+(error|warning|info)\\s+(\\w{1,2}\\d+)\\s*:\\s*(.*)$",
"file": 1,
"location": 2,
"severity": 3,
"code": 4,
"message": 5
}
]
}
]
}
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ jobs:
uses: icrawl/action-tsc@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
job-name: tsc
```

## Contributing
Expand Down
8 changes: 6 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ branding:
icon: alert-circle
color: blue
inputs:
job-name:
description: 'Optional name of the annotation author.'
project:
description: 'Optional project path.'
required: false
build:
description: 'Optional build reference path.'
required: false
runs:
using: 'node12'
main: 'dist/index.js'
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,21 @@
},
"dependencies": {
"@actions/core": "^1.2.0",
"@actions/exec": "^1.0.2",
"@actions/github": "^2.0.0"
"@actions/exec": "^1.0.2"
},
"devDependencies": {
"@octokit/rest": "^16.35.2",
"@types/node": "^12.12.21",
"@typescript-eslint/eslint-plugin": "^2.12.0",
"@typescript-eslint/parser": "^2.12.0",
"@zeit/ncc": "^0.20.5",
"eslint": "^6.8.0",
"eslint-config-marine": "^5.3.2",
"eslint-config-prettier": "^6.7.0",
"eslint-plugin-prettier": "^3.1.2",
"prettier": "^1.19.1",
"typescript": "^3.7.4"
},
"eslintConfig": {
"extends": "marine/node"
"extends": "marine/prettier/node"
}
}
169 changes: 17 additions & 152 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,160 +1,25 @@
import { join } from 'path';
import { exec } from '@actions/exec';
import { getInput, setFailed, debug } from '@actions/core';
import { GitHub, context } from '@actions/github';
import { ChecksUpdateParamsOutputAnnotations, ChecksCreateParams } from '@octokit/rest';

const { GITHUB_TOKEN, GITHUB_SHA } = process.env;

const ACTION_NAME = 'TSC';

async function lint(data: string) {
const annotations: ChecksUpdateParamsOutputAnnotations[] = [];
const results = [...data.matchAll(/^([^()]+)\((\d+),(\d+)\): (error|warning) (.+): (.+)$/gm)];
for (const res of results) {
const [, path, line, column, severity, ruleId, message] = res;
annotations.push({
path,
start_line: parseInt(line, 10),
end_line: parseInt(line, 10),
start_column: parseInt(column, 10),
end_column: parseInt(column, 10),
annotation_level: severity === 'error' ? 'failure' : 'warning',
title: ruleId || ACTION_NAME,
message
});
}

return {
conclusion: annotations.length ? 'success' : 'failure' as ChecksCreateParams['conclusion'],
output: {
title: ACTION_NAME,
summary: annotations.length ? 'Green lights' : 'TSC error',
annotations
}
};
}

async function check(data: string) {
const octokit = new GitHub(GITHUB_TOKEN!);

let currentSha: string;
let info;
if (context.issue && context.issue.number) {
try {
info = await octokit.graphql(`query($owner: String!, $name: String!, $prNumber: Int!) {
repository(owner: $owner, name: $name) {
pullRequest(number: $prNumber) {
files(first: 100) {
nodes {
path
}
}
commits(last: 1) {
nodes {
commit {
oid
}
}
}
}
}
}`,
{
owner: context.repo.owner,
name: context.repo.repo,
prNumber: context.issue.number
});
} catch {
console.log('##[warning] Token doesn\'t have permission to access this resource.');
}
if (info) currentSha = info.repository.pullRequest.commits.nodes[0].commit.oid;
else currentSha = GITHUB_SHA!;
} else {
try {
info = await octokit.repos.getCommit({ owner: context.repo.owner, repo: context.repo.repo, ref: GITHUB_SHA! });
} catch {
console.log('##[warning] Token doesn\'t have permission to access this resource.');
}
currentSha = GITHUB_SHA!;
}
debug(`Commit: ${currentSha}`);

let id: number | undefined;
const jobName = getInput('job-name');
if (jobName) {
try {
const checks = await octokit.checks.listForRef({
...context.repo,
status: 'in_progress',
ref: currentSha
});
const check = checks.data.check_runs.find(({ name }) => name.toLowerCase() === jobName.toLowerCase());
if (check) id = check.id;
} catch {
console.log('##[warning] Token doesn\'t have permission to access this resource.');
}
}
if (!id) {
try {
id = (await octokit.checks.create({
...context.repo,
name: ACTION_NAME,
head_sha: currentSha,
status: 'in_progress',
started_at: new Date().toISOString()
})).data.id;
} catch {
console.log('##[warning] Token doesn\'t have permission to access this resource.');
}
}

try {
const { conclusion, output } = await lint(data);
if (id) {
try {
await octokit.checks.update({
...context.repo,
check_run_id: id,
completed_at: new Date().toISOString(),
conclusion,
output
});
} catch {
console.log('##[warning] Token doesn\'t have permission to access this resource.');
}
}
debug(output.summary);
if (conclusion === 'failure') setFailed(output.summary);
} catch (error) {
if (id) {
try {
await octokit.checks.update({
...context.repo,
check_run_id: id,
conclusion: 'failure',
completed_at: new Date().toISOString()
});
} catch {
console.log('##[warning] Token doesn\'t have permission to access this resource.');
}
}
setFailed(error.message);
}
}
import { getInput } from '@actions/core';

async function run() {
try {
await exec('node', [`${join(process.cwd(), 'node_modules/typescript/bin/tsc')}`, '--noEmit', '--noErrorTruncation', '--pretty', 'false'], {
listeners: {
stdout: async (data: Buffer) => {
await check(data.toString());
}
}
});
} catch (error) {
setFailed(error.message);
const project = getInput('project');
const build = getInput('project');
console.log(`##[add-matcher]${join(__dirname, '..', '.github', 'tsc.json')}`);
const args = [
`${join(process.cwd(), 'node_modules/typescript/bin/tsc')}`,
'--noEmit',
'--noErrorTruncation',
'--pretty',
'false',
];
if (project) {
args.push('--project', project);
}
if (build) {
args.push('--build', build);
}
await exec('node', args);
}

run();
1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"esnext.asynciterable",
"esnext.intl",
"esnext.symbol",
"es2020"
],
"sourceMap": false,
"inlineSourceMap": true,
Expand Down
Loading

0 comments on commit 20c7522

Please sign in to comment.