generated from JetBrains/intellij-platform-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
65 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/main/kotlin/com/github/iml885203/intellijgitopen/GitHelper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.github.iml885203.intellijgitopen | ||
|
||
import org.eclipse.jgit.api.Git | ||
import org.eclipse.jgit.transport.RemoteConfig | ||
import org.eclipse.jgit.transport.URIish | ||
import org.jetbrains.annotations.NonNls | ||
import org.jetbrains.annotations.SystemIndependent | ||
import java.io.File | ||
|
||
object GitHelper { | ||
fun isGitRepository(projectPath: @SystemIndependent @NonNls String): Boolean { | ||
return File(projectPath, ".git").exists() | ||
} | ||
|
||
fun getRemoteUrl(projectPath: String): String { | ||
val git = Git.open(File(projectPath)) | ||
val config = git.repository.config | ||
val remoteConfig = RemoteConfig(config, "origin") | ||
val uris: List<URIish> = remoteConfig.urIs | ||
return uris[0].toString().replace(".git", "") | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/kotlin/com/github/iml885203/intellijgitopen/actions/GitOpenMergeRequestAction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.github.iml885203.intellijgitopen.actions | ||
|
||
import com.github.iml885203.intellijgitopen.GitHelper | ||
import com.github.iml885203.intellijgitopen.MyNotifier | ||
import com.intellij.ide.BrowserUtil | ||
import com.intellij.openapi.actionSystem.AnAction | ||
import com.intellij.openapi.actionSystem.AnActionEvent | ||
import com.intellij.openapi.diagnostic.thisLogger | ||
import org.eclipse.jgit.api.Git | ||
import org.eclipse.jgit.errors.RepositoryNotFoundException | ||
import org.eclipse.jgit.transport.RemoteConfig | ||
import org.eclipse.jgit.transport.URIish | ||
import java.io.File | ||
|
||
class GitOpenMergeRequestAction : AnAction() { | ||
override fun actionPerformed(e: AnActionEvent) { | ||
val project = e.project ?: return | ||
val projectPath = project.basePath ?: return | ||
if (!GitHelper.isGitRepository(projectPath)) { | ||
MyNotifier.notifyError(project, "The current project is not a Git repository.") | ||
return | ||
} | ||
|
||
val remoteUrl = GitHelper.getRemoteUrl(projectPath) | ||
|
||
val url = when { | ||
remoteUrl.contains("github.com") -> "$remoteUrl/pulls" | ||
else -> "$remoteUrl/merge_requests" | ||
} | ||
|
||
BrowserUtil.browse(url) | ||
} | ||
} |
25 changes: 7 additions & 18 deletions
25
src/main/kotlin/com/github/iml885203/intellijgitopen/actions/GitOpenRemoteRepoAction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,22 @@ | ||
package com.github.iml885203.intellijgitopen.actions | ||
|
||
import com.github.iml885203.intellijgitopen.GitHelper | ||
import com.github.iml885203.intellijgitopen.MyNotifier | ||
import com.intellij.ide.BrowserUtil | ||
import com.intellij.openapi.actionSystem.AnAction | ||
import com.intellij.openapi.actionSystem.AnActionEvent | ||
import com.intellij.openapi.diagnostic.thisLogger | ||
import org.eclipse.jgit.api.Git | ||
import org.eclipse.jgit.errors.RepositoryNotFoundException | ||
import org.eclipse.jgit.transport.RemoteConfig | ||
import org.eclipse.jgit.transport.URIish | ||
import java.io.File | ||
|
||
class GitOpenRemoteRepoAction : AnAction() { | ||
override fun actionPerformed(e: AnActionEvent) { | ||
val project = e.project ?: return | ||
try { | ||
val remoteUrl = getRemoteUrl(project.basePath ?: return) | ||
thisLogger().info("Remote URL: $remoteUrl") | ||
BrowserUtil.browse(remoteUrl) | ||
} catch (ex: RepositoryNotFoundException) { | ||
MyNotifier.notifyError(project, "The Git repository could not be found at the specified location.") | ||
val projectPath = project.basePath ?: return | ||
if (!GitHelper.isGitRepository(projectPath)) { | ||
MyNotifier.notifyError(project, "The current project is not a Git repository.") | ||
return | ||
} | ||
} | ||
|
||
private fun getRemoteUrl(projectPath: String): String { | ||
val git = Git.open(File(projectPath)) | ||
val config = git.repository.config | ||
val remoteConfig = RemoteConfig(config, "origin") | ||
val uris: List<URIish> = remoteConfig.urIs | ||
return uris[0].toString() | ||
val remoteUrl = GitHelper.getRemoteUrl(projectPath) | ||
BrowserUtil.browse(remoteUrl) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters