From eafee4dae7c3e903086f5c0133e15e1f6a8aabdf Mon Sep 17 00:00:00 2001
From: Brent Huisman <brenthuisman@users.noreply.github.com>
Date: Wed, 15 Dec 2021 15:46:48 +0100
Subject: [PATCH] Monthly cron job checking for submodule updates (#1741)

* Create a script that generates `diff.log` if git submodules have tags more recent that the commit they're currently at.
* A Github Action runs this script monthly.
* _If_ updates found, Github Action creates an Issue like so: https://github.com/brenthuisman/arbor/issues/9
* Actually updating the git submodule and Spack file are still manual

First step in addressing #1731
---
 .github/workflows/check-submodules.yml | 28 ++++++++++++++++++++++++++
 .gitmodules                            |  3 +++
 scripts/submodule-diff.sh              | 13 ++++++++++++
 3 files changed, 44 insertions(+)
 create mode 100644 .github/workflows/check-submodules.yml
 create mode 100755 scripts/submodule-diff.sh

diff --git a/.github/workflows/check-submodules.yml b/.github/workflows/check-submodules.yml
new file mode 100644
index 00000000..6ffc328a
--- /dev/null
+++ b/.github/workflows/check-submodules.yml
@@ -0,0 +1,28 @@
+name: Update submodules
+
+on:
+  schedule:
+    # - cron: '0 2 * * 0' # run at 2 AM every sunday
+    - cron: '5 4 2 * *' # 04:05 every 2nd of month
+  push:
+    branches: [ 'ci/auto-update-submodules' ]
+    
+jobs:
+  update_submodules:
+    runs-on: ubuntu-latest
+    steps:
+      - name: Clone w/ submodules
+        uses: actions/checkout@v2
+        with:
+          submodules: recursive
+          fetch-depth: 0
+      - name: Check submodules for updates
+        run: scripts/submodule-diff.sh
+      - name: Create Issue From File
+        uses: peter-evans/create-issue-from-file@v3
+        with:
+          title: '[AUTOMATED] Git submodule updates found'
+          content-filepath: ./diff.log
+          labels: |
+            dependency update
+          assignees: brenthuisman
diff --git a/.gitmodules b/.gitmodules
index 7a46b5b6..52423617 100644
--- a/.gitmodules
+++ b/.gitmodules
@@ -1,9 +1,12 @@
 [submodule "google-benchmark"]
 	path = ext/google-benchmark
 	url = https://github.com/google/benchmark
+	branch = main
 [submodule "python/pybind11"]
 	path = python/pybind11
 	url = https://github.com/pybind/pybind11.git
+	branch = master
 [submodule "ext/fmt"]
 	path = ext/fmt
 	url = https://github.com/fmtlib/fmt.git
+	branch = master
diff --git a/scripts/submodule-diff.sh b/scripts/submodule-diff.sh
new file mode 100755
index 00000000..b056895c
--- /dev/null
+++ b/scripts/submodule-diff.sh
@@ -0,0 +1,13 @@
+#!/usr/bin/env bash
+# Check any git submodule remotes for updates, and print difference with current Arbor repo state to `diff.log`
+
+git submodule foreach 'git describe HEAD --tags' | tee current_state_of_git_submodules_in_arbor_repo.log
+git submodule foreach 'git fetch'
+git submodule foreach 'git describe `git log --branches -1 --pretty=format:"%H"` --tags --abbrev=0' | tee upstream_state_of_git_submodules.log
+if diff current_state_of_git_submodules_in_arbor_repo.log upstream_state_of_git_submodules.log ; then
+    echo "No submodule updates found"
+else
+    diff current_state_of_git_submodules_in_arbor_repo.log upstream_state_of_git_submodules.log -U 10000 > diff.log
+fi
+rm current_state_of_git_submodules_in_arbor_repo.log
+rm upstream_state_of_git_submodules.log
-- 
GitLab