Skip to content

Commit

Permalink
feature: add get commit call (ala-797)
Browse files Browse the repository at this point in the history
  • Loading branch information
manufacturist committed Jan 12, 2024
1 parent 89cedb3 commit aaebbfd
Showing 1 changed file with 19 additions and 27 deletions.
Expand Up @@ -2,11 +2,14 @@ package com.codacy.client.bitbucket.v2.service

import java.net.URLEncoder
import com.codacy.client.bitbucket.client.{BitbucketClient, Request, RequestResponse}
import com.codacy.client.bitbucket.v2.{CommitComment, SimpleCommitSha}
import com.codacy.client.bitbucket.v2.{CommitComment, SimpleCommit, SimpleCommitSha}
import play.api.libs.json.{JsNumber, JsObject, JsString, Json}

class CommitServices(client: BitbucketClient) {

def getCommit(author: String, repository: String, commit: String): RequestResponse[SimpleCommit] =
client.execute(Request(generateCommitUrl(author, repository, commit), classOf[SimpleCommit]))

def createComment(
author: String,
repository: String,
Expand All @@ -15,7 +18,7 @@ class CommitServices(client: BitbucketClient) {
file: Option[String] = None,
line: Option[Int] = None
): RequestResponse[CommitComment] = {
val commitCommentUrl = generateCommitCommentUrl(author, repository, commit)
val commitCommentUrl = generateCommentUrl(author, repository, commit)

val params = for {
filename <- file
Expand All @@ -29,46 +32,35 @@ class CommitServices(client: BitbucketClient) {
client.postJson(Request(commitCommentUrl, classOf[CommitComment]), values)
}

def listComments(author: String, repository: String, commit: String): RequestResponse[Seq[CommitComment]] = {
val commitCommentUrl = generateCommitCommentUrl(author, repository, commit)

def listComments(author: String, repository: String, commit: String): RequestResponse[Seq[CommitComment]] =
client
.executePaginated(Request(commitCommentUrl, classOf[Seq[CommitComment]]))
.executePaginated(Request(generateCommentUrl(author, repository, commit), classOf[Seq[CommitComment]]))
.map(_.filterNot(_.deleted))
}

def deleteComment(author: String, repository: String, commit: String, commentId: Long): RequestResponse[Boolean] = {
val commitCommentUrl = generateCommitCommentUrl(author, repository, commit)
val url = s"$commitCommentUrl/$commentId"

client.delete(url)
}
def deleteComment(author: String, repository: String, commit: String, commentId: Long): RequestResponse[Boolean] =
client.delete(generateCommentUrl(author, repository, commit, commentId.toString))

// https://developer.atlassian.com/cloud/bitbucket/rest/api-group-commits/#api-repositories-workspace-repo-slug-diff-spec-get
def getCommitDiff(
workspace: String,
repository: String,
fromCommitSha: String,
toCommitSha: String
): RequestResponse[String] = {
): RequestResponse[String] =
client.getRaw(s"${client.repositoriesBaseUrl}/$workspace/$repository/diff/$fromCommitSha..$toCommitSha")
}

def getCommitSha(workspace: String, repository: String, commitSha: String): RequestResponse[SimpleCommitSha] = {
client
.execute(Request(generateGetCommitUrl(workspace, repository, commitSha, client), classOf[SimpleCommitSha]))
}
def getCommitSha(workspace: String, repository: String, commitSha: String): RequestResponse[SimpleCommitSha] =
client.execute(Request(generateCommitUrl(workspace, repository, commitSha), classOf[SimpleCommitSha]))

private def generateCommitCommentUrl(author: String, repository: String, commit: String): String = {
private def generateCommitUrl(author: String, repository: String, commitSha: String, paths: String*): String = {
val encodedAuthor = URLEncoder.encode(author, "UTF-8")
val encodedRepository = URLEncoder.encode(repository, "UTF-8")
val encodedCommit = URLEncoder.encode(commit, "UTF-8")
s"${client.repositoriesBaseUrl}/$encodedAuthor/$encodedRepository/commit/$encodedCommit/comments"
}
val encodedCommit = URLEncoder.encode(commitSha, "UTF-8")
val encodedPaths = if (paths.isEmpty) "" else "/" + paths.map(URLEncoder.encode(_, "UFT-8")).mkString("/")

private def generateGetCommitUrl(owner: String, repo: String, commitSha: String, client: BitbucketClient): String = {
val encodedOwner = URLEncoder.encode(owner, "UTF-8")
val encodedRepo = URLEncoder.encode(repo, "UTF-8")
s"${client.apiBaseUrl}/repositories/$encodedOwner/$encodedRepo/commit/$commitSha"
s"${client.repositoriesBaseUrl}/$encodedAuthor/$encodedRepository/commit/$encodedCommit$encodedPaths"
}

private def generateCommentUrl(author: String, repository: String, commit: String, extraPaths: String*): String =
generateCommitUrl(author, repository, commit, "comments" +: extraPaths: _*)
}

0 comments on commit aaebbfd

Please sign in to comment.