PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/org/bitbucket/connectors/jetbrains/RepositoryInfo.java

https://bitbucket.org/atlassian/jetbrains-bitbucket-connector/
Java | 115 lines | 94 code | 21 blank | 0 comment | 25 complexity | e676503ebd077c1c48f3ab7ce219195a MD5 | raw file
  1. package org.bitbucket.connectors.jetbrains;
  2. import org.apache.commons.httpclient.URIException;
  3. import org.apache.commons.httpclient.util.URIUtil;
  4. import org.jdom.Element;
  5. public class RepositoryInfo implements Comparable<RepositoryInfo> {
  6. private Element myRepositoryElement;
  7. private String myCheckoutUrl;
  8. RepositoryInfo(Element repositoryElement) {
  9. myRepositoryElement = repositoryElement;
  10. }
  11. public String getName() {
  12. return myRepositoryElement.getChildText("name");
  13. }
  14. public String getSlug() {
  15. return myRepositoryElement.getChildText("slug");
  16. }
  17. public String getOwner() {
  18. return myRepositoryElement.getChildText("owner");
  19. }
  20. public String isPrivate() {
  21. return myRepositoryElement.getChildText("is_private");
  22. }
  23. public String getCheckoutUrl() throws URIException {
  24. if (myCheckoutUrl == null) {
  25. myCheckoutUrl = calculateCheckoutUrl();
  26. }
  27. return myCheckoutUrl;
  28. }
  29. public boolean isGit() {
  30. return "git".equals(myRepositoryElement.getChild("scm").getText());
  31. }
  32. private String calculateCheckoutUrl() throws URIException {
  33. BitbucketSettings settings = BitbucketSettings.getInstance();
  34. boolean ssh = BitbucketUtil.sshEnabled(null, settings.getLogin(), settings.getPassword());
  35. return getCheckoutUrl(ssh, true);
  36. }
  37. public String getCheckoutUrl(boolean ssh, boolean addPassword) throws URIException {
  38. BitbucketSettings settings = BitbucketSettings.getInstance();
  39. String name = getSlug();
  40. if (isGit()) {
  41. name += ".git";
  42. }
  43. String owner = getOwner();
  44. if (ssh) {
  45. String user = isGit() ? "git" : "hg";
  46. return "ssh://" + user + "@" + BitbucketUtil.BITBUCKET_DN + "/" + owner + "/" + name;
  47. } else {
  48. String cred = URIUtil.encodeWithinAuthority(settings.getLogin()) + ":" + URIUtil.encodeWithinAuthority(settings.getPassword());
  49. return "https://" + cred + "@" + BitbucketUtil.BITBUCKET_DN + "/" + owner + "/" + name;
  50. }
  51. }
  52. public static String addPassword(String url, boolean git) {
  53. if (url == null) {
  54. return null;
  55. }
  56. int pos = url.indexOf("@");
  57. if (pos != -1 && !git) {
  58. int start = url.lastIndexOf("/", pos);
  59. if (start != -1) {
  60. String name = url.substring(start + 1, pos);
  61. if (name.indexOf(":") != -1) {
  62. return url;
  63. }
  64. }
  65. String password = BitbucketSettings.getInstance().getPassword();
  66. return url.substring(0, pos) + ":" + password + url.substring(pos);
  67. }
  68. return url;
  69. }
  70. public boolean isMy() {
  71. return BitbucketSettings.getInstance().getLogin().equals(getOwner());
  72. }
  73. public int compareTo(RepositoryInfo r) {
  74. if (r == this) {
  75. return 0;
  76. }
  77. if (r == null) {
  78. return -1;
  79. }
  80. if (isMy() == r.isMy()) {
  81. int res = getOwner().compareTo(r.getOwner());
  82. if (res != 0) {
  83. return res;
  84. }
  85. return getName().compareTo(r.getName());
  86. } else {
  87. return isMy() ? -1 : 1;
  88. }
  89. }
  90. public boolean isCreating() {
  91. Element state = myRepositoryElement.getChild("state");
  92. return state != null && "creating".equals(state.getText());
  93. }
  94. }