Skip to content

Commit

Permalink
feat(notify): turn off version
Browse files Browse the repository at this point in the history
  • Loading branch information
Airkro committed Dec 21, 2023
1 parent 4cc15e7 commit 7371a77
Show file tree
Hide file tree
Showing 5 changed files with 294 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/bring-it.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
uses: airkro/bring-it@actions
with:
npm-token: ${{ secrets.NPM_TOKEN }}
publish-command: npx @bring-it/npm@0.3.9 npm
publish-command: npx -p @bring-it/npm@latest bring-it npm
custom-command: |
cd packages
mkdir -p cli/dist
Expand Down
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,21 @@
"@nice-move/prettier-config": "^0.10.0",
"best-shot": "^0.5.0",
"eslint": "^8.56.0",
"garou": "^0.6.20",
"garou": "^0.6.22",
"prettier": "^3.1.1"
},
"peerDependencies": {
"@bring-it/npm": "workspace:*",
"@bring-it/utils": "workspace:*"
},
"engines": {
"node": "^18.0.0 || ^20.0.0",
"npm": ">=9.4.0"
},
"packageManager": "[email protected]",
"pnpm": {
"overrides": {
"cpu-features": "npm:garou@0.0.0"
"cpu-features": "npm:cheetor@0.0.0"
}
},
"eslintConfig": {
Expand Down
206 changes: 181 additions & 25 deletions packages/notify/lib/utils.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import { createRequire } from 'node:module';

import { sync } from 'conventional-commits-parser';
import { clean } from 'fast-clean';
import gitlog from 'gitlog';
import groupBy from 'lodash/groupBy.js';
import sortBy from 'lodash/sortBy.js';
import { toMarkdown } from 'mdast-util-to-markdown';
import semverPrerelease from 'semver/functions/prerelease.js';

Expand All @@ -10,13 +17,26 @@ const {
DEPOT_NAME,
GIT_COMMIT_SHORT,
GIT_COMMIT,
GIT_PREVIOUS_COMMIT,
GIT_HTTP_URL,
JOB_ID,
npm_package_version = '未知',
PROJECT_NAME,
PROJECT_WEB_URL,
} = process.env;

function getVersion({ isLatest, version }) {
let name = npm_package_version;

if (version !== true) {
try {
name = createRequire(import.meta.url)(`${version}/package.json`).version;
} catch {}
}

return isLatest ? ['latest', name].join(' / ') : name;
}

export function dingtalk({ markdown, title, token }) {
console.log({ title });

Expand All @@ -31,9 +51,14 @@ export function dingtalk({ markdown, title, token }) {
markdown: {
title,
text: markdown
.trim()
// eslint-disable-next-line unicorn/prefer-string-replace-all
.replace(/\n\n/g, '\n\n<br/>\n\n'),
.split('更新历史:')
.map((text, i) =>
i === 0
? text.replaceAll('\n\n', '\n\n<br/>\n\n')
: text.replaceAll('\n\n##', '\n\n<br/>\n\n##'),
)
.join('更新历史:')
.trim(),
},
},
});
Expand All @@ -43,17 +68,138 @@ function isTest() {
return BRANCH_NAME !== 'master' || semverPrerelease(npm_package_version);
}

const configs = {
feat: {
label: '功能变化',
level: 0,
},
fix: {
label: '功能修复',
level: 1,
},
perf: {
label: '性能优化',
level: 2,
},
refactor: {
label: '重构',
level: 3,
},
revert: {
label: '回滚',
level: 4,
},
chore: {
label: '杂项',
level: 5,
},
style: {
label: '代码风格',
level: 6,
},
test: {
label: '测试',
level: 7,
},
build: {
label: '编译配置',
level: 8,
},
ci: {
label: '自动化',
level: 9,
},
docs: {
label: '补充文档',
level: 10,
},
};

function getCommits() {
const io =
GIT_COMMIT === GIT_PREVIOUS_COMMIT
? []
: sortBy(
Object.entries(
groupBy(
gitlog
.default({
repo: '.',
since: GIT_PREVIOUS_COMMIT,
until: GIT_COMMIT,
fields: ['hash', 'abbrevHash', 'subject'],
})
.map(({ abbrevHash, hash, subject }) => ({
hash,
abbrevHash,
message: sync(subject),
subject,
})),
({ message }) => message.type,
),
),
([type]) => configs[type].level,
).flatMap(([type, list]) => [
{
type: 'heading',
depth: 4,
children: [
{
type: 'text',
value: configs[type]?.label || type,
},
],
},
{
type: 'list',
spread: false,
children: list.map(({ hash, subject }) => ({
type: 'listItem',
children: [
{
type: 'link',
url: `${PROJECT_WEB_URL}/d/${DEPOT_NAME}/git/commit/${hash}`,
children: [
{
type: 'text',
value: subject,
},
],
},
],
})),
},
]);

return io.length > 0
? [
{
type: 'heading',
depth: 3,
children: [
{
type: 'text',
value: '更新历史:',
},
],
},
...io,
]
: io;
}

export function createContent({
project = '未命名项目',
type,
manual = true,
banner,
isLatest = false,
image,
version = true,
}) {
return toMarkdown({
type: 'root',
children: [
children: clean([
banner
? {
type: 'paragraph',
Expand All @@ -67,7 +213,7 @@ export function createContent({
: undefined,
{
type: 'heading',
depth: 3,
depth: 2,
children: [
{
type: 'text',
Expand Down Expand Up @@ -122,8 +268,13 @@ export function createContent({
type: 'listItem',
children: [
{
type: 'text',
value: `发布类型:${type}`,
type: 'paragraph',
children: [
{
type: 'text',
value: `发布类型:${type}`,
},
],
},
],
}
Expand All @@ -139,25 +290,23 @@ export function createContent({
],
}
: undefined,
{
type: 'listItem',
children: [
{
type: 'paragraph',
version
? {
type: 'listItem',
children: [
{
type: 'text',
value: `版本编号:${
isLatest
? ['latest', npm_package_version].join(' / ')
: npm_package_version
}`,
type: 'paragraph',
children: [
{
type: 'text',
value: `版本编号:${getVersion({ isLatest, version })}`,
},
],
},
],
},
],
},
].filter(Boolean),
}
: undefined,
],
},
{
type: 'paragraph',
Expand Down Expand Up @@ -254,7 +403,13 @@ export function createContent({
children: [
{
type: 'text',
value: GIT_COMMIT_SHORT,
value:
GIT_COMMIT === GIT_PREVIOUS_COMMIT
? GIT_COMMIT_SHORT
: [
GIT_PREVIOUS_COMMIT.slice(0, 7),
GIT_COMMIT_SHORT,
].join('...'),
},
],
},
Expand Down Expand Up @@ -289,8 +444,9 @@ export function createContent({
],
}
: undefined,
].filter(Boolean),
],
},
].filter(Boolean),
...getCommits(),
]),
});
}
6 changes: 5 additions & 1 deletion packages/notify/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bring-it/notify",
"version": "0.2.0",
"version": "0.2.1",
"description": "Send releases notifications",
"license": "MIT",
"author": {
Expand Down Expand Up @@ -37,6 +37,10 @@
"prepublishOnly": "pnpm run build"
},
"devDependencies": {
"conventional-commits-parser": "^5.0.0",
"fast-clean": "^1.3.2",
"gitlog": "^4.0.8",
"lodash": "^4.17.21",
"mdast-util-to-markdown": "^2.1.0",
"semver": "^7.5.4"
},
Expand Down
Loading

0 comments on commit 7371a77

Please sign in to comment.