Skip to content

Commit

Permalink
Support retrievement of changed files for commits. (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
CamaradeRoman authored May 20, 2024
1 parent 87c38ed commit f6d5486
Show file tree
Hide file tree
Showing 26 changed files with 414 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import feign.RequestLine
import java.util.Date
import org.octopusden.octopus.vcsfacade.client.common.dto.Branch
import org.octopusden.octopus.vcsfacade.client.common.dto.Commit
import org.octopusden.octopus.vcsfacade.client.common.dto.CommitWithFiles
import org.octopusden.octopus.vcsfacade.client.common.dto.CreatePullRequest
import org.octopusden.octopus.vcsfacade.client.common.dto.PullRequest
import org.octopusden.octopus.vcsfacade.client.common.dto.SearchIssueInRangesResponse
Expand All @@ -22,9 +23,20 @@ interface VcsFacadeClient {
@Param("toHashOrRef") toHashOrRef: String
): List<Commit>

@RequestLine("GET rest/api/2/repository/commits/files?sshUrl={sshUrl}&fromHashOrRef={fromHashOrRef}&fromDate={fromDate}&toHashOrRef={toHashOrRef}")
fun getCommitsWithFiles(
@Param("sshUrl") sshUrl: String,
@Param("fromHashOrRef") fromHashOrRef: String?,
@Param("fromDate", expander = DateToISOExpander::class) fromDate: Date?,
@Param("toHashOrRef") toHashOrRef: String
): List<CommitWithFiles>

@RequestLine("GET rest/api/2/repository/commit?sshUrl={sshUrl}&hashOrRef={hashOrRef}")
fun getCommit(@Param("sshUrl") sshUrl: String, @Param("hashOrRef") hashOrRef: String): Commit

@RequestLine("GET rest/api/2/repository/commit/files?sshUrl={sshUrl}&hashOrRef={hashOrRef}")
fun getCommitWithFiles(@Param("sshUrl") sshUrl: String, @Param("hashOrRef") hashOrRef: String): CommitWithFiles

@RequestLine("GET rest/api/2/repository/issues?sshUrl={sshUrl}&fromHashOrRef={fromHashOrRef}&fromDate={fromDate}&toHashOrRef={toHashOrRef}")
fun getIssuesFromCommits(
@Param("sshUrl") sshUrl: String,
Expand Down Expand Up @@ -53,6 +65,9 @@ interface VcsFacadeClient {
@RequestLine("GET rest/api/2/repository/find/{issueKey}/commits")
fun findCommitsByIssueKey(@Param("issueKey") issueKey: String): List<Commit>

@RequestLine("GET rest/api/2/repository/find/{issueKey}/commits/files")
fun findCommitsWithFilesByIssueKey(@Param("issueKey") issueKey: String): List<CommitWithFiles>

@RequestLine("GET rest/api/2/repository/find/{issueKey}/pull-requests")
fun findPullRequestsByIssueKey(@Param("issueKey") issueKey: String): List<PullRequest>
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,13 @@ class ClassicVcsFacadeClient(
override fun getCommits(sshUrl: String, fromHashOrRef: String?, fromDate: Date?, toHashOrRef: String) =
client.getCommits(sshUrl, fromHashOrRef, fromDate, toHashOrRef)

override fun getCommitsWithFiles(sshUrl: String, fromHashOrRef: String?, fromDate: Date?, toHashOrRef: String) =
client.getCommitsWithFiles(sshUrl, fromHashOrRef, fromDate, toHashOrRef)

override fun getCommit(sshUrl: String, hashOrRef: String) = client.getCommit(sshUrl, hashOrRef)

override fun getCommitWithFiles(sshUrl: String, hashOrRef: String) = client.getCommitWithFiles(sshUrl, hashOrRef)

override fun getIssuesFromCommits(sshUrl: String, fromHashOrRef: String?, fromDate: Date?, toHashOrRef: String) =
client.getIssuesFromCommits(sshUrl, fromHashOrRef, fromDate, toHashOrRef)

Expand All @@ -51,6 +56,8 @@ class ClassicVcsFacadeClient(

override fun findCommitsByIssueKey(issueKey: String) = client.findCommitsByIssueKey(issueKey)

override fun findCommitsWithFilesByIssueKey(issueKey: String) = client.findCommitsWithFilesByIssueKey(issueKey)

override fun findPullRequestsByIssueKey(issueKey: String) = client.findPullRequestsByIssueKey(issueKey)

fun setUrl(apiUrl: String, timeRetryInMillis: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ data class Commit(
val parents: List<String>,
val link: String,
val repository: Repository
) : Comparable<Commit> {
) : VcsFacadeResponse, Comparable<Commit> {
override fun compareTo(other: Commit) =
compareBy(Commit::repository).thenByDescending(Commit::date).compare(this, other)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.octopusden.octopus.vcsfacade.client.common.dto

data class CommitWithFiles(
val commit: Commit,
val files: List<FileChange>
) : VcsFacadeResponse, Comparable<CommitWithFiles> {
override fun compareTo(other: CommitWithFiles) = commit compareTo other.commit
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package org.octopusden.octopus.vcsfacade.client.common.dto

data class FileChange(val type: FileChangeType, val path: String, val link: String)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.octopusden.octopus.vcsfacade.client.common.dto

enum class FileChangeType {
ADD, MODIFY, DELETE
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class VcsFacadeFunctionalTestBitbucket : BaseVcsFacadeFunctionalTest(
"absent-repo" to "Repository $PROJECT/absent does not exist.",
"commitById" to "Commit '$DEFAULT_ID' does not exist in repository '$REPOSITORY'.",
"commitsException_1" to "Commit '$DEFAULT_ID' does not exist in repository '$REPOSITORY'.",
"commitsException_2" to "Params 'fromHashOrRef' and 'fromDate' can not be used together",
"commitsException_2" to "'hashOrRef' and 'date' can not be used together",
"commitsException_3" to "Cannot find commit '${MESSAGE_3.commitId(REPOSITORY)}' in commit graph for commit '${MESSAGE_1.commitId(REPOSITORY)}' in '$PROJECT:$REPOSITORY'",
"pr_1" to "Project absent does not exist.",
"pr_2" to "Source branch 'absent' not found in '$PROJECT:$REPOSITORY'",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class VcsFacadeFunctionalTestGitea : BaseVcsFacadeFunctionalTest(
"absent-repo" to "The target couldn't be found.",
"commitById" to DEFAULT_ID,
"commitsException_1" to "object does not exist [id: $DEFAULT_ID, rel_path: ]",
"commitsException_2" to "Params 'fromHashOrRef' and 'fromDate' can not be used together",
"commitsException_2" to "'hashOrRef' and 'date' can not be used together",
"commitsException_3" to "Cannot find commit '${MESSAGE_3.commitId(REPOSITORY)}' in commit graph for commit '${
MESSAGE_1.commitId(
REPOSITORY
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class VcsFacadeFunctionalTestGitlab : BaseVcsFacadeFunctionalTest(
"absent-repo" to "Repository '$PROJECT:absent' does not exist.",
"commitById" to "Commit '$DEFAULT_ID' does not exist in repository '$PROJECT:$REPOSITORY'.",
"commitsException_1" to "Commit '$DEFAULT_ID' does not exist in repository '$PROJECT:$REPOSITORY'.",
"commitsException_2" to "Params 'fromHashOrRef' and 'fromDate' can not be used together",
"commitsException_2" to "'hashOrRef' and 'date' can not be used together",
"commitsException_3" to "Can't find commit '${MESSAGE_3.commitId(REPOSITORY)}' in graph but it exists in the '$PROJECT:$REPOSITORY'",
"pr_1" to "Group 'absent' does not exist.",
"pr_2" to "Source branch 'absent' not found in '$PROJECT:$REPOSITORY'",
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ bmuschko-docker-plugin.version=9.4.0
docker-compose-plugin.version=0.16.9
spring-data-opensearch.version=1.3.0
springdoc-openapi.version=2.3.0
external-systems-client.version=2.0.35
external-systems-client.version=2.0.38
gitlab4j-api.version=6.0.0-rc.4
bitbucket.version=8.14.0-jdk11
gitea.version=1.21.10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import java.util.concurrent.Future
import java.util.concurrent.TimeUnit
import org.octopusden.octopus.vcsfacade.client.common.Constants
import org.octopusden.octopus.vcsfacade.client.common.dto.Commit
import org.octopusden.octopus.vcsfacade.client.common.dto.CommitWithFiles
import org.octopusden.octopus.vcsfacade.client.common.dto.CreatePullRequest
import org.octopusden.octopus.vcsfacade.client.common.dto.PullRequest
import org.octopusden.octopus.vcsfacade.client.common.dto.SearchIssuesInRangesRequest
Expand Down Expand Up @@ -59,13 +60,41 @@ class RepositoryController(
RepositoryResponse(vcsManager.getCommits(sshUrl, fromHashOrRef, fromDate, toHashOrRef))
}.data.sorted()

@GetMapping("commits/files", produces = [MediaType.APPLICATION_JSON_VALUE])
fun getCommitsWithFiles(
@RequestParam("sshUrl") sshUrl: String,
@RequestParam("fromHashOrRef", required = false) fromHashOrRef: String?,
@RequestParam("fromDate", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) fromDate: Date?,
@RequestParam("toHashOrRef") toHashOrRef: String,
@RequestHeader(Constants.DEFERRED_RESULT_HEADER, required = false) requestId: String?
) = processRequest(requestId ?: UUID.randomUUID().toString()) {
log.info(
"Get commits ({},{}] with files in `{}` repository",
(fromHashOrRef ?: fromDate?.toString()).orEmpty(),
toHashOrRef,
sshUrl
)
RepositoryResponse(vcsManager.getCommitsWithFiles(sshUrl, fromHashOrRef, fromDate, toHashOrRef))
}.data.sorted()

@GetMapping("commit", produces = [MediaType.APPLICATION_JSON_VALUE])
fun getCommit(
@RequestParam("sshUrl") sshUrl: String,
@RequestParam("hashOrRef") hashOrRef: String
): Commit {
@RequestParam("hashOrRef") hashOrRef: String,
@RequestHeader(Constants.DEFERRED_RESULT_HEADER, required = false) requestId: String?
) = processRequest(requestId ?: UUID.randomUUID().toString()) {
log.info("Get commit {} in `{}` repository", hashOrRef, sshUrl)
return vcsManager.getCommit(sshUrl, hashOrRef)
vcsManager.getCommit(sshUrl, hashOrRef)
}

@GetMapping("commit/files", produces = [MediaType.APPLICATION_JSON_VALUE])
fun getCommitWithFiles(
@RequestParam("sshUrl") sshUrl: String,
@RequestParam("hashOrRef") hashOrRef: String,
@RequestHeader(Constants.DEFERRED_RESULT_HEADER, required = false) requestId: String?
) = processRequest(requestId ?: UUID.randomUUID().toString()) {
log.info("Get commit {} in `{}` repository", hashOrRef, sshUrl)
vcsManager.getCommitWithFiles(sshUrl, hashOrRef)
}

@GetMapping("issues", produces = [MediaType.APPLICATION_JSON_VALUE])
Expand Down Expand Up @@ -152,6 +181,15 @@ class RepositoryController(
RepositoryResponse(vcsManager.findCommits(issueKey))
}.data.sorted()

@GetMapping("find/{issueKey}/commits/files", produces = [MediaType.APPLICATION_JSON_VALUE])
fun findCommitsWithFilesByIssueKey(
@PathVariable("issueKey") issueKey: String,
@RequestHeader(Constants.DEFERRED_RESULT_HEADER, required = false) requestId: String?
) = processRequest(requestId ?: UUID.randomUUID().toString()) {
log.info("Find commits with files by issue key {}", issueKey)
RepositoryResponse(vcsManager.findCommitsWithFiles(issueKey))
}.data.sorted()

@GetMapping("find/{issueKey}/pull-requests", produces = [MediaType.APPLICATION_JSON_VALUE])
fun findPullRequestsByIssueKey(
@PathVariable("issueKey") issueKey: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class CommitDocument(
@Field(type = FieldType.Date) val date: Date,
@Field(type = FieldType.Object) val author: UserDocument,
@Field(type = FieldType.Keyword) val parents: List<String>,
@Field(type = FieldType.Keyword) val link: String
@Field(type = FieldType.Keyword) val link: String,
@Field(type = FieldType.Object) val files: List<FileChangeDocument>
) : BaseDocument(id(repository.id, hash)) {
override fun toString() =
"CommitDocument(id=$id, repository=$repository, hash=$hash, message=$message, date=$date, author=$author, parents=$parents, link=$link)"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.octopusden.octopus.vcsfacade.document

import org.octopusden.octopus.vcsfacade.client.common.dto.FileChangeType
import org.springframework.data.elasticsearch.annotations.Field
import org.springframework.data.elasticsearch.annotations.FieldType

data class FileChangeDocument(
@Field(type = FieldType.Keyword) val type: FileChangeType,
@Field(type = FieldType.Text, analyzer = "classic") val path: String,
@Field(type = FieldType.Keyword) val link: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.octopusden.octopus.vcsfacade.dto

import java.util.Date
import org.octopusden.octopus.vcsfacade.client.common.exception.ArgumentsNotCompatibleException

sealed class HashOrRefOrDate<out String, out Date> {
class HashOrRefValue(val value: String) : HashOrRefOrDate<String, Nothing>()
class DateValue(val value: Date) : HashOrRefOrDate<Nothing, Date>()

companion object {
fun create(hashOrRef: String?, date: Date?) = if (hashOrRef != null) {
if (date != null) {
throw ArgumentsNotCompatibleException("'hashOrRef' and 'date' can not be used together")
}
HashOrRefValue(hashOrRef)
} else if (date != null) {
DateValue(date)
} else null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package org.octopusden.octopus.vcsfacade.service

import org.octopusden.octopus.vcsfacade.client.common.dto.Branch
import org.octopusden.octopus.vcsfacade.client.common.dto.Commit
import org.octopusden.octopus.vcsfacade.client.common.dto.CommitWithFiles
import org.octopusden.octopus.vcsfacade.client.common.dto.FileChange
import org.octopusden.octopus.vcsfacade.client.common.dto.PullRequest
import org.octopusden.octopus.vcsfacade.client.common.dto.PullRequestReviewer
import org.octopusden.octopus.vcsfacade.client.common.dto.Ref
Expand All @@ -11,6 +13,7 @@ import org.octopusden.octopus.vcsfacade.client.common.dto.SearchSummary
import org.octopusden.octopus.vcsfacade.client.common.dto.Tag
import org.octopusden.octopus.vcsfacade.client.common.dto.User
import org.octopusden.octopus.vcsfacade.document.CommitDocument
import org.octopusden.octopus.vcsfacade.document.FileChangeDocument
import org.octopusden.octopus.vcsfacade.document.PullRequestDocument
import org.octopusden.octopus.vcsfacade.document.PullRequestReviewerDocument
import org.octopusden.octopus.vcsfacade.document.RefDocument
Expand All @@ -37,9 +40,9 @@ interface OpenSearchService {
fun savePullRequests(pullRequests: List<PullRequestDocument>)
fun deletePullRequestsByIds(pullRequestsIds: List<String>)
fun deletePullRequestsByRepositoryId(repositoryId: String)
fun findBranchesByIssueKey(issueKey: String): List<Branch>
fun findCommitsByIssueKey(issueKey: String): List<Commit>
fun findPullRequestsByIssueKey(issueKey: String): List<PullRequest>
fun findBranchesByIssueKey(issueKey: String): List<RefDocument>
fun findCommitsByIssueKey(issueKey: String): List<CommitDocument>
fun findPullRequestsByIssueKey(issueKey: String): List<PullRequestDocument>
fun findByIssueKey(issueKey: String): SearchSummary

companion object {
Expand All @@ -51,12 +54,21 @@ interface OpenSearchService {
RefType.TAG -> Tag(name, hash, link, repository.toDto())
}

fun Commit.toDocument(repositoryDocument: RepositoryDocument) = CommitDocument(
repositoryDocument, hash, message, date, author.toDocument(), parents, link
fun CommitWithFiles.toDocument(repositoryDocument: RepositoryDocument) = CommitDocument(
repositoryDocument,
commit.hash,
commit.message,
commit.date,
commit.author.toDocument(),
commit.parents,
commit.link,
files.map { it.toDocument() }
)

fun CommitDocument.toDto() =
Commit(hash, message, date, author.toDto(), parents, link, repository.toDto())
fun CommitDocument.toDto() = CommitWithFiles(
Commit(hash, message, date, author.toDto(), parents, link, repository.toDto()),
files.map { it.toDto() }
)

fun PullRequest.toDocument(repositoryDocument: RepositoryDocument) = PullRequestDocument(
repositoryDocument,
Expand Down Expand Up @@ -98,6 +110,10 @@ interface OpenSearchService {

private fun UserDocument.toDto() = User(name, avatar)

private fun FileChange.toDocument() = FileChangeDocument(type, path, link)

private fun FileChangeDocument.toDto() = FileChange(type, path, link)

fun RepositoryDocument.toDto() = Repository(sshUrl, link, avatar)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.octopusden.octopus.vcsfacade.service
import java.util.Date
import org.octopusden.octopus.vcsfacade.client.common.dto.Branch
import org.octopusden.octopus.vcsfacade.client.common.dto.Commit
import org.octopusden.octopus.vcsfacade.client.common.dto.CommitWithFiles
import org.octopusden.octopus.vcsfacade.client.common.dto.CreatePullRequest
import org.octopusden.octopus.vcsfacade.client.common.dto.PullRequest
import org.octopusden.octopus.vcsfacade.client.common.dto.SearchIssueInRangesResponse
Expand All @@ -13,11 +14,14 @@ import org.octopusden.octopus.vcsfacade.client.common.dto.Tag
interface VcsManager {
fun getTags(sshUrl: String): List<Tag>
fun getCommits(sshUrl: String, fromHashOrRef: String?, fromDate: Date?, toHashOrRef: String): List<Commit>
fun getCommitsWithFiles(sshUrl: String, fromHashOrRef: String?, fromDate: Date?, toHashOrRef: String): List<CommitWithFiles>
fun getCommit(sshUrl: String, hashOrRef: String): Commit
fun getCommitWithFiles(sshUrl: String, hashOrRef: String): CommitWithFiles
fun createPullRequest(sshUrl: String, createPullRequest: CreatePullRequest): PullRequest
fun searchIssuesInRanges(searchRequest: SearchIssuesInRangesRequest): SearchIssueInRangesResponse
fun findBranches(issueKey: String): List<Branch>
fun findCommits(issueKey: String): List<Commit>
fun findCommitsWithFiles(issueKey: String): List<CommitWithFiles>
fun findPullRequests(issueKey: String): List<PullRequest>
fun find(issueKey: String): SearchSummary
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package org.octopusden.octopus.vcsfacade.service
import java.util.Date
import org.octopusden.octopus.vcsfacade.client.common.dto.Branch
import org.octopusden.octopus.vcsfacade.client.common.dto.Commit
import org.octopusden.octopus.vcsfacade.client.common.dto.CommitWithFiles
import org.octopusden.octopus.vcsfacade.client.common.dto.CreatePullRequest
import org.octopusden.octopus.vcsfacade.client.common.dto.PullRequest
import org.octopusden.octopus.vcsfacade.client.common.dto.Tag
import org.octopusden.octopus.vcsfacade.config.VcsConfig
import org.octopusden.octopus.vcsfacade.dto.HashOrRefOrDate

abstract class VcsService(vcsProperties: VcsConfig.VcsProperties) {
protected val httpUrl = vcsProperties.host.lowercase().trimEnd('/')
Expand All @@ -19,14 +21,16 @@ abstract class VcsService(vcsProperties: VcsConfig.VcsProperties) {

abstract fun getBranches(group: String, repository: String): List<Branch>
abstract fun getTags(group: String, repository: String): List<Tag>
abstract fun getCommits(group: String, repository: String, fromHashOrRef: String, toHashOrRef: String): List<Commit>
abstract fun getCommits(group: String, repository: String, fromDate: Date?, toHashOrRef: String): List<Commit>
abstract fun getCommits(group: String, repository: String, from: HashOrRefOrDate<String, Date>?, toHashOrRef: String): List<Commit>
abstract fun getCommitsWithFiles(group: String, repository: String, from: HashOrRefOrDate<String, Date>?, toHashOrRef: String): List<CommitWithFiles>
abstract fun getCommit(group: String, repository: String, hashOrRef: String): Commit
abstract fun getCommitWithFiles(group: String, repository: String, hashOrRef: String): CommitWithFiles
abstract fun createPullRequest(group: String, repository: String, createPullRequest: CreatePullRequest): PullRequest
abstract fun getPullRequest(group: String, repository: String, index: Long): PullRequest
abstract fun findCommits(group: String, repository: String, hashes: Set<String>): List<Commit>
abstract fun findPullRequests(group: String, repository: String, indexes: Set<Long>): List<PullRequest>
abstract fun findBranches(issueKey: String): List<Branch>
abstract fun findCommits(issueKey: String): List<Commit>
abstract fun findCommitsWithFiles(issueKey: String): List<CommitWithFiles>
abstract fun findPullRequests(issueKey: String): List<PullRequest>
}
Loading

0 comments on commit f6d5486

Please sign in to comment.