From 08dd02298f7a17e8ad56416b605ee97905d67fc0 Mon Sep 17 00:00:00 2001 From: peaceiris <30958501+peaceiris@users.noreply.github.com> Date: Sun, 8 Mar 2020 03:01:44 +0900 Subject: [PATCH] feat: Add main features --- src/main.ts | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 64 insertions(+), 3 deletions(-) diff --git a/src/main.ts b/src/main.ts index f461c481..440abb7a 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,16 +1,77 @@ import * as core from '@actions/core'; -import * as github from '@actions/github'; +import {context, GitHub} from '@actions/github'; import {Inputs} from './interfaces'; import {getInputs} from './get-inputs'; +import fs from 'fs'; +import yaml from 'js-yaml'; + +async function closeIssue( + githubClient: GitHub, + issueNumber: number +): Promise { + await githubClient.issues.update({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: issueNumber, // eslint-disable-line @typescript-eslint/camelcase + state: 'closed' + }); + return; +} + +async function openIssue( + githubClient: GitHub, + issueNumber: number +): Promise { + await githubClient.issues.update({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: issueNumber, // eslint-disable-line @typescript-eslint/camelcase + state: 'open' + }); + return; +} export async function run(): Promise { try { const inps: Inputs = getInputs(); core.info(`[INFO] config_file: ${inps.ConfigFilePath}`); - const context = github.context; console.log(context); - console.log(context.payload); + + const action = context.payload.action; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const labelName = (context.payload as any).label.name; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const issueNumber = (context.payload as any).issue.number; + + const configFilePath = inps.ConfigFilePath; + const config = yaml.safeLoad(fs.readFileSync(configFilePath, 'utf8')); + let commentBody = ''; + let finalAction = ''; + Object.keys(config[`${action}`]).forEach(label => { + if (config.inputs[label]['name'] === labelName) { + commentBody = config.inputs[label]['body']; + finalAction = config.inputs[label]['action']; + } + }); + + const githubToken = inps.GithubToken; + const githubClient = new GitHub(githubToken); + await githubClient.issues.createComment({ + // eslint-disable-next-line @typescript-eslint/camelcase + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: commentBody + }); + + if (finalAction === 'close') { + await closeIssue(githubClient, issueNumber); + } else if (finalAction === 'open') { + await openIssue(githubClient, issueNumber); + } + + return; } catch (error) { throw new Error(error.message); }