Skip to content
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
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-729.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: IntelliJ GitHub issue navigation
links:
- https://github.com/palantir/gradle-baseline/pull/729
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import groovy.transform.CompileStatic
import groovy.xml.XmlUtil
import java.nio.file.Files
import java.nio.file.Paths
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.transport.URIish
import org.gradle.api.Action
import org.gradle.api.Project
import org.gradle.api.Task
Expand Down Expand Up @@ -52,6 +54,7 @@ class BaselineIdea extends AbstractBaselinePlugin {
addGit(node)
addInspectionProjectProfile(node)
addJavacSettings(node)
addGitHubIssueNavigation(node)
}

ideaRootModel.workspace.iws.withXml { provider ->
Expand Down Expand Up @@ -203,6 +206,46 @@ class BaselineIdea extends AbstractBaselinePlugin {
""".stripIndent()))
}

private void addGitHubIssueNavigation(node) {
maybeGitHubUri().ifPresent { githubUri ->
node.append(new XmlParser().parseText("""
<component name="IssueNavigationConfiguration">
<option name="links">
<list>
<IssueNavigationLink>
<option name="issueRegexp" value="TODO.*\\#([0-9]+)" />
<option name="linkRegexp" value="${githubUri}/issues/\$1" />
</IssueNavigationLink>
</list>
</option>
</component>
""".stripIndent()))
}
}

private Optional<String> maybeGitHubUri() {
try {
Git git = Git.open(project.projectDir);
return git.remoteList().call().stream()
.flatMap { remoteConfig -> remoteConfig.getURIs().stream() }
.map { uri -> gitHubProjectFromRemote(uri) }
.filter { uri -> uri.contains("github") }
.findFirst()
} catch (Exception e) {
project.logger.info("Failed to detect Git remotes", e)
return Optional.empty()
}
}

/**
* Naiive conversion from ssh git remote to a Github project uri
*/
static String gitHubProjectFromRemote(URIish uri) {
def path = uri.path.endsWith('.git') ? uri.path.substring(0, uri.path.length() - 4) : uri.path
path = path.startsWith('/') ? path.substring(1) : path
return "https://${uri.host}/${path}"
}

/**
* Configure the default working directory of RunManager configurations to be the module directory.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.palantir.baseline

import com.palantir.baseline.plugins.BaselineIdea
import org.eclipse.jgit.transport.URIish
import org.gradle.api.Project
import org.gradle.testfixtures.ProjectBuilder
import spock.lang.Specification
Expand All @@ -36,4 +37,17 @@ class BaselineIdeaTest extends Specification {
expect:
project.plugins.hasPlugin(BaselineIdea.class)
}

def testGitHubUriExtraction(String remoteUri, String projectUri) {
expect:
assert BaselineIdea.gitHubProjectFromRemote(new URIish(remoteUri)) == projectUri

where:
remoteUri | projectUri
"[email protected]:palantir/gradle-baseline.git" | "https://github.com/palantir/gradle-baseline"
"[email protected]:palantir/gradle-baseline.git" | "https://github.my.company/palantir/gradle-baseline"
"https://github.com/palantir/gradle-baseline.git" | "https://github.com/palantir/gradle-baseline"
"https://github.my.company/palantir/gradle-baseline.git" | "https://github.my.company/palantir/gradle-baseline"
}

}