UNPKG

@bring-it/notify

Version:
1 lines 8.07 kB
export const __webpack_esm_id__=297;export const __webpack_esm_ids__=[297];export const __webpack_esm_modules__={3297:(e,t,r)=>{r.d(t,{action:()=>se}),r.r(t);var n=r(1455),a=r(3140);const{CNB_BUILD_ID:l,CNB_BRANCH:i,CNB_COMMIT:o,CNB_COMMIT_SHORT:c,CNB_BEFORE_SHA:s,CNB_REPO_URL_HTTPS:p,CNB_DOCKER_REGISTRY:d,CNB_PIPELINE_NAME:h,CNB_REPO_SLUG_LOWERCASE:y,CNB_BUILD_WEB_URL:u}=process.env,{CNB:g,npm_package_version:v="\u672a\u77e5",CUSTOM_ARTIFACT_URL:m,BRANCH_NAME:_=(g?i:void 0),CCI_JOB_NAME:b=(g?h:void 0),CI_BUILD_NUMBER:f,DEPOT_NAME:$=(g?y:void 0),GIT_COMMIT_SHORT:k=(g?c:void 0),GIT_COMMIT:x=(g?o:void 0),GIT_PREVIOUS_COMMIT:I=(g?s:void 0),GIT_HTTP_URL:T=(g?p:void 0),JOB_ID:C=(g?l:void 0),PROJECT_WEB_URL:w=(g?`https://cnb.cool/${y}`:void 0),DOCKER_REG_HOST:E=(g?d:void 0)}=process.env,O=g?u:`${w}/ci/job/${C}/build/${f}/pipeline`,R=g?w:`${w}/d/${$}`,j=g?`${w}/-/tree/${_}`:`${w}/d/${$}/git/tree/${_}`,B=g?`${w}/-/commit`:`${R}/git/commit`,N=`${function(e=""){return e.slice(0,7)}(I)}...${k}`,M=I&&x!==I?N:k,S=I&&x!==I?`${g?`${w}/-/compare`:`${R}/git/compare`}/${N}`:`${B}/${x}`,A=g?`${w}/-/packages`:`${w}/ci/job/${C}/build/${f}/artifacts`,{red:D,cyan:H,green:L,yellow:U,magenta:P}=a.Ay,G=L("[okay]"),J=D("[fail]"),q=U("[warn]");var F=r(1421),W=r(8995),K=r(5076),V=r(8237),Y=r(108),z=r(6149),Q=r(5246),X=r(1937),Z=r(3306);const ee=e=>{try{const t=(0,F.execSync)(`docker images --format "{{.Digest}}" ${E}/${e}`,{encoding:"utf8"});return[...new Set(t.trim().split("\n").map(e=>e.trim()))][0]}catch(e){return console.error(e),""}};function te({markdown:e,token:t}){return console.log(e),function({url:e,query:t,json:r,method:n="GET"}){const a=new URL(e);for(const[e,r]of Object.entries(t))a.searchParams.set(e,r);return fetch(a.href,{method:n,headers:{"Content-Type":"application/json"},body:JSON.stringify(r)}).then(e=>e.json())}({url:"https://oapi.dingtalk.com/robot/send",query:{access_token:t},method:"POST",json:{msgtype:"markdown",markdown:{title:"\u7248\u672c\u53d1\u5e03\u901a\u77e5",text:e.split("\u66f4\u65b0\u5386\u53f2\uff1a").map((e,t)=>0===t?e.replaceAll("\n\n","\n\n<br/>\n\n"):e.replaceAll("\n\n##","\n\n<br/>\n\n##")).join("\u66f4\u65b0\u5386\u53f2\uff1a").trim()}}}).then(e=>{console.log(e)}).catch(e=>{console.error(e),process.exitCode=1})}const re={feat:{label:"\u529f\u80fd\u53d8\u5316",order:0},fix:{label:"\u529f\u80fd\u4fee\u590d",order:1},perf:{label:"\u6027\u80fd\u4f18\u5316",order:2},refactor:{label:"\u4ee3\u7801\u91cd\u6784",order:3},revert:{label:"\u56de\u6eda",order:4},chore:{label:"\u5176\u4ed6\u6742\u9879",order:5},style:{label:"\u4ee3\u7801\u98ce\u683c",order:6},test:{label:"\u6d4b\u8bd5",order:7},build:{label:"\u7f16\u8bd1\u914d\u7f6e",order:8},ci:{label:"\u81ea\u52a8\u5316",order:9},docs:{label:"\u8865\u5145\u6587\u6863",order:10}};async function ne(e,t){return console.log({from:e,to:t}),(0,Y.A)({repo:process.cwd(),number:100,branch:`${e}...${t}`,fields:["hash","abbrevHash","subject"]}).then(e=>e.filter(({status:e})=>e.length))}const ae=new K.Jf({headerPattern:/^(\w*)(?:\((\S*)\))?:\s?(.*)$/});async function le({project:e="\u672a\u547d\u540d\u9879\u76ee",type:t,manual:r=!0,banner:n,isLatest:a=!1,image:l,version:i=!0,rc:o=!1}){const c=await async function(){const e=!I||x&&x===I?[]:Q(Object.entries(z((await async function(){try{if(!I)throw new Error("GIT_PREVIOUS_COMMIT is null");return await ne(I,x)}catch(e){console.error(e);try{return await ne("HEAD~5","HEAD")}catch(e){console.error(e);try{return await ne("HEAD~1","HEAD")}catch(e){return console.error(e),[]}}}}()).map(({abbrevHash:e,hash:t,subject:r})=>({hash:t,abbrevHash:e,message:ae.parse(r),subject:r,url:`${B}/${t}`})),({message:e})=>e?.type||"\u672a\u5206\u7c7b")),([e])=>re[e]?.order||99).flatMap(([e,t])=>[{type:"heading",depth:4,meta:{type:e},children:[{type:"text",value:re[e]?.label||e}]},{type:"list",spread:!1,children:t.map(({subject:e,url:t})=>({type:"listItem",children:[{type:"link",url:t,children:[{type:"text",value:e}]}]}))}]);return e.length>0?[{type:"heading",depth:3,children:[{type:"text",value:"\u66f4\u65b0\u5386\u53f2\uff1a"}]},...e]:e}(),s=!0===l?$:l,p=l?ee(s):void 0,d=function({isLatest:e,version:t,rc:r}){let n=v;if(!0!==t)try{n=(0,W.createRequire)(import.meta.url)(`${t}/package.json`).version}catch{}const a=r&&f?`${n}-rc${f}`:n;return e?["latest",a].join(" / "):a}({isLatest:a,version:i,rc:o});return{levels:c.filter(e=>"heading"===e.type&&e.meta?.type).map(({meta:e})=>e.type),markdown:(0,X.l)({type:"root",children:(0,V.clean)([n?{type:"paragraph",children:[{type:"image",url:n}]}:void 0,{type:"heading",depth:2,children:[{type:"text",value:"\u4efb\u52a1 "},{type:"text",value:b},{type:"text",value:" - "},{type:"link",url:O,children:[{type:"text",value:`#${String(C).replace(/^cnb-/,"")}`}]}]},{type:"paragraph",children:[{type:"text",value:" \u53d1\u5e03\u4e86\u65b0\u7684\u7248\u672c"}]},{type:"list",spread:!1,children:[{type:"listItem",children:[{type:"paragraph",children:[{type:"text",value:`\u6240\u5c5e\u9879\u76ee\uff1a${e}`}]}]},t?{type:"listItem",children:[{type:"paragraph",children:[{type:"text",value:`\u53d1\u5e03\u7c7b\u578b\uff1a${t}`}]}]}:void 0,l?{type:"listItem",children:[{type:"text",value:`\u955c\u50cf\u4ed3\u5e93\uff1a${E}`}]}:void 0,l?{type:"listItem",children:[{type:"text",value:`\u955c\u50cf\u540d\u79f0\uff1a${s}`}]}:void 0,i?{type:"listItem",children:[{type:"paragraph",children:[{type:"text",value:`\u7248\u672c\u7f16\u53f7\uff1a${d}`}]}]}:void 0]},{type:"paragraph",children:[{type:"text",value:r?"\u8bf7\u62e9\u65f6\u90e8\u7f72\u5230 ":"\u5df2\u81ea\u52a8\u90e8\u7f72\u5230 "},{type:"strong",children:[{type:"text",value:"master"!==_||Z(v)?"\u5185\u90e8\u6d4b\u8bd5":"\u5916\u90e8\u6b63\u5f0f"}]},{type:"text",value:" \u73af\u5883"}]},...m?[{type:"heading",depth:3,children:[{type:"text",value:"\u90e8\u7f72\u8d44\u6e90\u5305\uff1a"}]},{type:"code",lang:"bash",value:[`curl ${m}`,`wget ${m}`].join("\n")}]:[],{type:"list",spread:!1,children:[$?{type:"listItem",children:[{type:"paragraph",children:[{type:"text",value:"\u4ee3\u7801\u4ed3\u5e93\uff1a"},{type:"link",url:T,children:[{type:"text",value:$}]}]}]}:void 0,_?{type:"listItem",children:[{type:"paragraph",children:[{type:"text",value:"\u6267\u884c\u5206\u652f\uff1a"},{type:"link",url:j,children:[{type:"text",value:_}]}]}]}:void 0,x?{type:"listItem",children:[{type:"paragraph",children:[{type:"text",value:"\u7248\u672c\u53d8\u66f4\uff1a"},{type:"link",url:S,children:[{type:"text",value:M}]}]}]}:void 0,!C||l||g?void 0:{type:"listItem",children:[{type:"paragraph",children:[{type:"text",value:"\u6784\u5efa\u4ea7\u7269\uff1a"},{type:"link",url:A,children:[{type:"text",value:`CI - #${C}`}]}]}]}]},...l?[{type:"blockquote",children:[{type:"paragraph",children:[{type:"text",value:p}]}]}]:[],...s?[{type:"blockquote",children:[{type:"paragraph",children:[{type:"text",value:`docker pull ${E}/${s}:${d.split("/").at(-1).trim()}`}]}]}]:[],...c])})}}const ie="notify",oe=new class{constructor(e){this.name=`[${H("bring-it")}:${P(e)}]`}okay(...e){this.log(G,...e)}fail(...e){this.log(J,...e)}warn(...e){this.log(q,...e)}task(...e){this.log("[task]",...e)}file(...e){this.log("[file]",...e)}info(...e){this.log("[info]",...e)}json(e){this.log("[json]",JSON.stringify(e,"",2))}log(...e){console.log(this.name,...e)}}(ie),{DingTalkRobotToken:ce}=process.env;async function se({name:e}){try{const{[e]:t,...r}=await function(e,t){return function(e,t){return(0,n.readFile)(e,"utf8").then(e=>JSON.parse(e)).then(e=>{if(_){const{branches:t,...r}=e;return{...r,...t?.[_]}}return e}).catch(e=>(t.warn(e.message),t.info("Fallback to default configuration"),{}))}(`.bring-it/${e}.config.json`,t)}(ie,oe),a={...r,...t};if(oe.json({name:e,...a}),!ce&&!a.subscribe?.length)return oe.fail("env.DingTalkRobotToken or config.subscribe is required"),process.exitCode=1,!1;const{markdown:l,levels:i}=await le(a);if(oe.json({levels:i}),l)if(a.subscribe?.length)for(const{DingTalkRobotToken:e,levels:t}of a.subscribe)e&&(!t||t?.length&&i.some(e=>t.includes(e)))&&te({markdown:l,token:e});else te({markdown:l,token:ce})}catch(e){oe.fail(e.message)}}}};