From 520f89d865ad6c3dfc35431e02d37989108663db Mon Sep 17 00:00:00 2001
From: Hugo Renard <hugo.renard@protonmail.com>
Date: Wed, 5 Jun 2024 12:12:47 +0200
Subject: [PATCH] feat: update to v3.2.2 & pin compatibles plugins

---
 .gitmodules                        | 48 ++++++++++++++++++++
 Dockerfile                         |  7 +--
 Makefile                           | 13 ++++++
 VERSION                            |  1 +
 install/discourse-air              |  1 +
 install/discourse-akismet          |  1 +
 install/discourse-assign           |  1 +
 install/discourse-calendar         |  1 +
 install/discourse-docs             |  1 +
 install/discourse-docs-card-filter |  1 +
 install/discourse-encrypt          |  1 +
 install/discourse-events           |  1 +
 install/discourse-legal-tools      |  1 +
 install/discourse-locations        |  1 +
 install/discourse-math             |  1 +
 install/discourse-openid-connect   |  1 +
 install/discourse-reactions        |  1 +
 install/discourse-solved           |  1 +
 install/discourse-templates        |  1 +
 install/discourse-topic-voting     |  1 +
 install/plugin-list                | 16 -------
 scripts/discourse-plugins.py       | 72 ++++++++++++++++++++++++++++++
 22 files changed, 151 insertions(+), 22 deletions(-)
 create mode 100644 .gitmodules
 create mode 100644 Makefile
 create mode 100644 VERSION
 create mode 160000 install/discourse-air
 create mode 160000 install/discourse-akismet
 create mode 160000 install/discourse-assign
 create mode 160000 install/discourse-calendar
 create mode 160000 install/discourse-docs
 create mode 160000 install/discourse-docs-card-filter
 create mode 160000 install/discourse-encrypt
 create mode 160000 install/discourse-events
 create mode 160000 install/discourse-legal-tools
 create mode 160000 install/discourse-locations
 create mode 160000 install/discourse-math
 create mode 160000 install/discourse-openid-connect
 create mode 160000 install/discourse-reactions
 create mode 160000 install/discourse-solved
 create mode 160000 install/discourse-templates
 create mode 160000 install/discourse-topic-voting
 delete mode 100644 install/plugin-list
 create mode 100755 scripts/discourse-plugins.py

diff --git a/.gitmodules b/.gitmodules
new file mode 100644
index 0000000..4e0951f
--- /dev/null
+++ b/.gitmodules
@@ -0,0 +1,48 @@
+[submodule "install/discourse-akismet"]
+	path = install/discourse-akismet
+	url = https://github.com/discourse/discourse-akismet
+[submodule "install/discourse-assign"]
+	path = install/discourse-assign
+	url = https://github.com/discourse/discourse-assign
+[submodule "install/discourse-calendar"]
+	path = install/discourse-calendar
+	url = https://github.com/discourse/discourse-calendar
+[submodule "install/discourse-docs"]
+	path = install/discourse-docs
+	url = https://github.com/discourse/discourse-docs
+[submodule "install/discourse-encrypt"]
+	path = install/discourse-encrypt
+	url = https://github.com/discourse/discourse-encrypt
+[submodule "install/discourse-locations"]
+	path = install/discourse-locations
+	url = https://github.com/paviliondev/discourse-locations
+[submodule "install/discourse-legal-tools"]
+	path = install/discourse-legal-tools
+	url = https://github.com/paviliondev/discourse-legal-tools
+[submodule "install/discourse-math"]
+	path = install/discourse-math
+	url = https://github.com/discourse/discourse-math
+[submodule "install/discourse-openid-connect"]
+	path = install/discourse-openid-connect
+	url = https://github.com/discourse/discourse-openid-connect
+[submodule "install/discourse-solved"]
+	path = install/discourse-solved
+	url = https://github.com/discourse/discourse-solved
+[submodule "install/discourse-topic-voting"]
+	path = install/discourse-topic-voting
+	url = https://github.com/discourse/discourse-topic-voting
+[submodule "install/discourse-air"]
+	path = install/discourse-air
+	url = https://github.com/discourse/discourse-air
+[submodule "install/discourse-reactions"]
+	path = install/discourse-reactions
+	url = https://github.com/discourse/discourse-reactions
+[submodule "install/discourse-templates"]
+	path = install/discourse-templates
+	url = https://github.com/discourse/discourse-templates
+[submodule "install/discourse-docs-card-filter"]
+	path = install/discourse-docs-card-filter
+	url = https://github.com/discourse/discourse-docs-card-filter
+[submodule "install/discourse-events"]
+	path = install/discourse-events
+	url = https://github.com/paviliondev/discourse-events
diff --git a/Dockerfile b/Dockerfile
index a289840..65359a1 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -119,12 +119,7 @@ RUN bundle config --local deployment true &&\
     yarn cache clean &&\
     find /var/www/discourse/vendor/bundle -name tmp -type d -exec rm -rf {} +
 
-COPY install /tmp/install
-
-RUN cd /var/www/discourse/plugins \
- && for plugin in $(cat /tmp/install/plugin-list); do \
-      git clone --depth 1 $plugin; \
-    done
+COPY --chown=1000:1000 install/ /var/www/discourse/plugins/
 
 RUN cd app/assets/javascripts/discourse && \
      /var/www/discourse/app/assets/javascripts/node_modules/.bin/ember build -prod
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..89747aa
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,13 @@
+VERSION=$(shell cat VERSION)
+TAG?=${VERSION}
+
+all: update build push
+
+update:
+	python3 scripts/discourse-plugins.py ${VERSION}
+
+build:
+	sudo podman build -t quay.io/libre.sh/discourse:${TAG} --build-arg DISCOURSE_VERSION=${VERSION} .
+
+push:
+	sudo podman push quay.io/libre.sh/discourse:${TAG}
\ No newline at end of file
diff --git a/VERSION b/VERSION
new file mode 100644
index 0000000..6835fff
--- /dev/null
+++ b/VERSION
@@ -0,0 +1 @@
+v3.2.2
\ No newline at end of file
diff --git a/install/discourse-air b/install/discourse-air
new file mode 160000
index 0000000..85dc24d
--- /dev/null
+++ b/install/discourse-air
@@ -0,0 +1 @@
+Subproject commit 85dc24d6b58d1b16e6d225ae710633dc20c34d08
diff --git a/install/discourse-akismet b/install/discourse-akismet
new file mode 160000
index 0000000..248ccf7
--- /dev/null
+++ b/install/discourse-akismet
@@ -0,0 +1 @@
+Subproject commit 248ccf70bdb8b5f3e773688cb2954548cd0fb9d9
diff --git a/install/discourse-assign b/install/discourse-assign
new file mode 160000
index 0000000..56b0de3
--- /dev/null
+++ b/install/discourse-assign
@@ -0,0 +1 @@
+Subproject commit 56b0de3896361b6a87523537c8f5b450d2fe0807
diff --git a/install/discourse-calendar b/install/discourse-calendar
new file mode 160000
index 0000000..84ef46a
--- /dev/null
+++ b/install/discourse-calendar
@@ -0,0 +1 @@
+Subproject commit 84ef46a38cf02748ecacad16c5d9c6fec12dc8da
diff --git a/install/discourse-docs b/install/discourse-docs
new file mode 160000
index 0000000..94c7b7d
--- /dev/null
+++ b/install/discourse-docs
@@ -0,0 +1 @@
+Subproject commit 94c7b7da216c66d773f800a714493f087affaac9
diff --git a/install/discourse-docs-card-filter b/install/discourse-docs-card-filter
new file mode 160000
index 0000000..a601e3c
--- /dev/null
+++ b/install/discourse-docs-card-filter
@@ -0,0 +1 @@
+Subproject commit a601e3c5807b4152e3fe6fc6673bc2a99e6a829d
diff --git a/install/discourse-encrypt b/install/discourse-encrypt
new file mode 160000
index 0000000..9013a8e
--- /dev/null
+++ b/install/discourse-encrypt
@@ -0,0 +1 @@
+Subproject commit 9013a8ea8c7c7df55b36f329b3bb09b754a04cfe
diff --git a/install/discourse-events b/install/discourse-events
new file mode 160000
index 0000000..cab3840
--- /dev/null
+++ b/install/discourse-events
@@ -0,0 +1 @@
+Subproject commit cab38405bdd58a17ae67cb2166fab045d5c65019
diff --git a/install/discourse-legal-tools b/install/discourse-legal-tools
new file mode 160000
index 0000000..f98b0de
--- /dev/null
+++ b/install/discourse-legal-tools
@@ -0,0 +1 @@
+Subproject commit f98b0deb554a773fa4248d2d6a37c972c71cb8ad
diff --git a/install/discourse-locations b/install/discourse-locations
new file mode 160000
index 0000000..0671f57
--- /dev/null
+++ b/install/discourse-locations
@@ -0,0 +1 @@
+Subproject commit 0671f57cf999d381cfcd25b2c8d1ec28ac1e2830
diff --git a/install/discourse-math b/install/discourse-math
new file mode 160000
index 0000000..2984713
--- /dev/null
+++ b/install/discourse-math
@@ -0,0 +1 @@
+Subproject commit 2984713f68044544129b3575b9e2f9be31105293
diff --git a/install/discourse-openid-connect b/install/discourse-openid-connect
new file mode 160000
index 0000000..f31a869
--- /dev/null
+++ b/install/discourse-openid-connect
@@ -0,0 +1 @@
+Subproject commit f31a869611a2fad8efadd9b2504440215169a391
diff --git a/install/discourse-reactions b/install/discourse-reactions
new file mode 160000
index 0000000..354695c
--- /dev/null
+++ b/install/discourse-reactions
@@ -0,0 +1 @@
+Subproject commit 354695cf5fce2bf516c9be451fe4bb56e487b95a
diff --git a/install/discourse-solved b/install/discourse-solved
new file mode 160000
index 0000000..526a446
--- /dev/null
+++ b/install/discourse-solved
@@ -0,0 +1 @@
+Subproject commit 526a44644a7b3f0c2a3ba4fc16e72f364e9fce6d
diff --git a/install/discourse-templates b/install/discourse-templates
new file mode 160000
index 0000000..bb410b2
--- /dev/null
+++ b/install/discourse-templates
@@ -0,0 +1 @@
+Subproject commit bb410b2a7d84f4503a9fa8a1fbbe017d627348d8
diff --git a/install/discourse-topic-voting b/install/discourse-topic-voting
new file mode 160000
index 0000000..ba41633
--- /dev/null
+++ b/install/discourse-topic-voting
@@ -0,0 +1 @@
+Subproject commit ba41633e0abe0535fd358a0809e0b4e0c79be128
diff --git a/install/plugin-list b/install/plugin-list
deleted file mode 100644
index a8d7383..0000000
--- a/install/plugin-list
+++ /dev/null
@@ -1,16 +0,0 @@
-https://github.com/discourse/discourse-akismet
-https://github.com/discourse/discourse-assign
-https://github.com/discourse/discourse-calendar
-https://github.com/discourse/discourse-docs
-https://github.com/discourse/discourse-encrypt
-https://github.com/paviliondev/discourse-locations
-https://github.com/paviliondev/discourse-legal-tools
-https://github.com/discourse/discourse-math
-https://github.com/discourse/discourse-openid-connect
-https://github.com/discourse/discourse-solved
-https://github.com/discourse/discourse-topic-voting 
-https://github.com/discourse/discourse-air
-https://github.com/discourse/discourse-reactions
-https://github.com/discourse/discourse-templates
-https://github.com/discourse/discourse-docs-card-filter
-https://github.com/paviliondev/discourse-events
diff --git a/scripts/discourse-plugins.py b/scripts/discourse-plugins.py
new file mode 100755
index 0000000..eabed82
--- /dev/null
+++ b/scripts/discourse-plugins.py
@@ -0,0 +1,72 @@
+#!/usr/bin/python3
+
+import argparse,os,git,re,semver
+
+folder = "install"
+p = re.compile('^((.{1,2}) )?(.+): (.+)$')
+
+def parse_version(v):
+    v = v.removeprefix("v").split(".")
+    pre = None
+    if len(v) > 3:
+        pre = v[3]
+    return semver.Version(major=v[0], minor=v[1], patch=v[2], prerelease=pre)
+
+def parse_rule(rule):
+    m = p.match(rule)
+    op = m.group(2)
+    if op is None:
+        op = '<='
+    v = m.group(3)
+    ref = m.group(4)
+    return op, v, ref 
+
+def get_rev(discourse_version, rules, kept_rev):
+    rule = next(rules, None)
+    if rule is None:
+        return kept_rev
+
+    op, v, rev = parse_rule(rule)
+    version = parse_version(v)
+    r = discourse_version.compare(version)
+    if r == 1 or (r == 0 and op == "<"):
+        return kept_rev
+
+    return get_rev(discourse_version, rules, rev)
+
+
+def get_rules(repo):
+    try:
+        blob = repo.head.commit.tree[".discourse-compatibility"]
+    except:
+        return iter([])
+    rules = [
+        line.decode() for line in blob.data_stream.read().splitlines()
+    ]
+    return iter(rules)
+
+def main(args):
+    discourse_version = parse_version(args.version)
+    print("Updating plugins for Discourse %s" % discourse_version)
+    for d in os.listdir(folder):
+        try:
+            repo = git.Repo(folder + "/" + d)
+            repo.remotes.origin.pull()
+            rules = get_rules(repo)
+            ref = get_rev(discourse_version, rules, repo.head.commit.hexsha)
+            repo.head.reset(commit=ref, working_tree=True)
+            log = d.ljust(40) + ref
+            repo.active_branch
+            if ref == repo.remotes.origin.refs.HEAD.commit.hexsha:
+                log += " (head)"
+            print(log)
+        except Exception as e:
+            print(d.ljust(40)+"failed")
+            print(e)
+            exit(1)
+
+if __name__ == '__main__':
+    parser = argparse.ArgumentParser(description='Helper script to manage Discourse plugins.')
+    parser.add_argument("version")
+    args = parser.parse_args()
+    main(args)
\ No newline at end of file
-- 
GitLab