From 6b86a2f2875405fb8766c2b261f7e2357a79f48b Mon Sep 17 00:00:00 2001 From: Eric Date: Thu, 14 Dec 2023 19:16:04 +0800 Subject: [PATCH] feat(notify): use mdast --- package-lock.json | 270 +++++++++++++++++++++++++++++++++ packages/notify/lib/action.mjs | 4 +- packages/notify/lib/utils.mjs | 242 ++++++++++++++++++++++++++--- packages/notify/package.json | 5 +- 4 files changed, 502 insertions(+), 19 deletions(-) diff --git a/package-lock.json b/package-lock.json index 315c729..83d509b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2056,6 +2056,29 @@ } } }, + "node_modules/decode-named-character-reference": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/decode-named-character-reference/-/decode-named-character-reference-1.0.2.tgz", + "integrity": "sha512-O8x12RzrUF8xyVcY0KJowWsmaJxQbmy0/EtnNtHRpsOcT7dFk5W598coHqBVpmWo1oQQfsCqfCmkZN5DJrZVdg==", + "dev": true, + "dependencies": { + "character-entities": "^2.0.0" + }, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/wooorm" + } + }, + "node_modules/decode-named-character-reference/node_modules/character-entities": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/character-entities/-/character-entities-2.0.2.tgz", + "integrity": "sha512-shx7oQ0Awen/BRIdkjkvz54PnEEI/EjwXDSIZp86/KKdbafHh1Df/RYGBhn4hbe2+uKC9FnT5UCEdyPz3ai9hQ==", + "dev": true, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/wooorm" + } + }, "node_modules/deep-is": { "version": "0.1.4", "resolved": "https://registry.npmjs.org/deep-is/-/deep-is-0.1.4.tgz", @@ -4324,6 +4347,16 @@ "node": ">=8" } }, + "node_modules/longest-streak": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/longest-streak/-/longest-streak-3.1.0.tgz", + "integrity": "sha512-9Ri+o0JYgehTaVBBDoMqIl8GXtbWg711O3srftcHhZ0dqnETqLaoIK0x17fUw9rFSlK/0NlsKe0Ahhyl5pXE2g==", + "dev": true, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/wooorm" + } + }, "node_modules/lru-cache": { "version": "5.1.1", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-5.1.1.tgz", @@ -4365,6 +4398,77 @@ "url": "https://opencollective.com/unified" } }, + "node_modules/mdast-util-phrasing": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/mdast-util-phrasing/-/mdast-util-phrasing-4.0.0.tgz", + "integrity": "sha512-xadSsJayQIucJ9n053dfQwVu1kuXg7jCTdYsMK8rqzKZh52nLfSH/k0sAxE0u+pj/zKZX+o5wB+ML5mRayOxFA==", + "dev": true, + "dependencies": { + "@types/mdast": "^4.0.0", + "unist-util-is": "^6.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, + "node_modules/mdast-util-phrasing/node_modules/@types/mdast": { + "version": "4.0.3", + "resolved": "https://registry.npmjs.org/@types/mdast/-/mdast-4.0.3.tgz", + "integrity": "sha512-LsjtqsyF+d2/yFOYaN22dHZI1Cpwkrj+g06G8+qtUKlhovPW89YhqSnfKtMbkgmEtYpH2gydRNULd6y8mciAFg==", + "dev": true, + "dependencies": { + "@types/unist": "*" + } + }, + "node_modules/mdast-util-to-markdown": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/mdast-util-to-markdown/-/mdast-util-to-markdown-2.1.0.tgz", + "integrity": "sha512-SR2VnIEdVNCJbP6y7kVTJgPLifdr8WEU440fQec7qHoHOUz/oJ2jmNRqdDQ3rbiStOXb2mCDGTuwsK5OPUgYlQ==", + "dev": true, + "dependencies": { + "@types/mdast": "^4.0.0", + "@types/unist": "^3.0.0", + "longest-streak": "^3.0.0", + "mdast-util-phrasing": "^4.0.0", + "mdast-util-to-string": "^4.0.0", + "micromark-util-decode-string": "^2.0.0", + "unist-util-visit": "^5.0.0", + "zwitch": "^2.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, + "node_modules/mdast-util-to-markdown/node_modules/@types/mdast": { + "version": "4.0.3", + "resolved": "https://registry.npmjs.org/@types/mdast/-/mdast-4.0.3.tgz", + "integrity": "sha512-LsjtqsyF+d2/yFOYaN22dHZI1Cpwkrj+g06G8+qtUKlhovPW89YhqSnfKtMbkgmEtYpH2gydRNULd6y8mciAFg==", + "dev": true, + "dependencies": { + "@types/unist": "*" + } + }, + "node_modules/mdast-util-to-markdown/node_modules/@types/unist": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/@types/unist/-/unist-3.0.2.tgz", + "integrity": "sha512-dqId9J8K/vGi5Zr7oo212BGii5m3q5Hxlkwy3WpYuKPklmBEvsbMYYyLxAQpSffdLl/gdW0XUpKWFvYmyoWCoQ==", + "dev": true + }, + "node_modules/mdast-util-to-markdown/node_modules/mdast-util-to-string": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/mdast-util-to-string/-/mdast-util-to-string-4.0.0.tgz", + "integrity": "sha512-0H44vDimn51F0YwvxSJSm0eCDOJTRlmN0R1yBh4HLj9wiV1Dn0QoXGbvFAWj2hSItVTlCmBF1hqKlIyUBVFLPg==", + "dev": true, + "dependencies": { + "@types/mdast": "^4.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, "node_modules/mdast-util-to-string": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/mdast-util-to-string/-/mdast-util-to-string-2.0.0.tgz", @@ -4410,6 +4514,99 @@ "parse-entities": "^2.0.0" } }, + "node_modules/micromark-util-character": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/micromark-util-character/-/micromark-util-character-2.0.1.tgz", + "integrity": "sha512-3wgnrmEAJ4T+mGXAUfMvMAbxU9RDG43XmGce4j6CwPtVxB3vfwXSZ6KhFwDzZ3mZHhmPimMAXg71veiBGzeAZw==", + "dev": true, + "funding": [ + { + "type": "GitHub Sponsors", + "url": "https://github.com/sponsors/unifiedjs" + }, + { + "type": "OpenCollective", + "url": "https://opencollective.com/unified" + } + ], + "dependencies": { + "micromark-util-symbol": "^2.0.0", + "micromark-util-types": "^2.0.0" + } + }, + "node_modules/micromark-util-decode-numeric-character-reference": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/micromark-util-decode-numeric-character-reference/-/micromark-util-decode-numeric-character-reference-2.0.1.tgz", + "integrity": "sha512-bmkNc7z8Wn6kgjZmVHOX3SowGmVdhYS7yBpMnuMnPzDq/6xwVA604DuOXMZTO1lvq01g+Adfa0pE2UKGlxL1XQ==", + "dev": true, + "funding": [ + { + "type": "GitHub Sponsors", + "url": "https://github.com/sponsors/unifiedjs" + }, + { + "type": "OpenCollective", + "url": "https://opencollective.com/unified" + } + ], + "dependencies": { + "micromark-util-symbol": "^2.0.0" + } + }, + "node_modules/micromark-util-decode-string": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/micromark-util-decode-string/-/micromark-util-decode-string-2.0.0.tgz", + "integrity": "sha512-r4Sc6leeUTn3P6gk20aFMj2ntPwn6qpDZqWvYmAG6NgvFTIlj4WtrAudLi65qYoaGdXYViXYw2pkmn7QnIFasA==", + "dev": true, + "funding": [ + { + "type": "GitHub Sponsors", + "url": "https://github.com/sponsors/unifiedjs" + }, + { + "type": "OpenCollective", + "url": "https://opencollective.com/unified" + } + ], + "dependencies": { + "decode-named-character-reference": "^1.0.0", + "micromark-util-character": "^2.0.0", + "micromark-util-decode-numeric-character-reference": "^2.0.0", + "micromark-util-symbol": "^2.0.0" + } + }, + "node_modules/micromark-util-symbol": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/micromark-util-symbol/-/micromark-util-symbol-2.0.0.tgz", + "integrity": "sha512-8JZt9ElZ5kyTnO94muPxIGS8oyElRJaiJO8EzV6ZSyGQ1Is8xwl4Q45qU5UOg+bGH4AikWziz0iN4sFLWs8PGw==", + "dev": true, + "funding": [ + { + "type": "GitHub Sponsors", + "url": "https://github.com/sponsors/unifiedjs" + }, + { + "type": "OpenCollective", + "url": "https://opencollective.com/unified" + } + ] + }, + "node_modules/micromark-util-types": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/micromark-util-types/-/micromark-util-types-2.0.0.tgz", + "integrity": "sha512-oNh6S2WMHWRZrmutsRmDDfkzKtxF+bc2VxLC9dvtrDIRFln627VsFP6fLMgTryGDljgLPjkrzQSDcPrjPyDJ5w==", + "dev": true, + "funding": [ + { + "type": "GitHub Sponsors", + "url": "https://github.com/sponsors/unifiedjs" + }, + { + "type": "OpenCollective", + "url": "https://opencollective.com/unified" + } + ] + }, "node_modules/micromatch": { "version": "4.0.5", "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.5.tgz", @@ -6123,6 +6320,25 @@ "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==", "dev": true }, + "node_modules/unist-util-is": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/unist-util-is/-/unist-util-is-6.0.0.tgz", + "integrity": "sha512-2qCTHimwdxLfz+YzdGfkqNlH0tLi9xjTnHddPmJwtIG9MGsdbutfTc4P+haPD7l7Cjxf/WZj+we5qfVPvvxfYw==", + "dev": true, + "dependencies": { + "@types/unist": "^3.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, + "node_modules/unist-util-is/node_modules/@types/unist": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/@types/unist/-/unist-3.0.2.tgz", + "integrity": "sha512-dqId9J8K/vGi5Zr7oo212BGii5m3q5Hxlkwy3WpYuKPklmBEvsbMYYyLxAQpSffdLl/gdW0XUpKWFvYmyoWCoQ==", + "dev": true + }, "node_modules/unist-util-stringify-position": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/unist-util-stringify-position/-/unist-util-stringify-position-2.0.3.tgz", @@ -6136,6 +6352,47 @@ "url": "https://opencollective.com/unified" } }, + "node_modules/unist-util-visit": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/unist-util-visit/-/unist-util-visit-5.0.0.tgz", + "integrity": "sha512-MR04uvD+07cwl/yhVuVWAtw+3GOR/knlL55Nd/wAdblk27GCVt3lqpTivy/tkJcZoNPzTwS1Y+KMojlLDhoTzg==", + "dev": true, + "dependencies": { + "@types/unist": "^3.0.0", + "unist-util-is": "^6.0.0", + "unist-util-visit-parents": "^6.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, + "node_modules/unist-util-visit-parents": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/unist-util-visit-parents/-/unist-util-visit-parents-6.0.1.tgz", + "integrity": "sha512-L/PqWzfTP9lzzEa6CKs0k2nARxTdZduw3zyh8d2NVBnsyvHjSX4TWse388YrrQKbvI8w20fGjGlhgT96WwKykw==", + "dev": true, + "dependencies": { + "@types/unist": "^3.0.0", + "unist-util-is": "^6.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, + "node_modules/unist-util-visit-parents/node_modules/@types/unist": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/@types/unist/-/unist-3.0.2.tgz", + "integrity": "sha512-dqId9J8K/vGi5Zr7oo212BGii5m3q5Hxlkwy3WpYuKPklmBEvsbMYYyLxAQpSffdLl/gdW0XUpKWFvYmyoWCoQ==", + "dev": true + }, + "node_modules/unist-util-visit/node_modules/@types/unist": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/@types/unist/-/unist-3.0.2.tgz", + "integrity": "sha512-dqId9J8K/vGi5Zr7oo212BGii5m3q5Hxlkwy3WpYuKPklmBEvsbMYYyLxAQpSffdLl/gdW0XUpKWFvYmyoWCoQ==", + "dev": true + }, "node_modules/update-browserslist-db": { "version": "1.0.13", "resolved": "https://registry.npmjs.org/update-browserslist-db/-/update-browserslist-db-1.0.13.tgz", @@ -6549,6 +6806,16 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/zwitch": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/zwitch/-/zwitch-2.0.4.tgz", + "integrity": "sha512-bXE4cR/kVZhKZX/RjPEflHaKVhUVl85noU3v6b8apfQEc1x4A+zBxjZ4lN8LqGd6WZ3dl98pY4o717VFmoPp+A==", + "dev": true, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/wooorm" + } + }, "packages/cli": { "name": "@bring-it/cli", "version": "0.8.0", @@ -6573,6 +6840,9 @@ "name": "@bring-it/notify", "version": "0.1.1", "license": "MIT", + "devDependencies": { + "mdast-util-to-markdown": "^2.1.0" + }, "engines": { "node": "^18.0.0 || ^20.0.0", "npm": ">=9.0.0" diff --git a/packages/notify/lib/action.mjs b/packages/notify/lib/action.mjs index 896f668..c76cdb3 100644 --- a/packages/notify/lib/action.mjs +++ b/packages/notify/lib/action.mjs @@ -37,7 +37,9 @@ export async function action({ mode }) { title: all.project || 'bring-it', token: DingTalkRobotToken, }) - .then(console.log) + .then((resp) => { + logger.okay(resp.status); + }) .catch((error) => { console.error(error); process.exitCode = 1; diff --git a/packages/notify/lib/utils.mjs b/packages/notify/lib/utils.mjs index af2c5bb..f4d4819 100644 --- a/packages/notify/lib/utils.mjs +++ b/packages/notify/lib/utils.mjs @@ -1,5 +1,9 @@ +import { toMarkdown } from 'mdast-util-to-markdown'; + import { http } from '@bring-it/utils/index.mjs'; +const unknown = '未知'; + const { BRANCH_NAME, CCI_JOB_NAME, @@ -15,6 +19,8 @@ const { } = process.env; export function dingtalk({ markdown, title, token }) { + console.log(markdown); + return http({ url: 'https://oapi.dingtalk.com/robot/send', query: { access_token: token }, @@ -32,21 +38,223 @@ export function dingtalk({ markdown, title, token }) { }); } -export function createContent({ project = '未命名项目', type }) { - return ` -### ${CCI_JOB_NAME} - -[${PROJECT_NAME}](${PROJECT_WEB_URL}) 发布了新的版本 - -- 所属项目:${project} -- 发布类型:${type} -- 版本编号:${npm_package_version} - -请择时部署到 **线上** 环境 - -- 代码仓库:[${DEPOT_NAME}](${GIT_HTTP_URL}) -- 执行分支:[${BRANCH_NAME}](${PROJECT_WEB_URL}/d/${DEPOT_NAME}/git/tree/${BRANCH_NAME}) -- 版本变更:[${GIT_COMMIT_SHORT}](${PROJECT_WEB_URL}/d/${DEPOT_NAME}/git/commit/${GIT_COMMIT}) -- 构建产物:[CI - #${CI_BUILD_NUMBER}](${PROJECT_WEB_URL}/ci/job/${JOB_ID}/build/${CI_BUILD_NUMBER}/artifacts) -`; +export function createContent({ project = '未命名项目', type, manual = true }) { + return toMarkdown({ + type: 'root', + children: [ + CCI_JOB_NAME + ? { + type: 'heading', + depth: 3, + children: [ + { + type: 'text', + value: CCI_JOB_NAME, + }, + ], + } + : undefined, + { + type: 'paragraph', + children: [ + PROJECT_NAME + ? { + type: 'link', + url: PROJECT_WEB_URL, + children: [ + { + type: 'text', + value: PROJECT_NAME, + }, + ], + } + : { + type: 'text', + value: project, + }, + { + type: 'text', + value: ' 发布了新的版本', + }, + ], + }, + { + type: 'list', + spread: false, + children: [ + { + type: 'listItem', + children: [ + { + type: 'paragraph', + children: [ + { + type: 'text', + value: `所属项目:${project}`, + }, + ], + }, + ], + }, + type + ? { + type: 'listItem', + children: [ + { + type: 'text', + value: `发布类型:${type}`, + }, + ], + } + : undefined, + { + type: 'listItem', + children: [ + { + type: 'paragraph', + children: [ + { + type: 'text', + value: `版本编号:${npm_package_version || unknown}`, + }, + ], + }, + ], + }, + ].filter(Boolean), + }, + { + type: 'paragraph', + children: [ + { + type: 'text', + value: manual ? '请择时部署到 ' : '已自动部署到 ', + }, + { + type: 'strong', + children: [ + { + type: 'text', + value: BRANCH_NAME === 'master' ? '外部正式' : '内部测试', + }, + ], + }, + { + type: 'text', + value: ' 环境', + }, + ], + }, + { + type: 'list', + spread: false, + children: [ + DEPOT_NAME + ? { + type: 'listItem', + children: [ + { + type: 'paragraph', + children: [ + { + type: 'text', + value: '代码仓库:', + }, + { + type: 'link', + url: GIT_HTTP_URL, + children: [ + { + type: 'text', + value: DEPOT_NAME, + }, + ], + }, + ], + }, + ], + } + : undefined, + BRANCH_NAME + ? { + type: 'listItem', + children: [ + { + type: 'paragraph', + children: [ + { + type: 'text', + value: '执行分支:', + }, + { + type: 'link', + url: `${PROJECT_WEB_URL}/d/${DEPOT_NAME}/git/tree/${BRANCH_NAME}`, + children: [ + { + type: 'text', + value: BRANCH_NAME, + }, + ], + }, + ], + }, + ], + } + : undefined, + GIT_COMMIT + ? { + type: 'listItem', + children: [ + { + type: 'paragraph', + children: [ + { + type: 'text', + value: '版本变更:', + }, + { + type: 'link', + url: `${PROJECT_WEB_URL}/d/${DEPOT_NAME}/git/commit/${GIT_COMMIT}`, + children: [ + { + type: 'text', + value: GIT_COMMIT_SHORT, + }, + ], + }, + ], + }, + ], + } + : undefined, + JOB_ID + ? { + type: 'listItem', + children: [ + { + type: 'paragraph', + children: [ + { + type: 'text', + value: '构建产物:', + }, + { + type: 'link', + url: `${PROJECT_WEB_URL}/ci/job/${JOB_ID}/build/${CI_BUILD_NUMBER}/artifacts`, + children: [ + { + type: 'text', + value: `CI - #${CI_BUILD_NUMBER}`, + }, + ], + }, + ], + }, + ], + } + : undefined, + ].filter(Boolean), + }, + ].filter(Boolean), + }); } diff --git a/packages/notify/package.json b/packages/notify/package.json index 82cac11..d0d2285 100644 --- a/packages/notify/package.json +++ b/packages/notify/package.json @@ -1,6 +1,6 @@ { "name": "@bring-it/notify", - "version": "0.1.1", + "version": "0.1.2", "description": "Send releases notifications", "license": "MIT", "author": { @@ -35,6 +35,9 @@ "build": "best-shot prod", "prepublishOnly": "npm run build" }, + "devDependencies": { + "mdast-util-to-markdown": "^2.1.0" + }, "peerDependencies": { "@bring-it/cli": "^0.8.0" },