Skip to content

Commit

Permalink
✨ support open merge request
Browse files Browse the repository at this point in the history
  • Loading branch information
iml885203 committed Jun 22, 2024
1 parent 2eac6b5 commit bbdefe2
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 22 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pluginGroup = com.github.iml885203.intellijgitopen
pluginName = IntelliJ-git-open
pluginRepositoryUrl = https://github.com/iml885203/IntelliJ-git-open
# SemVer format -> https://semver.org
pluginVersion = 2024.6.1
pluginVersion = 2024.6.2

# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
pluginSinceBuild = 232
Expand Down
22 changes: 22 additions & 0 deletions src/main/kotlin/com/github/iml885203/intellijgitopen/GitHelper.kt
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", "")
}
}
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

Check warning on line 8 in src/main/kotlin/com/github/iml885203/intellijgitopen/actions/GitOpenMergeRequestAction.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import directive

Unused import directive
import org.eclipse.jgit.api.Git

Check warning on line 9 in src/main/kotlin/com/github/iml885203/intellijgitopen/actions/GitOpenMergeRequestAction.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import directive

Unused import directive
import org.eclipse.jgit.errors.RepositoryNotFoundException

Check warning on line 10 in src/main/kotlin/com/github/iml885203/intellijgitopen/actions/GitOpenMergeRequestAction.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import directive

Unused import directive
import org.eclipse.jgit.transport.RemoteConfig

Check warning on line 11 in src/main/kotlin/com/github/iml885203/intellijgitopen/actions/GitOpenMergeRequestAction.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import directive

Unused import directive
import org.eclipse.jgit.transport.URIish

Check warning on line 12 in src/main/kotlin/com/github/iml885203/intellijgitopen/actions/GitOpenMergeRequestAction.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import directive

Unused import directive
import java.io.File

Check warning on line 13 in src/main/kotlin/com/github/iml885203/intellijgitopen/actions/GitOpenMergeRequestAction.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import directive

Unused import directive

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)
}
}
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

Check warning on line 8 in src/main/kotlin/com/github/iml885203/intellijgitopen/actions/GitOpenRemoteRepoAction.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import directive

Unused import directive
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)
}
}
5 changes: 2 additions & 3 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
</applicationListeners>

<actions>
<action class="com.github.iml885203.intellijgitopen.actions.GitOpenRemoteRepoAction" id="com.github.iml885203.intellijgitopen.actions.GitOpenRemoteRepoAction" text="Git Open Remote Repo">
<keyboard-shortcut keymap="$default" first-keystroke="ctrl alt G"/>
</action>
<action class="com.github.iml885203.intellijgitopen.actions.GitOpenRemoteRepoAction" id="com.github.iml885203.intellijgitopen.actions.GitOpenRemoteRepoAction" text="Git Open Remote Repo" />
<action class="com.github.iml885203.intellijgitopen.actions.GitOpenMergeRequestAction" id="com.github.iml885203.intellijgitopen.actions.GitOpenMergeRequestAction" text="Git Open Merge Request" />
</actions>
</idea-plugin>

0 comments on commit bbdefe2

Please sign in to comment.