Skip to content

Commit

Permalink
feat: notify
Browse files Browse the repository at this point in the history
  • Loading branch information
Airkro committed Dec 14, 2023
1 parent 5b19d68 commit ef138d6
Show file tree
Hide file tree
Showing 12 changed files with 334 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Deployment tool for frontend.

## Packages

- [@bring-it/notify](./packages/notify/)
- [@bring-it/npm](./packages/npm/)
- [@bring-it/sample](./packages/sample/)
- [@bring-it/sentry](./packages/sentry/)
Expand Down
39 changes: 39 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions packages/cli/.best-shot/config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export const config = {
target: 'node16',
output: {
path: 'dist',
module: true,
},
entry: {
cli: './lib/bin.mjs',
},
externals: {
yargs: 'yargs',
},
optimization: {
// splitChunks: {
// cacheGroups: {
// vendors: {
// name: 'share',
// chunks: 'all',
// minChunks: 2,
// enforce: true,
// reuseExistingChunk: true,
// },
// },
// },
},
};
13 changes: 13 additions & 0 deletions packages/cli/lib/bin.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Cheetor } from 'cheetor';

import * as pack from '@bring-it/utils/cmd/pack.mjs';

new Cheetor('../package.json', import.meta.url)
.command(pack)
.commandSafe('@bring-it/notify/dist/sub.mjs')
.commandSafe('@bring-it/npm/dist/sub.mjs')
.commandSafe('@bring-it/sample/dist/sub.mjs')
.commandSafe('@bring-it/sentry/dist/sub.mjs')
.commandSafe('@bring-it/sftp/dist/sub.mjs')
.config((cli) => cli.wrap(null))
.setup();
54 changes: 54 additions & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"name": "@bring-it/cli",
"version": "0.8.0",
"description": "Common command line interface of 'bring-it'",
"license": "MIT",
"author": {
"name": "Eric Chen",
"email": "[email protected]"
},
"keywords": [
"bring-it",
"ci",
"cli",
"deploy",
"deployment",
"frontend",
"release"
],
"homepage": "https://github.com/airkro/bring-it/tree/master/packages/cli",
"repository": {
"type": "git",
"url": "https://github.com/airkro/bring-it.git",
"directory": "packages/cli"
},
"bugs": {
"url": "https://github.com/airkro/bring-it/issues"
},
"bin": {
"bring-it": "dist/cli.mjs"
},
"files": [
"dist"
],
"type": "module",
"scripts": {
"build": "best-shot prod",
"prepublishOnly": "npm run build"
},
"dependencies": {
"yargs": "^17.7.2"
},
"devDependencies": {
"@bring-it/utils": "*",
"cheetor": "^0.13.0"
},
"engines": {
"node": "^18.0.0 || ^20.0.0",
"npm": ">=9.0.0"
},
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org/"
}
}
10 changes: 10 additions & 0 deletions packages/notify/.best-shot/config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const config = {
target: 'node18',
output: {
path: 'dist',
module: true,
},
entry: {
sub: './lib/cmd.mjs',
},
};
48 changes: 48 additions & 0 deletions packages/notify/lib/action.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Logger, readConfig } from '@bring-it/utils/index.mjs';

import { createContent, dingtalk } from './utils.mjs';

const task = 'notify';

const logger = new Logger(task);

const { DingTalkRobotToken } = process.env;

// eslint-disable-next-line consistent-return
export async function action({ mode }) {
if (!DingTalkRobotToken) {
logger.fail('env.DingTalkRobotToken is required');

process.exitCode = -1;

return false;
}

try {
const { [mode]: config, ...rest } = await readConfig(task, logger);

const all = { ...rest, ...config };

const { project } = all;

logger.json({
mode,
project,
});

logger.task('checking...');

dingtalk({
markdown: createContent(all),
title: project,
token: DingTalkRobotToken,
})
.then(console.log)
.catch((error) => {
console.error(error);
process.exitCode = 1;
});
} catch (error) {
logger.fail(error.message);
}
}
21 changes: 21 additions & 0 deletions packages/notify/lib/cmd.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export const command = 'notify';

export const describe = 'Send releases notifications';

export function builder(cli) {
cli.option('mode', {
alias: 'm',
describe: 'notify mode',
default: 'noop',
type: 'string',
});
}

export function handler(io) {
import('./action.mjs')
.then(({ action }) => action(io))
.catch((error) => {
process.exitCode = 1;
console.error(error);
});
}
52 changes: 52 additions & 0 deletions packages/notify/lib/utils.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { http } from '@bring-it/utils/index.mjs';

const {
BRANCH_NAME,
CCI_JOB_NAME,
CI_BUILD_NUMBER,
DEPOT_NAME,
GIT_COMMIT_SHORT,
GIT_COMMIT,
GIT_HTTP_URL,
JOB_ID,
npm_package_version,
PROJECT_NAME,
PROJECT_WEB_URL,
} = process.env;

export function dingtalk({ markdown, title, token }) {
return http({
url: 'https://oapi.dingtalk.com/robot/send',
query: { access_token: token },
method: 'POST',
json: {
msgtype: 'markdown',
markdown: {
title,
text: markdown
.trim()
// eslint-disable-next-line unicorn/prefer-string-replace-all
.replace(/\n\n/g, '\n\n<br/>\n\n'),
},
},
});
}

export function createContent({ description, type }) {
return `
### ${CCI_JOB_NAME}
[${PROJECT_NAME}](${PROJECT_WEB_URL}) 发布了新的版本
- 所属项目:${description}
- 发布类型:${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)
`;
}
49 changes: 49 additions & 0 deletions packages/notify/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"name": "@bring-it/notify",
"version": "0.0.0",
"description": "Send releases notifications",
"license": "MIT",
"author": {
"name": "Eric Chen",
"email": "[email protected]"
},
"keywords": [
"bring-it",
"ci",
"cli",
"deployment",
"dingtalk",
"frontend",
"release",
"wechat",
"wecom"
],
"homepage": "https://github.com/airkro/bring-it/tree/master/packages/notify",
"repository": {
"type": "git",
"url": "https://github.com/airkro/bring-it.git",
"directory": "packages/notify"
},
"bugs": {
"url": "https://github.com/airkro/bring-it/issues"
},
"files": [
"dist"
],
"type": "module",
"scripts": {
"build": "best-shot prod",
"prepublishOnly": "npm run build"
},
"peerDependencies": {
"@bring-it/cli": "^0.8.0"
},
"engines": {
"node": "^18.0.0 || ^20.0.0",
"npm": ">=9.0.0"
},
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org/"
}
}
2 changes: 1 addition & 1 deletion packages/sentry/lib/utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { rm } from 'node:fs/promises';
import spawn from '@npmcli/promise-spawn';
import { globby } from 'globby';

import { ignore, Logger, readJSON } from '../../utils/index.mjs';
import { ignore, Logger, readJSON } from '@bring-it/utils/index.mjs';

const logger = new Logger('sentry');

Expand Down
20 changes: 20 additions & 0 deletions packages/utils/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,23 @@ export function readJSON(configName, logger) {
return {};
});
}

export function readConfig(name, logger) {
return readJSON(`.bring-it/${name}.config.json`, logger);
}

export function http({ url, query, json, method = 'GET' }) {
const io = new URL(url);

for (const [key, value] of Object.entries(query)) {
url.searchParams.set(key, value);
}

return fetch(io.href, {
method,
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(json),
});
}

0 comments on commit ef138d6

Please sign in to comment.