feat: support remove-labels

This commit is contained in:
元凛
2022-09-17 19:13:15 +08:00
parent 78a8afb859
commit 7c5763d4cc
11 changed files with 5851 additions and 323 deletions

View File

@@ -1,5 +1,11 @@
# Changelog
## v1.2.0
`2022.09.17`
- feat: support `remove-labels`.
## v1.1.0
`2021.03.17`

View File

@@ -42,6 +42,7 @@ jobs:
| way | The way to query issues. Options: `title` `body` `commit` | string | ✔ |
| filter-label | Further filter issues through label | string | ✖ |
| issues-labels | Extra labels on issues | string | ✖ |
| remove-labels | Remove labels on issues | string | ✖ |
| issues-comment | Extra comment on issues | string | ✖ |
| issues-close | Extra close issues | string | ✖ |

View File

@@ -22,6 +22,8 @@ inputs:
description: Extra operations on issues
issues-comment:
description: Extra operations on issues
remove-labels:
description: Will remove labels on issues
issues-close:
description: Extra operations on issues

5963
dist/index.js vendored

File diff suppressed because one or more lines are too long

View File

@@ -1,16 +1,15 @@
{
"name": "pr-extract-issues",
"version": "1.0.0",
"private": true,
"description": "A GitHub Action help you extract issues from pr commit or title or body.",
"license": "MIT",
"author": "xrkffgg",
"main": "src/main.js",
"scripts": {
"package": "ncc build",
"format": "prettier --write src/*.js",
"format-check": "prettier --check src/*.js"
"format-check": "prettier --check src/*.js",
"pub": "sh -e ./scripts/pub.sh"
},
"author": "xrkffgg",
"license": "MIT",
"dependencies": {
"@actions/core": "^1.2.6",
"@actions/github": "^4.0.0",
@@ -20,6 +19,10 @@
"devDependencies": {
"@umijs/fabric": "^2.5.6",
"@vercel/ncc": "^0.27.0",
"prettier": "^2.2.1"
"chalk": "^4.1.2",
"new-github-release-url": "^1.0.0",
"open": "^7.3.0",
"prettier": "^2.2.1",
"simple-git": "^2.46.0"
}
}

23
scripts/check-commit.js Executable file
View File

@@ -0,0 +1,23 @@
const chalk = require('chalk');
const simpleGit = require('simple-git/promise');
const cwd = process.cwd();
const git = simpleGit(cwd);
async function checkCommit({ files }) {
if (files.length) {
console.log(chalk.yellow('🙄 You forgot something to commit.'));
files.forEach(({ path: filePath }) => {
console.log(' -', chalk.red(filePath));
});
console.log('');
process.exit(1);
}
}
async function run() {
const status = await git.status();
await checkCommit(status);
}
run();

9
scripts/const.js Normal file
View File

@@ -0,0 +1,9 @@
const CHANGELOG_NAME = 'CHANGELOG.md';
const user = 'actions-cool';
const repo = 'pr-extract-issues';
module.exports = {
CHANGELOG_NAME,
user,
repo,
};

14
scripts/pub.sh Normal file
View File

@@ -0,0 +1,14 @@
echo "[TEST] check format"
npm run format-check
echo "[TEST] test package"
npm run package
echo "[TEST] test commit"
npm run check-commit
echo "[Action] do tag"
npm run tag
echo "[Action] do release"
npm run release

55
scripts/release.js Normal file
View File

@@ -0,0 +1,55 @@
const chalk = require('chalk');
const open = require('open');
const newGithubReleaseUrl = require('new-github-release-url');
const { readFileSync } = require('fs');
const path = require('path');
const {
CHANGELOG_NAME,
user,
repo
} = require('./const');
let tag = '';
function getChangelog(content) {
const lines = content.split('\n');
const changeLog = [];
const pin = /^## /;
let begin = false;
for (let i = 0; i < lines.length; i += 1) {
const line = lines[i];
if (begin && pin.test(line)) {
break;
}
if (begin && line) {
changeLog.push(line);
}
if (!begin) {
begin = pin.test(line);
if (begin) {
tag = line.substring(3, line.length).trim();
}
}
}
return changeLog.join('\n');
}
const changelogPath = path.join(__dirname, '..', CHANGELOG_NAME);
const changelog = readFileSync(changelogPath, 'utf-8');
const body = getChangelog(changelog);
async function run() {
const url = newGithubReleaseUrl({
user,
repo,
tag,
body: body,
});
await open(url);
console.log(chalk.yellow('🚀 Please check tag and changelog. Then click publish!'));
}
run();

70
scripts/tag.js Normal file
View File

@@ -0,0 +1,70 @@
const chalk = require('chalk');
const simpleGit = require('simple-git/promise');
const { execSync } = require('child_process');
const { readFileSync } = require('fs');
const path = require('path');
const { CHANGELOG_NAME } = require('./const');
const CHANGELOG_PATH = path.join(__dirname, '..', CHANGELOG_NAME);
const CHANGELOG = readFileSync(CHANGELOG_PATH, 'utf-8');
const cwd = process.cwd();
const git = simpleGit(cwd);
async function run() {
execSync(`git pull`);
const data = await git.tags();
const tags = data.all;
let tag = tags.reverse()[0];
console.log(chalk.green(`[Git Query] tag: ${tag}`));
const tagChangelog = getChangelogTag(CHANGELOG);
if (tagChangelog && tag != tagChangelog) {
console.log(chalk.yellow(`[Git Action] Push new ${tagChangelog} tag!`));
execSync(`git tag ${tagChangelog}`);
execSync(`git push origin ${tagChangelog}:${tagChangelog}`);
execSync(`git pull`);
tag = tagChangelog;
} else {
console.log(chalk.yellow('🙄 Please add new release changelog first.'));
console.log('');
process.exit(1);
}
const tagSimple = tag.startsWith('v') ? tag.substring(0, 2) : tag.substring(0, 1);
console.log(chalk.green(`[Git Query] tagSimple: ${tagSimple}`));
if (tags.includes(tagSimple)) {
console.log(chalk.yellow(`[Git Action] Delete ${tagSimple} tag`));
execSync(`git push origin :refs/tags/${tagSimple}`);
}
console.log(chalk.yellow(`[Git Action] Add new simple ${tagSimple} tag`));
execSync(`git push origin ${tag}:${tagSimple}`);
console.log(chalk.green('🎉 Done!'));
}
function getChangelogTag(content) {
const lines = content.split('\n');
const pin = /^## /;
let begin = false;
let tag = '';
for (let i = 0; i < lines.length; i += 1) {
const line = lines[i];
if (begin && pin.test(line)) {
break;
}
if (!begin) {
begin = pin.test(line);
if (begin) {
tag = line.substring(3, line.length);
}
}
}
return tag.trim();
}
run();

View File

@@ -1,7 +1,7 @@
const core = require('@actions/core');
const { Octokit } = require('@octokit/rest');
const github = require('@actions/github');
const { dealStringToArr } = require('actions-util');
const { dealStringToArr, THANKS } = require('actions-util');
// **********************************************************
const token = core.getInput('token');
@@ -78,6 +78,8 @@ async function run() {
core.setOutput('issues', issues);
const labels = core.getInput('issues-labels');
const removeLabelsString = core.getInput('remove-labels');
const removeLabels = dealStringToArr(removeLabelsString);
const comment = core.getInput('issues-comment');
const close = core.getInput('issues-close');
@@ -95,6 +97,17 @@ async function run() {
});
core.info(`Actions: [add-labels][${issue}][${labels}] success!`);
}
if (removeLabels && removeLabels.length) {
for (const label of removeLabels) {
await octokit.issues.removeLabel({
owner,
repo,
issue_number: issue,
name: label,
});
core.info(`Actions: [remove-label][${issue}][${label}] success!`);
}
}
if (comment) {
const body = comment.replace('${number}', `#${issue}`);
await octokit.issues.createComment({
@@ -118,6 +131,7 @@ async function run() {
} else {
core.setFailed(outEventErr);
}
core.info(THANKS);
} catch (error) {
core.setFailed(error.message);
}