From 7c54e3c9740d092d3eff9fdcd0e8c06f7515a30c Mon Sep 17 00:00:00 2001
From: Damien Couchez <damien.couchez@gmail.com>
Date: Fri, 2 Jul 2021 16:15:43 +0200
Subject: [PATCH 1/3] feat: Add comment on PR

---
 .github/workflows/github_pages.yml |  3 ++-
 .github/workflows/netlify.yml      |  4 +++-
 .github/workflows/vercel_pr.yml    |  3 ++-
 .github/workflows/vercel_push.yml  |  3 ++-
 action.yml                         |  3 +++
 package.json                       |  2 +-
 src/index.ts                       | 25 +++++++++++++++++++++++++
 yarn.lock                          |  4 ++--
 8 files changed, 40 insertions(+), 7 deletions(-)

diff --git a/.github/workflows/github_pages.yml b/.github/workflows/github_pages.yml
index fb116287..0b47c34f 100644
--- a/.github/workflows/github_pages.yml
+++ b/.github/workflows/github_pages.yml
@@ -16,7 +16,7 @@ jobs:
         uses: actions/checkout@v2
         with:
           repository: algolia/algoliasearch-crawler-github-actions 
-          ref: v0.5.0
+          ref: v0.6.0
       - name: Sleep for 30s
         run: sleep 30
       - name: Github-pages-MAIN => Algolia crawler creation and recrawl (Push on Main branch)
@@ -26,6 +26,7 @@ jobs:
           crawler-user-id: ${{ secrets.CRAWLER_USER_ID }}
           crawler-api-key: ${{ secrets.CRAWLER_API_KEY }}
           crawler-api-base-url: 'https://crawler-dev.algolia.com/api/1/'
+          github-token: ${{ secrets.GIT_HUB_TOKEN }}
           crawler-name: gpages-github-actions-test-${{ github.ref }}
           algolia-app-id: ${{ secrets.ALGOLIA_APP_ID }}
           algolia-api-key: ${{ secrets.ALGOLIA_API_KEY }}
diff --git a/.github/workflows/netlify.yml b/.github/workflows/netlify.yml
index 30e0a039..56cbbff5 100644
--- a/.github/workflows/netlify.yml
+++ b/.github/workflows/netlify.yml
@@ -18,7 +18,7 @@ jobs:
         uses: actions/checkout@v2
         with:
           repository: algolia/algoliasearch-crawler-github-actions 
-          ref: v0.5.0
+          ref: v0.6.0
       - name: Sleep for 30s
         run: sleep 30
       - name: Netlify-PR => Algolia crawler creation and recrawl on preview (Pull Request)
@@ -29,6 +29,7 @@ jobs:
           crawler-user-id: ${{ secrets.CRAWLER_USER_ID }}
           crawler-api-key: ${{ secrets.CRAWLER_API_KEY }}
           crawler-api-base-url: 'https://crawler-dev.algolia.com/api/1/'
+          github-token: ${{ secrets.GIT_HUB_TOKEN }}
           crawler-name: netlify-github-actions-test-${{ github.ref }}
           algolia-app-id: ${{ secrets.ALGOLIA_APP_ID }}
           algolia-api-key: ${{ secrets.ALGOLIA_API_KEY }}
@@ -42,6 +43,7 @@ jobs:
           crawler-user-id: ${{ secrets.CRAWLER_USER_ID }}
           crawler-api-key: ${{ secrets.CRAWLER_API_KEY }}
           crawler-api-base-url: 'https://crawler-dev.algolia.com/api/1/'
+          github-token: ${{ secrets.GIT_HUB_TOKEN }}
           crawler-name: netlify-github-actions-test-${{ github.ref }}
           algolia-app-id: ${{ secrets.ALGOLIA_APP_ID }}
           algolia-api-key: ${{ secrets.ALGOLIA_API_KEY }}
diff --git a/.github/workflows/vercel_pr.yml b/.github/workflows/vercel_pr.yml
index 41e6b564..c34f3a80 100644
--- a/.github/workflows/vercel_pr.yml
+++ b/.github/workflows/vercel_pr.yml
@@ -22,7 +22,7 @@ jobs:
         uses: actions/checkout@v2
         with:
           repository: algolia/algoliasearch-crawler-github-actions 
-          ref: v0.5.0
+          ref: v0.6.0
       - name: Vercel-PR => Algolia crawler creation and recrawl on preview (Pull Request)
         uses: ./
         id: crawler_pr
@@ -30,6 +30,7 @@ jobs:
           crawler-user-id: ${{ secrets.CRAWLER_USER_ID }}
           crawler-api-key: ${{ secrets.CRAWLER_API_KEY }}
           crawler-api-base-url: 'https://crawler-dev.algolia.com/api/1/'
+          github-token: ${{ secrets.GIT_HUB_TOKEN }}
           crawler-name: vercel-github-actions-test-${{ github.ref }}
           algolia-app-id: ${{ secrets.ALGOLIA_APP_ID }}
           algolia-api-key: ${{ secrets.ALGOLIA_API_KEY }}
diff --git a/.github/workflows/vercel_push.yml b/.github/workflows/vercel_push.yml
index 3b23068c..77bceedb 100644
--- a/.github/workflows/vercel_push.yml
+++ b/.github/workflows/vercel_push.yml
@@ -16,7 +16,7 @@ jobs:
         uses: actions/checkout@v2
         with:
           repository: algolia/algoliasearch-crawler-github-actions 
-          ref: v0.5.0
+          ref: v0.6.0
       - name: Vercel-MAIN => Algolia crawler creation and recrawl on preview (Push on Main branch)
         uses: ./
         id: crawler_push
@@ -24,6 +24,7 @@ jobs:
           crawler-user-id: ${{ secrets.CRAWLER_USER_ID }}
           crawler-api-key: ${{ secrets.CRAWLER_API_KEY }}
           crawler-api-base-url: 'https://crawler-dev.algolia.com/api/1/'
+          github-token: ${{ secrets.GIT_HUB_TOKEN }}
           crawler-name: vercel-github-actions-test-${{ github.ref }}
           algolia-app-id: ${{ secrets.ALGOLIA_APP_ID }}
           algolia-api-key: ${{ secrets.ALGOLIA_API_KEY }}
diff --git a/action.yml b/action.yml
index 0312dd22..a79facc8 100644
--- a/action.yml
+++ b/action.yml
@@ -12,6 +12,9 @@ inputs:
     description: 'Crawler API URL'
     required: false
     default: 'https://crawler.algolia.com/api/1/'
+  github-token:
+    description: 'Github token'
+    required: true
 
   # CRAWLER CONFIGURATION
   crawler-name:
diff --git a/package.json b/package.json
index eb9b6fe6..a52f04e6 100644
--- a/package.json
+++ b/package.json
@@ -59,7 +59,7 @@
   },
   "dependencies": {
     "@actions/core": "1.4.0",
-    "@actions/github": "5.0.0",
+    "@actions/github": "^5.0.0",
     "node-fetch": "2.6.1"
   }
 }
diff --git a/src/index.ts b/src/index.ts
index d1f6202a..cf28096a 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -1,5 +1,6 @@
 /* eslint-disable no-console */
 import * as core from '@actions/core';
+import * as github from '@actions/github';
 
 import { CrawlerApiClient } from './crawler-api-client';
 import type { ConfigJson } from './types/configJson';
@@ -9,6 +10,7 @@ import type { GetCrawlersResponseBody } from './types/publicApiJsonResponses';
 const CRAWLER_USER_ID = core.getInput('crawler-user-id');
 const CRAWLER_API_KEY = core.getInput('crawler-api-key');
 const CRAWLER_API_BASE_URL = core.getInput('crawler-api-base-url');
+const GITHUB_TOKEN = core.getInput('github-token');
 
 // CRAWLER CONFIGURATION
 const CRAWLER_NAME = core.getInput('crawler-name').replace(/\//g, '-');
@@ -54,6 +56,28 @@ function getRecordExtractorSource(): string {
 }`;
 }
 
+function addComment(): void {
+  try {
+    const message = 'Test msg';
+
+    const context = github.context;
+    if (context.payload.pull_request === undefined) {
+      core.setFailed('No pull request found.');
+      return;
+    }
+    const prNumber = context.payload.pull_request.number;
+
+    const octokit = github.getOctokit(GITHUB_TOKEN);
+    octokit.rest.issues.createComment({
+      ...context.repo,
+      issue_number: prNumber,
+      body: message,
+    });
+  } catch (error) {
+    core.setFailed(error.message);
+  }
+}
+
 async function crawlerReindex(): Promise<void> {
   let crawlerId = '';
 
@@ -80,6 +104,7 @@ async function crawlerReindex(): Promise<void> {
 
   console.log(`---------- Reindexing crawler ${crawlerId} ----------`);
   await client.reindex(crawlerId);
+  addComment();
 }
 
 console.log('---------CRAWLER CONFIG---------');
diff --git a/yarn.lock b/yarn.lock
index 8e5b54b6..bc095233 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -7,9 +7,9 @@
   resolved "https://registry.npmjs.org/@actions/core/-/core-1.4.0.tgz"
   integrity sha512-CGx2ilGq5i7zSLgiiGUtBCxhRRxibJYU6Fim0Q1Wg2aQL2LTnF27zbqZOrxfvFQ55eSBW0L8uVStgtKMpa0Qlg==
 
-"@actions/github@5.0.0":
+"@actions/github@^5.0.0":
   version "5.0.0"
-  resolved "https://registry.npmjs.org/@actions/github/-/github-5.0.0.tgz"
+  resolved "https://registry.yarnpkg.com/@actions/github/-/github-5.0.0.tgz#1754127976c50bd88b2e905f10d204d76d1472f8"
   integrity sha512-QvE9eAAfEsS+yOOk0cylLBIO/d6WyWIOvsxxzdrPFaud39G6BOkUwScXZn1iBzQzHyu9SBkkLSWlohDWdsasAQ==
   dependencies:
     "@actions/http-client" "^1.0.11"

From 7e85a3487d6f4527fe7684a137b7957dcda1911c Mon Sep 17 00:00:00 2001
From: Damien Couchez <damien.couchez@gmail.com>
Date: Mon, 12 Jul 2021 16:55:28 +0200
Subject: [PATCH 2/3] format comment

---
 src/index.ts | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/src/index.ts b/src/index.ts
index cf28096a..465b9e01 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -56,9 +56,15 @@ function getRecordExtractorSource(): string {
 }`;
 }
 
-function addComment(): void {
+function addComment(crawlerId: string): void {
   try {
-    const message = 'Test msg';
+    const pathArray = CRAWLER_API_BASE_URL.split('/');
+    const protocol = pathArray[0];
+    const host = pathArray[2];
+    const baseUrl = `${protocol}//${host}`;
+
+    const message = `<p>Check Crawler: ${baseUrl}/admin/crawlers/${crawlerId}/overview</p>
+    <p>Check Application: https://www.algolia.com/apps/${ALGOLIA_APP_ID}/explorer/browse/${CRAWLER_NAME}</p>`;
 
     const context = github.context;
     if (context.payload.pull_request === undefined) {
@@ -104,7 +110,7 @@ async function crawlerReindex(): Promise<void> {
 
   console.log(`---------- Reindexing crawler ${crawlerId} ----------`);
   await client.reindex(crawlerId);
-  addComment();
+  addComment(crawlerId);
 }
 
 console.log('---------CRAWLER CONFIG---------');

From 5a19575524306e90e967c75e60fd47c98b3d0809 Mon Sep 17 00:00:00 2001
From: Damien Couchez <damien.couchez@gmail.com>
Date: Tue, 13 Jul 2021 11:08:56 +0200
Subject: [PATCH 3/3] review changes

---
 .github/workflows/github_pages.yml | 1 -
 .github/workflows/netlify.yml      | 2 --
 .github/workflows/vercel_pr.yml    | 1 -
 .github/workflows/vercel_push.yml  | 1 -
 action.yml                         | 1 +
 package.json                       | 2 +-
 src/index.ts                       | 6 +++---
 yarn.lock                          | 2 +-
 8 files changed, 6 insertions(+), 10 deletions(-)

diff --git a/.github/workflows/github_pages.yml b/.github/workflows/github_pages.yml
index 0b47c34f..d8358ef9 100644
--- a/.github/workflows/github_pages.yml
+++ b/.github/workflows/github_pages.yml
@@ -26,7 +26,6 @@ jobs:
           crawler-user-id: ${{ secrets.CRAWLER_USER_ID }}
           crawler-api-key: ${{ secrets.CRAWLER_API_KEY }}
           crawler-api-base-url: 'https://crawler-dev.algolia.com/api/1/'
-          github-token: ${{ secrets.GIT_HUB_TOKEN }}
           crawler-name: gpages-github-actions-test-${{ github.ref }}
           algolia-app-id: ${{ secrets.ALGOLIA_APP_ID }}
           algolia-api-key: ${{ secrets.ALGOLIA_API_KEY }}
diff --git a/.github/workflows/netlify.yml b/.github/workflows/netlify.yml
index 56cbbff5..fc08320b 100644
--- a/.github/workflows/netlify.yml
+++ b/.github/workflows/netlify.yml
@@ -29,7 +29,6 @@ jobs:
           crawler-user-id: ${{ secrets.CRAWLER_USER_ID }}
           crawler-api-key: ${{ secrets.CRAWLER_API_KEY }}
           crawler-api-base-url: 'https://crawler-dev.algolia.com/api/1/'
-          github-token: ${{ secrets.GIT_HUB_TOKEN }}
           crawler-name: netlify-github-actions-test-${{ github.ref }}
           algolia-app-id: ${{ secrets.ALGOLIA_APP_ID }}
           algolia-api-key: ${{ secrets.ALGOLIA_API_KEY }}
@@ -43,7 +42,6 @@ jobs:
           crawler-user-id: ${{ secrets.CRAWLER_USER_ID }}
           crawler-api-key: ${{ secrets.CRAWLER_API_KEY }}
           crawler-api-base-url: 'https://crawler-dev.algolia.com/api/1/'
-          github-token: ${{ secrets.GIT_HUB_TOKEN }}
           crawler-name: netlify-github-actions-test-${{ github.ref }}
           algolia-app-id: ${{ secrets.ALGOLIA_APP_ID }}
           algolia-api-key: ${{ secrets.ALGOLIA_API_KEY }}
diff --git a/.github/workflows/vercel_pr.yml b/.github/workflows/vercel_pr.yml
index c34f3a80..08d3bd31 100644
--- a/.github/workflows/vercel_pr.yml
+++ b/.github/workflows/vercel_pr.yml
@@ -30,7 +30,6 @@ jobs:
           crawler-user-id: ${{ secrets.CRAWLER_USER_ID }}
           crawler-api-key: ${{ secrets.CRAWLER_API_KEY }}
           crawler-api-base-url: 'https://crawler-dev.algolia.com/api/1/'
-          github-token: ${{ secrets.GIT_HUB_TOKEN }}
           crawler-name: vercel-github-actions-test-${{ github.ref }}
           algolia-app-id: ${{ secrets.ALGOLIA_APP_ID }}
           algolia-api-key: ${{ secrets.ALGOLIA_API_KEY }}
diff --git a/.github/workflows/vercel_push.yml b/.github/workflows/vercel_push.yml
index 77bceedb..3ceba6d5 100644
--- a/.github/workflows/vercel_push.yml
+++ b/.github/workflows/vercel_push.yml
@@ -24,7 +24,6 @@ jobs:
           crawler-user-id: ${{ secrets.CRAWLER_USER_ID }}
           crawler-api-key: ${{ secrets.CRAWLER_API_KEY }}
           crawler-api-base-url: 'https://crawler-dev.algolia.com/api/1/'
-          github-token: ${{ secrets.GIT_HUB_TOKEN }}
           crawler-name: vercel-github-actions-test-${{ github.ref }}
           algolia-app-id: ${{ secrets.ALGOLIA_APP_ID }}
           algolia-api-key: ${{ secrets.ALGOLIA_API_KEY }}
diff --git a/action.yml b/action.yml
index a79facc8..52724c9f 100644
--- a/action.yml
+++ b/action.yml
@@ -15,6 +15,7 @@ inputs:
   github-token:
     description: 'Github token'
     required: true
+    default: ${{ github.token }}
 
   # CRAWLER CONFIGURATION
   crawler-name:
diff --git a/package.json b/package.json
index a52f04e6..eb9b6fe6 100644
--- a/package.json
+++ b/package.json
@@ -59,7 +59,7 @@
   },
   "dependencies": {
     "@actions/core": "1.4.0",
-    "@actions/github": "^5.0.0",
+    "@actions/github": "5.0.0",
     "node-fetch": "2.6.1"
   }
 }
diff --git a/src/index.ts b/src/index.ts
index 465b9e01..74d6b64d 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -63,12 +63,12 @@ function addComment(crawlerId: string): void {
     const host = pathArray[2];
     const baseUrl = `${protocol}//${host}`;
 
-    const message = `<p>Check Crawler: ${baseUrl}/admin/crawlers/${crawlerId}/overview</p>
-    <p>Check Application: https://www.algolia.com/apps/${ALGOLIA_APP_ID}/explorer/browse/${CRAWLER_NAME}</p>`;
+    const message = `<p>Check your created <a href="${baseUrl}/admin/crawlers/${crawlerId}/overview" target="_blank">Crawler</a></p>
+    <p>Check your created index on your <a href="https://www.algolia.com/apps/${ALGOLIA_APP_ID}/explorer/browse/${CRAWLER_NAME}" target="_blank">Algolia Application</a></p>`;
 
     const context = github.context;
     if (context.payload.pull_request === undefined) {
-      core.setFailed('No pull request found.');
+      core.info('No pull request found.');
       return;
     }
     const prNumber = context.payload.pull_request.number;
diff --git a/yarn.lock b/yarn.lock
index bc095233..0c613e6f 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -7,7 +7,7 @@
   resolved "https://registry.npmjs.org/@actions/core/-/core-1.4.0.tgz"
   integrity sha512-CGx2ilGq5i7zSLgiiGUtBCxhRRxibJYU6Fim0Q1Wg2aQL2LTnF27zbqZOrxfvFQ55eSBW0L8uVStgtKMpa0Qlg==
 
-"@actions/github@^5.0.0":
+"@actions/github@5.0.0":
   version "5.0.0"
   resolved "https://registry.yarnpkg.com/@actions/github/-/github-5.0.0.tgz#1754127976c50bd88b2e905f10d204d76d1472f8"
   integrity sha512-QvE9eAAfEsS+yOOk0cylLBIO/d6WyWIOvsxxzdrPFaud39G6BOkUwScXZn1iBzQzHyu9SBkkLSWlohDWdsasAQ==