You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by po...@apache.org on 2020/12/27 13:57:20 UTC

[airflow-label-when-approved] 04/09: Add option to add comment to PR (#1)

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

potiuk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/airflow-label-when-approved.git

commit d664559a0ad1ce208dd20422262350be0e1896b8
Author: Tobiasz Kędzierski <to...@gmail.com>
AuthorDate: Tue Oct 27 10:26:17 2020 +0100

    Add option to add comment to PR (#1)
    
    * Add optional comment to the PR
    
    * fixup! Add optional comment to the PR
---
 README.md     | 17 +++++++++--------
 action.yml    | 17 +++++++++++++----
 dist/index.js | 29 ++++++++++++++++++++++-------
 src/main.ts   | 34 +++++++++++++++++++++++++++-------
 4 files changed, 71 insertions(+), 26 deletions(-)

diff --git a/README.md b/README.md
index a23ec7a..2bfadcb 100644
--- a/README.md
+++ b/README.md
@@ -35,19 +35,20 @@ projects.
 
 ## Inputs
 
-| Input                         | Required | Example                       | Comment                                                                 |
-|-------------------------------|----------|-------------------------------|-------------------------------------------------------------------------|
-| `token`                       | yes      | `${{ secrets.GITHUB_TOKEN }}` | The github token passed from `${{ secrets.GITHUB_TOKEN }}`              |
-| `label`                       | no       | `Approved by committers`      | Label to be added/removed to the Pull Request if approved/not approved  |
-| `require_committers_approval` | no       | `true`                        | Is approval from user with write permission required                    |
+| Input                         | Required | Example                                                         | Comment                                                                 |
+|-------------------------------|----------|-----------------------------------------------------------------|-------------------------------------------------------------------------|
+| `token`                       | yes      | `${{ secrets.GITHUB_TOKEN }}`                                   | The github token passed from `${{ secrets.GITHUB_TOKEN }}`              |
+| `label`                       | no       | `spproved by committers`                                        | Label to be added/removed to the Pull Request if approved/not approved  |
+| `require_committers_approval` | no       | `true`                                                          | Is approval from user with write permission required                    |
+| `comment`                     | no       | `This became approved, rerun tests manually or rebase and push` | Add optional comment to the PR when approved                            |
 
 ## Outputs
 
 | Output         |                              |
 |----------------|------------------------------|
-| `isApproved`   | is Pull Reqeuest is approved |
-| `labelSet`     | is label was set             |
-| `labelRemoved` | is label was removed         |
+| `isApproved`   | is Pull Reqeuest approved    |
+| `labelSet`     | was label set                |
+| `labelRemoved` | was label removed            |
 
 # Examples
 
diff --git a/action.yml b/action.yml
index bd6f4e3..ae16b48 100644
--- a/action.yml
+++ b/action.yml
@@ -3,15 +3,24 @@ description: 'Set label when PR is approved.'
 author: 'TobKed'
 inputs:
   token:
-    description: The GITHUB_TOKEN secret of the repository
+    description: 'The GITHUB_TOKEN secret of the repository'
     required: true
   label:
-    description: A label to be checked/added/removed
+    description: 'A label to be checked/added/removed'
     required: false
   require_committers_approval:
-    description: Is approval from person with write access to repo required
+    description: 'Is approval from person with write access to repo required'
     required: false
-    default: 'false'
+  comment:
+    description: 'Comment to be added to Pull Request if approved'
+    required: false
+outputs:
+  isApproved:
+    description: 'Is Pull Reqeuest approved'
+  labelSet:
+    description: 'Was label set'
+  labelRemoved:
+    description: 'Was label removed'
 runs:
   using: 'node12'
   main: 'dist/index.js'
diff --git a/dist/index.js b/dist/index.js
index 39e2efb..a6f8087 100644
--- a/dist/index.js
+++ b/dist/index.js
@@ -1585,6 +1585,18 @@ function removeLabel(octokit, owner, repo, pullRequestNumber, label) {
         });
     });
 }
+function addComment(octokit, owner, repo, pullRequestNumber, comment) {
+    return __awaiter(this, void 0, void 0, function* () {
+        core.info(`Adding comment: ${comment}`);
+        yield octokit.issues.createComment({
+            owner,
+            repo,
+            // eslint-disable-next-line @typescript-eslint/camelcase
+            issue_number: pullRequestNumber,
+            body: comment
+        });
+    });
+}
 function getWorkflowId(octokit, runId, owner, repo) {
     return __awaiter(this, void 0, void 0, function* () {
         const reply = yield octokit.actions.getWorkflowRun({
@@ -1650,10 +1662,9 @@ function run() {
     var _a, _b;
     return __awaiter(this, void 0, void 0, function* () {
         const token = core.getInput('token', { required: true });
-        const userLabel = core.getInput('label', { required: false }) || 'not set';
-        const requireCommittersApproval = core.getInput('require_committers_approval', {
-            required: false
-        }) === 'true';
+        const userLabel = core.getInput('label') || 'not set';
+        const requireCommittersApproval = core.getInput('require_committers_approval') === 'true';
+        const comment = core.getInput('comment') || '';
         const octokit = new github.GitHub(token);
         const context = github.context;
         const repository = getRequiredEnv('GITHUB_REPOSITORY');
@@ -1665,7 +1676,8 @@ function run() {
         const sha = (_b = context.payload.pull_request) === null || _b === void 0 ? void 0 : _b.head.sha;
         core.info(`\n############### Set Label When Approved start ##################\n` +
             `label: "${userLabel}"\n` +
-            `requireCommittersApproval: ${requireCommittersApproval}`);
+            `requireCommittersApproval: ${requireCommittersApproval}\n` +
+            `comment: ${comment}`);
         if (eventName !== 'pull_request_review') {
             throw Error(`This action is only useful in "pull_request_review" triggered runs and you used it in "${eventName}"`);
         }
@@ -1683,10 +1695,13 @@ function run() {
             isLabelShouldBeSet = isApproved && !labelNames.includes(userLabel);
             isLabelShouldBeRemoved = !isApproved && labelNames.includes(userLabel);
             if (isLabelShouldBeSet) {
-                setLabel(octokit, owner, repo, pullRequest.number, userLabel);
+                yield setLabel(octokit, owner, repo, pullRequest.number, userLabel);
+                if (comment !== '') {
+                    yield addComment(octokit, owner, repo, pullRequest.number, comment);
+                }
             }
             else if (isLabelShouldBeRemoved) {
-                removeLabel(octokit, owner, repo, pullRequest.number, userLabel);
+                yield removeLabel(octokit, owner, repo, pullRequest.number, userLabel);
             }
         }
         //// Future option to rerun workflows if PR approved
diff --git a/src/main.ts b/src/main.ts
index dac2b83..38679b8 100644
--- a/src/main.ts
+++ b/src/main.ts
@@ -162,6 +162,23 @@ async function removeLabel(
   })
 }
 
+async function addComment(
+  octokit: github.GitHub,
+  owner: string,
+  repo: string,
+  pullRequestNumber: number,
+  comment: string
+): Promise<void> {
+  core.info(`Adding comment: ${comment}`)
+  await octokit.issues.createComment({
+    owner,
+    repo,
+    // eslint-disable-next-line @typescript-eslint/camelcase
+    issue_number: pullRequestNumber,
+    body: comment
+  })
+}
+
 async function getWorkflowId(
   octokit: github.GitHub,
   runId: number,
@@ -248,11 +265,10 @@ async function printDebug(
 
 async function run(): Promise<void> {
   const token = core.getInput('token', {required: true})
-  const userLabel = core.getInput('label', {required: false}) || 'not set'
+  const userLabel = core.getInput('label') || 'not set'
   const requireCommittersApproval =
-    core.getInput('require_committers_approval', {
-      required: false
-    }) === 'true'
+    core.getInput('require_committers_approval') === 'true'
+  const comment = core.getInput('comment') || ''
   const octokit = new github.GitHub(token)
   const context = github.context
   const repository = getRequiredEnv('GITHUB_REPOSITORY')
@@ -266,7 +282,8 @@ async function run(): Promise<void> {
   core.info(
     `\n############### Set Label When Approved start ##################\n` +
       `label: "${userLabel}"\n` +
-      `requireCommittersApproval: ${requireCommittersApproval}`
+      `requireCommittersApproval: ${requireCommittersApproval}\n` +
+      `comment: ${comment}`
   )
 
   if (eventName !== 'pull_request_review') {
@@ -305,9 +322,12 @@ async function run(): Promise<void> {
     isLabelShouldBeRemoved = !isApproved && labelNames.includes(userLabel)
 
     if (isLabelShouldBeSet) {
-      setLabel(octokit, owner, repo, pullRequest.number, userLabel)
+      await setLabel(octokit, owner, repo, pullRequest.number, userLabel)
+      if (comment !== '') {
+        await addComment(octokit, owner, repo, pullRequest.number, comment)
+      }
     } else if (isLabelShouldBeRemoved) {
-      removeLabel(octokit, owner, repo, pullRequest.number, userLabel)
+      await removeLabel(octokit, owner, repo, pullRequest.number, userLabel)
     }
   }