From e523e26d3e94174afdb44e52f361d88d52928a8b Mon Sep 17 00:00:00 2001
From: jurgenhaas <juergen@paragon-es.de>
Date: Mon, 30 May 2022 16:10:47 +0200
Subject: [PATCH] gitlab-ci-cd/drupal#28 Add script to determine next semantic
 version string

---
 Dockerfile     |  2 ++
 bin/gen-semver | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 57 insertions(+)
 create mode 100755 bin/gen-semver

diff --git a/Dockerfile b/Dockerfile
index ed7f70a..2d35071 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -80,6 +80,8 @@ RUN echo 'hosts: files dns' > /etc/nsswitch.conf && \
     ln -s /lib/libgcc_s.so.1 /usr/glibc-compat/lib/ && \
     ln -s python3 /usr/bin/python && \
     ln -s pip3 /usr/bin/pip && \
+    python -m pip install --upgrade pip && \
+    pip install semver && \
     \
     wget -q https://github.com/ahoy-cli/ahoy/releases/download/${AHOY_VERSION}/ahoy-bin-`uname -s`-amd64 -O /usr/local/bin/ahoy && \
     chmod +x /usr/local/bin/ahoy && \
diff --git a/bin/gen-semver b/bin/gen-semver
new file mode 100755
index 0000000..1468170
--- /dev/null
+++ b/bin/gen-semver
@@ -0,0 +1,55 @@
+#!/usr/bin/env python3
+
+import argparse
+import semver
+import subprocess
+import sys
+
+
+def git(*args):
+    return subprocess.check_output(["git"] + list(args))
+
+
+def bump(latest, major, minor):
+    prefix = ''
+    try:
+        if latest[0] == 'v' or latest[0] == 'V':
+            prefix = latest[0]
+            latest = latest[1:]
+        if major:
+            version = semver.bump_major(latest)
+        elif minor:
+            version = semver.bump_minor(latest)
+        else:
+            version = semver.bump_patch(latest)
+    except ValueError:
+        # Latest tag is not SemVer compatible
+        version = "1.0.0"
+    return prefix + version
+
+
+def main():
+    parser = argparse.ArgumentParser(description='Get new semantic version of git repo, bumps patch version by default')
+    parser.add_argument('--minor', action='store_true', default=False,
+                        help='Bump the minor version, would be overwritten by --major')
+    parser.add_argument('--major', action='store_true', default=False,
+                        help='Bump the major version')
+    args, extras = parser.parse_known_args()
+    try:
+        latest = git("describe", "--tags").decode().strip()
+    except subprocess.CalledProcessError:
+        # No tags in the repository
+        version = "1.0.0"
+    else:
+        # Skip already tagged commits
+        if '-' not in latest:
+            version = latest
+        else:
+            version = bump(latest, args.major, args.minor)
+
+    print(version)
+    return 0
+
+
+if __name__ == "__main__":
+    sys.exit(main())
-- 
GitLab