Repository: pulumi/actions (ts)

Issue:
Commenting the diff on Pull requests fails silently since v3.2.0
Since the release of v3.2.0 the pulumi action silently fails when putting the comment showing the diff.

## Steps to reproduce

1. using the following workflow yaml

```
    - uses: actions/setup-node@v2
      with:
        node-version: 16.x
    - name: Run Pulumi
      uses: pulumi/actions@v3
      with:
        command: preview
        comment-on-pr: true
        github-token: ${{ secrets.GITHUB_TOKEN }}
        stack-name: test
        diff: true
```

Expected: A comment showing the diff on the pull request
Actual: 
```
(node:1908) UnhandledPromiseRejectionWarning: HttpError: Bad credentials
    at /home/runner/work/_actions/pulumi/actions/v3/webpack:/pulumi-github-action/node_modules/@octokit/request/dist-node/index.js:66:1
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
(node:1908) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:1908) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
```

Workaround: Downgrading to v3.1.0
```    
   - name: Run Pulumi
      uses: pulumi/actions@v3.1.0
      with:
        command: preview
        comment-on-pr: true
        github-token: ${{ secrets.GITHUB_TOKEN }}
        stack-name: test
        diff: true
```

Pull request:
fix: pull request message authentication
fixes #291

Patch:
diff --git a/package.json b/package.json
index 517f9d7..5cec3a0 100644
--- a/package.json
+++ b/package.json
@@ -21,6 +21,7 @@
     "*.md": "prettier --write"
   },
   "devDependencies": {
+    "@types/dedent": "^0.7.0",
     "@types/faker": "^5.5.6",
     "@types/jest": "~26.0.23",
     "@types/node": "~14.17.4",
@@ -47,6 +48,7 @@
     "@actions/io": "^1.1.1",
     "@actions/tool-cache": "^1.7.1",
     "@pulumi/pulumi": "3.6.0",
+    "dedent": "^0.7.0",
     "envalid": "^7.1.0",
     "got": "^11.8.2",
     "runtypes": "^6.3.1",
diff --git a/src/libs/pr.ts b/src/libs/pr.ts
index c2d50ce..d8b3e71 100644
--- a/src/libs/pr.ts
+++ b/src/libs/pr.ts
@@ -1,37 +1,60 @@
+import * as core from '@actions/core';
 import { context, getOctokit } from '@actions/github';
+import * as dedent from 'dedent';
+import { Config } from '../config';
 import { invariant } from './utils';
 
-
 export async function handlePullRequestMessage(
-  body: string,
-  githubToken: string,
-  command: string,
-  stackName: string,
-  editCommentOnPr?: boolean,
+  config: Config,
+  output: string,
 ): Promise<void> {
+  const {
+    githubToken,
+    command,
+    stackName,
+    options: { editCommentOnPr },
+  } = config;
+
+  const heading = `#### :tropical_drink: \`${command}\` on ${stackName}`;
+  const body = dedent`
+    ${heading}
+    \`\`\`
+    ${output}
+    \`\`\`
+  `;
+
   const { payload, repo } = context;
   invariant(payload.pull_request, 'Missing pull request event data.');
-  const text = `#### :tropical_drink: \`${command}\` on ${stackName}`
+
   const octokit = getOctokit(githubToken);
-  const { data: comments} = await octokit.rest.issues.listComments({
-    ...repo,
-    issue_number: payload.pull_request.number
-  })
-  const comment = comments.find(comment => comment.body.startsWith(text));
 
-  if (body && githubToken) {
-    if (comment && editCommentOnPr) {
-      await octokit.rest.issues.updateComment({
-        ...repo,
-        comment_id: comment.id,
-        body,
-      });
-    } else {
-      await octokit.rest.issues.createComment({
+  try {
+    if (editCommentOnPr) {
+      const { data: comments } = await octokit.rest.issues.listComments({
         ...repo,
         issue_number: payload.pull_request.number,
-        body,
       });
+      const comment = comments.find((comment) =>
+        comment.body.startsWith(heading),
+      );
+
+      // If comment exists, update it.
+      if (comment) {
+        await octokit.rest.issues.updateComment({
+          ...repo,
+          comment_id: comment.id,
+          body,
+        });
+        return;
+      }
     }
+  } catch {
+    core.warning('Not able to edit comment, defaulting to creating a new comment.');
   }
+
+  await octokit.rest.issues.createComment({
+    ...repo,
+    issue_number: payload.pull_request.number,
+    body,
+  });
 }
diff --git a/src/main.ts b/src/main.ts
index 68665a6..cdaead0 100644
--- a/src/main.ts
+++ b/src/main.ts
@@ -85,16 +85,7 @@ const main = async () => {
   if (config.commentOnPr) {
     core.debug(`Commenting on pull request`);
     invariant(config.githubToken, 'github-token is missing.');
-    handlePullRequestMessage(
-      `#### :tropical_drink: \`${config.command}\` on ${config.stackName}
-\`\`\`
-${output}
-\`\`\``,
-      config.command,
-      config.stackName,
-      config.githubToken,
-      config.options.editCommentOnPr
-    );
+    handlePullRequestMessage(config, output);
   }
 
   core.endGroup();
diff --git a/yarn.lock b/yarn.lock
index 097783a..34f3936 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -839,6 +839,11 @@
   resolved "https://registry.yarnpkg.com/@types/color-name/-/color-name-1.1.1.tgz#1c1261bbeaa10a8055bbc5d8ab84b7b2afc846a0"
   integrity sha512-rr+OQyAjxze7GgWrSaJwydHStIhHq2lvY3BOC2Mj7KnzI7XK0Uw1TOOdI9lDoajEbSWLiYgoo4f1R51erQfhPQ==
 
+"@types/dedent@^0.7.0":
+  version "0.7.0"
+  resolved "https://registry.yarnpkg.com/@types/dedent/-/dedent-0.7.0.tgz#155f339ca404e6dd90b9ce46a3f78fd69ca9b050"
+  integrity sha512-EGlKlgMhnLt/cM4DbUSafFdrkeJoC9Mvnj0PUCU7tFmTjMjNRT957kXCx0wYm3JuEq4o4ZsS5vG+NlkM2DMd2A==
+
 "@types/faker@^5.5.6":
   version "5.5.6"
   resolved "https://registry.yarnpkg.com/@types/faker/-/faker-5.5.6.tgz#039b700a9d8ad9150ecc842bf5e717e2027b6f75"


Test patch:
diff --git a/src/libs/__tests__/pr.test.ts b/src/libs/__tests__/pr.test.ts
index ed6ee85..6b5464c 100644
--- a/src/libs/__tests__/pr.test.ts
+++ b/src/libs/__tests__/pr.test.ts
@@ -1,4 +1,5 @@
 import * as gh from '@actions/github';
+import { Config } from '../../config';
 import { handlePullRequestMessage } from '../pr';
 
 const comments = [{ id: 2, body: 'test' }];
@@ -33,8 +34,7 @@ describe('pr.ts', () => {
 
     process.env.GITHUB_REPOSITORY = 'pulumi/actions';
 
-    await handlePullRequestMessage('test', 'test', 'test', 'test');
-    expect(listComments).toHaveBeenCalled();
+    await handlePullRequestMessage({ options: {} } as Config, 'test');
     expect(createComment).toHaveBeenCalled();
   });
 
@@ -42,8 +42,8 @@ describe('pr.ts', () => {
     process.env.GITHUB_REPOSITORY = 'pulumi/actions';
     // @ts-ignore
     gh.context = { payload: {} };
-    await expect(handlePullRequestMessage('test', 'test', 'test', 'test')).rejects.toThrowError(
-      'Missing pull request event data',
-    );
+    await expect(
+      handlePullRequestMessage({ options: {} } as Config, 'test'),
+    ).rejects.toThrowError('Missing pull request event data');
   });
 });