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

zregvart pushed a commit to branch pr/github-auth
in repository https://gitbox.apache.org/repos/asf/camel-website.git

commit f3a4d9f160e7d347dd65ea5e1292b6af6cd5d30c
Author: Zoran Regvart <zregv...@apache.org>
AuthorDate: Fri May 14 23:40:24 2021 +0200

    chore: proxy to pass authentication for GitHub API
    
    Implements and configures a proxy used add `Authorization` header to
    invocations of GitHub REST API.
    
    As we're sometimes failing the build due to GitHub API rate limits, and
    since in Hugo we can't pass HTTP headers to include the `Authorization`
    header which would allow us a higher API invocation limit. An
    alternative to use a proxy through which all invocations of the GitHub
    API will go through and have the `Authorization` header added.
    
    To use the proxy three environment variables must be set at the same
    time:
     - `HUGO_PARAMS_GitHubAPI=http://localhost:22635`
     - `GITHUB_USR=<GitHub username>`
     - `GITHUB_PSW=<GitHub token>`
---
 .gitignore                                         |  1 +
 .pnp.js                                            |  2 +
 Jenkinsfile                                        |  8 +--
 config.toml                                        |  1 +
 github-proxy.js                                    | 60 ++++++++++++++++++++++
 layouts/blog/post.html                             |  2 +-
 layouts/blog/summary.html                          |  2 +-
 layouts/partials/releases/camel-k-runtime.html     |  2 +-
 layouts/partials/releases/camel-k.html             |  2 +-
 .../partials/releases/camel-kafka-connector.html   |  2 +-
 layouts/partials/releases/camel-quarkus.html       |  2 +-
 package.json                                       |  5 +-
 yarn.lock                                          |  3 +-
 13 files changed, 81 insertions(+), 11 deletions(-)

diff --git a/.gitignore b/.gitignore
index 2c94153..1ab379a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -21,3 +21,4 @@ resources
 !**/.yarn/sdks
 !**/.yarn/unplugged
 !**/.yarn/versions
+*.github-proxy.*
diff --git a/.pnp.js b/.pnp.js
index 1f18d68..a3ecbac 100755
--- a/.pnp.js
+++ b/.pnp.js
@@ -62,6 +62,7 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) {
             ["gulp-cheerio", "npm:1.0.0"],
             ["gulp-htmlmin", "npm:5.0.1"],
             ["html-validate", "npm:4.11.0"],
+            ["http-proxy", "npm:1.18.1"],
             ["hugo-extended", "npm:0.73.0"],
             ["netlify-cli", "npm:2.69.11"],
             ["npm-run-all", "npm:4.1.5"],
@@ -4389,6 +4390,7 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) {
             ["gulp-cheerio", "npm:1.0.0"],
             ["gulp-htmlmin", "npm:5.0.1"],
             ["html-validate", "npm:4.11.0"],
+            ["http-proxy", "npm:1.18.1"],
             ["hugo-extended", "npm:0.73.0"],
             ["netlify-cli", "npm:2.69.11"],
             ["npm-run-all", "npm:4.1.5"],
diff --git a/Jenkinsfile b/Jenkinsfile
index 067d197..757889d 100644
--- a/Jenkinsfile
+++ b/Jenkinsfile
@@ -35,9 +35,11 @@ pipeline {
     }
 
     environment {
-        ANTORA_CACHE_DIR  = "$WORKSPACE/.antora-cache"
-        HUGO_CACHEDIR     = "$WORKSPACE/.hugo-cache"
-        CAMEL_ENV         = 'production'
+        ANTORA_CACHE_DIR      = "$WORKSPACE/.antora-cache"
+        CAMEL_ENV             = 'production'
+        GITHUB                = 
credentials('399061d0-5ab5-4142-a186-a52081fef742') // asf-ci credential
+        HUGO_CACHEDIR         = "$WORKSPACE/.hugo-cache"
+        HUGO_PARAMS_GitHubAPI = 'http://localhost:22635'
     }
 
     stages {
diff --git a/config.toml b/config.toml
index fb3f87f..d24fb6b 100644
--- a/config.toml
+++ b/config.toml
@@ -18,6 +18,7 @@ defaultMarkdownHandler = "blackfriday"
     socialProfiles = ["https://twitter.com/ApacheCamel";]
     organizationLogo = "https://camel.apache.org/_/img/logo-d.svg";
     organizationDescription = "Apache Camel ™ is a versatile open-source 
integration framework based on known Enterprise Integration Patterns. Camel 
empowers you to define routing and mediation rules in a variety of 
domain-specific languages, including a Java-based Fluent API, Spring or 
Blueprint XML Configuration files, and a Scala DSL."
+    GitHubAPI = "https://api.github.com";
 
 [caches]
 
diff --git a/github-proxy.js b/github-proxy.js
new file mode 100644
index 0000000..9ed1a71
--- /dev/null
+++ b/github-proxy.js
@@ -0,0 +1,60 @@
+const PID_FILE = '.github-proxy.pid';
+
+if (process.argv.indexOf('stop') > 0) {
+  const { readFile, existsSync, unlink } = require('fs');
+
+  if (!existsSync(PID_FILE)) {
+    return;
+  }
+
+  readFile(PID_FILE, (err, data) => {
+    if (err) {
+      throw err;
+    }
+
+    try {
+      process.kill(Number.parseInt(data), 'SIGINT');
+    } catch (e) {
+      unlink(PID_FILE);
+    }
+  });
+} else if (process.argv.indexOf('start') > 0) {
+  const fs = require('fs');
+
+  const log = fs.createWriteStream('.github-proxy.log', { flags: 'a' });
+  process.stdout.write = process.stderr.write = log.write.bind(log);
+
+  if (!(process.env.GITHUB_USR && process.env.GITHUB_PSW)) {
+    console.log('No GITHUB_USR and GITHUB_PSW environment variables set');
+    process.exit(1);
+  }
+
+  fs.writeFile(PID_FILE, process.pid.toString());
+  process.on('SIGINT', (code) => {
+    console.log('Stopping');
+    log.uncork();
+    log.end();
+    fs.unlink(PID_FILE);
+    process.exit(0);
+  });
+
+  const httpProxy = require('http-proxy');
+
+  const proxy = httpProxy.createProxyServer({
+    auth: `${process.env.GITHUB_USR}:${process.env.GITHUB_PSW}`,
+    changeOrigin: true,
+    target: 'https://api.github.com'
+  }).listen(22635)
+  console.info('Proxy started.');
+
+  proxy.on('error', console.error);
+} else {
+  const { spawn } = require('child_process');
+
+  const proxy = spawn(process.argv[0], ['github-proxy.js', 'start'], {
+    detached: true,
+    stdio: 'ignore'
+  });
+
+  proxy.unref();
+}
diff --git a/layouts/blog/post.html b/layouts/blog/post.html
index add0640..33a406a 100644
--- a/layouts/blog/post.html
+++ b/layouts/blog/post.html
@@ -13,7 +13,7 @@
             Posted on <time itemprop="published" datetime="{{ dateFormat 
"2006-01-02" .PublishDate }}"
                 title="{{ dateFormat "Monday, January 2, 2006" .PublishDate 
}}">{{ dateFormat "January 2, 2006" .PublishDate }}</time>,
             by {{ range $author := .Params.authors }}
-            {{ with getJSON "https://api.github.com/users/"; $author }}
+            {{ with getJSON $.Site.Params.GitHubAPI "/users/" $author }}
             <figure>
                 <img src="{{ .avatar_url }}" alt="{{ .name }}">
                 <figcaption rel="author">{{ .name }}</figcaption>
diff --git a/layouts/blog/summary.html b/layouts/blog/summary.html
index 56f7f2e..a2fb0e5 100644
--- a/layouts/blog/summary.html
+++ b/layouts/blog/summary.html
@@ -2,7 +2,7 @@
 
     <header>
         <a href="{{ .RelPermalink }}"><h1>{{ .Title }}</h1></a>
-        <time itemprop="published" datetime="{{ dateFormat "2006-01-02" 
.PublishDate }}" title="{{ dateFormat "Monday, January 2, 2006" .PublishDate 
}}">{{ dateFormat "January 2, 2006" .PublishDate }}</time>, by {{ range $idx, 
$author := .Params.authors }}{{ if gt $idx 0 }}, {{ end }}<span rel="author">{{ 
(getJSON  "https://api.github.com/users/"; $author).name }}</span>{{ end }}
+        <time itemprop="published" datetime="{{ dateFormat "2006-01-02" 
.PublishDate }}" title="{{ dateFormat "Monday, January 2, 2006" .PublishDate 
}}">{{ dateFormat "January 2, 2006" .PublishDate }}</time>, by {{ range $idx, 
$author := .Params.authors }}{{ if gt $idx 0 }}, {{ end }}<span rel="author">{{ 
(getJSON $.Site.Params.GitHubAPI "/users/" $author).name }}</span>{{ end }}
     </header>
     <p>{{ .Summary }}</p>
     <p><a class="continue" href="{{ .RelPermalink }}">Continue reading 
&#10095;</a></p>
diff --git a/layouts/partials/releases/camel-k-runtime.html 
b/layouts/partials/releases/camel-k-runtime.html
index 026b5b8..1ec1e17 100644
--- a/layouts/partials/releases/camel-k-runtime.html
+++ b/layouts/partials/releases/camel-k-runtime.html
@@ -11,7 +11,7 @@ git checkout camel-k-runtime-parent-{{ .Params.version 
}}</pre>
 
 <h2 id="resolved"><a class="anchor" href="#resolved"></a>Resolved issues</h2>
 <p>Here is a list of all the issues that have been resolved for this 
release</p>
-{{ $issues := getJSON 
"https://api.github.com/repos/apache/camel-k-runtime/issues?state=closed&milestone=";
 (string .Params.milestone ) }}
+{{ $issues := getJSON $.Site.Params.GitHubAPI 
"/repos/apache/camel-k-runtime/issues?state=closed&milestone=" (string 
.Params.milestone ) }}
 <dl>
 {{ range $issues }}
   <dt><a href="{{ .html_url }}">#{{ .number }}</a></dt><dd>{{ .title }}</dd>
diff --git a/layouts/partials/releases/camel-k.html 
b/layouts/partials/releases/camel-k.html
index 4b45397..13c760f 100644
--- a/layouts/partials/releases/camel-k.html
+++ b/layouts/partials/releases/camel-k.html
@@ -11,7 +11,7 @@ git checkout v{{ .Params.version }}</pre>
 
 <h2 id="resolved"><a class="anchor" href="#resolved"></a>Resolved issues</h2>
 <p>Here is a list of all the issues that have been resolved for this 
release</p>
-{{ $issues := getJSON 
"https://api.github.com/repos/apache/camel-k/issues?state=closed&milestone="; 
(string .Params.milestone ) }}
+{{ $issues := getJSON $.Site.Params.GitHubAPI 
"/repos/apache/camel-k/issues?state=closed&milestone=" (string 
.Params.milestone ) }}
 <dl>
 {{ range $issues }}
   <dt><a href="{{ .html_url }}">#{{ .number }}</a></dt><dd>{{ .title }}</dd>
diff --git a/layouts/partials/releases/camel-kafka-connector.html 
b/layouts/partials/releases/camel-kafka-connector.html
index 6bed094..287d703 100644
--- a/layouts/partials/releases/camel-kafka-connector.html
+++ b/layouts/partials/releases/camel-kafka-connector.html
@@ -26,7 +26,7 @@ git checkout camel-kafka-connector-{{ .Params.version }}</pre>
 
 <h2 id="resolved"><a class="anchor" href="#resolved"></a>Resolved issues</h2>
 <p>Here is a list of all the issues that have been resolved for this 
release</p>
-{{ $issues := getJSON 
"https://api.github.com/repos/apache/camel-kafka-connector/issues?state=closed&labels=";
 (string .Params.version) }}
+{{ $issues := getJSON $.Site.Params.GitHubAPI 
"/repos/apache/camel-kafka-connector/issues?state=closed&labels=" (string 
.Params.version) }}
 <dl>
 {{ range $issues }}
   <dt><a href="{{ .html_url }}">#{{ .number }}</a></dt><dd>{{ .title }}</dd>
diff --git a/layouts/partials/releases/camel-quarkus.html 
b/layouts/partials/releases/camel-quarkus.html
index c5a1f86..bf4519c 100644
--- a/layouts/partials/releases/camel-quarkus.html
+++ b/layouts/partials/releases/camel-quarkus.html
@@ -11,7 +11,7 @@ git checkout {{ .Params.version }}</pre>
 
 <h2 id="resolved"><a class="anchor" href="#resolved"></a>Resolved issues</h2>
 <p>Here is a list of all the issues that have been resolved for this 
release</p>
-{{ $issues := getJSON 
"https://api.github.com/repos/apache/camel-quarkus/issues?state=closed&milestone=";
 (string .Params.milestone ) }}
+{{ $issues := getJSON $.Site.Params.GitHubAPI 
"/repos/apache/camel-quarkus/issues?state=closed&milestone=" (string 
.Params.milestone ) }}
 <dl>
 {{ range $issues }}
   <dt><a href="{{ .html_url }}">#{{ .number }}</a></dt><dd>{{ .title }}</dd>
diff --git a/package.json b/package.json
index 1a6085e..a279f59 100644
--- a/package.json
+++ b/package.json
@@ -5,9 +5,11 @@
   "scripts": {
     "build:antora": "antora --clean --fetch antora-playbook.yml --stacktrace 
--require ./patch-sitemap.js",
     "build:hugo": "hugo --cacheDir ${HUGO_CACHE_DIR:-$(pwd)/.hugo_data}",
+    "build:hugo-with-proxy": "run-s build:github-proxy build:hugo 
'build:github-proxy stop'",
     "build:minify": "gulp minify",
+    "build:github-proxy": "node github-proxy.js",
     "build:sitemap": "gulp sitemap",
-    "build": "run-s build:antora build:hugo build:sitemap build:minify",
+    "build": "run-s build:antora build:hugo-with-proxy build:sitemap 
build:minify",
     "build-all": "yarn workspaces foreach --topological-dev run build",
     "clean": "gulp clean",
     "preview": "run-s preview:antora preview:hugo",
@@ -40,6 +42,7 @@
     "gulp-cheerio": "~1.0",
     "gulp-htmlmin": "~5.0",
     "html-validate": "~4",
+    "http-proxy": "~1.18",
     "hugo-extended": "^0.73.0",
     "netlify-cli": "^2.69.11",
     "npm-run-all": "~4",
diff --git a/yarn.lock b/yarn.lock
index b610fc1..537c03b 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -3219,6 +3219,7 @@ __metadata:
     gulp-cheerio: ~1.0
     gulp-htmlmin: ~5.0
     html-validate: ~4
+    http-proxy: ~1.18
     hugo-extended: ^0.73.0
     netlify-cli: ^2.69.11
     npm-run-all: ~4
@@ -9912,7 +9913,7 @@ fsevents@~2.1.2:
   languageName: node
   linkType: hard
 
-"http-proxy@npm:^1.18.0, http-proxy@npm:^1.18.1":
+"http-proxy@npm:^1.18.0, http-proxy@npm:^1.18.1, http-proxy@npm:~1.18":
   version: 1.18.1
   resolution: "http-proxy@npm:1.18.1"
   dependencies:

Reply via email to