Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

open repository in external browser with branch #284

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package org.abapgit.adt.ui.internal.repositories.actions;

import java.net.MalformedURLException;
import java.net.URL;
import java.net.URI;
import java.net.URISyntaxException;

import org.abapgit.adt.backend.model.abapgitrepositories.IRepository;
import org.abapgit.adt.ui.AbapGitUIPlugin;
Expand All @@ -22,6 +23,13 @@
*/
public class OpenRepositoryAction extends Action {
private final IViewPart view;
private static final String REFS_HEADS = "refs/heads/"; //$NON-NLS-1$
private static final String GITHUB_DOMAIN = "github.com"; //$NON-NLS-1$
private static final String GITLAB_DOMAIN = "gitlab.com"; //$NON-NLS-1$
private static final String GITHUB_WDF_SAP_DOMAIN = "github.wdf.sap.corp"; //$NON-NLS-1$
private static final String GITHUB_INFRA_HANA_DOMAIN = "github.infra.hana.ondemand.com"; //$NON-NLS-1$
private static final String GITHUB_TOOLS_DOMAIN = "github.tools.sap"; //$NON-NLS-1$
private static final String BIT_BUCKET_DOMAIN = "bitbucket.org"; //$NON-NLS-1$

/**
* @param view
Expand All @@ -39,9 +47,12 @@ public void run() {
IRepository repository = getRepository();
if (repository != null) {
try {
// Open default external browser
PlatformUI.getWorkbench().getBrowserSupport().getExternalBrowser().openURL(new URL(repository.getUrl()));
} catch (PartInitException | MalformedURLException exception) {
// Get Repository link
String repoLink = getLink(repository);
// Open the link in default external browser
URI repositoryUri = new URI(repoLink);
PlatformUI.getWorkbench().getBrowserSupport().getExternalBrowser().openURL(repositoryUri.toURL());
} catch (PartInitException | MalformedURLException | URISyntaxException exception) {
MessageDialog.openError(this.view.getViewSite().getShell(), Messages.AbapGitView_action_open_repo_error_dialog_title,
exception.getMessage());
AbapGitUIPlugin.getDefault().getLog()
Expand All @@ -50,6 +61,63 @@ public void run() {
}
}

public String getLink(IRepository repository) throws URISyntaxException {
// Get Connected branch
String repoLink = repository.getUrl();
URI repoURI = new URI(repository.getUrl());
String domain = repoURI.getHost();
String path = getPath(repoURI);
String branch = repository.getBranchName();
if (isGithubDomain(domain) || isGitlabDomain(domain)) {
branch = branch.replace(REFS_HEADS, "/tree/"); //$NON-NLS-1$
repoLink = constructRepoBranchURI(repoURI, path + branch);
} else if (isBitbucketDomain(domain)) {
branch = branch.replace(REFS_HEADS, "/src/"); //$NON-NLS-1$
repoLink = constructRepoBranchURI(repoURI, path + branch);
}
// return the concatenated link that redirects to the branch
return repoLink;
}

// method to construct URI from repository URI with new path and branch details
private String constructRepoBranchURI(URI repoURI, String path) throws URISyntaxException {
URI reconstructedRepoURI = new URI(repoURI.getScheme(), null, repoURI.getHost(), repoURI.getPort(), path, null, null); // not taking the username for the link
return reconstructedRepoURI.toString();
}

// method to check if the domain is a github domain
private boolean isGithubDomain(String domain) {
if (domain.equals(GITHUB_DOMAIN) || domain.equals(GITHUB_TOOLS_DOMAIN) || domain.equals(GITHUB_INFRA_HANA_DOMAIN)
|| domain.equals(GITHUB_WDF_SAP_DOMAIN)) {
return true;
}
return false;
}

// method to check if the domain is a gitlab domain
private boolean isGitlabDomain(String domain) {
if (domain.equals(GITLAB_DOMAIN)) {
return true;
}
return false;
}

// method to check if the domain is a bitbucket domain
private boolean isBitbucketDomain(String domain) {
if (domain.equals(BIT_BUCKET_DOMAIN)) {
return true;
}
return false;
}

private String getPath(URI repoURI) {
String path = repoURI.getPath();
if (path.endsWith(".git")) { //$NON-NLS-1$
path = path.substring(0, path.length() - 4); // Remove ".git"
}
return path;
}

private IRepository getRepository() {
if (this.view instanceof IAbapGitStagingView) {
//abapGit staging view
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.abapgit.adt.ui.internal.repositories;

import org.abapgit.adt.backend.model.abapgitrepositories.IRepository;
import org.abapgit.adt.backend.model.abapgitrepositories.impl.AbapgitrepositoriesFactoryImpl;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRunnable;
Expand Down Expand Up @@ -67,6 +69,17 @@ public void run(IProgressMonitor monitor) throws CoreException {
}, new NullProgressMonitor());
return projects[0].getProject();
}

public IRepository createDummyRepository() {
IRepository dummy = AbapgitrepositoriesFactoryImpl.eINSTANCE.createRepository();
dummy.setUrl("https://github.com/dummy_url");
dummy.setPackage("$AP_GITHUB");
dummy.setCreatedEmail("dummy_user_one@email.com");
dummy.setBranchName("refs/heads/master");
dummy.setDeserializedAt("20200322180503");
dummy.setStatusText("dummy_status");
return dummy;
}

protected void waitInUI(long timeout){
Display display = Display.getCurrent();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.abapgit.adt.ui.internal.repositories.actions;

import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.Assert;

import java.net.URISyntaxException;

import org.abapgit.adt.backend.model.abapgitrepositories.IRepository;
import org.abapgit.adt.backend.model.abapgitrepositories.impl.AbapgitrepositoriesFactoryImpl;
import org.abapgit.adt.ui.internal.repositories.AbapGitView;
import org.abapgit.adt.ui.internal.repositories.TestsPdeAbapGitRepositoriesUtil;
import org.eclipse.core.runtime.CoreException;

public class TestUnitAbapGitRepositoriesOpenAction {

private static OpenRepositoryAction openAction;
private static AbapGitView view;
private static IRepository dummyGitSelection;
private static IRepository dummyBitSelection;
private static TestsPdeAbapGitRepositoriesUtil utils;

@BeforeClass
public static void setup() throws CoreException {
utils = new TestsPdeAbapGitRepositoriesUtil();
view = utils.initializeView();
// git based host
dummyGitSelection = utils.createDummyRepository();
// bitbucket host
dummyBitSelection = AbapgitrepositoriesFactoryImpl.eINSTANCE.createRepository();
dummyBitSelection.setUrl("https://user1234@bitbucket.org/user1234/dummy.git");
dummyBitSelection.setPackage("$AP_GITHUB");
dummyBitSelection.setCreatedEmail("dummy_user_one@email.com");
dummyBitSelection.setBranchName("refs/heads/master");
dummyBitSelection.setDeserializedAt("20200322180503");
dummyBitSelection.setStatusText("dummy_status");
openAction = new OpenRepositoryAction(view);
}

@Test
public void testOpenRepositoryInBrowserAction() throws URISyntaxException {
String actualGitLink = openAction.getLink(dummyGitSelection);
String expectedGitLink = "https://github.com/dummy_url/tree/master";
Assert.assertEquals(actualGitLink, expectedGitLink);

String actualBitLink = openAction.getLink(dummyBitSelection);
String expectedBitLink = "https://bitbucket.org/user1234/dummy/src/master";
Assert.assertEquals(actualBitLink, expectedBitLink);
}
}