/
GithubWeb.java
112 lines (100 loc) · 3.62 KB
/
GithubWeb.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package hudson.plugins.git.browser;
import hudson.EnvVars;
import hudson.Extension;
import hudson.model.AbstractProject;
import hudson.model.Descriptor;
import hudson.model.EnvironmentContributor;
import hudson.model.ItemGroup;
import hudson.model.Job;
import hudson.model.TaskListener;
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.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
/**
* Git Browser URLs
*/
public class GithubWeb extends GitRepositoryBrowser {
private static final long serialVersionUID = 1L;
@DataBoundConstructor
public GithubWeb(String repoUrl) {
super(repoUrl);
}
@Override
public URL getChangeSetLink(GitChangeSet changeSet) throws IOException {
URL url = getUrl();
return new URL(url, url.getPath()+"commit/" + changeSet.getId().toString());
}
/**
* Creates a link to the file diff.
* http://[GitHib URL]/commit/573670a3bb1f3b939e87f1dee3e99b6bfe281fcb#diff-N
*
* @param path affected file path
* @return diff link
* @throws IOException
*/
@Override
public URL getDiffLink(Path path) throws IOException {
if (path.getEditType() != EditType.EDIT || path.getSrc() == null || path.getDst() == null
|| path.getChangeSet().getParentCommit() == null) {
return null;
}
return getDiffLinkRegardlessOfEditType(path);
}
/**
* Return a diff link regardless of the edit type by appending the index of the pathname in the changeset.
*
* @param path
* @return url for differences
* @throws IOException
*/
private URL getDiffLinkRegardlessOfEditType(Path path) throws IOException {
final GitChangeSet changeSet = path.getChangeSet();
final ArrayList<String> affectedPaths = new ArrayList<String>(changeSet.getAffectedPaths());
// Github seems to sort the output alphabetically by the path.
Collections.sort(affectedPaths);
final String pathAsString = path.getPath();
final int i = Collections.binarySearch(affectedPaths, pathAsString);
assert i >= 0;
return new URL(getChangeSetLink(changeSet), "#diff-" + String.valueOf(i));
}
/**
* Creates a link to the file.
* http://[GitHib URL]/blob/573670a3bb1f3b939e87f1dee3e99b6bfe281fcb/src/main/java/hudson/plugins/git/browser/GithubWeb.java
* Github seems to have no URL for deleted files, so just return
* a difflink instead.
*
* @param path file
* @return file link
* @throws IOException
*/
@Override
public URL getFileLink(Path path) throws IOException {
if (path.getEditType().equals(EditType.DELETE)) {
return getDiffLinkRegardlessOfEditType(path);
} else {
final String spec = "blob/" + path.getChangeSet().getId() + "/" + path.getPath();
URL url = getUrl();
return new URL(url, url.getPath() + spec);
}
}
@Extension
public static class GithubWebDescriptor extends Descriptor<RepositoryBrowser<?>> {
public String getDisplayName() {
return "githubweb";
}
@Override
public GithubWeb newInstance(StaplerRequest req, JSONObject jsonObject) throws FormException {
return req.bindJSON(GithubWeb.class, jsonObject);
}
}
}