Skip to content
This repository was archived by the owner on Nov 14, 2023. It is now read-only.

Commit d28abfe

Browse files
authored
feat: Add option to replace values in remote url (#23)
* Add RemoteUrlReplacements feature and update defaultBranch
1 parent 1d75242 commit d28abfe

File tree

6 files changed

+46
-21
lines changed

6 files changed

+46
-21
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ The plugin is configurable _globally_ by creating a `sourcegraph-jetbrains.prope
3434

3535
```
3636
url = https://sourcegraph.example.com
37+
defaultBranch = example-branch
38+
remoteUrlReplacements = git.example.com, git-web.example.com
3739
```
3840

3941
You may also choose to configure it _per repository_ using a `.idea/sourcegraph.xml` file in your repository like so:
@@ -43,6 +45,8 @@ You may also choose to configure it _per repository_ using a `.idea/sourcegraph.
4345
<project version="4">
4446
<component name="Config">
4547
<option name="url" value="https://sourcegraph.example.com" />
48+
<option name="defaultBranch" value="example-branch" />
49+
<option name="remoteUrlReplacements" value="git.example.com, git-web.example.com" />
4650
</component>
4751
</project>
4852
```
@@ -78,6 +82,8 @@ Please file an issue: https://github.com/sourcegraph/sourcegraph-jetbrains/issue
7882

7983
#### v1.2.1
8084
- Added `Open In Sourcegraph` action to VCS History and Git Log to open a revision in the Sourcegraph diff view.
85+
- Added `defaultBranch` configuration option that allows opening files in a specific branch on Sourcegraph.
86+
- Added `remoteUrlReplacements` configuration option that allow users to replace specified values in the remote url with new strings.
8187

8288
#### v1.2.0
8389

src/main/java/Config.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ public String getDefaultBranch() {
2323
return defaultBranch;
2424
}
2525

26+
public String remoteUrlReplacements;
27+
28+
public String getRemoteUrlReplacements() {
29+
return remoteUrlReplacements;
30+
}
31+
2632
@Nullable
2733
@Override
2834
public Config getState() {
@@ -33,6 +39,7 @@ public Config getState() {
3339
public void loadState(@NotNull Config config) {
3440
this.url = config.url;
3541
this.defaultBranch = config.defaultBranch;
42+
this.remoteUrlReplacements = config.remoteUrlReplacements;
3643
}
3744

3845
@Nullable static Config getInstance(Project project) {

src/main/java/FileAction.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import java.io.UnsupportedEncodingException;
1717
import java.net.URI;
1818
import java.net.URLEncoder;
19+
import java.util.Arrays;
20+
import java.util.List;
1921

2022
public abstract class FileAction extends AnAction {
2123

@@ -45,7 +47,7 @@ public void actionPerformed(AnActionEvent e) {
4547
SelectionModel sel = editor.getSelectionModel();
4648

4749
// Get repo information.
48-
RepoInfo repoInfo = Util.repoInfo(currentFile.getPath());
50+
RepoInfo repoInfo = Util.repoInfo(currentFile.getPath(), project);
4951
if (repoInfo.remoteURL == "") {
5052
return;
5153
}
@@ -54,19 +56,13 @@ public void actionPerformed(AnActionEvent e) {
5456
String productName = ApplicationInfo.getInstance().getVersionName();
5557
String productVersion = ApplicationInfo.getInstance().getFullVersion();
5658
String uri;
57-
String branch = repoInfo.branch;
58-
59-
// set defaultBranch only if not config is not null
60-
if(Util.setDefaultBranch(project)!=null) {
61-
branch = Util.setDefaultBranch(project);
62-
};
6359

6460
try {
6561
LogicalPosition start = editor.visualToLogicalPosition(sel.getSelectionStartPosition());
6662
LogicalPosition end = editor.visualToLogicalPosition(sel.getSelectionEndPosition());
6763
uri = Util.sourcegraphURL(project)+"-/editor"
6864
+ "?remote_url=" + URLEncoder.encode(repoInfo.remoteURL, "UTF-8")
69-
+ "&branch=" + URLEncoder.encode(branch, "UTF-8")
65+
+ "&branch=" + URLEncoder.encode(repoInfo.branch, "UTF-8")
7066
+ "&file=" + URLEncoder.encode(repoInfo.fileRel, "UTF-8")
7167
+ "&editor=" + URLEncoder.encode("JetBrains", "UTF-8")
7268
+ "&version=" + URLEncoder.encode(Util.VERSION, "UTF-8")

src/main/java/OpenRevisionAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void actionPerformed(@NotNull AnActionEvent e) {
6161
try {
6262
String productName = ApplicationInfo.getInstance().getVersionName();
6363
String productVersion = ApplicationInfo.getInstance().getFullVersion();
64-
RepoInfo repoInfo = Util.repoInfo(context.getProject().getProjectFilePath());
64+
RepoInfo repoInfo = Util.repoInfo(context.getProject().getProjectFilePath(), context.getProject());
6565

6666
CommitViewUriBuilder builder = new CommitViewUriBuilder();
6767
URI uri = builder.build(Util.sourcegraphURL(context.getProject()), context.getRevisionNumber(), repoInfo, productName, productVersion);

src/main/java/SearchActionBase.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void actionPerformedMode(AnActionEvent e, String mode) {
4040
SelectionModel sel = editor.getSelectionModel();
4141

4242
// Get repo information.
43-
RepoInfo repoInfo = Util.repoInfo(currentFile.getPath());
43+
RepoInfo repoInfo = Util.repoInfo(currentFile.getPath(), project);
4444

4545
String q = sel.getSelectedText();
4646
if (q == null || q.equals("")) {
@@ -51,12 +51,6 @@ public void actionPerformedMode(AnActionEvent e, String mode) {
5151
String uri;
5252
String productName = ApplicationInfo.getInstance().getVersionName();
5353
String productVersion = ApplicationInfo.getInstance().getFullVersion();
54-
String branch = repoInfo.branch;
55-
56-
// set defaultBranch only if not config is not null
57-
if(Util.setDefaultBranch(project)!=null) {
58-
branch = Util.setDefaultBranch(project);
59-
};
6054

6155
try {
6256
uri = Util.sourcegraphURL(project)+"-/editor"
@@ -68,7 +62,7 @@ public void actionPerformedMode(AnActionEvent e, String mode) {
6862

6963
if (mode == "search.repository") {
7064
uri += "&search_remote_url=" + URLEncoder.encode(repoInfo.remoteURL, "UTF-8")
71-
+ "&search_branch=" + URLEncoder.encode(branch, "UTF-8");
65+
+ "&search_branch=" + URLEncoder.encode(repoInfo.branch, "UTF-8");
7266
}
7367

7468
} catch (UnsupportedEncodingException err) {

src/main/java/Util.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ public static String setDefaultBranch(Project project) {
6868
return defaultBranch;
6969
}
7070

71+
// get remoteUrlReplacements configuration option
72+
public static String setRemoteUrlReplacements(Project project) {
73+
String replacements = Config.getInstance(project).getRemoteUrlReplacements();
74+
if (replacements == null || replacements.length() == 0) {
75+
Properties props = readProps();
76+
replacements = props.getProperty("remoteUrlReplacements", null);
77+
}
78+
return replacements;
79+
}
80+
7181
// readProps tries to read the $HOME/sourcegraph-jetbrains.properties file.
7282
private static Properties readProps() {
7383
Properties props = new Properties();
@@ -94,7 +104,7 @@ private static Properties readProps() {
94104
// repoInfo returns the Sourcegraph repository URI, and the file path
95105
// relative to the repository root. If the repository URI cannot be
96106
// determined, a RepoInfo with empty strings is returned.
97-
public static RepoInfo repoInfo(String fileName) {
107+
public static RepoInfo repoInfo(String fileName, Project project) {
98108
String fileRel = "";
99109
String remoteURL = "";
100110
String branch = "";
@@ -106,12 +116,24 @@ public static RepoInfo repoInfo(String fileName) {
106116
// Determine file path, relative to repository root.
107117
fileRel = fileName.substring(repoRoot.length()+1);
108118
remoteURL = configuredGitRemoteURL(repoRoot);
109-
branch = gitBranch(repoRoot);
119+
branch = Util.setDefaultBranch(project)!=null ? Util.setDefaultBranch(project) : gitBranch(repoRoot);
110120

111-
// If on a branch that does not exist on the remote, use "master" instead.
121+
// If on a branch that does not exist on the remote or if defaultBranch does not exist on the remote,
122+
// use "master" instead.
112123
if (!isRemoteBranch(branch, repoRoot)) {
113124
branch = "master";
114125
}
126+
127+
// replace remoteURL if config option is not null
128+
String r = Util.setRemoteUrlReplacements(project);
129+
130+
if(r!=null) {
131+
String[] replacements = r.trim().split("\\s*,\\s*");
132+
// Check if the entered values are pairs
133+
for (int i = 0; i < replacements.length && replacements.length % 2 == 0; i += 2) {
134+
remoteURL = remoteURL.replace(replacements[i], replacements[i+1]);
135+
}
136+
}
115137
} catch (Exception err) {
116138
Logger.getInstance(Util.class).info(err);
117139
err.printStackTrace();
@@ -129,7 +151,7 @@ public static String exec(String cmd, String dir) throws IOException {
129151
BufferedReader stdout = new BufferedReader(new InputStreamReader(p.getInputStream()));
130152
BufferedReader stderr = new BufferedReader(new InputStreamReader(p.getErrorStream()));
131153

132-
// Log any stderr ouput.
154+
// Log any stderr output.
133155
Logger logger = Logger.getInstance(Util.class);
134156
String s;
135157
while ((s = stderr.readLine()) != null) {

0 commit comments

Comments
 (0)