Skip to content

Commit

Permalink
feat: Add mode input
Browse files Browse the repository at this point in the history
  • Loading branch information
peaceiris committed Sep 17, 2020
1 parent 7bb03ce commit b73bfe6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 27 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/suggest-related-links.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ jobs:
restore-keys: |
${{ runner.os }}-action-
- uses: peaceiris/actions-suggest-related-links@v0.10.0
if: github.event_name != 'pull_request'

- name: Setup model
if: github.event_name == 'issues' || github.event_name == 'pull_request'
run: |
Expand All @@ -58,7 +61,8 @@ jobs:
python3 train.py -d training-data.json -test input.txt
cp ./suggestions.json ~/actions-suggest-related-links-tmp/
- uses: peaceiris/actions-suggest-related-links@v0.9.0
- uses: peaceiris/actions-suggest-related-links@v0.10.0
if: github.event_name != 'pull_request'
with:
mode: 'suggest'
repository: 'peaceiris/actions-gh-pages'
45 changes: 35 additions & 10 deletions actions/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export async function run(): Promise<void> {
const eventName = context.eventName;

if (eventName === 'workflow_dispatch' || eventName === 'schedule') {
// mode train
// save issues
core.info(`[INFO] event: ${eventName}, mode: train`);

// fetch issues and comments
Expand Down Expand Up @@ -69,16 +69,41 @@ export async function run(): Promise<void> {
core.info(`[INFO] ${uploadResult}`);
core.endGroup();
} else if (eventName === 'issues') {
// mode suggest
core.info(`[INFO] event: ${eventName}, mode: suggest`);
const eventType = context.payload.action;
if (eventType === 'opened') {
core.info(`[INFO] event type: ${eventType}`);
await suggest(inps, tmpDir);
} else if (eventType === 'edited') {
core.warning(`[WARN] ${eventType} event type is not supported`);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const body = `${(context.payload.issue as any).body}`;

if (inps.Mode === 'save') {
// mode: save body
core.startGroup('Save input.txt');
const inputBody = (() => {
if (body === '') {
return 'context body is empty';
} else {
return removeSymbols(md2text(body));
}
})();
core.info(`[INFO] save input.txt`);
fs.writeFileSync(path.join(tmpDir, 'input.txt'), inputBody);
core.endGroup();
} else if (inps.Mode === 'suggest') {
// mode: suggest
core.info(`[INFO] event: ${eventName}, mode: suggest`);
if (body === '') {
core.info('[INFO] context body is empty, skip suggesting');
return;
}
const eventType = context.payload.action;
if (eventType === 'opened') {
core.info(`[INFO] event type: ${eventType}`);
await suggest(inps, tmpDir);
} else if (eventType === 'edited') {
core.warning(`[WARN] ${eventType} event type is not supported`);
} else {
core.warning(`[WARN] ${eventType} event type is not supported`);
}
} else {
core.warning(`[WARN] ${eventType} event type is not supported`);
// unsupported mode
throw new Error(`${inps.Mode} mode is not supported`);
}
} else {
// unsupported event
Expand Down
20 changes: 4 additions & 16 deletions actions/src/suggest.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
import {GitHubAPI} from './github-api';
import {Inputs} from './interfaces';
import {context} from '@actions/github/lib/utils';
import {md2text, removeSymbols} from './preprocess';
import fs from 'fs';
import path from 'path';

export async function suggest(inps: Inputs, tmpDir: string): Promise<void> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const body = `${(context.payload.issue as any).body}`;
const suggestBody = (() => {
if (body === '') {
return 'context body is empty';
} else {
return removeSymbols(md2text(body));
}
})();
console.log(suggestBody);

const results = JSON.parse(fs.readFileSync(path.join(tmpDir, 'suggestions.json'), 'utf8'));
const topNcount = 3;
const topNcount = Number(inps.MaxLinks);
let commentBody = '';
for (let i = 0; i < topNcount; i++) {
commentBody += `- [${results[i].title}](${results[i].html_url}) (${results[
i
].probability.toFixed(3)})\n`;
commentBody +=
`- (${results[i].probability.toFixed(3)}) ` +
`[${results[i].title}](${results[i].html_url})\n`;
}

const githubAPI = new GitHubAPI(inps.GithubToken, context.repo.owner, context.repo.repo);
Expand Down

0 comments on commit b73bfe6

Please sign in to comment.