From fc1a9de2c1a68cfa0dd5a0e4210d83460ff865b0 Mon Sep 17 00:00:00 2001 From: h1alexbel Date: Sat, 9 Mar 2024 16:17:37 +0300 Subject: [PATCH 1/2] feat(#15): comment.ts --- src/comment.ts | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.ts | 30 ++++++++++++++--- 2 files changed, 112 insertions(+), 5 deletions(-) create mode 100644 src/comment.ts diff --git a/src/comment.ts b/src/comment.ts new file mode 100644 index 0000000..bd4f8e8 --- /dev/null +++ b/src/comment.ts @@ -0,0 +1,87 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2023-2024 Tracehub.git + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +import {Octokit} from "@octokit/rest"; + +/** + * Comment. + */ +export class Comment { + + /** + * Github. + */ + private readonly github: Octokit; + + /** + * Issue. + */ + private readonly issue: { + owner: string, + repo: string, + number: number, + user: { + login: string + } + }; + + /** + * Text to post. + */ + private readonly text: string; + + /** + * Ctor. + * @param github Github + * @param issue Issue + * @param text Text + */ + constructor( + github: Octokit, + issue: { + owner: string, + repo: string, + number: number, + user: { + login: string + } + }, + text: string + ) { + this.github = github; + this.issue = issue; + this.text = text; + } + + /** + * Post a comment. + */ + async post() { + await this.github.issues.createComment({ + owner: this.issue.owner, + repo: this.issue.repo, + issue_number: this.issue.number, + body: this.text + }); + }; +} diff --git a/src/main.ts b/src/main.ts index 777550c..0508ec8 100644 --- a/src/main.ts +++ b/src/main.ts @@ -24,6 +24,7 @@ import * as core from "@actions/core"; import {Octokit} from "@octokit/rest"; import {IssueBody} from "./issue-body"; +import {Comment} from "./comment"; export let github: { context: { @@ -31,6 +32,9 @@ export let github: { owner: string, repo: string, number: number + user: { + login: string + } } } }; @@ -42,7 +46,10 @@ if (process.env.GITHUB_ACTIONS) { issue: { owner: "test", repo: "test", - number: 123 + number: 123, + user: { + login: "test" + } } } }; @@ -53,16 +60,29 @@ async function run() { try { const issue = github.context.issue; if (issue) { - console.log(`Found new issue: ${issue.number}`); + console.log(`Found new issue: #${issue.number}`); + const octokit = new Octokit( + {auth: core.getInput("github_token")} + ); const body = await new IssueBody( - new Octokit( - {auth: core.getInput("github_token")} - ), + octokit, issue ).fetch(); console.log(`body: ${body}`); // quality analysis. + if (!body) { + await new Comment( + octokit, + issue, + ` + @${issue.user.login} the issue body is empty. + Please provide more details. + ` + ).post(); + console.log(`Comment posted to the #${issue.number}`); + } + } else { console.log("No opened issue found"); } From 466de4f1ae10800e52fb40691827b51e7197ba70 Mon Sep 17 00:00:00 2001 From: h1alexbel Date: Sat, 9 Mar 2024 16:20:51 +0300 Subject: [PATCH 2/2] feat(#15): dist sync --- dist/comment.js | 43 ++++++++++++++++++++++++++++++ dist/index.js | 69 ++++++++++++++++++++++++++++++++++++++++++++++--- dist/main.js | 18 ++++++++++--- 3 files changed, 124 insertions(+), 6 deletions(-) create mode 100644 dist/comment.js diff --git a/dist/comment.js b/dist/comment.js new file mode 100644 index 0000000..7490552 --- /dev/null +++ b/dist/comment.js @@ -0,0 +1,43 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.Comment = void 0; +/** + * Comment. + */ +class Comment { + /** + * Ctor. + * @param github Github + * @param issue Issue + * @param text Text + */ + constructor(github, issue, text) { + this.github = github; + this.issue = issue; + this.text = text; + } + /** + * Post a comment. + */ + post() { + return __awaiter(this, void 0, void 0, function* () { + yield this.github.issues.createComment({ + owner: this.issue.owner, + repo: this.issue.repo, + issue_number: this.issue.number, + body: this.text + }); + }); + } + ; +} +exports.Comment = Comment; diff --git a/dist/index.js b/dist/index.js index 42bf4a8..0a1709c 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,6 +1,57 @@ require('./sourcemap-register.js');/******/ (() => { // webpackBootstrap /******/ var __webpack_modules__ = ({ +/***/ 4761: +/***/ (function(__unused_webpack_module, exports) { + +"use strict"; + +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +Object.defineProperty(exports, "__esModule", ({ value: true })); +exports.Comment = void 0; +/** + * Comment. + */ +class Comment { + /** + * Ctor. + * @param github Github + * @param issue Issue + * @param text Text + */ + constructor(github, issue, text) { + this.github = github; + this.issue = issue; + this.text = text; + } + /** + * Post a comment. + */ + post() { + return __awaiter(this, void 0, void 0, function* () { + yield this.github.issues.createComment({ + owner: this.issue.owner, + repo: this.issue.repo, + issue_number: this.issue.number, + body: this.text + }); + }); + } + ; +} +exports.Comment = Comment; + + +/***/ }), + /***/ 9324: /***/ (function(__unused_webpack_module, exports) { @@ -116,6 +167,7 @@ exports.github = void 0; const core = __importStar(__nccwpck_require__(2186)); const rest_1 = __nccwpck_require__(5375); const issue_body_1 = __nccwpck_require__(9324); +const comment_1 = __nccwpck_require__(4761); if (process.env.GITHUB_ACTIONS) { exports.github = __nccwpck_require__(5438); } @@ -125,7 +177,10 @@ else { issue: { owner: "test", repo: "test", - number: 123 + number: 123, + user: { + login: "test" + } } } }; @@ -136,10 +191,18 @@ function run() { try { const issue = exports.github.context.issue; if (issue) { - console.log(`Found new issue: ${issue.number}`); - const body = yield new issue_body_1.IssueBody(new rest_1.Octokit({ auth: core.getInput("github_token") }), issue).fetch(); + console.log(`Found new issue: #${issue.number}`); + const octokit = new rest_1.Octokit({ auth: core.getInput("github_token") }); + const body = yield new issue_body_1.IssueBody(octokit, issue).fetch(); console.log(`body: ${body}`); // quality analysis. + if (!body) { + yield new comment_1.Comment(octokit, issue, ` + @${issue.user.login} the issue body is empty. + Please provide more details. + `).post(); + console.log(`Comment posted to the #${issue.number}`); + } } else { console.log("No opened issue found"); diff --git a/dist/main.js b/dist/main.js index 2a90fda..43cd8ce 100644 --- a/dist/main.js +++ b/dist/main.js @@ -59,6 +59,7 @@ exports.github = void 0; const core = __importStar(require("@actions/core")); const rest_1 = require("@octokit/rest"); const issue_body_1 = require("./issue-body"); +const comment_1 = require("./comment"); if (process.env.GITHUB_ACTIONS) { exports.github = require("@actions/github"); } @@ -68,7 +69,10 @@ else { issue: { owner: "test", repo: "test", - number: 123 + number: 123, + user: { + login: "test" + } } } }; @@ -79,10 +83,18 @@ function run() { try { const issue = exports.github.context.issue; if (issue) { - console.log(`Found new issue: ${issue.number}`); - const body = yield new issue_body_1.IssueBody(new rest_1.Octokit({ auth: core.getInput("github_token") }), issue).fetch(); + console.log(`Found new issue: #${issue.number}`); + const octokit = new rest_1.Octokit({ auth: core.getInput("github_token") }); + const body = yield new issue_body_1.IssueBody(octokit, issue).fetch(); console.log(`body: ${body}`); // quality analysis. + if (!body) { + yield new comment_1.Comment(octokit, issue, ` + @${issue.user.login} the issue body is empty. + Please provide more details. + `).post(); + console.log(`Comment posted to the #${issue.number}`); + } } else { console.log("No opened issue found");