PageRenderTime 51ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/atlassian/jira/plugins/bitbucket/spi/bitbucket/BitbucketChangesetFactory.java

https://bitbucket.org/atlassian/jira-bitbucket-connector/
Java | 99 lines | 76 code | 10 blank | 13 comment | 2 complexity | 6c0638dc2f1b787d1acd45cc99e2833b MD5 | raw file
  1. package com.atlassian.jira.plugins.bitbucket.spi.bitbucket;
  2. import com.atlassian.jira.plugins.bitbucket.api.Changeset;
  3. import com.atlassian.jira.plugins.bitbucket.api.ChangesetFile;
  4. import com.atlassian.jira.plugins.bitbucket.api.SourceControlException;
  5. import com.atlassian.jira.plugins.bitbucket.spi.DefaultBitbucketChangeset;
  6. import com.atlassian.jira.util.json.JSONArray;
  7. import com.atlassian.jira.util.json.JSONException;
  8. import com.atlassian.jira.util.json.JSONObject;
  9. import java.text.ParseException;
  10. import java.text.SimpleDateFormat;
  11. import java.util.ArrayList;
  12. import java.util.Date;
  13. import java.util.List;
  14. import java.util.TimeZone;
  15. /**
  16. * Factory for {@link Changeset} implementations
  17. */
  18. public class BitbucketChangesetFactory
  19. {
  20. private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  21. static
  22. {
  23. DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("utc"));
  24. }
  25. /**
  26. * Parse the json object as a bitbucket changeset
  27. *
  28. * @param owner the owner of the repository this changeset belongs to
  29. * @param slug the slug of the repository this changeset belons to
  30. * @param json the json object describing the change
  31. * @return the parsed {@link Changeset}
  32. */
  33. public static Changeset parse(int repositoryId, JSONObject baseJson, JSONArray filesJson)
  34. {
  35. try
  36. {
  37. List<ChangesetFile> files = fileList(filesJson);
  38. return new DefaultBitbucketChangeset(
  39. repositoryId,
  40. baseJson.getString("node"),
  41. baseJson.getString("raw_author"),
  42. baseJson.getString("author"),
  43. parseDate(baseJson.getString("utctimestamp")),
  44. baseJson.getString("raw_node"),
  45. baseJson.getString("branch"), // TODO if null, set to "default"?
  46. baseJson.getString("message"),
  47. stringList(baseJson.getJSONArray("parents")),
  48. files,
  49. files.size()
  50. );
  51. } catch (JSONException e)
  52. {
  53. throw new SourceControlException("Invalid json object: " + baseJson.toString(), e);
  54. }
  55. }
  56. public static Date parseDate(String dateStr)
  57. {
  58. // example: 2011-05-26 10:54:41+xx:xx (timezone is ignored because we parse utc timestamp date with utc parser)
  59. try
  60. {
  61. return DATE_FORMAT.parse(dateStr);
  62. } catch (ParseException e)
  63. {
  64. throw new SourceControlException("Could not parse date string from JSON.", e);
  65. }
  66. }
  67. public static String getDateString(Date datetime)
  68. {
  69. // example: 2011-05-26 10:54:41
  70. return DATE_FORMAT.format(datetime);
  71. }
  72. private static List<String> stringList(JSONArray parents) throws JSONException
  73. {
  74. List<String> list = new ArrayList<String>();
  75. for (int i = 0; i < parents.length(); i++)
  76. list.add((String) parents.get(i));
  77. return list;
  78. }
  79. private static List<ChangesetFile> fileList(JSONArray parents) throws JSONException
  80. {
  81. List<ChangesetFile> list = new ArrayList<ChangesetFile>();
  82. for (int i = 0; i < parents.length(); i++)
  83. list.add(BitbucketChangesetFileFactory.parse((JSONObject) parents.get(i)));
  84. return list;
  85. }
  86. private BitbucketChangesetFactory()
  87. {
  88. }
  89. }