mirror of
https://gitea.com/Lydanne/issues-helper.git
synced 2025-08-20 02:35:58 +08:00
done
This commit is contained in:
149
src/advanced.js
149
src/advanced.js
@@ -2,22 +2,113 @@ require('dotenv').config();
|
||||
const core = require("@actions/core");
|
||||
const { Octokit } = require('@octokit/rest');
|
||||
|
||||
const token = core.getInput('token') || process.env.GH_TOKEN;
|
||||
var dayjs = require('dayjs');
|
||||
var utc = require('dayjs/plugin/utc');
|
||||
dayjs.extend(utc);
|
||||
var isSameOrBefore = require('dayjs/plugin/isSameOrBefore');
|
||||
dayjs.extend(isSameOrBefore);
|
||||
|
||||
const {
|
||||
doAddLabels,
|
||||
doCloseIssue,
|
||||
doLockIssue
|
||||
} = require('./base.js');
|
||||
|
||||
const token = core.getInput('token');
|
||||
const octokit = new Octokit({ auth: `token ${token}` });
|
||||
|
||||
const commentAuth = core.getInput("comment-auth") || 'xrkffgg';
|
||||
const bodyIncludes = core.getInput('body-includes') || 'is';
|
||||
const titleIncludes = core.getInput('title-includes') || 'test';
|
||||
let direction = core.getInput("direction");
|
||||
direction = direction === 'desc' ? 'desc' : 'asc';
|
||||
|
||||
const issueCreator = core.getInput("issue-creator") || 'xrkffgg';
|
||||
const commentAuth = core.getInput("comment-auth");
|
||||
const bodyIncludes = core.getInput('body-includes');
|
||||
const titleIncludes = core.getInput('title-includes');
|
||||
|
||||
const issueCreator = core.getInput("issue-creator");
|
||||
const issueAssignee = core.getInput('issue-assignee');
|
||||
const issueMentioned = core.getInput('issue-mentioned');
|
||||
|
||||
let issueState = core.getInput("issue-state") || 'all';
|
||||
|
||||
if (issueState != 'open' && issueState != 'closed') {
|
||||
issueState = 'all';
|
||||
}
|
||||
|
||||
const inactiveDay = core.getInput("inactive-day");
|
||||
const inactiveLabel = core.getInput("inactive-label") || 'inactive';
|
||||
|
||||
async function doCheckInactive (owner, repo, labels) {
|
||||
const issues = await doQueryIssues(owner, repo, labels, issueState);
|
||||
|
||||
if (issues.length) {
|
||||
for (let i = 0; i < issues.length; i++) {
|
||||
if (!JSON.stringify(issues[i].labels).includes(inactiveLabel)) {
|
||||
await doAddLabels(owner, repo, issues[i].number, inactiveLabel);
|
||||
} else {
|
||||
core.info(`Actions: [add-inactive] issue ${issues[i].number} has label!`);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
core.info(`Actions: [query-issues] empty!`);
|
||||
}
|
||||
};
|
||||
|
||||
async function doCloseIssues (owner, repo, labels) {
|
||||
const issues = await doQueryIssues(owner, repo, labels, 'open');
|
||||
|
||||
if (issues.length) {
|
||||
for (let i = 0; i < issues.length; i++) {
|
||||
await doCloseIssue(owner, repo, issues[i].number);
|
||||
}
|
||||
} else {
|
||||
core.info(`Actions: [query-issues] empty!`);
|
||||
}
|
||||
};
|
||||
|
||||
async function doFindComments (owner, repo, issueNumber) {
|
||||
const res = await octokit.issues.listComments({
|
||||
owner,
|
||||
repo,
|
||||
issue_number: issueNumber
|
||||
});
|
||||
core.info(`Actions: [find-comments][${issueNumber}] success!`);
|
||||
let comments = [];
|
||||
res.data.forEach(item => {
|
||||
const a = commentAuth ? item.user.login === commentAuth : true;
|
||||
const b = bodyIncludes ? item.body.includes(bodyIncludes) : true;
|
||||
if (a && b) {
|
||||
comments.push({
|
||||
id: item.id,
|
||||
auth: item.user.login,
|
||||
body: item.body,
|
||||
created: item.created_at,
|
||||
updated: item.updated_at
|
||||
})
|
||||
if (direction === 'desc') {
|
||||
comments.reverse();
|
||||
}
|
||||
}
|
||||
})
|
||||
core.setOutput("comments", comments);
|
||||
};
|
||||
|
||||
async function doLockIssues (owner, repo, labels) {
|
||||
const issues = await doQueryIssues(owner, repo, labels, issueState);
|
||||
|
||||
if (issues.length) {
|
||||
for (let i = 0; i < issues.length; i++) {
|
||||
await doLockIssue(owner, repo, issues[i].number);
|
||||
}
|
||||
} else {
|
||||
core.info(`Actions: [query-issues] empty!`);
|
||||
}
|
||||
};
|
||||
|
||||
async function doQueryIssues (owner, repo, labels, state) {
|
||||
let params = {
|
||||
owner,
|
||||
repo,
|
||||
state: 'open',
|
||||
state,
|
||||
};
|
||||
|
||||
issueCreator ? params.creator = issueCreator : null;
|
||||
@@ -31,46 +122,34 @@ async function doCloseIssues (owner, repo, labels) {
|
||||
let temp = '';
|
||||
labels.forEach((it,index) => {
|
||||
index == labels.length - 1 ? temp += `${it}` : temp += `${it},`;
|
||||
})
|
||||
});
|
||||
params.labels = temp;
|
||||
}
|
||||
}
|
||||
|
||||
console.log(params.labels)
|
||||
|
||||
const res = await octokit.issues.listForRepo(params);
|
||||
console.log(res);
|
||||
let issues = [];
|
||||
for (let i = 0; i < res.data.length; i++) {
|
||||
wip
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
async function doFindComments (owner, repo, issueNumber) {
|
||||
const res = await octokit.issues.listComments({
|
||||
owner,
|
||||
repo,
|
||||
issue_number: issueNumber
|
||||
});
|
||||
core.info(`Actions: [find-comments][${issueNumber}] success!`);
|
||||
let comments = [];
|
||||
res.data.forEach(item => {
|
||||
if ((commentAuth ? item.user.login === commentAuth : true) && (bodyIncludes ? item.body.includes(bodyIncludes) : true)) {
|
||||
comments.push({
|
||||
id: item.id,
|
||||
body: item.body
|
||||
})
|
||||
res.data.forEach(iss => {
|
||||
const a = bodyIncludes ? iss.body.includes(bodyIncludes) : true;
|
||||
const b = titleIncludes ? iss.title.includes(titleIncludes) : true;
|
||||
if (a && b) {
|
||||
if (inactiveDay && typeof(inactiveDay) === 'number') {
|
||||
let lastTime = dayjs.utc().subtract(inactiveDay, 'day');
|
||||
if (iss.updated_at.isSameOrBefore(lastTime)) {
|
||||
issues.push(iss);
|
||||
}
|
||||
} else {
|
||||
issues.push(iss);
|
||||
}
|
||||
}
|
||||
})
|
||||
core.setOutput("comments", comments);
|
||||
|
||||
return issues;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
module.exports = {
|
||||
doCheckInactive,
|
||||
doCloseIssues,
|
||||
doFindComments,
|
||||
doLockIssues,
|
||||
};
|
||||
|
58
src/base.js
58
src/base.js
@@ -13,27 +13,37 @@ const ALLREACTIONS = [
|
||||
"eyes",
|
||||
];
|
||||
|
||||
const token = core.getInput('token') || process.env.GH_TOKEN;
|
||||
const { dealInput } = require('./util.js');
|
||||
|
||||
const token = core.getInput('token');
|
||||
const octokit = new Octokit({ auth: `token ${token}` });
|
||||
|
||||
const contents = core.getInput("contents");
|
||||
|
||||
async function doAddAssignees (owner, repo, issueNumber, assignees) {
|
||||
if (core.getInput("body")) {
|
||||
await doCreateComment(owner, repo, issueNumber, core.getInput("body"))
|
||||
}
|
||||
|
||||
await octokit.issues.addAssignees({
|
||||
owner,
|
||||
repo,
|
||||
issue_number: issueNumber,
|
||||
assignees
|
||||
assignees: dealInput(assignees)
|
||||
});
|
||||
core.info(`Actions: [add-assignees][${assignees}] success!`);
|
||||
};
|
||||
|
||||
async function doAddLabels (owner, repo, issueNumber, labels) {
|
||||
if (core.getInput("body")) {
|
||||
await doCreateComment(owner, repo, issueNumber, core.getInput("body"))
|
||||
}
|
||||
|
||||
await octokit.issues.addLabels({
|
||||
owner,
|
||||
repo,
|
||||
issue_number: issueNumber,
|
||||
labels
|
||||
labels: dealInput(labels)
|
||||
});
|
||||
core.info(`Actions: [add-labels][${labels}] success!`);
|
||||
};
|
||||
@@ -97,13 +107,10 @@ async function doCreateIssue (owner, repo, title, body, labels, assignees) {
|
||||
repo,
|
||||
title,
|
||||
body,
|
||||
labels
|
||||
labels: dealInput(labels),
|
||||
assignees: dealInput(assignees),
|
||||
};
|
||||
if (typeof(assignees) === 'string') {
|
||||
params.assignees.push(assignees);
|
||||
} else {
|
||||
params.assignees = assignees;
|
||||
}
|
||||
|
||||
const { data } = await octokit.issues.create(params);
|
||||
core.info(`Actions: [create-issue][${title}] success!`);
|
||||
core.setOutput("issue-number", data.number);
|
||||
@@ -147,6 +154,10 @@ async function doDeleteComment (owner, repo, commentId) {
|
||||
};
|
||||
|
||||
async function doLockIssue (owner, repo, issueNumber) {
|
||||
if (core.getInput("body")) {
|
||||
await doCreateComment(owner, repo, issueNumber, core.getInput("body"))
|
||||
}
|
||||
|
||||
await octokit.issues.lock({
|
||||
owner,
|
||||
repo,
|
||||
@@ -170,26 +181,38 @@ async function doOpenIssue (owner, repo, issueNumber) {
|
||||
};
|
||||
|
||||
async function doRemoveAssignees (owner, repo, issueNumber, assignees) {
|
||||
if (core.getInput("body")) {
|
||||
await doCreateComment(owner, repo, issueNumber, core.getInput("body"))
|
||||
}
|
||||
|
||||
await octokit.issues.removeAssignees({
|
||||
owner,
|
||||
repo,
|
||||
issue_number: issueNumber,
|
||||
assignees
|
||||
assignees: dealInput(assignees),
|
||||
});
|
||||
core.info(`Actions: [remove-assignees][${assignees}] success!`);
|
||||
};
|
||||
|
||||
async function doSetLabels (owner, repo, issueNumber, labels) {
|
||||
if (core.getInput("body")) {
|
||||
await doCreateComment(owner, repo, issueNumber, core.getInput("body"))
|
||||
}
|
||||
|
||||
await octokit.issues.setLabels({
|
||||
owner,
|
||||
repo,
|
||||
issue_number: issueNumber,
|
||||
labels
|
||||
labels: dealInput(labels)
|
||||
});
|
||||
core.info(`Actions: [set-labels][${labels}] success!`);
|
||||
};
|
||||
|
||||
async function doUnlockIssue (owner, repo, issueNumber) {
|
||||
if (core.getInput("body")) {
|
||||
await doCreateComment(owner, repo, issueNumber, core.getInput("body"))
|
||||
}
|
||||
|
||||
await octokit.issues.unlock({
|
||||
owner,
|
||||
repo,
|
||||
@@ -276,17 +299,8 @@ async function doUpdateIssue (
|
||||
}
|
||||
params.body = next_body;
|
||||
|
||||
if (core.getInput("assignees")) {
|
||||
if (typeof(assignees) === 'string') {
|
||||
params.assignees.push(assignees);
|
||||
} else {
|
||||
params.assignees = assignees;
|
||||
}
|
||||
} else {
|
||||
params.assignees = issue_assignees;
|
||||
}
|
||||
|
||||
params.labels = labels ? labels : issue_labels;
|
||||
params.labels = labels ? dealInput(labels) : issue_labels;
|
||||
params.assignees = assignees ? dealInput(assignees) : issue_assignees;
|
||||
|
||||
await octokit.issues.update(params);
|
||||
core.info(`Actions: [update-issue][${issueNumber}] success!`);
|
||||
|
33
src/main.js
33
src/main.js
@@ -20,8 +20,10 @@ const {
|
||||
} = require('./base.js');
|
||||
|
||||
const {
|
||||
doCheckInactive,
|
||||
doCloseIssues,
|
||||
doFindComments,
|
||||
doLockIssues,
|
||||
} = require('./advanced.js');
|
||||
|
||||
const ALLACTIONS = [
|
||||
@@ -41,6 +43,7 @@ const ALLACTIONS = [
|
||||
'update-issue',
|
||||
|
||||
// advanced
|
||||
'check-inactive',
|
||||
'close-issues',
|
||||
'find-comments',
|
||||
'lock-issues',
|
||||
@@ -48,12 +51,10 @@ const ALLACTIONS = [
|
||||
|
||||
async function main() {
|
||||
try {
|
||||
// const owner = github.context.repo.owner;
|
||||
// const repo = github.context.repo.repo;
|
||||
const owner = 'actions-cool';
|
||||
const repo = 'issue-helper';
|
||||
const owner = github.context.repo.owner;
|
||||
const repo = github.context.repo.repo;
|
||||
|
||||
const issueNumber = core.getInput('issue-number') || 1;
|
||||
const issueNumber = core.getInput('issue-number');
|
||||
const commentId = core.getInput('comment-id');
|
||||
|
||||
const defaultBody = `Currently at ${owner}/${repo}. And this is default comment.`
|
||||
@@ -64,16 +65,15 @@ async function main() {
|
||||
|
||||
const assignees = core.getInput("assignees");
|
||||
|
||||
const labels = core.getInput("labels") || ['test'];
|
||||
const labels = core.getInput("labels");
|
||||
const state = core.getInput("state") || 'open';
|
||||
|
||||
let updateMode = core.getInput("update-mode") || 'replace';
|
||||
let updateMode = core.getInput("update-mode");
|
||||
if (updateMode !== 'append') {
|
||||
updateMode = 'replace';
|
||||
}
|
||||
|
||||
// const actions = core.getInput("actions", { required: true });
|
||||
const actions = 'close-issues';
|
||||
const actions = core.getInput("actions", { required: true });
|
||||
|
||||
if (typeof(actions) === 'object') {
|
||||
actions.forEach(item => {
|
||||
@@ -151,6 +151,13 @@ async function main() {
|
||||
break;
|
||||
|
||||
// advanced
|
||||
case 'check-inactive':
|
||||
await doCheckInactive(
|
||||
owner,
|
||||
repo,
|
||||
labels
|
||||
)
|
||||
break;
|
||||
case 'close-issues':
|
||||
await doCloseIssues(
|
||||
owner,
|
||||
@@ -165,7 +172,13 @@ async function main() {
|
||||
issueNumber
|
||||
);
|
||||
break;
|
||||
|
||||
case 'lock-issues':
|
||||
await doLockIssues(
|
||||
owner,
|
||||
repo,
|
||||
labels
|
||||
);
|
||||
break;
|
||||
// default
|
||||
default:
|
||||
break;
|
||||
|
15
src/util.js
Normal file
15
src/util.js
Normal file
@@ -0,0 +1,15 @@
|
||||
function dealInput (para) {
|
||||
let arr = [];
|
||||
if (para) {
|
||||
if (typeof(para) === 'string') {
|
||||
arr.push(para);
|
||||
} else {
|
||||
arr = para;
|
||||
}
|
||||
}
|
||||
return arr;
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
dealInput,
|
||||
};
|
Reference in New Issue
Block a user