mock.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. from fixtures.integrations import MockService
  2. from fixtures.integrations.jira.stub_client import StubJiraApiClient
  3. DEFAULT_PROJECT_ID = "10000"
  4. class MockJira(StubJiraApiClient, MockService):
  5. def get_projects_list(self):
  6. """
  7. List all projects in the Jira data format.
  8. :return: list of project objects
  9. """
  10. return [
  11. {
  12. "self": f"http://www.example.com/jira/rest/api/2/project/{project_name}",
  13. "id": project_name,
  14. "key": project_name,
  15. "name": project_name,
  16. "projectCategory": {
  17. "self": f"http://www.example.com/jira/rest/api/2/projectCategory/{project_name}",
  18. "id": project_name,
  19. "name": project_name,
  20. "description": project_name,
  21. },
  22. "simplified": False,
  23. }
  24. for project_name in self._get_project_names() + [DEFAULT_PROJECT_ID]
  25. ]
  26. def set_createmeta(self, project, createmeta):
  27. """
  28. This special method lets you seed the stub data with your own metadata.
  29. # TODO validate createmeta
  30. :param project:
  31. :param createmeta:
  32. :return:
  33. """
  34. return self._set_data(project, "createmeta", createmeta)
  35. def get_create_meta_for_project(self, project):
  36. """
  37. Get the Jira "createmeta" for a project.
  38. :param project: String name of a Jira project
  39. :return: Object containing the "createmeta" of the project.
  40. """
  41. self._throw_if_broken()
  42. createmeta = self._get_data(project, "createmeta")
  43. if createmeta:
  44. return createmeta
  45. # Fallback to stub data
  46. return super().get_create_meta_for_project(project)
  47. def create_issue(self, raw_form_data):
  48. """
  49. Create a new Jira issue. Currently overwrites if the issue already exists.
  50. :param raw_form_data: Object containing issue parameters
  51. :return: Object containing the newly created ticket's "key" as a string.
  52. """
  53. self._throw_if_broken()
  54. project = raw_form_data.get("project", {}).get("id")
  55. ticket_key = self._get_new_ticket_name(project)
  56. self._set_data(project, ticket_key, {"fields": raw_form_data})
  57. return {"key": ticket_key}
  58. def get_issue(self, issue_key):
  59. """
  60. Get a saved issue from Jira.
  61. :param issue_key: string
  62. :return: Object containing Jira Issue data
  63. """
  64. project = issue_key.split("-")[0]
  65. data = self._get_data(project, issue_key)
  66. if not data:
  67. return None
  68. data.update(
  69. {
  70. "id": issue_key,
  71. "key": issue_key,
  72. }
  73. )
  74. return data