package hudson.plugins.git.browser;

import hudson.Extension;
import hudson.model.Descriptor;
import hudson.plugins.git.GitChangeSet;
import hudson.plugins.git.GitChangeSet.Path;
import hudson.scm.EditType;
import hudson.scm.RepositoryBrowser;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Git Browser for GitLab
 */
public class GitLab extends GitRepositoryBrowser {

    private static final long serialVersionUID = 1L;

    private final double version;

    @DataBoundConstructor
    public GitLab(String repoUrl, String version) {
        super(repoUrl);
        this.version = Double.valueOf(version);
    }

    public double getVersion() {
        return version;
    }

    /**
     * Creates a link to the changeset
     *
     * https://[GitLab URL]/commits/a9182a07750c9a0dfd89a8461adf72ef5ef0885b
     *
     * @return diff link
     * @throws IOException
     */
    @Override
    public URL getChangeSetLink(GitChangeSet changeSet) throws IOException {
        String  commitPrefix;

        return new URL(getUrl(), calculatePrefix() + changeSet.getId().toString());
    }

    /**
     * Creates a link to the commit diff.
     * 
     * https://[GitLab URL]/commits/a9182a07750c9a0dfd89a8461adf72ef5ef0885b#[path to file]
     * 
     * @param path
     * @return diff link
     * @throws IOException
     */
    @Override
    public URL getDiffLink(Path path) throws IOException {
        final GitChangeSet changeSet = path.getChangeSet();
        return new URL(getUrl(), calculatePrefix() + changeSet.getId().toString() + "#" + path.getPath());
    }

    /**
     * Creates a link to the file.
     * https://[GitLab URL]/a9182a07750c9a0dfd89a8461adf72ef5ef0885b/tree/pom.xml
     * 
     * @param path
     * @return file link
     * @throws IOException
     */
    @Override
    public URL getFileLink(Path path) throws IOException {
        if (path.getEditType().equals(EditType.DELETE)) {
            return getDiffLink(path);
        } else {
            if(getVersion() >= 5.1) {
                return new URL(getUrl(), "blob/" + path.getChangeSet().getId() + "/" + path.getPath());
            } else {
                return new URL(getUrl(), path.getChangeSet().getId() + "/tree/" + path.getPath());
            }
        }
    }

    @Extension
    public static class GitLabDescriptor extends Descriptor<RepositoryBrowser<?>> {
        public String getDisplayName() {
            return "gitlab";
        }

        @Override
        public GitLab newInstance(StaplerRequest req, JSONObject jsonObject) throws FormException {
            return req.bindJSON(GitLab.class, jsonObject);
        }
    }

    private String calculatePrefix() {
        if(getVersion() >= 3){
            return "commit/";
        }

        return "commits/";
    } 

}

@@ -2167,18 +2167,17 @@
 rsion() 
-%3E=
+%3C
  5.1) %7B%0A
@@ -2220,18 +2220,8 @@
 l(),
- %22blob/%22 +
  pat
@@ -2249,16 +2249,21 @@
 d() + %22/
+tree/
 %22 + path
@@ -2328,32 +2328,42 @@
 ew URL(getUrl(),
+ %22blob/%22 +
  path.getChangeS
@@ -2371,37 +2371,32 @@
 t().getId() + %22/
-tree/
 %22 + path.getPath
@@ -2872,13 +2872,13 @@
 n() 
-%3E=
+%3C
  3)
+ 
 %7B%0A  
@@ -2901,16 +2901,17 @@
  %22commit
+s
 /%22;%0A    
@@ -2915,26 +2915,33 @@
        %7D
-%0A%0A
+ else %7B%0A 
         
 return %22
@@ -2924,32 +2924,35 @@
 else %7B%0A         
+   
 return %22commits/
@@ -2945,27 +2945,36 @@
 turn %22commit
-s
 /%22;%0A
+        %7D%0A
     %7D %0A%0A%7D%0A

reorder version checks in code