Alexander Dudko avatar Alexander Dudko committed fcc3274

Run-time variables added

Comments (0)

Files changed (2)

jirabulkloader/task_extractor.py

     def __init__(self, jira_url, username, password, options, dry_run = False):
         self.h5_tasks_to_link_to_h4_task = [] # will be used to link h5-tasks to the root task
         self.tmpl_vars = {} # template variables dict
-        self.tmpl_json = {}
+        self.tmpl_json = {} # template json structures, for example {"project": {"key": "KEY"}}
+        self.rt_vars = {} # run-time variables (issueIDs)
 
         self.username = username
         self.password = password
             task_json['tmpl_ext'] = self.tmpl_json.copy()
         if match.group(5):
             task_json.setdefault('tmpl_ext', {}).update(self._validated_json_loads(match.group(5)))
+        if match.group(6):
+            task_json['rt_ext'] = match.group(6)
         return task_json
 
     def _add_task_description(self, task_json, input_line):
 
         for line in task_list:
             if 'markup' in line:
+                if ('description' in line): 
+                    line['description'] = self._replace_realtime_vars(line['description'])
                 if line['markup'] == 'h5.':
                     if 'h5_task_key' in vars(): # if new h5 task begins
                         h5_summary_list = self._h5_task_completion(h5_task_key, h5_task_caption, h5_task_desc, h5_task_ext)
         summary_list = [caption]
         if ext:
             desc = '\n'.join([desc, ext]) if desc else ext
-            self.update_issue_desc(key, desc)
+            self.update_issue_desc(key, self._replace_realtime_vars(desc))
         if desc:
             summary_list.append(desc)
         return '\n'.join(summary_list)
 #####################################################################################
 
     def create_issue(self, issue):
+        if ('description' in issue) and self.rt_vars:
+            issue['description'] = self._replace_realtime_vars(issue['description'])
+        issue_id = self._create_issue_http(issue)
+        if issue.has_key('rt_ext'):
+            self._add_runtime_variable(issue['rt_ext'], issue_id)
+        return issue_id
+
+    def _add_runtime_variable(self, name, value):
+        self.rt_vars.update({name:value})
+        self.rt_vars_regex = re.compile("\$(" + "|".join(map(re.escape, sorted(self.rt_vars.keys( ), reverse=True))) + ")")
+
+    def _replace_realtime_vars(self, desc):
+        return self.rt_vars_regex.sub(lambda match: self.rt_vars[match.group(1)], desc) if self.rt_vars else desc
+
+    def _create_issue_http(self, issue):
         """
+        Invoke JIRA HTTP API to create issue
         """
 
         if not self.dry_run:

jirabulkloader/test/test_task_extractor.py

     MagicMock.assert_called_once_with(self.te.update_issue_desc, 'DRY-RUN-XXXX', '# sub-task (DRY-RUN-XXXX)')
 
   def test_create_tasks_Tasks_with_text(self):
-    self.te.create_issue = MagicMock()
-    self.te.create_issue.return_value = 'DRY-RUN-XXXX'
+    self.te.create_issue_http = MagicMock()
+    self.te.create_issue_http.return_value = 'DRY-RUN-XXXX'
     input_list = [{'assignee': 'assignee', 'markup': 'h4.', 'summary': 'h4 task'}, \
         {'assignee': 'assignee', 'markup': 'h5.', 'summary': 'h5 task', 'description':'h5 desc'}, {'text':'text line'}, \
         {'assignee': 'assignee', 'markup': '#', 'summary': 'sub-task'}]
   def test_load_recognize_run_time_variables(self):
     input_text = """
 [VAR1=1]
-h5. h5 task *assignee* [TASK_KEY=$?]
+h5. h5 task *assignee* [TASK_KEY]
 =line1 description
 """
     expected_result = [{'assignee': 'assignee', 'markup': 'h5.', 'summary': 'h5 task', 'description':'line1 description', 'rt_ext':'TASK_KEY'}]
     self.assertEquals(expected_result, self.te.load(input_text))
     self.assertEquals({'VAR1':'1'}, self.te.tmpl_vars)
 
+  def test_create_issue_add_rt_var(self):
+    test_issue_id = 'TEST-RUN-XXXX'
+    self.te._create_issue_http = MagicMock()
+    self.te._create_issue_http.return_value = test_issue_id
+    input_dict = {'assignee': 'assignee', 'markup': 'h5.', 'summary': 'h5 task', 'rt_ext':'TASK_KEY'}
+    self.assertEquals(test_issue_id, self.te.create_issue(input_dict))
+    self.assertEquals({'TASK_KEY':test_issue_id}, self.te.rt_vars)
+
+  def test_create_issue_replace_rt_variable(self):
+    input_text = """
+h5. h5 task1 *assignee* [TASK_KEY1]
+h5. h5 task2 *assignee* [TASK_KEY2]
+h5. h5 task3 *assignee*
+=description $TASK_KEY1
+# Sub-task *assignee*
+=description $TASK_KEY2
+"""
+    test_issue_id = 'TEST-RUN-XXXX'
+    self.te._create_issue_http = MagicMock()
+    self.te._create_issue_http.return_value = test_issue_id
+    self.te.update_issue_desc = MagicMock()
+
+    expected_result = [{'assignee': 'assignee', 'markup': 'h5.', 'summary': 'h5 task1', 'rt_ext':'TASK_KEY1'},
+            {'assignee': 'assignee', 'markup': 'h5.', 'summary': 'h5 task2', 'rt_ext':'TASK_KEY2'},
+        {'assignee': 'assignee', 'markup': 'h5.', 'summary': 'h5 task3', 'description':'description $TASK_KEY1'},
+        {'assignee': 'assignee', 'markup': '#', 'summary': 'Sub-task', 'description':'description $TASK_KEY2'}]
+    load_result = self.te.load(input_text)
+    self.assertEquals(expected_result, load_result)
+
+    expected_result_load = 'h5. h5 task1 (TEST-RUN-XXXX)\nh5. h5 task2 (TEST-RUN-XXXX)\nh5. h5 task3 (TEST-RUN-XXXX)\ndescription TEST-RUN-XXXX\n# Sub-task (TEST-RUN-XXXX)'
+    self.assertEquals(self.te.create_tasks(load_result), expected_result_load)
+    self.te.update_issue_desc.assert_called_once_with(test_issue_id, 'description TEST-RUN-XXXX\n# Sub-task (TEST-RUN-XXXX)')
+
 
 #if __name__ == "__main__":
 #      unittest.main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.