mirror of
https://github.com/marocchino/sticky-pull-request-comment.git
synced 2025-12-12 11:41:14 +00:00
refactor: extract config (#257)
* refactor: extract config * test: mock dynamic value
This commit is contained in:
parent
9945cf63fa
commit
56ac27318d
8 changed files with 201 additions and 76 deletions
22
__tests__/config.test.ts
Normal file
22
__tests__/config.test.ts
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import * as process from 'process'
|
||||
|
||||
test('test runs', () => {
|
||||
process.env['INPUT_HEADER'] = ''
|
||||
process.env['INPUT_NUMBER'] = '123'
|
||||
process.env['INPUT_APPEND'] = 'false'
|
||||
process.env['INPUT_RECREATE'] = 'false'
|
||||
process.env['INPUT_DELETE'] = 'false'
|
||||
process.env['INPUT_GITHUB_TOKEN'] = 'some-token'
|
||||
process.env['GITHUB_REPOSITORY'] = 'marocchino/stick-pull-request-comment'
|
||||
expect(require('../src/config')).toMatchObject({
|
||||
pullRequestNumber: expect.any(Number),
|
||||
repo: {owner: 'marocchino', repo: 'stick-pull-request-comment'},
|
||||
message: '',
|
||||
path: '',
|
||||
header: '',
|
||||
append: false,
|
||||
recreate: false,
|
||||
deleteOldComment: false,
|
||||
githubToken: 'some-token'
|
||||
})
|
||||
})
|
||||
|
|
@ -4,15 +4,19 @@ author: "marocchino"
|
|||
inputs:
|
||||
header:
|
||||
description: "Header to determine if the comment is to be updated, not shown on screen. It can be used when you want to add multiple comments independently at the same time."
|
||||
default: ""
|
||||
required: false
|
||||
append:
|
||||
description: "Indicate if new comment messages should be appended to previous comment message. Only `true` is allowed. Just skip this item when you don't need it."
|
||||
default: "false"
|
||||
required: false
|
||||
recreate:
|
||||
description: "Indicate if previous comment should be removed before creating a new comment. Only `true` is allowed. Just skip this item when you don't need it."
|
||||
default: "false"
|
||||
required: false
|
||||
delete:
|
||||
description: "delete the previously created comment. Only `true` is allowed. Just skip this item when you don't need it."
|
||||
default: "false"
|
||||
required: false
|
||||
message:
|
||||
description: "comment message"
|
||||
|
|
|
|||
94
dist/index.js
generated
vendored
94
dist/index.js
generated
vendored
|
|
@ -77,6 +77,55 @@ function deleteComment(octokit, repo, comment_id) {
|
|||
exports.deleteComment = deleteComment;
|
||||
|
||||
|
||||
/***/ }),
|
||||
|
||||
/***/ 88:
|
||||
/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {
|
||||
|
||||
"use strict";
|
||||
|
||||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
||||
}) : (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
o[k2] = m[k];
|
||||
}));
|
||||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
||||
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
||||
}) : function(o, v) {
|
||||
o["default"] = v;
|
||||
});
|
||||
var __importStar = (this && this.__importStar) || function (mod) {
|
||||
if (mod && mod.__esModule) return mod;
|
||||
var result = {};
|
||||
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
||||
__setModuleDefault(result, mod);
|
||||
return result;
|
||||
};
|
||||
var _a, _b;
|
||||
Object.defineProperty(exports, "__esModule", ({ value: true }));
|
||||
exports.githubToken = exports.deleteOldComment = exports.recreate = exports.append = exports.header = exports.path = exports.message = exports.repo = exports.pullRequestNumber = void 0;
|
||||
const core = __importStar(__nccwpck_require__(186));
|
||||
const github_1 = __nccwpck_require__(438);
|
||||
exports.pullRequestNumber = ((_b = (_a = github_1.context === null || github_1.context === void 0 ? void 0 : github_1.context.payload) === null || _a === void 0 ? void 0 : _a.pull_request) === null || _b === void 0 ? void 0 : _b.number) ||
|
||||
+core.getInput('number', { required: false });
|
||||
exports.repo = buildRepo();
|
||||
exports.message = core.getInput('message', { required: false });
|
||||
exports.path = core.getInput('path', { required: false });
|
||||
exports.header = core.getInput('header', { required: false });
|
||||
exports.append = core.getInput('append', { required: true }) === 'true';
|
||||
exports.recreate = core.getInput('recreate', { required: true }) === 'true';
|
||||
exports.deleteOldComment = core.getInput('delete', { required: true }) === 'true';
|
||||
exports.githubToken = core.getInput('GITHUB_TOKEN', { required: true });
|
||||
function buildRepo() {
|
||||
return {
|
||||
owner: github_1.context.repo.owner,
|
||||
repo: core.getInput('repo', { required: false }) || github_1.context.repo.repo
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
/***/ }),
|
||||
|
||||
/***/ 109:
|
||||
|
|
@ -116,56 +165,45 @@ Object.defineProperty(exports, "__esModule", ({ value: true }));
|
|||
const core = __importStar(__nccwpck_require__(186));
|
||||
const github = __importStar(__nccwpck_require__(438));
|
||||
const comment_1 = __nccwpck_require__(667);
|
||||
const config_1 = __nccwpck_require__(88);
|
||||
const fs_1 = __nccwpck_require__(747);
|
||||
function run() {
|
||||
var _a, _b, _c;
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
const number = ((_c = (_b = (_a = github.context) === null || _a === void 0 ? void 0 : _a.payload) === null || _b === void 0 ? void 0 : _b.pull_request) === null || _c === void 0 ? void 0 : _c.number) ||
|
||||
+core.getInput('number', { required: false });
|
||||
if (isNaN(number) || number < 1) {
|
||||
if (isNaN(config_1.pullRequestNumber) || config_1.pullRequestNumber < 1) {
|
||||
core.info('no pull request numbers given: skip step');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const repo = github.context.repo;
|
||||
repo.repo = core.getInput('repo', { required: false }) || repo.repo;
|
||||
const message = core.getInput('message', { required: false });
|
||||
const path = core.getInput('path', { required: false });
|
||||
const header = core.getInput('header', { required: false }) || '';
|
||||
const append = (core.getInput('append', { required: false }) || 'false') === 'true';
|
||||
const recreate = (core.getInput('recreate', { required: false }) || 'false') === 'true';
|
||||
const deleteOldComment = (core.getInput('delete', { required: false }) || 'false') === 'true';
|
||||
const githubToken = core.getInput('GITHUB_TOKEN', { required: true });
|
||||
const octokit = github.getOctokit(githubToken);
|
||||
const previous = yield comment_1.findPreviousComment(octokit, repo, number, header);
|
||||
if (!deleteOldComment && !message && !path) {
|
||||
const octokit = github.getOctokit(config_1.githubToken);
|
||||
const previous = yield comment_1.findPreviousComment(octokit, config_1.repo, config_1.pullRequestNumber, config_1.header);
|
||||
if (!config_1.deleteOldComment && !config_1.message && !config_1.path) {
|
||||
throw new Error('Either message or path input is required');
|
||||
}
|
||||
if (deleteOldComment && recreate) {
|
||||
if (config_1.deleteOldComment && config_1.recreate) {
|
||||
throw new Error('delete and recreate cannot be both set to true');
|
||||
}
|
||||
let body;
|
||||
if (path) {
|
||||
body = fs_1.readFileSync(path, 'utf-8');
|
||||
if (config_1.path) {
|
||||
body = fs_1.readFileSync(config_1.path, 'utf-8');
|
||||
}
|
||||
else {
|
||||
body = message;
|
||||
body = config_1.message;
|
||||
}
|
||||
if (previous) {
|
||||
const previousBody = append ? previous.body : undefined;
|
||||
if (deleteOldComment) {
|
||||
yield comment_1.deleteComment(octokit, repo, previous.id);
|
||||
const previousBody = config_1.append ? previous.body : undefined;
|
||||
if (config_1.deleteOldComment) {
|
||||
yield comment_1.deleteComment(octokit, config_1.repo, previous.id);
|
||||
}
|
||||
else if (recreate) {
|
||||
yield comment_1.deleteComment(octokit, repo, previous.id);
|
||||
yield comment_1.createComment(octokit, repo, number, body, header, previousBody);
|
||||
else if (config_1.recreate) {
|
||||
yield comment_1.deleteComment(octokit, config_1.repo, previous.id);
|
||||
yield comment_1.createComment(octokit, config_1.repo, config_1.pullRequestNumber, body, config_1.header, previousBody);
|
||||
}
|
||||
else {
|
||||
yield comment_1.updateComment(octokit, repo, previous.id, body, header, previousBody);
|
||||
yield comment_1.updateComment(octokit, config_1.repo, previous.id, body, config_1.header, previousBody);
|
||||
}
|
||||
}
|
||||
else {
|
||||
yield comment_1.createComment(octokit, repo, number, body, header);
|
||||
yield comment_1.createComment(octokit, config_1.repo, config_1.pullRequestNumber, body, config_1.header);
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
|
|
|
|||
2
dist/index.js.map
generated
vendored
2
dist/index.js.map
generated
vendored
File diff suppressed because one or more lines are too long
41
lib/config.js
generated
Normal file
41
lib/config.js
generated
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
"use strict";
|
||||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
||||
}) : (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
o[k2] = m[k];
|
||||
}));
|
||||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
||||
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
||||
}) : function(o, v) {
|
||||
o["default"] = v;
|
||||
});
|
||||
var __importStar = (this && this.__importStar) || function (mod) {
|
||||
if (mod && mod.__esModule) return mod;
|
||||
var result = {};
|
||||
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
||||
__setModuleDefault(result, mod);
|
||||
return result;
|
||||
};
|
||||
var _a, _b;
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.githubToken = exports.deleteOldComment = exports.recreate = exports.append = exports.header = exports.path = exports.message = exports.repo = exports.pullRequestNumber = void 0;
|
||||
const core = __importStar(require("@actions/core"));
|
||||
const github_1 = require("@actions/github");
|
||||
exports.pullRequestNumber = ((_b = (_a = github_1.context === null || github_1.context === void 0 ? void 0 : github_1.context.payload) === null || _a === void 0 ? void 0 : _a.pull_request) === null || _b === void 0 ? void 0 : _b.number) ||
|
||||
+core.getInput('number', { required: false });
|
||||
exports.repo = buildRepo();
|
||||
exports.message = core.getInput('message', { required: false });
|
||||
exports.path = core.getInput('path', { required: false });
|
||||
exports.header = core.getInput('header', { required: false });
|
||||
exports.append = core.getInput('append', { required: true }) === 'true';
|
||||
exports.recreate = core.getInput('recreate', { required: true }) === 'true';
|
||||
exports.deleteOldComment = core.getInput('delete', { required: true }) === 'true';
|
||||
exports.githubToken = core.getInput('GITHUB_TOKEN', { required: true });
|
||||
function buildRepo() {
|
||||
return {
|
||||
owner: github_1.context.repo.owner,
|
||||
repo: core.getInput('repo', { required: false }) || github_1.context.repo.repo
|
||||
};
|
||||
}
|
||||
45
lib/main.js
generated
45
lib/main.js
generated
|
|
@ -31,56 +31,45 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|||
const core = __importStar(require("@actions/core"));
|
||||
const github = __importStar(require("@actions/github"));
|
||||
const comment_1 = require("./comment");
|
||||
const config_1 = require("./config");
|
||||
const fs_1 = require("fs");
|
||||
function run() {
|
||||
var _a, _b, _c;
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
const number = ((_c = (_b = (_a = github.context) === null || _a === void 0 ? void 0 : _a.payload) === null || _b === void 0 ? void 0 : _b.pull_request) === null || _c === void 0 ? void 0 : _c.number) ||
|
||||
+core.getInput('number', { required: false });
|
||||
if (isNaN(number) || number < 1) {
|
||||
if (isNaN(config_1.pullRequestNumber) || config_1.pullRequestNumber < 1) {
|
||||
core.info('no pull request numbers given: skip step');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const repo = github.context.repo;
|
||||
repo.repo = core.getInput('repo', { required: false }) || repo.repo;
|
||||
const message = core.getInput('message', { required: false });
|
||||
const path = core.getInput('path', { required: false });
|
||||
const header = core.getInput('header', { required: false }) || '';
|
||||
const append = (core.getInput('append', { required: false }) || 'false') === 'true';
|
||||
const recreate = (core.getInput('recreate', { required: false }) || 'false') === 'true';
|
||||
const deleteOldComment = (core.getInput('delete', { required: false }) || 'false') === 'true';
|
||||
const githubToken = core.getInput('GITHUB_TOKEN', { required: true });
|
||||
const octokit = github.getOctokit(githubToken);
|
||||
const previous = yield comment_1.findPreviousComment(octokit, repo, number, header);
|
||||
if (!deleteOldComment && !message && !path) {
|
||||
const octokit = github.getOctokit(config_1.githubToken);
|
||||
const previous = yield comment_1.findPreviousComment(octokit, config_1.repo, config_1.pullRequestNumber, config_1.header);
|
||||
if (!config_1.deleteOldComment && !config_1.message && !config_1.path) {
|
||||
throw new Error('Either message or path input is required');
|
||||
}
|
||||
if (deleteOldComment && recreate) {
|
||||
if (config_1.deleteOldComment && config_1.recreate) {
|
||||
throw new Error('delete and recreate cannot be both set to true');
|
||||
}
|
||||
let body;
|
||||
if (path) {
|
||||
body = fs_1.readFileSync(path, 'utf-8');
|
||||
if (config_1.path) {
|
||||
body = fs_1.readFileSync(config_1.path, 'utf-8');
|
||||
}
|
||||
else {
|
||||
body = message;
|
||||
body = config_1.message;
|
||||
}
|
||||
if (previous) {
|
||||
const previousBody = append ? previous.body : undefined;
|
||||
if (deleteOldComment) {
|
||||
yield comment_1.deleteComment(octokit, repo, previous.id);
|
||||
const previousBody = config_1.append ? previous.body : undefined;
|
||||
if (config_1.deleteOldComment) {
|
||||
yield comment_1.deleteComment(octokit, config_1.repo, previous.id);
|
||||
}
|
||||
else if (recreate) {
|
||||
yield comment_1.deleteComment(octokit, repo, previous.id);
|
||||
yield comment_1.createComment(octokit, repo, number, body, header, previousBody);
|
||||
else if (config_1.recreate) {
|
||||
yield comment_1.deleteComment(octokit, config_1.repo, previous.id);
|
||||
yield comment_1.createComment(octokit, config_1.repo, config_1.pullRequestNumber, body, config_1.header, previousBody);
|
||||
}
|
||||
else {
|
||||
yield comment_1.updateComment(octokit, repo, previous.id, body, header, previousBody);
|
||||
yield comment_1.updateComment(octokit, config_1.repo, previous.id, body, config_1.header, previousBody);
|
||||
}
|
||||
}
|
||||
else {
|
||||
yield comment_1.createComment(octokit, repo, number, body, header);
|
||||
yield comment_1.createComment(octokit, config_1.repo, config_1.pullRequestNumber, body, config_1.header);
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
|
|
|
|||
23
src/config.ts
Normal file
23
src/config.ts
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
import * as core from '@actions/core'
|
||||
import {context} from '@actions/github'
|
||||
|
||||
export const pullRequestNumber =
|
||||
context?.payload?.pull_request?.number ||
|
||||
+core.getInput('number', {required: false})
|
||||
|
||||
export const repo = buildRepo()
|
||||
export const message = core.getInput('message', {required: false})
|
||||
export const path = core.getInput('path', {required: false})
|
||||
export const header = core.getInput('header', {required: false})
|
||||
export const append = core.getInput('append', {required: true}) === 'true'
|
||||
export const recreate = core.getInput('recreate', {required: true}) === 'true'
|
||||
export const deleteOldComment =
|
||||
core.getInput('delete', {required: true}) === 'true'
|
||||
export const githubToken = core.getInput('GITHUB_TOKEN', {required: true})
|
||||
|
||||
function buildRepo(): {repo: string; owner: string} {
|
||||
return {
|
||||
owner: context.repo.owner,
|
||||
repo: core.getInput('repo', {required: false}) || context.repo.repo
|
||||
}
|
||||
}
|
||||
46
src/main.ts
46
src/main.ts
|
|
@ -6,32 +6,33 @@ import {
|
|||
updateComment,
|
||||
deleteComment
|
||||
} from './comment'
|
||||
import {
|
||||
pullRequestNumber,
|
||||
repo,
|
||||
message,
|
||||
path,
|
||||
header,
|
||||
append,
|
||||
recreate,
|
||||
deleteOldComment,
|
||||
githubToken
|
||||
} from './config'
|
||||
import {readFileSync} from 'fs'
|
||||
|
||||
async function run(): Promise<undefined> {
|
||||
const number =
|
||||
github.context?.payload?.pull_request?.number ||
|
||||
+core.getInput('number', {required: false})
|
||||
if (isNaN(number) || number < 1) {
|
||||
if (isNaN(pullRequestNumber) || pullRequestNumber < 1) {
|
||||
core.info('no pull request numbers given: skip step')
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const repo = github.context.repo
|
||||
repo.repo = core.getInput('repo', {required: false}) || repo.repo
|
||||
const message = core.getInput('message', {required: false})
|
||||
const path = core.getInput('path', {required: false})
|
||||
const header = core.getInput('header', {required: false}) || ''
|
||||
const append =
|
||||
(core.getInput('append', {required: false}) || 'false') === 'true'
|
||||
const recreate =
|
||||
(core.getInput('recreate', {required: false}) || 'false') === 'true'
|
||||
const deleteOldComment =
|
||||
(core.getInput('delete', {required: false}) || 'false') === 'true'
|
||||
const githubToken = core.getInput('GITHUB_TOKEN', {required: true})
|
||||
const octokit = github.getOctokit(githubToken)
|
||||
const previous = await findPreviousComment(octokit, repo, number, header)
|
||||
const previous = await findPreviousComment(
|
||||
octokit,
|
||||
repo,
|
||||
pullRequestNumber,
|
||||
header
|
||||
)
|
||||
|
||||
if (!deleteOldComment && !message && !path) {
|
||||
throw new Error('Either message or path input is required')
|
||||
|
|
@ -55,7 +56,14 @@ async function run(): Promise<undefined> {
|
|||
await deleteComment(octokit, repo, previous.id)
|
||||
} else if (recreate) {
|
||||
await deleteComment(octokit, repo, previous.id)
|
||||
await createComment(octokit, repo, number, body, header, previousBody)
|
||||
await createComment(
|
||||
octokit,
|
||||
repo,
|
||||
pullRequestNumber,
|
||||
body,
|
||||
header,
|
||||
previousBody
|
||||
)
|
||||
} else {
|
||||
await updateComment(
|
||||
octokit,
|
||||
|
|
@ -67,7 +75,7 @@ async function run(): Promise<undefined> {
|
|||
)
|
||||
}
|
||||
} else {
|
||||
await createComment(octokit, repo, number, body, header)
|
||||
await createComment(octokit, repo, pullRequestNumber, body, header)
|
||||
}
|
||||
} catch (error) {
|
||||
core.setFailed(error.message)
|
||||
|
|
|
|||
Loading…
Reference in a new issue