From b73bfe654141c79c58d5cbe403335f83c8dd0821 Mon Sep 17 00:00:00 2001 From: peaceiris <30958501+peaceiris@users.noreply.github.com> Date: Fri, 18 Sep 2020 04:17:52 +0900 Subject: [PATCH] feat: Add mode input --- .github/workflows/suggest-related-links.yml | 6 ++- actions/src/main.ts | 45 ++++++++++++++++----- actions/src/suggest.ts | 20 ++------- 3 files changed, 44 insertions(+), 27 deletions(-) diff --git a/.github/workflows/suggest-related-links.yml b/.github/workflows/suggest-related-links.yml index d3ccf45..ec0204c 100644 --- a/.github/workflows/suggest-related-links.yml +++ b/.github/workflows/suggest-related-links.yml @@ -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: | @@ -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' diff --git a/actions/src/main.ts b/actions/src/main.ts index ce6da70..27a4ce1 100644 --- a/actions/src/main.ts +++ b/actions/src/main.ts @@ -32,7 +32,7 @@ export async function run(): Promise { 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 @@ -69,16 +69,41 @@ export async function run(): Promise { 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 diff --git a/actions/src/suggest.ts b/actions/src/suggest.ts index 1e2074d..675aaaf 100644 --- a/actions/src/suggest.ts +++ b/actions/src/suggest.ts @@ -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 { - // 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);