Repository: 8398a7/action-slack
Pull request #42 (2020-02-22 14:59:09)

bump v2.7.0


Patch:
diff --git a/.eslintrc.json b/.eslintrc.json
index a40bd16..2675f69 100644
--- a/.eslintrc.json
+++ b/.eslintrc.json
@@ -14,7 +14,7 @@
       "@typescript-eslint/no-unused-vars": "error",
       "@typescript-eslint/explicit-member-accessibility": ["error", {"accessibility": "no-public"}],
       "@typescript-eslint/no-require-imports": "error",
-      "@typescript-eslint/array-type": "error",
+      "@typescript-eslint/array-type": "off",
       "@typescript-eslint/await-thenable": "error",
       "@typescript-eslint/ban-ts-ignore": "error",
       "camelcase": "off",
diff --git a/README.md b/README.md
index 9350ed2..9baf656 100644
--- a/README.md
+++ b/README.md
@@ -43,7 +43,7 @@ See here for `payload` reference or [Custom Notification](https://github.com/839
     status: ${{ job.status }}
     author_name: Integration Test # default: 8398a7@action-slack
   env:
-    GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # required
+    GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # optional
     SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} # required
   if: always() # Pick up events even if the job fails or is canceled.
 ```
@@ -56,7 +56,7 @@ When adding to text, write as follows.
     status: ${{ job.status }}
     text: overwrite text
   env:
-    GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # required
+    GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # optional
     SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} # required
 ```
 
@@ -78,7 +78,7 @@ The specified `secrets.SLACK_WEBHOOK_URL` must be legacy.
     icon_emoji: ':octocat:'
     channel: '#integration-test'
   env:
-    GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # required
+    GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # optional
     SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} # required
 ```
 
@@ -139,6 +139,6 @@ It is assumed that the input is in csv format.
     status: ${{ job.status }}
     fields: repo,message,action,author
   env:
-    GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # required
+    GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # optional
     SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} # required
 ```
diff --git a/dist/index.js b/dist/index.js
index 4c35e95..33233bb 100644
--- a/dist/index.js
+++ b/dist/index.js
@@ -10327,10 +10327,7 @@ const groupMention = ['here', 'channel'];
 class Client {
     constructor(props, token, webhookUrl) {
         this.with = props;
-        if (props.status !== 'custom') {
-            if (token === undefined) {
-                throw new Error('Specify secrets.GITHUB_TOKEN');
-            }
+        if (token !== undefined) {
             this.github = new github.GitHub(token);
         }
         if (webhookUrl === undefined) {
@@ -10394,32 +10391,38 @@ class Client {
         });
     }
     fields() {
+        var _a, _b;
         return __awaiter(this, void 0, void 0, function* () {
-            if (this.github === undefined) {
-                throw Error('Specify secrets.GITHUB_TOKEN');
-            }
             const { sha } = github.context;
             const { owner, repo } = github.context.repo;
-            const commit = yield this.github.repos.getCommit({ owner, repo, ref: sha });
-            const { author } = commit.data.commit;
-            return [
+            const commit = yield ((_a = this.github) === null || _a === void 0 ? void 0 : _a.repos.getCommit({
+                owner,
+                repo,
+                ref: sha,
+            }));
+            const author = (_b = commit) === null || _b === void 0 ? void 0 : _b.data.commit.author;
+            return this.filterField([
                 this.repo,
-                {
-                    title: 'message',
-                    value: commit.data.commit.message,
-                    short: true,
-                },
+                commit
+                    ? {
+                        title: 'message',
+                        value: commit.data.commit.message,
+                        short: true,
+                    }
+                    : undefined,
                 this.commit,
-                {
-                    title: 'author',
-                    value: `${author.name}<${author.email}>`,
-                    short: true,
-                },
+                author
+                    ? {
+                        title: 'author',
+                        value: `${author.name}<${author.email}>`,
+                        short: true,
+                    }
+                    : undefined,
                 this.action,
                 this.eventName,
                 this.ref,
                 this.workflow,
-            ];
+            ], undefined);
         });
     }
     get commit() {
@@ -10475,6 +10478,9 @@ class Client {
         }
         return '';
     }
+    filterField(array, diff) {
+        return array.filter(item => item !== diff);
+    }
 }
 exports.Client = Client;
 
diff --git a/package-lock.json b/package-lock.json
index 03cfb36..9e2f745 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,6 +1,6 @@
 {
   "name": "action-slack",
-  "version": "2.6.0",
+  "version": "2.7.0",
   "lockfileVersion": 1,
   "requires": true,
   "dependencies": {
@@ -543,6 +543,12 @@
         "axios": "^0.18.0"
       }
     },
+    "@tootallnate/once": {
+      "version": "1.0.0",
+      "resolved": "https://registry.npmjs.org/@tootallnate/once/-/once-1.0.0.tgz",
+      "integrity": "sha512-KYyTT/T6ALPkIRd2Ge080X/BsXvy9O0hcWTtMWkPvwAwF99+vn6Dv4GzrFT/Nn1LePr+FFDbRXXlqmsy9lw2zA==",
+      "dev": true
+    },
     "@types/babel__core": {
       "version": "7.1.3",
       "resolved": "https://registry.npmjs.org/@types/babel__core/-/babel__core-7.1.3.tgz",
@@ -799,12 +805,29 @@
       "dev": true
     },
     "agent-base": {
-      "version": "4.3.0",
-      "resolved": "https://registry.npmjs.org/agent-base/-/agent-base-4.3.0.tgz",
-      "integrity": "sha512-salcGninV0nPrwpGNn4VTXBb1SOuXQBiqbrNXoeizJsHrsL6ERFM2Ne3JUSBWRE6aeNJI2ROP/WEEIDUiDe3cg==",
+      "version": "6.0.0",
+      "resolved": "https://registry.npmjs.org/agent-base/-/agent-base-6.0.0.tgz",
+      "integrity": "sha512-j1Q7cSCqN+AwrmDd+pzgqc0/NpC655x2bUf5ZjRIO77DcNBFmh+OgRNzF6OKdCC9RSCb19fGd99+bhXFdkRNqw==",
       "dev": true,
       "requires": {
-        "es6-promisify": "^5.0.0"
+        "debug": "4"
+      },
+      "dependencies": {
+        "debug": {
+          "version": "4.1.1",
+          "resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz",
+          "integrity": "sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==",
+          "dev": true,
+          "requires": {
+            "ms": "^2.1.1"
+          }
+        },
+        "ms": {
+          "version": "2.1.2",
+          "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
+          "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==",
+          "dev": true
+        }
       }
     },
     "ajv": {
@@ -1405,16 +1428,16 @@
       "dev": true
     },
     "codecov": {
-      "version": "3.6.1",
-      "resolved": "https://registry.npmjs.org/codecov/-/codecov-3.6.1.tgz",
-      "integrity": "sha512-IUJB6WG47nWK7o50etF8jBadxdMw7DmoQg05yIljstXFBGB6clOZsIj6iD4P82T2YaIU3qq+FFu8K9pxgkCJDQ==",
+      "version": "3.6.5",
+      "resolved": "https://registry.npmjs.org/codecov/-/codecov-3.6.5.tgz",
+      "integrity": "sha512-v48WuDMUug6JXwmmfsMzhCHRnhUf8O3duqXvltaYJKrO1OekZWpB/eH6iIoaxMl8Qli0+u3OxptdsBOYiD7VAQ==",
       "dev": true,
       "requires": {
-        "argv": "^0.0.2",
-        "ignore-walk": "^3.0.1",
-        "js-yaml": "^3.13.1",
-        "teeny-request": "^3.11.3",
-        "urlgrey": "^0.4.4"
+        "argv": "0.0.2",
+        "ignore-walk": "3.0.3",
+        "js-yaml": "3.13.1",
+        "teeny-request": "6.0.1",
+        "urlgrey": "0.4.4"
       }
     },
     "collection-visit": {
@@ -1786,21 +1809,6 @@
         "is-symbol": "^1.0.2"
       }
     },
-    "es6-promise": {
-      "version": "4.2.8",
-      "resolved": "https://registry.npmjs.org/es6-promise/-/es6-promise-4.2.8.tgz",
-      "integrity": "sha512-HJDGx5daxeIvxdBxvG2cb9g4tEvwIk3i8+nhX0yGrYmZUzbkdg8QbDevheDB8gd0//uPj4c1EQua8Q+MViT0/w==",
-      "dev": true
-    },
-    "es6-promisify": {
-      "version": "5.0.0",
-      "resolved": "https://registry.npmjs.org/es6-promisify/-/es6-promisify-5.0.0.tgz",
-      "integrity": "sha1-UQnWLz5W6pZ8S2NQWu8IKRyKUgM=",
-      "dev": true,
-      "requires": {
-        "es6-promise": "^4.0.3"
-      }
-    },
     "escape-string-regexp": {
       "version": "1.0.5",
       "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz",
@@ -3646,6 +3654,34 @@
         "whatwg-encoding": "^1.0.1"
       }
     },
+    "http-proxy-agent": {
+      "version": "4.0.1",
+      "resolved": "https://registry.npmjs.org/http-proxy-agent/-/http-proxy-agent-4.0.1.tgz",
+      "integrity": "sha512-k0zdNgqWTGA6aeIRVpvfVob4fL52dTfaehylg0Y4UvSySvOq/Y+BOyPrgpUrA7HylqvU8vIZGsRuXmspskV0Tg==",
+      "dev": true,
+      "requires": {
+        "@tootallnate/once": "1",
+        "agent-base": "6",
+        "debug": "4"
+      },
+      "dependencies": {
+        "debug": {
+          "version": "4.1.1",
+          "resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz",
+          "integrity": "sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==",
+          "dev": true,
+          "requires": {
+            "ms": "^2.1.1"
+          }
+        },
+        "ms": {
+          "version": "2.1.2",
+          "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
+          "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==",
+          "dev": true
+        }
+      }
+    },
     "http-signature": {
       "version": "1.2.0",
       "resolved": "https://registry.npmjs.org/http-signature/-/http-signature-1.2.0.tgz",
@@ -3658,19 +3694,25 @@
       }
     },
     "https-proxy-agent": {
-      "version": "2.2.4",
-      "resolved": "https://registry.npmjs.org/https-proxy-agent/-/https-proxy-agent-2.2.4.tgz",
-      "integrity": "sha512-OmvfoQ53WLjtA9HeYP9RNrWMJzzAz1JGaSFr1nijg0PVR1JaD/xbJq1mdEIIlxGpXp9eSe/O2LgU9DJmTPd0Eg==",
+      "version": "4.0.0",
+      "resolved": "https://registry.npmjs.org/https-proxy-agent/-/https-proxy-agent-4.0.0.tgz",
+      "integrity": "sha512-zoDhWrkR3of1l9QAL8/scJZyLu8j/gBkcwcaQOZh7Gyh/+uJQzGVETdgT30akuwkpL8HTRfssqI3BZuV18teDg==",
       "dev": true,
       "requires": {
-        "agent-base": "^4.3.0",
-        "debug": "^3.1.0"
+        "agent-base": "5",
+        "debug": "4"
       },
       "dependencies": {
+        "agent-base": {
+          "version": "5.1.1",
+          "resolved": "https://registry.npmjs.org/agent-base/-/agent-base-5.1.1.tgz",
+          "integrity": "sha512-TMeqbNl2fMW0nMjTEPOwe3J/PRFP4vqeoNuQMG0HlMrtm5QxKqdvAkZ1pRBQ/ulIyDD5Yq0nJ7YbdD8ey0TO3g==",
+          "dev": true
+        },
         "debug": {
-          "version": "3.2.6",
-          "resolved": "https://registry.npmjs.org/debug/-/debug-3.2.6.tgz",
-          "integrity": "sha512-mel+jf7nrtEl5Pn1Qx46zARXKDpBbvzezse7p7LqINmdoIk8PYP5SySaxEmYv6TZ0JyEKA1hsCId6DIhgITtWQ==",
+          "version": "4.1.1",
+          "resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz",
+          "integrity": "sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==",
           "dev": true,
           "requires": {
             "ms": "^2.1.1"
@@ -6258,6 +6300,15 @@
       "integrity": "sha1-NbCYdbT/SfJqd35QmzCQoyJr8ks=",
       "dev": true
     },
+    "stream-events": {
+      "version": "1.0.5",
+      "resolved": "https://registry.npmjs.org/stream-events/-/stream-events-1.0.5.tgz",
+      "integrity": "sha512-E1GUzBSgvct8Jsb3v2X15pjzN1tYebtbLaMg+eBOUOAxgbLoSbT2NS91ckc5lJD1KfLjId+jXJRgo0qnV5Nerg==",
+      "dev": true,
+      "requires": {
+        "stubs": "^3.0.0"
+      }
+    },
     "string-length": {
       "version": "2.0.0",
       "resolved": "https://registry.npmjs.org/string-length/-/string-length-2.0.0.tgz",
@@ -6342,6 +6393,12 @@
       "integrity": "sha512-VTyMAUfdm047mwKl+u79WIdrZxtFtn+nBxHeb844XBQ9uMNTuTHdx2hc5RiAJYqwTj3wc/xe5HLSdJSkJ+WfZw==",
       "dev": true
     },
+    "stubs": {
+      "version": "3.0.0",
+      "resolved": "https://registry.npmjs.org/stubs/-/stubs-3.0.0.tgz",
+      "integrity": "sha1-6NK6H6nJBXAwPAMLaQD31fiavls=",
+      "dev": true
+    },
     "supports-color": {
       "version": "5.5.0",
       "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz",
@@ -6376,13 +6433,15 @@
       }
     },
     "teeny-request": {
-      "version": "3.11.3",
-      "resolved": "https://registry.npmjs.org/teeny-request/-/teeny-request-3.11.3.tgz",
-      "integrity": "sha512-CKncqSF7sH6p4rzCgkb/z/Pcos5efl0DmolzvlqRQUNcpRIruOhY9+T1FsIlyEbfWd7MsFpodROOwHYh2BaXzw==",
+      "version": "6.0.1",
+      "resolved": "https://registry.npmjs.org/teeny-request/-/teeny-request-6.0.1.tgz",
+      "integrity": "sha512-TAK0c9a00ELOqLrZ49cFxvPVogMUFaWY8dUsQc/0CuQPGF+BOxOQzXfE413BAk2kLomwNplvdtMpeaeGWmoc2g==",
       "dev": true,
       "requires": {
-        "https-proxy-agent": "^2.2.1",
+        "http-proxy-agent": "^4.0.0",
+        "https-proxy-agent": "^4.0.0",
         "node-fetch": "^2.2.0",
+        "stream-events": "^1.0.5",
         "uuid": "^3.3.2"
       }
     },
diff --git a/package.json b/package.json
index 58466e8..4ec1245 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "action-slack",
-  "version": "2.6.0",
+  "version": "2.7.0",
   "private": true,
   "description": "You can notify slack of GitHub Actions.",
   "main": "lib/main.js",
@@ -43,7 +43,7 @@
     "@types/semver": "^6.2.0",
     "@typescript-eslint/parser": "^2.13.0",
     "@zeit/ncc": "^0.20.5",
-    "codecov": "^3.6.1",
+    "codecov": "^3.6.5",
     "eslint": "^6.8.0",
     "eslint-plugin-github": "^3.4.0",
     "eslint-plugin-jest": "^23.1.1",
diff --git a/src/client.ts b/src/client.ts
index d2c5c29..439b9c5 100644
--- a/src/client.ts
+++ b/src/client.ts
@@ -13,6 +13,12 @@ export interface With {
   channel: string;
 }
 
+interface Field {
+  title: string;
+  value: string;
+  short: boolean;
+}
+
 const groupMention = ['here', 'channel'];
 
 export class Client {
@@ -23,10 +29,7 @@ export class Client {
   constructor(props: With, token?: string, webhookUrl?: string) {
     this.with = props;
 
-    if (props.status !== 'custom') {
-      if (token === undefined) {
-        throw new Error('Specify secrets.GITHUB_TOKEN');
-      }
+    if (token !== undefined) {
       this.github = new github.GitHub(token);
     }
 
@@ -90,36 +93,45 @@ export class Client {
     };
   }
 
-  private async fields() {
-    if (this.github === undefined) {
-      throw Error('Specify secrets.GITHUB_TOKEN');
-    }
+  private async fields(): Promise<Field[]> {
     const { sha } = github.context;
     const { owner, repo } = github.context.repo;
-    const commit = await this.github.repos.getCommit({ owner, repo, ref: sha });
-    const { author } = commit.data.commit;
-
-    return [
-      this.repo,
-      {
-        title: 'message',
-        value: commit.data.commit.message,
-        short: true,
-      },
-      this.commit,
-      {
-        title: 'author',
-        value: `${author.name}<${author.email}>`,
-        short: true,
-      },
-      this.action,
-      this.eventName,
-      this.ref,
-      this.workflow,
-    ];
+
+    const commit = await this.github?.repos.getCommit({
+      owner,
+      repo,
+      ref: sha,
+    });
+    const author = commit?.data.commit.author;
+
+    return this.filterField(
+      [
+        this.repo,
+        commit
+          ? {
+              title: 'message',
+              value: commit.data.commit.message,
+              short: true,
+            }
+          : undefined,
+        this.commit,
+        author
+          ? {
+              title: 'author',
+              value: `${author.name}<${author.email}>`,
+              short: true,
+            }
+          : undefined,
+        this.action,
+        this.eventName,
+        this.ref,
+        this.workflow,
+      ],
+      undefined,
+    );
   }
 
-  private get commit() {
+  private get commit(): Field {
     const { sha } = github.context;
     const { owner, repo } = github.context.repo;
 
@@ -130,7 +142,7 @@ export class Client {
     };
   }
 
-  private get repo() {
+  private get repo(): Field {
     const { owner, repo } = github.context.repo;
 
     return {
@@ -140,7 +152,7 @@ export class Client {
     };
   }
 
-  private get action() {
+  private get action(): Field {
     const { sha } = github.context;
     const { owner, repo } = github.context.repo;
 
@@ -151,7 +163,7 @@ export class Client {
     };
   }
 
-  private get eventName() {
+  private get eventName(): Field {
     return {
       title: 'eventName',
       value: github.context.eventName,
@@ -159,11 +171,11 @@ export class Client {
     };
   }
 
-  private get ref() {
+  private get ref(): Field {
     return { title: 'ref', value: github.context.ref, short: true };
   }
 
-  private get workflow() {
+  private get workflow(): Field {
     return { title: 'workflow', value: github.context.workflow, short: true };
   }
 
@@ -180,4 +192,14 @@ export class Client {
     }
     return '';
   }
+
+  private filterField<T extends Array<Field | undefined>, U extends undefined>(
+    array: T,
+    diff: U,
+  ) {
+    return array.filter(item => item !== diff) as Exclude<
+      T extends { [K in keyof T]: infer U } ? U : never,
+      U
+    >[];
+  }
 }


Test patch:
diff --git a/__tests__/client.test.ts b/__tests__/client.test.ts
index f7d8247..a979ed9 100644
--- a/__tests__/client.test.ts
+++ b/__tests__/client.test.ts
@@ -280,4 +280,24 @@ describe('8398a7/action-slack', () => {
     payload.attachments[0].color = 'danger';
     expect(await client.fail(msg)).toStrictEqual(payload);
   });
+
+  it('works without GITHUB_TOKEN', async () => {
+    const withParams: With = {
+      status: '',
+      mention: '',
+      author_name: '',
+      only_mention_fail: '',
+      username: '',
+      icon_emoji: '',
+      icon_url: '',
+      channel: '',
+    };
+    const client = new Client(withParams, undefined, '');
+    const payload = getTemplate(`${successMsg}\n`);
+    payload.attachments[0].color = 'good';
+    payload.attachments[0].fields = payload.attachments[0].fields.filter(
+      (field: any) => !['message', 'author'].includes(field.title),
+    );
+    expect(await client.success('')).toStrictEqual(payload);
+  });
 });