From 8d0872083c6ed2024b5a23112ea19345c29b323a Mon Sep 17 00:00:00 2001
From: Zack Galbreath <zack.galbreath@kitware.com>
Date: Fri, 26 Oct 2018 12:05:26 -0400
Subject: [PATCH] Report current git commit of Spack to CDash

When using the CDash reporter, upload a Update.xml file that
indicates the hash of Spack's current git commit.
---
 lib/spack/spack/reporters/cdash.py            | 29 +++++++++++++++----
 .../spack/templates/reports/cdash/Update.xml  | 11 ++++---
 2 files changed, 30 insertions(+), 10 deletions(-)

diff --git a/lib/spack/spack/reporters/cdash.py b/lib/spack/spack/reporters/cdash.py
index 38ed96a448..d6cde25fe9 100644
--- a/lib/spack/spack/reporters/cdash.py
+++ b/lib/spack/spack/reporters/cdash.py
@@ -16,11 +16,13 @@
 from six.moves.urllib.request import build_opener, HTTPHandler, Request
 from six.moves.urllib.parse import urlencode
 
+from llnl.util.filesystem import working_dir
 import spack.build_environment
 import spack.fetch_strategy
 import spack.package
 from spack.reporter import Reporter
 from spack.util.crypto import checksum
+from spack.util.executable import which
 from spack.util.log_parse import parse_log_events
 
 __all__ = ['CDash']
@@ -37,6 +39,7 @@
 
 # Initialize data structures common to each phase's report.
 cdash_phases = set(map_phases_to_cdash.values())
+cdash_phases.add('update')
 
 
 class CDash(Reporter):
@@ -65,6 +68,10 @@ def __init__(self, args):
         self.buildstamp = time.strftime(buildstamp_format,
                                         time.localtime(self.starttime))
         self.buildId = None
+        self.revision = ''
+        git = which('git')
+        with working_dir(spack.paths.spack_root):
+            self.revision = git('rev-parse', 'HEAD', output=str).strip()
 
     def build_report(self, filename, report_data):
         self.initialize_report(filename, report_data)
@@ -105,6 +112,8 @@ def build_report(self, filename, report_data):
                             report_data[cdash_phase]['log'] += \
                                 xml.sax.saxutils.escape(line) + "\n"
 
+        phases_encountered.append('update')
+
         # Move the build phase to the front of the list if it occurred.
         # This supports older versions of CDash that expect this phase
         # to be reported before all others.
@@ -147,15 +156,21 @@ def clean_log_event(event):
                     report_data[phase]['warnings'].append(
                         clean_log_event(warning))
 
+            if phase == 'update':
+                report_data[phase]['revision'] = self.revision
+
             # Write the report.
             report_name = phase.capitalize() + ".xml"
             phase_report = os.path.join(filename, report_name)
 
             with codecs.open(phase_report, 'w', 'utf-8') as f:
                 env = spack.tengine.make_environment()
-                site_template = os.path.join(self.template_dir, 'Site.xml')
-                t = env.get_template(site_template)
-                f.write(t.render(report_data))
+                if phase != 'update':
+                    # Update.xml stores site information differently
+                    # than the rest of the CTest XML files.
+                    site_template = os.path.join(self.template_dir, 'Site.xml')
+                    t = env.get_template(site_template)
+                    f.write(t.render(report_data))
 
                 phase_template = os.path.join(self.template_dir, report_name)
                 t = env.get_template(phase_template)
@@ -166,9 +181,11 @@ def clean_log_event(event):
     def concretization_report(self, filename, msg):
         report_data = {}
         self.initialize_report(filename, report_data)
-        report_data['starttime'] = self.starttime
-        report_data['endtime'] = self.starttime
-        report_data['msg'] = msg
+        report_data['update'] = {}
+        report_data['update']['starttime'] = self.starttime
+        report_data['update']['endtime'] = self.starttime
+        report_data['update']['revision'] = self.revision
+        report_data['update']['log'] = msg
 
         env = spack.tengine.make_environment()
         update_template = os.path.join(self.template_dir, 'Update.xml')
diff --git a/share/spack/templates/reports/cdash/Update.xml b/share/spack/templates/reports/cdash/Update.xml
index 2ef5de347d..97d5ed81f3 100644
--- a/share/spack/templates/reports/cdash/Update.xml
+++ b/share/spack/templates/reports/cdash/Update.xml
@@ -3,9 +3,12 @@
   <Site>{{ site }}</Site>
   <BuildName>{{ buildname }}</BuildName>
   <BuildStamp>{{ buildstamp }}</BuildStamp>
-  <StartTime>{{ starttime }}</StartTime>
-  <EndTime>{{ endtime }}</EndTime>
-{% if msg %}
-  <UpdateReturnStatus>{{ msg }}</UpdateReturnStatus>
+  <StartTime>{{ update.starttime }}</StartTime>
+  <UpdateCommand></UpdateCommand>
+  <UpdateType>GIT</UpdateType>
+  <Revision>{{ update.revision }}</Revision>
+  <EndTime>{{ update.endtime }}</EndTime>
+{% if update.log %}
+  <UpdateReturnStatus>{{ update.log }}</UpdateReturnStatus>
 {% endif %}
 </Update>
-- 
GitLab