Skip to content

Commit

Permalink
eradicate CommentManager
Browse files Browse the repository at this point in the history
  • Loading branch information
HighKo committed Jan 16, 2024
1 parent 6dc362a commit ef1b8ff
Showing 1 changed file with 41 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,48 +20,68 @@
package com.linkedplanet.kotlinjiraclient.sdk

import arrow.core.Either
import com.atlassian.jira.bc.JiraServiceContextImpl
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.bc.issue.comment.CommentService
import com.atlassian.jira.bc.issue.comment.CommentService.CommentParameters
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.util.SimpleErrorCollection
import com.linkedplanet.kotlinjiraclient.api.error.JiraClientError
import com.linkedplanet.kotlinjiraclient.api.interfaces.JiraCommentOperator
import com.linkedplanet.kotlinjiraclient.api.model.JiraIssueComment
import com.linkedplanet.kotlinjiraclient.sdk.util.catchJiraClientError
import com.linkedplanet.kotlinjiraclient.sdk.util.*

object SdkJiraCommentOperator : JiraCommentOperator {

private val issueManager by lazy { ComponentAccessor.getIssueManager() }
private val commentManager by lazy { ComponentAccessor.getCommentManager() }
private val issueService: IssueService by lazy { ComponentAccessor.getIssueService() }
private val commentService : CommentService by lazy { ComponentAccessor.getComponent(CommentService::class.java) }
private val jiraAuthenticationContext by lazy { ComponentAccessor.getJiraAuthenticationContext() }
private fun user() = jiraAuthenticationContext.loggedInUser
private fun dispatchEvent(): Boolean = true // default dispatch behaviour for this operator
private val dispatchEvent: Boolean = true // default dispatch behaviour for this operator

override suspend fun getComments(issueKey: String): Either<JiraClientError, List<JiraIssueComment>> =
Either.catchJiraClientError {
val issue = issueManager.getIssueByCurrentKey(issueKey)
val comments = commentManager.getComments(issue)
eitherAndCatch {
val issue = issueService.getIssue(user(), issueKey).toEither().bind().issue
val comments = commentService.getCommentsForUser(user(), issue)
comments.map { JiraIssueComment(it.id.toString(), it.body, it.authorFullName, it.updated.toString()) }
}
}

override suspend fun createComment(issueKey: String, content: String): Either<JiraClientError, Unit> =
Either.catchJiraClientError {
val issue = issueManager.getIssueByCurrentKey(issueKey)
commentManager.create(issue, user(), content, dispatchEvent())
eitherAndCatch {
val issue = issueService.getIssue(user(), issueKey).toEither().bind().issue
val commentParameters = commentParameters(issue, content)
val validateComment = commentService.validateCommentCreate(user(), commentParameters)
commentService.create(user(), validateComment, dispatchEvent)
}

private fun commentParameters(
issue: MutableIssue,
content: String
): CommentParameters = CommentParameters.CommentParametersBuilder()
.issue(issue)
.body(content)
.author(user())
.build()


override suspend fun updateComment(
issueKey: String,
commentId: String,
content: String
): Either<JiraClientError, Unit> =
Either.catchJiraClientError {
val comment = commentManager.getMutableComment(commentId.toLongOrNull()!!)
comment.body = content
comment.setUpdateAuthor(user())
commentManager.update(comment, dispatchEvent())
}
): Either<JiraClientError, Unit> = eitherAndCatch {
val issue = issueService.getIssue(user(), issueKey).toEither().bind().issue
val commentParameters = commentParameters(issue, content)
commentService.validateCommentUpdate(user(), commentId.toLong(), commentParameters).toEither().bind()
}

override suspend fun deleteComment(issueKey: String, id: String): Either<JiraClientError, Unit> =
Either.catchJiraClientError {
val comment = commentManager.getCommentById(id.toLongOrNull()!!)
commentManager.delete(comment, dispatchEvent(), user())
eitherAndCatch {
val simpleErrorCollection = SimpleErrorCollection()
val comment = commentService.getCommentById(user(), id.toLongOrNull()!!, simpleErrorCollection)
simpleErrorCollection.toEither().bind()
val jiraServiceContextImpl = JiraServiceContextImpl(user())
commentService.delete(jiraServiceContextImpl, comment, dispatchEvent)
jiraServiceContextImpl.errorCollection.toEither().bind()
}
}

0 comments on commit ef1b8ff

Please sign in to comment.