Skip to content

Commit

Permalink
Fix branch substitution in commit templates (#337)
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-gibson authored Sep 2, 2024
1 parent 3f94bf7 commit e1d25dc
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 49 deletions.
14 changes: 7 additions & 7 deletions src/main/kotlin/uk/co/ben_gibson/git/link/pipeline/Pipeline.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import uk.co.ben_gibson.url.URL
import java.util.*
import kotlin.collections.Set

@Service
@Service(Service.Level.PROJECT)
class Pipeline(private val project: Project) {
private val middlewares: Set<Middleware> = setOf(
project.service<GenerateUrl>(),
project.service<Timer>(),
project.service<RecordHit>(),
project.service<ForceHttps>(),
project.service<SendSupportNotification>(),
project.service<ResolveContext>(),
service<GenerateUrl>(),
service<Timer>(),
service<RecordHit>(),
service<ForceHttps>(),
service<SendSupportNotification>(),
service<ResolveContext>(),
)

fun accept(context: Context) : URL? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,26 @@ class GenerateUrl : Middleware {
val repositoryFile = File.forRepository(context.file, repository)

return when (context) {
is ContextFileAtCommit -> UrlOptions.UrlOptionsFileAtCommit(repositoryFile, context.commit, context.lineSelection)
is ContextCommit -> UrlOptions.UrlOptionsCommit(context.commit)
is ContextFileAtCommit -> UrlOptions.UrlOptionsFileAtCommit(
repositoryFile,
repository.currentBranch?.name ?: settings.fallbackBranch,
context.commit,
context.lineSelection
)
is ContextCommit -> UrlOptions.UrlOptionsCommit(
context.commit,
repository.currentBranch?.name ?: settings.fallbackBranch
)
is ContextCurrentFile -> {
val commit = resolveCommit(repository, remote, settings, pullRequestWorkflowSupported)

if (commit != null) {
UrlOptions.UrlOptionsFileAtCommit(repositoryFile, commit, context.lineSelection)
UrlOptions.UrlOptionsFileAtCommit(
repositoryFile,
repository.currentBranch?.name ?: settings.fallbackBranch,
commit,
context.lineSelection
)
} else {
UrlOptions.UrlOptionsFileAtBranch(
repositoryFile,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ fun ValidationInfoBuilder.fileAtCommitTemplate(value: String): ValidationInfo? {

val options = UrlOptions.UrlOptionsFileAtCommit(
File("foo.kt", false, "src/main", false),
"main",
Commit("734232a3c18f0625843bd161c3f5da272b9d53c1"),
LineSelection(10, 20)
)
Expand All @@ -89,7 +90,7 @@ fun ValidationInfoBuilder.commitTemplate(value: String): ValidationInfo? {
return null
}

val options = UrlOptions.UrlOptionsCommit(Commit("734232a3c18f0625843bd161c3f5da272b9d53c1"))
val options = UrlOptions.UrlOptionsCommit(Commit("734232a3c18f0625843bd161c3f5da272b9d53c1"), "main")

return urlTemplate(options, commit = value)
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/uk/co/ben_gibson/git/link/url/UrlOptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import uk.co.ben_gibson.git.link.git.File
import uk.co.ben_gibson.git.link.ui.LineSelection

sealed interface UrlOptions {
class UrlOptionsCommit(val commit: Commit) : UrlOptions
class UrlOptionsFileAtCommit(val file: File, val commit: Commit, val lineSelection: LineSelection? = null) : UrlOptions
class UrlOptionsCommit(val commit: Commit, val currentBranch: String,) : UrlOptions

Check notice on line 8 in src/main/kotlin/uk/co/ben_gibson/git/link/url/UrlOptions.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Trailing comma recommendations

Useless trailing comma
class UrlOptionsFileAtCommit(val file: File, val currentBranch: String, val commit: Commit, val lineSelection: LineSelection? = null) : UrlOptions
class UrlOptionsFileAtBranch(val file: File, val branch: String, val lineSelection: LineSelection? = null) : UrlOptions
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ open class TemplatedUrlFactory(private val templates: UrlTemplates) : UrlFactory

template = processFile(template, options.file)
template = processCommit(template, options.commit)
template = processBranch(template, options.currentBranch)
template = processLineSelection(template, options.lineSelection, options.file)

return template
Expand All @@ -54,6 +55,7 @@ open class TemplatedUrlFactory(private val templates: UrlTemplates) : UrlFactory
var template = templates.commit

template = processCommit(template, options.commit)
template = processBranch(template, options.currentBranch)

return template
}
Expand Down
16 changes: 9 additions & 7 deletions src/test/kotlin/uk/co/ben_gibson/git/link/url/AzureTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ class AzureTest {
),
Arguments.of(
REMOTE_BASE_URL_WITH_GIT,
UrlOptions.UrlOptionsFileAtCommit(FILE, COMMIT, LINE_SELECTION),
UrlOptions.UrlOptionsFileAtCommit(FILE, "main", COMMIT, LINE_SELECTION),
"https://dev.azure.com/ben-gibson/_git/test.git?version=GCb032a0707beac9a2f24b1b7d97ee4f7156de182c&path=src%2FFoo.java&line=10&lineEnd=21&lineStartColumn=1&lineEndColumn=1"
),
Arguments.of(
REMOTE_BASE_URL_WITH_GIT,
UrlOptions.UrlOptionsFileAtCommit(
File("resources", true, "src/foo", false),
"main",
COMMIT
),
"https://dev.azure.com/ben-gibson/_git/test.git?version=GCb032a0707beac9a2f24b1b7d97ee4f7156de182c&path=src%2Ffoo%2Fresources"
Expand All @@ -52,37 +53,38 @@ class AzureTest {
REMOTE_BASE_URL_WITH_GIT,
UrlOptions.UrlOptionsFileAtCommit(
File("my-project", true, "", true),
"main",
COMMIT
),
"https://dev.azure.com/ben-gibson/_git/test.git?version=GCb032a0707beac9a2f24b1b7d97ee4f7156de182c&path=%2F"),
Arguments.of(
REMOTE_BASE_URL_WITH_GIT,
UrlOptions.UrlOptionsFileAtCommit(FILE, COMMIT),
UrlOptions.UrlOptionsFileAtCommit(FILE, "main", COMMIT),
"https://dev.azure.com/ben-gibson/_git/test.git?version=GCb032a0707beac9a2f24b1b7d97ee4f7156de182c&path=src%2FFoo.java"
),
Arguments.of(
REMOTE_BASE_URL_WITH_GIT,
UrlOptions.UrlOptionsCommit(COMMIT),
UrlOptions.UrlOptionsCommit(COMMIT, "main"),
"https://dev.azure.com/ben-gibson/_git/test.git/commit/b032a0707beac9a2f24b1b7d97ee4f7156de182c"
),
Arguments.of(
REMOTE_BASE_URL_WITHOUT_GIT,
UrlOptions.UrlOptionsCommit(COMMIT),
UrlOptions.UrlOptionsCommit(COMMIT, "main"),
"https://dev.azure.com/ben-gibson/_git/test.git/commit/b032a0707beac9a2f24b1b7d97ee4f7156de182c"
),
Arguments.of(
REMOTE_BASE_URL_WITH_COMPANY_AND_GIT,
UrlOptions.UrlOptionsCommit(COMMIT),
UrlOptions.UrlOptionsCommit(COMMIT, "main"),
"https://dev.azure.com/company/project/_git/test.git/commit/b032a0707beac9a2f24b1b7d97ee4f7156de182c"
),
Arguments.of(
REMOTE_BASE_URL_WITH_COMPANY_WITHOUT_GIT,
UrlOptions.UrlOptionsCommit(COMMIT),
UrlOptions.UrlOptionsCommit(COMMIT, "main"),
"https://dev.azure.com/company/project/_git/test.git/commit/b032a0707beac9a2f24b1b7d97ee4f7156de182c"
),
Arguments.of(
URL.fromString("https://ssh.dev.azure.com/v3/ben-gibson/test/test"),
UrlOptions.UrlOptionsCommit(COMMIT),
UrlOptions.UrlOptionsCommit(COMMIT, "main"),
"https://dev.azure.com/ben-gibson/test/_git/test.git/commit/b032a0707beac9a2f24b1b7d97ee4f7156de182c"
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import uk.co.ben_gibson.git.link.ui.LineSelection
import java.util.stream.Stream
import uk.co.ben_gibson.git.link.git.Commit
import uk.co.ben_gibson.git.link.url.factory.BitbucketCloudUrlFactory
import uk.co.ben_gibson.git.link.url.factory.TemplatedUrlFactory
import uk.co.ben_gibson.git.link.url.template.UrlTemplates
import uk.co.ben_gibson.url.URL

class BitBucketCloudTest {
Expand Down Expand Up @@ -46,13 +44,14 @@ class BitBucketCloudTest {
),
Arguments.of(
REMOTE_BASE_URL,
UrlOptions.UrlOptionsFileAtCommit(FILE, COMMIT, LineSelection(10, 20)),
UrlOptions.UrlOptionsFileAtCommit(FILE, "main", COMMIT, LineSelection(10, 20)),
"https://bitbucket.org/foo/bar/src/b032a0707beac9a2f24b1b7d97ee4f7156de182c/src/Foo.java#lines-10:20"
),
Arguments.of(
REMOTE_BASE_URL,
UrlOptions.UrlOptionsFileAtCommit(
File("resources", true, "src/foo", false),
"main",
COMMIT
),
"https://bitbucket.org/foo/bar/src/b032a0707beac9a2f24b1b7d97ee4f7156de182c/src/foo/resources"
Expand All @@ -61,18 +60,19 @@ class BitBucketCloudTest {
REMOTE_BASE_URL,
UrlOptions.UrlOptionsFileAtCommit(
File("my-project", true, "", true),
"main",
COMMIT
),
"https://bitbucket.org/foo/bar/src/b032a0707beac9a2f24b1b7d97ee4f7156de182c"
),
Arguments.of(
REMOTE_BASE_URL,
UrlOptions.UrlOptionsFileAtCommit(FILE, COMMIT),
UrlOptions.UrlOptionsFileAtCommit(FILE, "main", COMMIT),
"https://bitbucket.org/foo/bar/src/b032a0707beac9a2f24b1b7d97ee4f7156de182c/src/Foo.java"
),
Arguments.of(
REMOTE_BASE_URL,
UrlOptions.UrlOptionsCommit(COMMIT),
UrlOptions.UrlOptionsCommit(COMMIT, "main"),
"https://bitbucket.org/foo/bar/commits/b032a0707beac9a2f24b1b7d97ee4f7156de182c"
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ class BitBucketServerTest {
),
Arguments.of(
REMOTE_BASE_URL,
UrlOptions.UrlOptionsFileAtCommit(FILE, COMMIT, LineSelection(10, 20)),
UrlOptions.UrlOptionsFileAtCommit(FILE, "main", COMMIT, LineSelection(10, 20)),
"https://stash.example.com/projects/foo/repos/bar/browse/src/Foo.java?at=b032a0707beac9a2f24b1b7d97ee4f7156de182c#10-20"
),
Arguments.of(
REMOTE_BASE_URL,
UrlOptions.UrlOptionsFileAtCommit(
File("resources", true, "src/foo", false),
"main",
COMMIT
),
"https://stash.example.com/projects/foo/repos/bar/browse/src/foo/resources?at=b032a0707beac9a2f24b1b7d97ee4f7156de182c"
Expand All @@ -50,18 +51,19 @@ class BitBucketServerTest {
REMOTE_BASE_URL,
UrlOptions.UrlOptionsFileAtCommit(
File("my-project", true, "", true),
"main",
COMMIT
),
"https://stash.example.com/projects/foo/repos/bar/browse?at=b032a0707beac9a2f24b1b7d97ee4f7156de182c"
),
Arguments.of(
REMOTE_BASE_URL,
UrlOptions.UrlOptionsFileAtCommit(FILE, COMMIT),
UrlOptions.UrlOptionsFileAtCommit(FILE, "main", COMMIT),
"https://stash.example.com/projects/foo/repos/bar/browse/src/Foo.java?at=b032a0707beac9a2f24b1b7d97ee4f7156de182c"
),
Arguments.of(
REMOTE_BASE_URL,
UrlOptions.UrlOptionsCommit(COMMIT),
UrlOptions.UrlOptionsCommit(COMMIT, "main"),
"https://stash.example.com/projects/foo/repos/bar/commits/b032a0707beac9a2f24b1b7d97ee4f7156de182c"
)
)
Expand Down
12 changes: 6 additions & 6 deletions src/test/kotlin/uk/co/ben_gibson/git/link/url/ChromiumTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,38 +51,38 @@ class ChromiumTest {
// Chromiumos file at commit
Arguments.of(
REMOTE_BASE_URL_CHROMIUMOS,
UrlOptions.UrlOptionsFileAtCommit(FILE, COMMIT, LINE_SELECTION),
UrlOptions.UrlOptionsFileAtCommit(FILE, "main", COMMIT, LINE_SELECTION),
"https://source.chromium.org/chromiumos/chromiumos/codesearch/+/b032a0707beac9a2f24b1b7d97ee4f7156de182c:src/platform/ec/board/foo.c;l=10-20"
),
Arguments.of(
REMOTE_BASE_URL_CHROMIUMOS,
UrlOptions.UrlOptionsFileAtCommit(FILE, COMMIT),
UrlOptions.UrlOptionsFileAtCommit(FILE, "main", COMMIT),
"https://source.chromium.org/chromiumos/chromiumos/codesearch/+/b032a0707beac9a2f24b1b7d97ee4f7156de182c:src/platform/ec/board/foo.c"
),

// Chromium file at commit
Arguments.of(
REMOTE_BASE_URL_CHROMIUM,
UrlOptions.UrlOptionsFileAtCommit(FILE, COMMIT, LINE_SELECTION),
UrlOptions.UrlOptionsFileAtCommit(FILE, "main", COMMIT, LINE_SELECTION),
"https://source.chromium.org/chromium/chromium/tools/build/+/b032a0707beac9a2f24b1b7d97ee4f7156de182c:board/foo.c;l=10-20"
),
Arguments.of(
REMOTE_BASE_URL_CHROMIUM,
UrlOptions.UrlOptionsFileAtCommit(FILE, COMMIT),
UrlOptions.UrlOptionsFileAtCommit(FILE, "main", COMMIT),
"https://source.chromium.org/chromium/chromium/tools/build/+/b032a0707beac9a2f24b1b7d97ee4f7156de182c:board/foo.c"
),

// Chromiumos commit
Arguments.of(
REMOTE_BASE_URL_CHROMIUMOS,
UrlOptions.UrlOptionsCommit(COMMIT),
UrlOptions.UrlOptionsCommit(COMMIT, "main"),
"https://source.chromium.org/chromiumos/_/chromium/chromiumos/platform/ec/+/b032a0707beac9a2f24b1b7d97ee4f7156de182c"
),

// Chromium commit
Arguments.of(
REMOTE_BASE_URL_CHROMIUM,
UrlOptions.UrlOptionsCommit(COMMIT),
UrlOptions.UrlOptionsCommit(COMMIT, "main"),
"https://source.chromium.org/chromium/chromium/tools/build/+/b032a0707beac9a2f24b1b7d97ee4f7156de182c"
),
)
Expand Down
8 changes: 5 additions & 3 deletions src/test/kotlin/uk/co/ben_gibson/git/link/url/GerritTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ class GerritTest {
),
Arguments.of(
REMOTE_BASE_URL,
UrlOptions.UrlOptionsFileAtCommit(FILE, COMMIT, LineSelection(10, 20)),
UrlOptions.UrlOptionsFileAtCommit(FILE, "main", COMMIT, LineSelection(10, 20)),
"https://gerrit.example.com/plugins/gitiles/foo/bar/+/b032a0707beac9a2f24b1b7d97ee4f7156de182c/src/Foo.java#10"
),
Arguments.of(
REMOTE_BASE_URL,
UrlOptions.UrlOptionsFileAtCommit(
File("resources", true, "src/foo", false),
"main",
COMMIT
),
"https://gerrit.example.com/plugins/gitiles/foo/bar/+/b032a0707beac9a2f24b1b7d97ee4f7156de182c/src/foo/resources"
Expand All @@ -50,18 +51,19 @@ class GerritTest {
REMOTE_BASE_URL,
UrlOptions.UrlOptionsFileAtCommit(
File("my-project", true, "", true),
"main",
COMMIT
),
"https://gerrit.example.com/plugins/gitiles/foo/bar/+/b032a0707beac9a2f24b1b7d97ee4f7156de182c"
),
Arguments.of(
REMOTE_BASE_URL,
UrlOptions.UrlOptionsFileAtCommit(FILE, COMMIT),
UrlOptions.UrlOptionsFileAtCommit(FILE, "main", COMMIT),
"https://gerrit.example.com/plugins/gitiles/foo/bar/+/b032a0707beac9a2f24b1b7d97ee4f7156de182c/src/Foo.java"
),
Arguments.of(
REMOTE_BASE_URL,
UrlOptions.UrlOptionsCommit(COMMIT),
UrlOptions.UrlOptionsCommit(COMMIT, "main"),
"https://gerrit.example.com/plugins/gitiles/foo/bar/+/b032a0707beac9a2f24b1b7d97ee4f7156de182c"
)
)
Expand Down
8 changes: 5 additions & 3 deletions src/test/kotlin/uk/co/ben_gibson/git/link/url/GitHubTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ class GitHubTest {
),
Arguments.of(
REMOTE_BASE_URL,
UrlOptions.UrlOptionsFileAtCommit(FILE, COMMIT, LineSelection(10, 20)),
UrlOptions.UrlOptionsFileAtCommit(FILE, "main", COMMIT, LineSelection(10, 20)),
"https://github.com/my/repo/blob/b032a0707beac9a2f24b1b7d97ee4f7156de182c/src/Foo.java#L10-L20"
),
Arguments.of(
REMOTE_BASE_URL,
UrlOptions.UrlOptionsFileAtCommit(
File("resources", true, "src/foo", false),
"main",
COMMIT
),
"https://github.com/my/repo/tree/b032a0707beac9a2f24b1b7d97ee4f7156de182c/src/foo/resources"
Expand All @@ -58,18 +59,19 @@ class GitHubTest {
REMOTE_BASE_URL,
UrlOptions.UrlOptionsFileAtCommit(
File("my-project", true, "", true),
"main",
COMMIT
),
"https://github.com/my/repo/tree/b032a0707beac9a2f24b1b7d97ee4f7156de182c"
),
Arguments.of(
REMOTE_BASE_URL,
UrlOptions.UrlOptionsFileAtCommit(FILE, COMMIT),
UrlOptions.UrlOptionsFileAtCommit(FILE, "main", COMMIT),
"https://github.com/my/repo/blob/b032a0707beac9a2f24b1b7d97ee4f7156de182c/src/Foo.java"
),
Arguments.of(
REMOTE_BASE_URL,
UrlOptions.UrlOptionsCommit(COMMIT),
UrlOptions.UrlOptionsCommit(COMMIT, "main"),
"https://github.com/my/repo/commit/b032a0707beac9a2f24b1b7d97ee4f7156de182c"
)
)
Expand Down
Loading

0 comments on commit e1d25dc

Please sign in to comment.