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 URL url;
    private final double version;

    @DataBoundConstructor
    public GitLab(String url, String version) throws MalformedURLException {
        this.version = Double.valueOf(version);
        this.url = normalizeToEndWithSlash(new URL(url));
    }

    public URL getUrl() {
        return url;
    }

    public double getVersion() {
        return version;
    }

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

        return new URL(url, 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(url, 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 {
            final String spec = "/" + path.getChangeSet().getId() + "/tree/" + path.getPath();
            return new URL(url, url.getPath() + spec);
        }
    }

    @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.bindParameters(GitLab.class, "Gitlab.");
        }
    }

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

        return "commits/";
    } 

}

@@ -2302,20 +2302,74 @@
     
-final 
 String
+ spec;%0A            if(getVersion() %3E= 5.2) %7B%0A               
  spe
@@ -2373,16 +2373,20 @@
 spec = %22
+blob
 /%22 + pat
@@ -2411,16 +2411,112 @@
 etId() +
+ %22/%22 + path.getPath();%0A            %7D else %7B%0A                spec = path.getChangeSet().getId() +
  %22/tree/
@@ -2527,32 +2527,46 @@
 path.getPath();%0A
+            %7D%0A
             retu

Adapt file link for GitLab 5.2