From 36cd6a5cd62ff0addcc7f3a3283e837ba1b33b80 Mon Sep 17 00:00:00 2001
From: Emmanuel Cazenave <ecazenave@entrouvert.com>
Date: Thu, 6 Jun 2019 17:35:40 +0200
Subject: [PATCH] create a jenkins pipeline (#33771)

---
 Jenkinsfile                | 38 ++++++++++++++++++++++++++++
 django_journal/__init__.py |  2 +-
 django_journal/tests.py    |  2 +-
 setup.py                   | 52 ++++++++++++++++++--------------------
 tox.ini                    | 14 ++++++++++
 5 files changed, 78 insertions(+), 30 deletions(-)
 create mode 100644 Jenkinsfile
 create mode 100644 tox.ini

diff --git a/Jenkinsfile b/Jenkinsfile
new file mode 100644
index 0000000..a557988
--- /dev/null
+++ b/Jenkinsfile
@@ -0,0 +1,38 @@
+@Library('eo-jenkins-lib@master') import eo.Utils
+
+pipeline {
+    agent any
+    options { disableConcurrentBuilds() }
+    stages {
+        stage('Unit Tests') {
+            steps {
+                sh 'tox -rv'
+            }
+            post {
+                always {
+                    junit 'junit.xml'
+                }
+            }
+        }
+        stage('Packaging') {
+            steps {
+                script {
+                    if (env.JOB_NAME == 'django-journal' && env.GIT_BRANCH == 'origin/master') {
+                        sh 'sudo -H -u eobuilder /usr/local/bin/eobuilder -d jessie,stretch django-journal'
+                    }
+                }
+            }
+        }
+    }
+    post {
+        always {
+            script {
+                utils = new Utils()
+                utils.mail_notify(currentBuild, env, 'admin+jenkins-django-journal@entrouvert.com')
+            }
+        }
+        success {
+            cleanWs()
+        }
+    }
+}
diff --git a/django_journal/__init__.py b/django_journal/__init__.py
index 6df2e69..73e706a 100644
--- a/django_journal/__init__.py
+++ b/django_journal/__init__.py
@@ -9,7 +9,7 @@ from django.conf import settings
 from decorator import atomic
 
 __all__ = ('record', 'error_record', 'Journal')
-__version__ = '1.25.2'
+
 
 def unicode_truncate(s, length, encoding='utf-8'):
     '''Truncate an unicode string so that its UTF-8 encoding is less than
diff --git a/django_journal/tests.py b/django_journal/tests.py
index 4fa4941..8bc9c89 100644
--- a/django_journal/tests.py
+++ b/django_journal/tests.py
@@ -13,7 +13,7 @@ class JournalTestCase(TestCase):
         models.JOURNAL_METADATA_CACHE_TIMEOUT = 0
         self.users = []
         self.groups = []
-        with transaction.commit_on_success():
+        with transaction.atomic():
             for i in range(20):
                 self.users.append(
                         User.objects.create(username='user%s' % i))
diff --git a/setup.py b/setup.py
index 563d5f1..c730872 100755
--- a/setup.py
+++ b/setup.py
@@ -1,5 +1,6 @@
 #!/usr/bin/python
 import os
+import subprocess
 import sys
 
 from setuptools import setup, find_packages
@@ -70,36 +71,31 @@ class install_lib(_install_lib):
 
 
 def get_version():
-    import glob
-    import re
-    import os
-
-    version = None
-    for d in glob.glob('*'):
-        if not os.path.isdir(d):
-            continue
-        module_file = os.path.join(d, '__init__.py')
-        if not os.path.exists(module_file):
-            continue
-        for v in re.findall("""__version__ *= *['"](.*)['"]""",
-                open(module_file).read()):
-            assert version is None
-            version = v
-        if version:
-            break
-    assert version is not None
+    '''Use the VERSION, if absent generates a version with git describe, if not
+       tag exists, take 0.0- and add the length of the commit log.
+    '''
+    if os.path.exists('VERSION'):
+        with open('VERSION', 'r') as v:
+            return v.read()
     if os.path.exists('.git'):
-        import subprocess
-        p = subprocess.Popen(['git','describe','--dirty', '--match=v*'],
-                stdout=subprocess.PIPE)
+        p = subprocess.Popen(
+            ['git', 'describe', '--dirty=.dirty', '--match=v*'],
+            stdout=subprocess.PIPE, stderr=subprocess.PIPE)
         result = p.communicate()[0]
-        assert p.returncode == 0, 'git returned non-zero'
-        new_version = result.split()[0][1:]
-        major_minor_release = new_version.split('-')[0]
-        assert  version == major_minor_release, \
-                '__version__ (%s) must match the last git annotated tag (%s)' % (version, major_minor_release)
-        version = new_version.replace('-', '.').replace('.g', '+g')
-    return version
+        if p.returncode == 0:
+            result = result.decode('ascii').strip()[1:]  # strip spaces/newlines and initial v
+            if '-' in result:  # not a tagged version
+                real_number, commit_count, commit_hash = result.split('-', 2)
+                version = '%s.post%s+%s' % (real_number, commit_count, commit_hash)
+            else:
+                version = result
+            return version
+        else:
+            return '0.0.post%s' % len(
+                    subprocess.check_output(
+                            ['git', 'rev-list', 'HEAD']).splitlines())
+    return '0.0'
+
 
 setup(name='django-journal',
       version=get_version(),
diff --git a/tox.ini b/tox.ini
new file mode 100644
index 0000000..a4b812a
--- /dev/null
+++ b/tox.ini
@@ -0,0 +1,14 @@
+[tox]
+toxworkdir = /tmp/tox-{env:USER}/combo/{env:BRANCH_NAME:}
+envlist = py2
+
+
+[testenv]
+usedevelop = True
+deps =
+  pytest
+  pytest-django
+setenv =
+  DJANGO_SETTINGS_MODULE=test_settings
+commands =
+  {posargs:py.test --junitxml=junit.xml django_journal/tests.py}
-- 
2.20.1

