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