From a17bd77b7125b374af0c4aaaa2cee0298cf8d7b8 Mon Sep 17 00:00:00 2001 From: novokrest Date: Mon, 11 Jun 2018 07:51:22 +0300 Subject: [PATCH] jira: Build request's data as dictionary to create/edit issues. Build data in `make_create_json` and `make_edit_json` methods as dictionary to send as JSON payload. --- .../bots/jira/fixtures/test_create.json | 12 +++- .../bots/jira/fixtures/test_create_error.json | 21 ++++++- .../bots/jira/fixtures/test_edit.json | 6 +- .../bots/jira/fixtures/test_edit_error.json | 21 ++++++- zulip_bots/zulip_bots/bots/jira/jira.py | 60 +++++++++---------- 5 files changed, 83 insertions(+), 37 deletions(-) diff --git a/zulip_bots/zulip_bots/bots/jira/fixtures/test_create.json b/zulip_bots/zulip_bots/bots/jira/fixtures/test_create.json index c551e20..12e60bb 100644 --- a/zulip_bots/zulip_bots/bots/jira/fixtures/test_create.json +++ b/zulip_bots/zulip_bots/bots/jira/fixtures/test_create.json @@ -6,7 +6,17 @@ "Authorization": "Basic ZXhhbXBsZUBleGFtcGxlLmNvbTpxd2VydHkhMTIz", "Content-type": "application/json" }, - "data": "{\"fields\": {\"summary\": \"Testing\",\"project\": { \"key\": \"TEST\" },\"issuetype\": { \"name\": \"Task\" }}}" + "data": { + "fields": { + "summary": "Testing", + "project": { + "key": "TEST" + }, + "issuetype": { + "name": "Task" + } + } + } }, "response": { "key": "TEST-16" diff --git a/zulip_bots/zulip_bots/bots/jira/fixtures/test_create_error.json b/zulip_bots/zulip_bots/bots/jira/fixtures/test_create_error.json index 5d2af2f..d49df11 100644 --- a/zulip_bots/zulip_bots/bots/jira/fixtures/test_create_error.json +++ b/zulip_bots/zulip_bots/bots/jira/fixtures/test_create_error.json @@ -6,7 +6,26 @@ "Authorization": "Basic ZXhhbXBsZUBleGFtcGxlLmNvbTpxd2VydHkhMTIz", "Content-type": "application/json" }, - "data": "{\"fields\": {\"summary\": \"Testing\",\"project\": { \"key\": \"TEST\" },\"issuetype\": { \"name\": \"Task\" },\"description\": \"This is a test description\",\"assignee\": { \"name\": \"testuser\" },\"priority\": { \"name\": \"Medium\" },\"labels\": [\"issues\",\"testing\"],\"duedate\": \"2018-06-11\"}}" + "data": { + "fields": { + "summary": "Testing", + "project": { + "key": "TEST" + }, + "issuetype": { + "name": "Task" + } + ,"description": "This is a test description", + "assignee": { + "name": "testuser" + }, + "priority": { + "name": "Medium" + }, + "labels": ["issues","testing"], + "duedate": "2018-06-11" + } + } }, "response": { "errors": { diff --git a/zulip_bots/zulip_bots/bots/jira/fixtures/test_edit.json b/zulip_bots/zulip_bots/bots/jira/fixtures/test_edit.json index 33819d8..0ca6656 100644 --- a/zulip_bots/zulip_bots/bots/jira/fixtures/test_edit.json +++ b/zulip_bots/zulip_bots/bots/jira/fixtures/test_edit.json @@ -6,7 +6,11 @@ "Authorization": "Basic ZXhhbXBsZUBleGFtcGxlLmNvbTpxd2VydHkhMTIz", "Content-type": "application/json" }, - "data": "{\"fields\": {\"description\": \"description\"}}" + "data": { + "fields": { + "description": "description" + } + } }, "response": { }, diff --git a/zulip_bots/zulip_bots/bots/jira/fixtures/test_edit_error.json b/zulip_bots/zulip_bots/bots/jira/fixtures/test_edit_error.json index 5e4b8cf..9b8c58e 100644 --- a/zulip_bots/zulip_bots/bots/jira/fixtures/test_edit_error.json +++ b/zulip_bots/zulip_bots/bots/jira/fixtures/test_edit_error.json @@ -6,7 +6,26 @@ "Authorization": "Basic ZXhhbXBsZUBleGFtcGxlLmNvbTpxd2VydHkhMTIz", "Content-type": "application/json" }, - "data": "{\"fields\": {\"summary\": \"Change the summary\",\"project\": { \"key\": \"TEST\" },\"issuetype\": { \"name\": \"Bug\" },\"description\": \"This is a test description\",\"assignee\": { \"name\": \"testuser\" },\"priority\": { \"name\": \"Low\" },\"labels\": [\"issues\",\"testing\"],\"duedate\": \"2018-06-11\"}}" + "data": { + "fields": { + "summary": "Change the summary", + "project": { + "key": "TEST" + }, + "issuetype": { + "name": "Bug" + }, + "description": "This is a test description", + "assignee": { + "name": "testuser" + }, + "priority": { + "name": "Low" + }, + "labels": ["issues","testing"], + "duedate": "2018-06-11" + } + } }, "response": { "errors": { diff --git a/zulip_bots/zulip_bots/bots/jira/jira.py b/zulip_bots/zulip_bots/bots/jira/jira.py index 13c3c90..1a8438b 100644 --- a/zulip_bots/zulip_bots/bots/jira/jira.py +++ b/zulip_bots/zulip_bots/bots/jira/jira.py @@ -242,7 +242,7 @@ def make_jira_auth(username: str, password: str) -> str: def make_create_json(summary: str, project_key: str, type_name: str, description: Optional[str], assignee: Optional[str], priority_name: Optional[str], labels: Optional[str], - due_date: Optional[str]) -> str: + due_date: Optional[str]) -> Any: '''Makes a JSON string for the Jira REST API editing endpoint based on fields that could be edited. @@ -257,35 +257,34 @@ def make_create_json(summary: str, project_key: str, type_name: str, comma-spaces. - due_date (optional): The Jira due date property. ''' - json = '{"fields": {' - - json += '"summary": "' + summary + '",' - json += '"project": { "key": "' + project_key + '" },' - json += '"issuetype": { "name": "' + type_name + '" },' + json_fields = { + 'summary': summary, + 'project': { + 'key': project_key + }, + 'issuetype': { + 'name': type_name + } + } if description: - json += '"description": "' + description + '",' + json_fields['description'] = description if assignee: - json += '"assignee": { "name": "' + assignee + '" },' + json_fields['assignee'] = {'name': assignee} if priority_name: - json += '"priority": { "name": "' + priority_name + '" },' + json_fields['priority'] = {'name': priority_name} if labels: - labels_list = labels.split(', ') - labels_json = '"' + '","'.join(labels_list) + '"' - json += '"labels": [' + labels_json + '],' + json_fields['labels'] = labels.split(', ') if due_date: - json += '"duedate": "' + due_date + '",' + json_fields['duedate'] = due_date - # Remove the trailing comma. - json = json[:-1] - - json += '}}' + json = {'fields': json_fields} return json def make_edit_json(summary: Optional[str], project_key: Optional[str], type_name: Optional[str], description: Optional[str], assignee: Optional[str], priority_name: Optional[str], - labels: Optional[str], due_date: Optional[str]) -> str: + labels: Optional[str], due_date: Optional[str]) -> Any: '''Makes a JSON string for the Jira REST API editing endpoint based on fields that could be edited. @@ -300,31 +299,26 @@ def make_edit_json(summary: Optional[str], project_key: Optional[str], comma-spaces. - due_date (optional): The Jira due date property. ''' - json = '{"fields": {' + json_fields = {} if summary: - json += '"summary": "' + summary + '",' + json_fields['summary'] = summary if project_key: - json += '"project": { "key": "' + project_key + '" },' + json_fields['project'] = {'key': project_key} if type_name: - json += '"issuetype": { "name": "' + type_name + '" },' + json_fields['issuetype'] = {'name': type_name} if description: - json += '"description": "' + description + '",' + json_fields['description'] = description if assignee: - json += '"assignee": { "name": "' + assignee + '" },' + json_fields['assignee'] = {'name': assignee} if priority_name: - json += '"priority": { "name": "' + priority_name + '" },' + json_fields['priority'] = {'name': priority_name} if labels: - labels_list = labels.split(', ') - labels_json = '"' + '","'.join(labels_list) + '"' - json += '"labels": [' + labels_json + '],' + json_fields['labels'] = labels.split(', ') if due_date: - json += '"duedate": "' + due_date + '",' + json_fields['duedate'] = due_date - # Remove the trailing comma. - json = json[:-1] - - json += '}}' + json = {'fields': json_fields} return json