Repository: pulumi/actions
Pull request #194 (2021-04-21 07:13:55)

chore(deps): bump runtypes from 6.0.0 to 6.2.1
Bumps [runtypes](https://github.com/pelotom/runtypes) from 6.0.0 to 6.2.1.
<details>
<summary>Release notes</summary>
<p><em>Sourced from <a href="https://github.com/pelotom/runtypes/releases">runtypes's releases</a>.</em></p>
<blockquote>
<h2>v6.2.1</h2>
<h3>Reverts</h3>
<ul>
<li>Revert &quot;Remove workaround for resolved TypeScript issue (<a href="https://github-redirect.dependabot.com/pelotom/runtypes/issues/230">#230</a>)&quot; (<a href="https://github-redirect.dependabot.com/pelotom/runtypes/issues/233">#233</a>)</li>
</ul>
<h2>v6.2.0</h2>
<h3>Features</h3>
<ul>
<li>Introduce Record <code>.pick</code> and <code>.omit</code> (<a href="https://github-redirect.dependabot.com/pelotom/runtypes/issues/161">#161</a>)</li>
</ul>
<h3>Bug Fixes</h3>
<ul>
<li>Handle objects with null prototypes (<a href="https://github-redirect.dependabot.com/pelotom/runtypes/issues/227">#227</a>)</li>
</ul>
<h3>Refactors</h3>
<ul>
<li>Remove workaround for resolved TypeScript issue (<a href="https://github-redirect.dependabot.com/pelotom/runtypes/issues/230">#230</a>)</li>
<li>Use <code>any</code> for internal type assertion (<a href="https://github-redirect.dependabot.com/pelotom/runtypes/issues/231">#231</a>)</li>
</ul>
<h2>v6.1.0</h2>
<h3>Features</h3>
<ul>
<li>Add <code>nullable</code> shorthand (<a href="https://github-redirect.dependabot.com/pelotom/runtypes/issues/226">#226</a>)</li>
</ul>
<h3>Refactors</h3>
<ul>
<li>Unuse <code>Exclude</code> internally in static type of <code>Record</code> (<a href="https://github-redirect.dependabot.com/pelotom/runtypes/issues/223">#223</a>)</li>
<li>Widen type parameter boundary to <code>RuntypeBase</code> (<a href="https://github-redirect.dependabot.com/pelotom/runtypes/issues/225">#225</a>)</li>
</ul>
</blockquote>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a href="https://github.com/pelotom/runtypes/commit/6b62147695b0152babe7e71f221a19aa620ea77a"><code>6b62147</code></a> v6.2.1</li>
<li><a href="https://github.com/pelotom/runtypes/commit/74a1e839ec6a8eccfe295feddbf8fc6c5504d77a"><code>74a1e83</code></a> Revert &quot;Remove workaround for resolved TypeScript issue (<a href="https://github-redirect.dependabot.com/pelotom/runtypes/issues/230">#230</a>)&quot; (<a href="https://github-redirect.dependabot.com/pelotom/runtypes/issues/233">#233</a>)</li>
<li><a href="https://github.com/pelotom/runtypes/commit/45512a2ce3e82c1fc0cfd12f8160315e1bf6f418"><code>45512a2</code></a> v6.2.0</li>
<li><a href="https://github.com/pelotom/runtypes/commit/869f9be5d3b4f921e67e5ede78a4411f6928fd3d"><code>869f9be</code></a> Use <code>any</code> for internal type assertion (<a href="https://github-redirect.dependabot.com/pelotom/runtypes/issues/231">#231</a>)</li>
<li><a href="https://github.com/pelotom/runtypes/commit/cb8809d3bb5c590ab76cee983ecf7eb66ce36087"><code>cb8809d</code></a> Remove workaround for resolved TypeScript issue (<a href="https://github-redirect.dependabot.com/pelotom/runtypes/issues/230">#230</a>)</li>
<li><a href="https://github.com/pelotom/runtypes/commit/f637e943bbcfb4de34c8cf5ab6e2a0122e1e19f3"><code>f637e94</code></a> Introduce Record <code>.pick</code> and <code>.omit</code> (<a href="https://github-redirect.dependabot.com/pelotom/runtypes/issues/161">#161</a>)</li>
<li><a href="https://github.com/pelotom/runtypes/commit/420479175e986c6db76256f373aa952000dc9e31"><code>4204791</code></a> Handle objects with null prototypes (<a href="https://github-redirect.dependabot.com/pelotom/runtypes/issues/227">#227</a>)</li>
<li><a href="https://github.com/pelotom/runtypes/commit/5034df3efb4a8805cbbbd950a64cbd80174925ef"><code>5034df3</code></a> v6.1.0</li>
<li><a href="https://github.com/pelotom/runtypes/commit/acca9dc2fa7d65e3b78c24b0046a8daf47480cfb"><code>acca9dc</code></a> Update readme</li>
<li><a href="https://github.com/pelotom/runtypes/commit/d0e58fb2e3ca21a0d6f8ce906439da3b4e337956"><code>d0e58fb</code></a> Add <code>nullable</code> shorthand (<a href="https://github-redirect.dependabot.com/pelotom/runtypes/issues/226">#226</a>)</li>
<li>Additional commits viewable in <a href="https://github.com/pelotom/runtypes/compare/v6.0.0...v6.2.1">compare view</a></li>
</ul>
</details>
<br />


[![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=runtypes&package-manager=npm_and_yarn&previous-version=6.0.0&new-version=6.2.1)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually


</details>


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 986849a..2d2d0d1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,7 +2,8 @@
 
 ## HEAD (Unreleased)
 
-_(none)_
+- Edit previous PR comment instead of posting new one
+  [#128](https://github.com/pulumi/actions/pull/148)
 
 ---
 
diff --git a/README.md b/README.md
index 304f507..b1eb7bb 100644
--- a/README.md
+++ b/README.md
@@ -73,6 +73,9 @@ The action can be configured with the following arguments:
 - `expect-no-changes` - (optional) Return an error if any changes occur during
   this update
 
+- `edit-pr-comment` - (optional) Edit previous PR comment instead of posting new
+  one
+
 - `diff` - (optional) Display operation as a rich diff showing the overall
   change
 
diff --git a/package.json b/package.json
index b8d9d7d..b3110b4 100644
--- a/package.json
+++ b/package.json
@@ -45,7 +45,7 @@
     "@actions/github": "^4.0.0",
     "@pulumi/pulumi": "3.1.0",
     "envalid": "^7.1.0",
-    "runtypes": "^6.0.0",
+    "runtypes": "^6.2.1",
     "typescript": "~4.1.3"
   }
 }
diff --git a/src/config.ts b/src/config.ts
index 181fa1e..80f83e0 100644
--- a/src/config.ts
+++ b/src/config.ts
@@ -20,6 +20,7 @@ export const options = rt.Partial({
   replace: rt.Array(rt.String),
   target: rt.Array(rt.String),
   targetDependents: rt.Boolean,
+  editCommentOnPr: rt.Boolean,
 });
 
 export const config = rt
@@ -61,6 +62,7 @@ export async function makeConfig(): Promise<Config> {
       replace: parseArray(getInput('replace')),
       target: parseArray(getInput('target')),
       targetDependents: parseBoolean(getInput('target-dependents')),
+      editCommentOnPr: parseBoolean(getInput('edit-pr-comment')),
     },
   });
 }
diff --git a/src/libs/pr.ts b/src/libs/pr.ts
index 677db82..caa02ef 100644
--- a/src/libs/pr.ts
+++ b/src/libs/pr.ts
@@ -1,17 +1,35 @@
 import { context, getOctokit } from '@actions/github';
 import { invariant } from './utils';
 
-export async function addPullRequestMessage(
+
+export async function handlePullRequestMessage(
   body: string,
   githubToken: string,
+  editCommentOnPr?: boolean,
 ): Promise<void> {
   const { payload, repo } = context;
   invariant(payload.pull_request, 'Missing pull request event data.');
-
+  const text = `#### :tropical_drink: `
   const octokit = getOctokit(githubToken);
-  await octokit.issues.createComment({
+  const { data: comments} = await octokit.issues.listComments({
     ...repo,
-    issue_number: payload.pull_request.number,
-    body,
-  });
+    issue_number: payload.pull_request.number
+  })
+  const comment = comments.find(comment => comment.body.startsWith(text));
+
+  if (body && githubToken) {
+    if (comment && editCommentOnPr) {
+      await octokit.issues.updateComment({
+        ...repo,
+        comment_id: comment.id,
+        body,
+      });
+    } else {
+      await octokit.issues.createComment({
+        ...repo,
+        issue_number: payload.pull_request.number,
+        body,
+      });
+    }
+  }
 }
diff --git a/src/main.ts b/src/main.ts
index f9cb00f..f6de36d 100644
--- a/src/main.ts
+++ b/src/main.ts
@@ -3,7 +3,7 @@ import * as core from '@actions/core';
 import { LocalProgramArgs, LocalWorkspace } from '@pulumi/pulumi/automation';
 import { Commands, makeConfig } from './config';
 import { environmentVariables } from './libs/envs';
-import { addPullRequestMessage } from './libs/pr';
+import { handlePullRequestMessage } from './libs/pr';
 import * as pulumiCli from './libs/pulumi-cli';
 import { invariant } from './libs/utils';
 
@@ -84,12 +84,13 @@ const main = async () => {
   if (config.commentOnPr) {
     core.debug(`Commenting on pull request`);
     invariant(config.githubToken, 'github-token is missing.');
-    addPullRequestMessage(
+    handlePullRequestMessage(
       `#### :tropical_drink: \`${config.command}\`
 \`\`\`
 ${output}
 \`\`\``,
       config.githubToken,
+      config.options.editCommentOnPr
     );
   }
 
diff --git a/yarn.lock b/yarn.lock
index df40154..d16c53a 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -4424,10 +4424,10 @@ run-parallel@^1.1.9:
   resolved "https://registry.yarnpkg.com/run-parallel/-/run-parallel-1.1.10.tgz#60a51b2ae836636c81377df16cb107351bcd13ef"
   integrity sha512-zb/1OuZ6flOlH6tQyMPUrE3x3Ulxjlo9WIVXR4yVYi4H9UXQaeIsPbLn2R3O3vQCnDKkAl2qHiuocKKX4Tz/Sw==
 
-runtypes@^6.0.0:
-  version "6.0.0"
-  resolved "https://registry.yarnpkg.com/runtypes/-/runtypes-6.0.0.tgz#0010e7e050a41d4ab38b6138051dfb8774a53f07"
-  integrity sha512-hVBZ2S0i9MmyViSpwCU7TGFuEZxjPIeTMbhCpz59/8C3IGbPL6N03bIVALGfVWZQ+MTXORjWpWnZUP3hPyw96w==
+runtypes@^6.2.1:
+  version "6.2.1"
+  resolved "https://registry.yarnpkg.com/runtypes/-/runtypes-6.2.1.tgz#d6767b3ad35dcc5ba1ef7ff0ff76cbabab3a93b5"
+  integrity sha512-ZzOBGSPYT7qas93oNfHyoVG6eZyuLJ1husI+892RB/gsHwd+Rc4u61/5XCunNMDjKb0jl3LB/2wq12UCuaWoxQ==
 
 rxjs@^6.6.3:
   version "6.6.3"


Test patch:
diff --git a/src/__tests__/config.test.ts b/src/__tests__/config.test.ts
index 5d906d0..f15b4ac 100644
--- a/src/__tests__/config.test.ts
+++ b/src/__tests__/config.test.ts
@@ -41,6 +41,7 @@ describe('config.ts', () => {
         replace: undefined,
         target: undefined,
         targetDependents: undefined,
+        editCommentOnPr: undefined,
       },
     });
   });
@@ -89,6 +90,7 @@ describe('config.ts', () => {
         replace: undefined,
         target: undefined,
         targetDependents: undefined,
+        editCommentOnPr: undefined,
       },
     });
   });
diff --git a/src/libs/__tests__/pr.test.ts b/src/libs/__tests__/pr.test.ts
index edf38fe..97b926b 100644
--- a/src/libs/__tests__/pr.test.ts
+++ b/src/libs/__tests__/pr.test.ts
@@ -1,12 +1,16 @@
 import * as gh from '@actions/github';
-import { addPullRequestMessage } from '../pr';
+import { handlePullRequestMessage } from '../pr';
 
+const comments = [{id: 2, body: 'test',}];
+const resp = {data: comments};
 const createComment = jest.fn();
+const listComments = jest.fn(() => resp);
 jest.mock('@actions/github', () => ({
   context: {},
   getOctokit: jest.fn(() => ({
     issues: {
       createComment,
+      listComments,
     },
   })),
 }));
@@ -27,14 +31,16 @@ describe('pr.ts', () => {
 
     process.env.GITHUB_REPOSITORY = 'pulumi/actions';
 
-    await addPullRequestMessage('test', 'test');
+    await handlePullRequestMessage('test', 'test');
+    expect(listComments).toHaveBeenCalled();
     expect(createComment).toHaveBeenCalled();
   });
+
   it('should fail if no pull request data', async () => {
     process.env.GITHUB_REPOSITORY = 'pulumi/actions';
     // @ts-ignore
     gh.context = { payload: {} };
-    await expect(addPullRequestMessage('test', 'test')).rejects.toThrowError(
+    await expect(handlePullRequestMessage('test', 'test')).rejects.toThrowError(
       'Missing pull request event data',
     );
   });