From e711b9f3b2d5e9a85a73035ef84c15e339e37777 Mon Sep 17 00:00:00 2001
From: "Kenneth Benzie (Benie)" <benie@infektor.net>
Date: Sat, 6 Apr 2024 16:33:03 +0100
Subject: [PATCH] Add optional GitHub API authentication headers

If the `GITHUB_TOKEN` is defined in the Ansible controller's
environment it is used to set the GitHub REST API authentication header.
This is they passed to all `uri` modules tasks which interact with the
GitHub REST API. If the `GITHUB_TOKEN` is not set, the authentication
header is not used.

Fixes #19
---
 playbooks/Linux.yaml                   | 4 ++++
 playbooks/LinuxCLI.yaml                | 4 ++++
 playbooks/UnixCLI.yaml                 | 4 ++++
 playbooks/UnixGUI.yaml                 | 4 ++++
 playbooks/WSL.yaml                     | 4 ++++
 playbooks/Windows.yaml                 | 5 ++++-
 playbooks/macOS.yaml                   | 4 ++++
 roles/bat/tasks/deb.yaml               | 1 +
 roles/fonts/tasks/Linux.yaml           | 1 +
 roles/git/tasks/Windows-installer.yaml | 1 +
 roles/jp/tasks/Darwin.yaml             | 1 +
 roles/jp/tasks/RedHat.yaml             | 1 +
 roles/llvm/tasks/Ubuntu.yaml           | 1 +
 roles/neovim/tasks/Debian.yaml         | 1 +
 roles/xremap/tasks/main.yaml           | 1 +
 roles/yq/tasks/Debian.yaml             | 1 +
 roles/yq/tasks/RedHat.yaml             | 1 +
 17 files changed, 38 insertions(+), 1 deletion(-)

diff --git a/playbooks/Linux.yaml b/playbooks/Linux.yaml
index d5186bd..f5e4b8c 100644
--- a/playbooks/Linux.yaml
+++ b/playbooks/Linux.yaml
@@ -2,6 +2,10 @@
 - import_playbook: LinuxCLI.yaml
 - import_playbook: UnixGUI.yaml
 - hosts: localhost
+  vars:
+    github_auth_headers: >-
+      {{ { 'Authorization': 'Bearer ' + lookup('env', 'GITHUB_TOKEN') }
+          if lookup('env', 'GITHUB_TOKEN') else {} }}
   roles:
     - role: firefox
     - role: kitty
diff --git a/playbooks/LinuxCLI.yaml b/playbooks/LinuxCLI.yaml
index 3663672..046e413 100644
--- a/playbooks/LinuxCLI.yaml
+++ b/playbooks/LinuxCLI.yaml
@@ -1,5 +1,9 @@
 ---
 - hosts: localhost
+  vars:
+    github_auth_headers: >-
+      {{ { 'Authorization': 'Bearer ' + lookup('env', 'GITHUB_TOKEN') }
+          if lookup('env', 'GITHUB_TOKEN') else {} }}
   roles:
     - role: rpmfusion
       when: ansible_os_family == 'RedHat' and ansible_distribution == 'Fedora'
diff --git a/playbooks/UnixCLI.yaml b/playbooks/UnixCLI.yaml
index e6bb244..670d7e4 100644
--- a/playbooks/UnixCLI.yaml
+++ b/playbooks/UnixCLI.yaml
@@ -1,5 +1,9 @@
 ---
 - hosts: localhost
+  vars:
+    github_auth_headers: >-
+      {{ { 'Authorization': 'Bearer ' + lookup('env', 'GITHUB_TOKEN') }
+          if lookup('env', 'GITHUB_TOKEN') else {} }}
   roles:
     - role: sudo
       when: ansible_user_id != "root"
diff --git a/playbooks/UnixGUI.yaml b/playbooks/UnixGUI.yaml
index 91be552..6b950f7 100644
--- a/playbooks/UnixGUI.yaml
+++ b/playbooks/UnixGUI.yaml
@@ -1,5 +1,9 @@
 ---
 - hosts: localhost
+  vars:
+    github_auth_headers: >-
+      {{ { 'Authorization': 'Bearer ' + lookup('env', 'GITHUB_TOKEN') }
+          if lookup('env', 'GITHUB_TOKEN') else {} }}
   roles:
     - role: flatpak
       when: ansible_os_family != "Darwin"
diff --git a/playbooks/WSL.yaml b/playbooks/WSL.yaml
index 6ab39a3..fac07de 100644
--- a/playbooks/WSL.yaml
+++ b/playbooks/WSL.yaml
@@ -1,5 +1,9 @@
 ---
 - import_playbook: LinuxCLI.yaml
 - hosts: localhost
+  vars:
+    github_auth_headers: >-
+      {{ { 'Authorization': 'Bearer ' + lookup('env', 'GITHUB_TOKEN') }
+          if lookup('env', 'GITHUB_TOKEN') else {} }}
   roles:
     - role: wsl
diff --git a/playbooks/Windows.yaml b/playbooks/Windows.yaml
index 586e028..57dbb0c 100644
--- a/playbooks/Windows.yaml
+++ b/playbooks/Windows.yaml
@@ -1,6 +1,9 @@
 ---
 - hosts: windows
-
+  vars:
+    github_auth_headers: >-
+      {{ { 'Authorization': 'Bearer ' + lookup('env', 'GITHUB_TOKEN') }
+          if lookup('env', 'GITHUB_TOKEN') else {} }}
   roles:
     - role: python
     - role: git
diff --git a/playbooks/macOS.yaml b/playbooks/macOS.yaml
index cf98510..df39cc3 100644
--- a/playbooks/macOS.yaml
+++ b/playbooks/macOS.yaml
@@ -5,6 +5,10 @@
     - role: system-info
 - import_playbook: UnixGUI.yaml
 - hosts: localhost
+  vars:
+    github_auth_headers: >-
+      {{ { 'Authorization': 'Bearer ' + lookup('env', 'GITHUB_TOKEN') }
+          if lookup('env', 'GITHUB_TOKEN') else {} }}
   roles:
     - role: mas
 
diff --git a/roles/bat/tasks/deb.yaml b/roles/bat/tasks/deb.yaml
index ba12ac3..206d556 100644
--- a/roles/bat/tasks/deb.yaml
+++ b/roles/bat/tasks/deb.yaml
@@ -2,6 +2,7 @@
 - name: get latest github release
   uri:
     url: https://api.github.com/repos/sharkdp/bat/releases/latest
+    headers: '{{github_auth_headers}}'
   register: latest
 
 - set_fact:
diff --git a/roles/fonts/tasks/Linux.yaml b/roles/fonts/tasks/Linux.yaml
index 721847f..86652bb 100644
--- a/roles/fonts/tasks/Linux.yaml
+++ b/roles/fonts/tasks/Linux.yaml
@@ -17,6 +17,7 @@
 - name: get latest release
   uri:
     url: https://api.github.com/repos/ryanoasis/nerd-fonts/releases/latest
+    headers: '{{github_auth_headers}}'
   register: latest
 
 - set_fact:
diff --git a/roles/git/tasks/Windows-installer.yaml b/roles/git/tasks/Windows-installer.yaml
index 3e7a937..6a27700 100644
--- a/roles/git/tasks/Windows-installer.yaml
+++ b/roles/git/tasks/Windows-installer.yaml
@@ -2,6 +2,7 @@
 - name: get json containing all releases
   win_uri:
     url: 'https://api.github.com/repos/git-for-windows/git/releases/tags/v{{git_version}}.windows.1'
+    headers: '{{github_auth_headers}}'
     return_content: true
   register: git_release
 
diff --git a/roles/jp/tasks/Darwin.yaml b/roles/jp/tasks/Darwin.yaml
index 7a14468..1d3dabc 100644
--- a/roles/jp/tasks/Darwin.yaml
+++ b/roles/jp/tasks/Darwin.yaml
@@ -2,6 +2,7 @@
 - name: get latest github release
   uri:
     url: https://api.github.com/repos/jmespath/jp/releases/latest
+    headers: '{{github_auth_headers}}'
   register: latest
 
 - set_fact:
diff --git a/roles/jp/tasks/RedHat.yaml b/roles/jp/tasks/RedHat.yaml
index 1ce1bdb..add3f2a 100644
--- a/roles/jp/tasks/RedHat.yaml
+++ b/roles/jp/tasks/RedHat.yaml
@@ -19,6 +19,7 @@
 - name: get latest release
   uri:
     url: 'https://api.github.com/repos/jmespath/jp/releases/latest'
+    headers: '{{github_auth_headers}}'
   register: latest
 
 - name: determine if jp needs installed
diff --git a/roles/llvm/tasks/Ubuntu.yaml b/roles/llvm/tasks/Ubuntu.yaml
index 42e7159..9a3bb87 100644
--- a/roles/llvm/tasks/Ubuntu.yaml
+++ b/roles/llvm/tasks/Ubuntu.yaml
@@ -2,6 +2,7 @@
 - name: get latest stable github release
   uri:
     url: https://api.github.com/repos/llvm/llvm-project/releases/latest
+    headers: '{{github_auth_headers}}'
   register: llvm_latest
 
 - name: read /etc/os-release
diff --git a/roles/neovim/tasks/Debian.yaml b/roles/neovim/tasks/Debian.yaml
index 19069da..7b860e0 100644
--- a/roles/neovim/tasks/Debian.yaml
+++ b/roles/neovim/tasks/Debian.yaml
@@ -40,6 +40,7 @@
 - name: get latest version
   uri:
     url: https://api.github.com/repos/neovim/neovim/releases/latest
+    headers: '{{github_auth_headers}}'
   register: latest
 
 - set_fact:
diff --git a/roles/xremap/tasks/main.yaml b/roles/xremap/tasks/main.yaml
index 89e4eb0..80bf563 100644
--- a/roles/xremap/tasks/main.yaml
+++ b/roles/xremap/tasks/main.yaml
@@ -30,6 +30,7 @@
 - name: get latest release
   uri:
     url: https://api.github.com/repos/k0kubun/xremap/releases/latest
+    headers: '{{github_auth_headers}}'
   register: latest
 
 - name: determine if install needed
diff --git a/roles/yq/tasks/Debian.yaml b/roles/yq/tasks/Debian.yaml
index da8d88e..55314aa 100644
--- a/roles/yq/tasks/Debian.yaml
+++ b/roles/yq/tasks/Debian.yaml
@@ -2,6 +2,7 @@
 - name: get latest github release
   uri:
     url: https://api.github.com/repos/mikefarah/yq/releases/latest
+    headers: '{{github_auth_headers}}'
   register: latest
 
 - set_fact:
diff --git a/roles/yq/tasks/RedHat.yaml b/roles/yq/tasks/RedHat.yaml
index 0605f1d..3223355 100644
--- a/roles/yq/tasks/RedHat.yaml
+++ b/roles/yq/tasks/RedHat.yaml
@@ -19,6 +19,7 @@
 - name: get latest release
   uri:
     url: 'https://api.github.com/repos/mikefarah/yq/releases/latest'
+    headers: '{{github_auth_headers}}'
   register: latest
 
 - name: determine if yq needs installed