diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 649850464eaa..bdc9663c304d 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -26,6 +26,9 @@ stages: versionSpec: '12.13.1' - script: npm install displayName: 'Install modules' + - script: | + node ./scripts/azure-github-comment.js '![Prepare preview](https://user-images.githubusercontent.com/5378891/72351368-2c979e00-371b-11ea-9652-eb4e825d745e.gif)' + displayName: 'Comment on github' - script: npm run site displayName: 'Build sites' - script: ls -al _site/ @@ -34,5 +37,7 @@ stages: export DEPLOY_DOMAIN=https://preview-${SYSTEM_PULLREQUEST_PULLREQUESTNUMBER}-ant-design.surge.sh echo "Deploy to $DEPLOY_DOMAIN" npx surge --project ./_site --domain $DEPLOY_DOMAIN - curl -X POST -u ${ACCESS_TOKEN} -H "Accept: application/json" -H "Content-Type:application/json" https://api.github.com/repos/ant-design/ant-design/issues/${SYSTEM_PULLREQUEST_PULLREQUESTNUMBER}/comments -d '{ "body": "Preview deploy to '${DEPLOY_DOMAIN}'" }' - displayName: 'Deploy Site' \ No newline at end of file + displayName: 'Deploy Site' + - script: | + node ./scripts/azure-github-comment.js "Preview deployed to https://preview-${SYSTEM_PULLREQUEST_PULLREQUESTNUMBER}-ant-design.surge.sh" + displayName: 'Update comment on github' \ No newline at end of file diff --git a/scripts/azure-github-comment.js b/scripts/azure-github-comment.js new file mode 100644 index 000000000000..9fa33d00302d --- /dev/null +++ b/scripts/azure-github-comment.js @@ -0,0 +1,55 @@ +const fetch = require('node-fetch'); + +const REPO = process.env.ACCESS_REPO; +const TOKEN = process.env.ACCESS_TOKEN; +const PR = process.env.SYSTEM_PULLREQUEST_PULLREQUESTNUMBER; +const REPLACE_MARK = ''; + +const argv = process.argv; + +const comment = argv[argv.length - 1]; + +const wrappedComment = ` + ${REPLACE_MARK} + ${comment} +`.trim(); + +async function withGithub(url, json, method) { + const res = await fetch(url, { + method: method || (json ? 'POST' : 'GET'), + headers: { + Accept: 'application/json', + 'Content-Type': 'application/json', + Authorization: `Basic ${Buffer.from(TOKEN).toString('base64')}`, + }, + body: json ? JSON.stringify(json) : undefined, + }); + + return res.json(); +} + +(async function run() { + const comments = await withGithub(`https://api.github.com/repos/${REPO}/issues/${PR}/comments`); + + // Find my comment + const updateComment = comments.find(({ body }) => body.includes(REPLACE_MARK)); + console.log('Origin comment:', updateComment); + + // Update + let res; + if (!updateComment) { + res = await withGithub(`https://api.github.com/repos/${REPO}/issues/${PR}/comments`, { + body: wrappedComment, + }); + } else { + res = await withGithub( + `https://api.github.com/repos/${REPO}/issues/comments/${updateComment.id}`, + { + body: wrappedComment, + }, + 'PATCH', + ); + } + + console.log(res); +})();