Skip to content

Commit

Permalink
dev: support push (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S committed Dec 16, 2020
1 parent a62547f commit 50de4e8
Show file tree
Hide file tree
Showing 14 changed files with 295 additions and 71 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ indent_size = 2
[package*.json]
indent_size = 2

[webpack.*.js]
indent_size = 2

[*.md]
indent_size = 2
trim_trailing_whitespace = false
27 changes: 14 additions & 13 deletions action-source/lib/github.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { context } from '@actions/github/lib/utils';
import { Octokit } from '@octokit/rest';

export interface GitContext {
Expand All @@ -13,24 +14,24 @@ export async function getPullRequestFiles(git: Octokit, prRef: PullRequestRef):
const { owner, repo, pull_number } = prRef;
const commits = await git.pulls.listCommits({ owner, repo, pull_number });

const prFiles: Set<string> = new Set();

for (const commitRef of commits.data) {
const ref = commitRef.sha;
if (!ref) continue;
const commit = await git.repos.getCommit({ owner, repo, ref });
const files = commit.data.files;
if (!files) continue;
files.forEach((f) => f.filename && prFiles.add(f.filename));
}
return fetchFilesForCommits(git, prRef, commits.data.map(c => c.sha).filter(isString));
}

return [...prFiles];
function isString(s: string | unknown): s is string {
return typeof s === 'string';
}

export async function fetchFilesForCommits(git: Octokit, context: GitContext, commitIds: string[]): Promise<string[]> {
const files: Set<string> = new Set();
for await (const file of fetchFilesForCommitsX(git, context, commitIds)) {
files.add(file);
}
return [...files];
}

export async function* fetchFilesForCommits(git: Octokit, context: GitContext, commitIds: string[]): AsyncIterableIterator<string> {
async function* fetchFilesForCommitsX(git: Octokit, context: GitContext, commitIds: string[]): AsyncIterableIterator<string> {
const { owner, repo } = context;
for await (const ref of commitIds) {
for (const ref of commitIds) {
const commit = await git.repos.getCommit({ owner, repo, ref });
const files = commit.data.files;
if (!files) continue;
Expand Down
33 changes: 23 additions & 10 deletions action-source/lib/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,43 @@ import * as core from '@actions/core';
import { issueCommand } from '@actions/core/lib/command';
import * as gitHubApi from '@actions/github';
import { getOctokit } from '@actions/github';
import { getPullRequestFiles } from './github';
import { fetchFilesForCommits, getPullRequestFiles } from './github';
import { Octokit } from '@octokit/rest';
import { } from '@octokit/rest';
import { } from '@octokit/core';
import { lint } from './spell';
import * as path from 'path';
import { GitHub } from '@actions/github/lib/utils';

type GitHub = ReturnType<typeof getOctokit>;

type Context = typeof gitHubApi.context;

export async function pullRequest(context: Context, github: GitHub): Promise<void> {
core.info(`Pull Request: ${context.eventName}`);
if (github) {
core.info('github');
const pull_number = context.payload.pull_request?.number || 0;
const files = await getPullRequestFiles(github as Octokit, { ...context.repo, pull_number });
await checkSpelling(files);
}
core.info('github');
const pull_number = context.payload.pull_request?.number || 0;
const files = await getPullRequestFiles(github as Octokit, { ...context.repo, pull_number });
await checkSpelling(files);
}

interface Commit {
id: string;
}

interface PushPayload {
commits?: Commit[];
}

export async function push(context: Context, github: GitHub): Promise<void> {
core.info(`Push: ${context.eventName}`);
context.sha;
const result = await github.git.getCommit({ commit_sha: context.sha, ...context.repo });
core.info(`result: ${JSON.stringify(result, null, 2)}`);

const push = context.payload as PushPayload;
const commits = push.commits?.map(c => c.id);
const files = commits && await fetchFilesForCommits(github as Octokit, context.repo, commits);
if (files) {
await checkSpelling(files);
}
}

async function checkSpelling(files: string[]) {
Expand Down
2 changes: 1 addition & 1 deletion action-source/lib/spell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export async function lint(files: string[], lintOptions: LintOptions, logger: Lo
filename,
elapsedTimeMs,
} = progress;
logger.info(`${fileNum}/${fileCount} ${filename} ${elapsedTimeMs?.toFixed(2)}`);
logger.info(`${fileNum}/${fileCount} ${filename} (${elapsedTimeMs?.toFixed(2)}ms)`);
}

function error(message: string, error: Error) {
Expand Down
5 changes: 2 additions & 3 deletions action-source/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
"private": true,
"scripts": {
"prepublishOnly": "npm run build",
"build": "webpack",
"build": "webpack -c webpack.prod.js",
"compile": "tsc -p .",
"smoke-test": "cd .. && env-cmd -f ./fixtures/pull_request.json node ./action/main.js",
"watch": "webpack --watch",
"watch": "webpack -c webpack.dev.js --watch",
"test": "jest"
},
"repository": {
Expand All @@ -29,7 +29,6 @@
},
"homepage": "https://github.com/streetsidesoftware/cspell-action#readme",
"devDependencies": {
"@octokit/graphql": "^4.5.8",
"@octokit/rest": "^18.0.12",
"@types/jest": "^26.0.19",
"@types/minimatch": "^3.0.3",
Expand Down
14 changes: 5 additions & 9 deletions action-source/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,14 @@ module.exports = {
__filename: false,
__dirname: false,
},
externalsType: 'commonjs-module',
externalsPresets: {
node: true,
},
externals: {
// cspell: 'commonjs2 cspell',
// 'cspell-lib': 'commonjs2 cspell-lib',
'@actions/core': 'commonjs2 @actions/core',
'@actions/github': 'commonjs2 @actions/github',
'@cspell/cspell-bundled-dicts': 'commonjs2 @cspell/cspell-bundled-dicts',
'@cspell/cspell-bundled-dicts/cspell-default.json': 'commonjs2 @cspell/cspell-bundled-dicts/cspell-default.json',
},
devtool: 'source-map',
externals: [
/^@action/,
/^@cspell\/cspell-bundled-dicts/,
],
output: {
filename: 'main.js',
path: path.resolve(__dirname, '..', 'action'),
Expand Down
7 changes: 7 additions & 0 deletions action-source/webpack.dev.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const config = require('./webpack.config');

module.exports = {
...config,
devtool: 'source-map',
};
3 changes: 1 addition & 2 deletions action/main.js

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion action/node_modules/@octokit/graphql/package.json

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

5 changes: 2 additions & 3 deletions action/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
"private": true,
"scripts": {
"prepublishOnly": "npm run build",
"build": "webpack",
"build": "webpack -c webpack.prod.js",
"compile": "tsc -p .",
"smoke-test": "cd .. && env-cmd -f ./fixtures/pull_request.json node ./action/main.js",
"watch": "webpack --watch",
"watch": "webpack -c webpack.dev.js --watch",
"test": "jest"
},
"repository": {
Expand All @@ -29,7 +29,6 @@
},
"homepage": "https://github.com/streetsidesoftware/cspell-action#readme",
"devDependencies": {
"@octokit/graphql": "^4.5.8",
"@octokit/rest": "^18.0.12",
"@types/jest": "^26.0.19",
"@types/minimatch": "^3.0.3",
Expand Down
13 changes: 13 additions & 0 deletions fixtures/push.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"INPUT_GITHUB_TOKEN": "$GITHUB_TOKEN",
"GITHUB_EVENT_PATH": "./fixtures/push_payload.json",
"GITHUB_EVENT_NAME": "push",
"GITHUB_SHA": "a16b47a16f6c11b63cfdcf510c15ba26edd0f3d1",
"GITHUB_REF": "refs/heads/fix-resolve-path",
"GITHUB_WORKFLOW": "test-action",
"GITHUB_ACTION": "self",
"GITHUB_ACTOR": "Jason3S",
"GITHUB_JOB": "test-action",
"GITHUB_RUN_NUMBER": "7",
"GITHUB_RUN_ID": "425984531"
}
61 changes: 32 additions & 29 deletions fixtures/push_context.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"payload": {
"after": "245401caa9cb567e577bd4af251789e7e87c726d",
"after": "a16b47a16f6c11b63cfdcf510c15ba26edd0f3d1",
"base_ref": null,
"before": "5ad22abe5ca10ccfb13bec47ba9a6c7c05fcff1a",
"before": "305de2c9caa6e6484d581eef3eb54c4c0208d24d",
"commits": [
{
"author": {
Expand All @@ -16,14 +16,14 @@
"username": "Jason3S"
},
"distinct": true,
"id": "245401caa9cb567e577bd4af251789e7e87c726d",
"message": "Update test.yml",
"timestamp": "2020-03-01T17:31:51+01:00",
"tree_id": "528922684f122cd3249cd680ebfca2156f14d320",
"url": "https://github.com/streetsidesoftware/cspell-action/commit/245401caa9cb567e577bd4af251789e7e87c726d"
"id": "a16b47a16f6c11b63cfdcf510c15ba26edd0f3d1",
"message": "Update .editorconfig",
"timestamp": "2020-12-16T16:31:07+01:00",
"tree_id": "0a22bcecf2e3cc7c7c851ae5dcb20b1affc5846c",
"url": "https://github.com/streetsidesoftware/cspell-action/commit/a16b47a16f6c11b63cfdcf510c15ba26edd0f3d1"
}
],
"compare": "https://github.com/streetsidesoftware/cspell-action/compare/5ad22abe5ca1...245401caa9cb",
"compare": "https://github.com/streetsidesoftware/cspell-action/compare/305de2c9caa6...a16b47a16f6c",
"created": false,
"deleted": false,
"forced": false,
Expand All @@ -39,15 +39,15 @@
"username": "Jason3S"
},
"distinct": true,
"id": "245401caa9cb567e577bd4af251789e7e87c726d",
"message": "Update test.yml",
"timestamp": "2020-03-01T17:31:51+01:00",
"tree_id": "528922684f122cd3249cd680ebfca2156f14d320",
"url": "https://github.com/streetsidesoftware/cspell-action/commit/245401caa9cb567e577bd4af251789e7e87c726d"
"id": "a16b47a16f6c11b63cfdcf510c15ba26edd0f3d1",
"message": "Update .editorconfig",
"timestamp": "2020-12-16T16:31:07+01:00",
"tree_id": "0a22bcecf2e3cc7c7c851ae5dcb20b1affc5846c",
"url": "https://github.com/streetsidesoftware/cspell-action/commit/a16b47a16f6c11b63cfdcf510c15ba26edd0f3d1"
},
"organization": {
"avatar_url": "https://avatars0.githubusercontent.com/u/50543896?v=4",
"description": null,
"description": "",
"events_url": "https://api.github.com/orgs/streetsidesoftware/events",
"hooks_url": "https://api.github.com/orgs/streetsidesoftware/hooks",
"id": 50543896,
Expand All @@ -63,7 +63,7 @@
"email": "[email protected]",
"name": "Jason3S"
},
"ref": "refs/heads/understand-github-context",
"ref": "refs/heads/fix-resolve-path",
"repository": {
"archive_url": "https://api.github.com/repos/streetsidesoftware/cspell-action/{archive_format}{/ref}",
"archived": false,
Expand Down Expand Up @@ -107,7 +107,7 @@
"issues_url": "https://api.github.com/repos/streetsidesoftware/cspell-action/issues{/number}",
"keys_url": "https://api.github.com/repos/streetsidesoftware/cspell-action/keys{/key_id}",
"labels_url": "https://api.github.com/repos/streetsidesoftware/cspell-action/labels{/name}",
"language": "JavaScript",
"language": "TypeScript",
"languages_url": "https://api.github.com/repos/streetsidesoftware/cspell-action/languages",
"license": {
"key": "mit",
Expand All @@ -123,8 +123,8 @@
"name": "cspell-action",
"node_id": "MDEwOlJlcG9zaXRvcnkyNDM4MTg4MDI=",
"notifications_url": "https://api.github.com/repos/streetsidesoftware/cspell-action/notifications{?since,all,participating}",
"open_issues": 0,
"open_issues_count": 0,
"open_issues": 2,
"open_issues_count": 2,
"organization": "streetsidesoftware",
"owner": {
"avatar_url": "https://avatars0.githubusercontent.com/u/50543896?v=4",
Expand All @@ -150,12 +150,12 @@
},
"private": false,
"pulls_url": "https://api.github.com/repos/streetsidesoftware/cspell-action/pulls{/number}",
"pushed_at": 1583080317,
"pushed_at": 1608132675,
"releases_url": "https://api.github.com/repos/streetsidesoftware/cspell-action/releases{/id}",
"size": 2195,
"size": 4764,
"ssh_url": "[email protected]:streetsidesoftware/cspell-action.git",
"stargazers": 1,
"stargazers_count": 1,
"stargazers": 3,
"stargazers_count": 3,
"stargazers_url": "https://api.github.com/repos/streetsidesoftware/cspell-action/stargazers",
"statuses_url": "https://api.github.com/repos/streetsidesoftware/cspell-action/statuses/{sha}",
"subscribers_url": "https://api.github.com/repos/streetsidesoftware/cspell-action/subscribers",
Expand All @@ -164,10 +164,10 @@
"tags_url": "https://api.github.com/repos/streetsidesoftware/cspell-action/tags",
"teams_url": "https://api.github.com/repos/streetsidesoftware/cspell-action/teams",
"trees_url": "https://api.github.com/repos/streetsidesoftware/cspell-action/git/trees{/sha}",
"updated_at": "2020-02-29T17:46:10Z",
"updated_at": "2020-12-16T14:46:51Z",
"url": "https://github.com/streetsidesoftware/cspell-action",
"watchers": 1,
"watchers_count": 1
"watchers": 3,
"watchers_count": 3
},
"sender": {
"avatar_url": "https://avatars1.githubusercontent.com/u/3740137?v=4",
Expand All @@ -191,9 +191,12 @@
}
},
"eventName": "push",
"sha": "245401caa9cb567e577bd4af251789e7e87c726d",
"ref": "refs/heads/understand-github-context",
"workflow": "build-test",
"sha": "a16b47a16f6c11b63cfdcf510c15ba26edd0f3d1",
"ref": "refs/heads/fix-resolve-path",
"workflow": "test-action",
"action": "self",
"actor": "Jason3S"
"actor": "Jason3S",
"job": "test-action",
"runNumber": 7,
"runId": 425984531
}
Loading

0 comments on commit 50de4e8

Please sign in to comment.