You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@echarts.apache.org by ov...@apache.org on 2022/06/14 03:08:08 UTC

[echarts-bot] 01/02: feat: improve pr document workflow

This is an automated email from the ASF dual-hosted git repository.

ovilia pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/echarts-bot.git

commit 3d9a00b5c9c057078c9e07a4e8ac5e03a7f9d5c9
Author: Ovilia <zw...@gmail.com>
AuthorDate: Mon Jun 13 13:01:14 2022 +0800

    feat: improve pr document workflow
---
 index.js     | 102 +++++++++++++++++++++++++++++++++++++++++++----------------
 src/label.js |   4 +++
 src/text.js  |  21 ++++++++++--
 src/util.js  |  19 ++++++++++-
 4 files changed, 115 insertions(+), 31 deletions(-)

diff --git a/index.js b/index.js
index 8844a34..8beb751 100644
--- a/index.js
+++ b/index.js
@@ -3,7 +3,7 @@ const text = require('./src/text');
 const labelText = require('./src/label');
 const logger = require('./src/logger');
 const { isCommitter } = require('./src/coreCommitters');
-const { replaceAll, removeHTMLComment } = require('./src/util');
+const { replaceAll, removeHTMLComment, isMissingDocInfo } = require('./src/util');
 
 module.exports = (/** @type import('probot').Probot */ app) => {
     app.on(['issues.opened'], async context => {
@@ -177,6 +177,7 @@ module.exports = (/** @type import('probot').Probot */ app) => {
             : text.PR_OPENED;
 
         const labelList = [];
+        const removeLabelList = [];
         const isDraft = context.payload.pull_request.draft;
         if (!isDraft) {
             labelList.push(labelText.PR_AWAITING_REVIEW);
@@ -185,13 +186,12 @@ module.exports = (/** @type import('probot').Probot */ app) => {
             labelList.push(labelText.PR_AUTHOR_IS_COMMITTER);
         }
 
-        const content = context.payload.pull_request.body;
-        if (content && content.indexOf('[x] The API has been changed') > -1) {
-            labelList.push(labelText.PR_AWAITING_DOC);
-            commentText += '\n\n' + text.PR_AWAITING_DOC;
-        }
-        if (content && content.indexOf('[x] This PR depends on ZRender changes') > -1) {
-            commentText += '\n\n' + text.PR_ZRENDER_CHANGED;
+        const content = context.payload.pull_request.body || '';
+
+        commentText = checkDoc(content, labelList, removeLabelList);
+
+        if (content.indexOf('[x] This PR depends on ZRender changes') > -1) {
+            commentText += text.PR_ZRENDER_CHANGED;
         }
 
         if (await isFirstTimeContributor(context)) {
@@ -200,7 +200,8 @@ module.exports = (/** @type import('probot').Probot */ app) => {
 
         return Promise.all([
             commentIssue(context, commentText),
-            addLabels(context, labelList)
+            addLabels(context, labelList),
+            removeLabels(context, removeLabelList)
         ]);
     });
 
@@ -224,15 +225,12 @@ module.exports = (/** @type import('probot').Probot */ app) => {
             addLabel.push(labelText.PR_AWAITING_REVIEW);
         }
 
-        const content = pr.body;
-        if (content && content.indexOf('[x] The API has been changed') > -1) {
-            addLabel.push(labelText.PR_AWAITING_DOC);
-        }
-        else {
-            removeLabel.push(labelText.PR_AWAITING_DOC);
-        }
+        const content = pr.body || '';
+
+        const commentText = checkDoc(content, '', addLabel, removeLabel);
 
         return Promise.all([
+            commentIssue(context, commentText),
             removeLabels(removeLabel),
             addLabels(addLabel)
         ]);
@@ -261,13 +259,25 @@ module.exports = (/** @type import('probot').Probot */ app) => {
 
     app.on(['pull_request_review.submitted'], async context => {
         const review = context.payload.review;
-        if (review.state === 'changes_requested'
-            && isCommitter(review.author_association, review.user.login)
-        ) {
-            return Promise.all([
-                addLabels(context, [labelText.PR_REVISION_NEEDED]),
-                removeLabels(context, [labelText.PR_AWAITING_REVIEW])
-            ]);
+        const addLabel = [];
+        const removeLabel = [];
+        if (isCommitter(review.author_association, review.user.login)) {
+            if (review.state === 'changes_requested') {
+                return Promise.all([
+                    addLabels(context, [labelText.PR_REVISION_NEEDED]),
+                    removeLabels(context, [labelText.PR_AWAITING_REVIEW])
+                ]);
+            }
+            else if (review.state === 'approved') {
+                const pr = context.payload.pull_request;
+                const content = pr.body || '';
+                const commentText = checkDoc(content, '', addLabel, removeLabel);
+                return Promise.all([
+                    commentIssue(context, commentText),
+                    addLabels(context, [labelText.PR_REVISION_NEEDED]),
+                    removeLabels(context, [labelText.PR_AWAITING_REVIEW])
+                ]);
+            }
         }
     });
 
@@ -340,11 +350,17 @@ function openIssue(context) {
  */
 function commentIssue(context, commentText) {
     // create comment
-    return context.octokit.issues.createComment(
-        context.issue({
-            body: commentText
-        })
-    );
+    return new Promise(resolve => {
+        if (!commentText) {
+            resolve();
+            return;
+        }
+        return context.octokit.issues.createComment(
+            context.issue({
+                body: commentText
+            })
+        );
+    });
 }
 
 /**
@@ -404,3 +420,33 @@ async function translateIssue(context, createdIssue) {
 function fixMarkdown(body) {
   return body.replace(/\! \[/g, '![').replace(/\] \(/g, '](')
 }
+
+function checkDoc(content, commentText, addLabelList, removeLabelList) {
+    if (isMissingDocInfo(content)) {
+        if (content.indexOf(text.PR_DOC_LATER) < 0) {
+            commentText += '\n\n' + text.PR_DOC_LAGACY;
+        }
+        else {
+            commentText += text.PR_MISSING_DOC_INFO;
+        }
+    }
+    else {
+        if (content.indexOf('[x] ' + text.PR_DOC_RREADY)) {
+            addLabelList.push(labelText.PR_DOC_READY);
+            removeLabelList.push(labelText.PR_DOC_UNCHANGED);
+            removeLabelList.push(labelText.PR_DOC_LATER);
+        }
+        else if (content.indexOf('[x] ' + text.PR_DOC_UNCHANGED)) {
+            addLabelList.push(labelText.PR_DOC_UNCHANGED);
+            removeLabelList.push(labelText.PR_DOC_READY);
+            removeLabelList.push(labelText.PR_DOC_LATER);
+        }
+        else if (content.indexOf('[x] ' + text.PR_DOC_LATER)) {
+            addLabelList.push(labelText.PR_AWAITING_DOC);
+            removeLabelList.push(labelText.PR_DOC_UNCHANGED);
+            removeLabelList.push(labelText.PR_DOC_READY);
+            commentText += text.PR_AWAITING_DOC;
+        }
+    }
+    return commentText;
+}
diff --git a/src/label.js b/src/label.js
index 7badd43..a6d1d38 100644
--- a/src/label.js
+++ b/src/label.js
@@ -22,6 +22,8 @@ const PRIORITY_HIGH = 'priority: high';
 
 const PR_AWAITING_REVIEW = 'PR: awaiting review';
 const PR_REVISION_NEEDED = 'PR: revision needed';
+const PR_DOC_READY = 'PR: doc ready';
+const PR_DOC_UNCHANGED = 'PR: doc unchanged';
 const PR_AWAITING_DOC = 'PR: awaiting doc';
 const PR_AUTHOR_IS_COMMITTER = 'PR: author is committer';
 const PR_FIRST_TIME_CONTRIBUTOR = 'PR: first-time contributor';
@@ -47,6 +49,8 @@ module.exports = {
     PRIORITY_HIGH,
     PR_AWAITING_REVIEW,
     PR_REVISION_NEEDED,
+    PR_DOC_READY,
+    PR_DOC_UNCHANGED,
     PR_AWAITING_DOC,
     PR_AUTHOR_IS_COMMITTER,
     PR_FIRST_TIME_CONTRIBUTOR
diff --git a/src/text.js b/src/text.js
index eda492e..cab9ba9 100644
--- a/src/text.js
+++ b/src/text.js
@@ -53,9 +53,21 @@ const PR_OPENED_BY_COMMITTER = PR_OPENED + `
 
 The pull request is marked to be \`PR: author is committer\` because you are a committer of this project.`;
 
-const PR_AWAITING_DOC = `Document changes are required in this PR. Please also make a PR to [apache/echarts-doc](https://github.com/apache/echarts-doc) for document changes and update the issue id in the PR description. When the doc PR is merged, the maintainers will remove the \`PR: awaiting doc\` label.`;
+const PR_DOC_UNCHANGED = `This PR doesn't relate to document changes`;
+const PR_DOC_LATER = `The document should be updated later`;
+const PR_DOC_RREADY = `The document changes have been made`;
 
-const PR_ZRENDER_CHANGED = 'This PR depends on [ZRender](https://github.com/ecomfe/zrender) changes. Please update the ZRender dependency to the latest nightly version including this change, which takes place everyday at 8:00 UTC (16:00 Beijing Time).\nYou can use `npm i zrender@npm:zrender-nightly@dev` to update package.json.\nIf you have any question about this, please leave a comment and we will give you extra help on this.';
+const PR_DOC_LAGACY = `Please make sure this PR has one of the following labels: \`PR: doc ready\`, \`PR: doc awaiting\`, \`PR: doc unchanged\``;
+
+const PR_MISSING_DOC_INFO = `
+
+⚠️ MISSING DOCUMENT INFO: Please make sure one of the document options are checked in this PR's description. Search "Document Info" in the description of this PR. This should be done either by the author or the reviewers of the PR.`;
+
+const PR_AWAITING_DOC = `
+
+Document changes are required in this PR. Please also make a PR to [apache/echarts-doc](https://github.com/apache/echarts-doc) for document changes and update the issue id in the PR description. When the doc PR is merged, the maintainers will remove the \`PR: awaiting doc\` label.`;
+
+const PR_ZRENDER_CHANGED = '\n\nThis PR depends on [ZRender](https://github.com/ecomfe/zrender) changes. Please update the ZRender dependency to the latest nightly version including this change, which takes place everyday at 8:00 UTC (16:00 Beijing Time).\nYou can use `npm i zrender@npm:zrender-nightly@dev` to update package.json.\nIf you have any question about this, please leave a comment and we will give you extra help on this.';
 
 const PR_MERGED =
     `Congratulations! Your PR has been merged. Thanks for your contribution! 👍`;
@@ -102,6 +114,11 @@ module.exports = {
     PR_MERGED,
     PR_OPENED_BY_COMMITTER,
     PR_NOT_MERGED,
+    PR_DOC_UNCHANGED,
+    PR_DOC_LATER,
+    PR_DOC_RREADY,
+    PR_DOC_LAGACY,
+    PR_MISSING_DOC_INFO,
     PR_AWAITING_DOC,
     PR_ZRENDER_CHANGED,
     ISSUE_TAGGED_WAITING_AUTHOR,
diff --git a/src/util.js b/src/util.js
index 51706f8..334772a 100644
--- a/src/util.js
+++ b/src/util.js
@@ -1,3 +1,5 @@
+const text = require('./src/text');
+
 function removeCodeAndComment(body) {
 	return body
         .replace(/<!--[\w\W\s]*?-->/gmi, '')
@@ -14,8 +16,23 @@ function replaceAll(str, search, replacement) {
 	return str.replace(new RegExp(search, 'g'), replacement);
 }
 
+function isMissingDocInfo(body) {
+    const docOptions = [
+        `[x] ${text.PR_DOC_UNCHANGED}`,
+        `[x] ${text.PR_DOC_LATER}`,
+        `[x] ${text.PR_DOC_RREADY}`
+    ];
+    for (let i = 0; i < docOptions.length; ++i) {
+        if (body.indexOf(docOptions[i]) > -1) {
+            return false;
+        }
+    }
+    return true;
+}
+
 module.exports = {
 	removeCodeAndComment,
     removeHTMLComment,
-	replaceAll
+    replaceAll,
+    isMissingDocInfo
 };


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@echarts.apache.org
For additional commands, e-mail: commits-help@echarts.apache.org