Skip to content

Commit f02aaa8

Browse files
committed
get rid of by lazy, introduce withErrorCollection
1 parent 3081c1f commit f02aaa8

File tree

4 files changed

+20
-10
lines changed

4 files changed

+20
-10
lines changed

kotlin-jira-client/kotlin-jira-client-sdk/src/main/kotlin/com/linkedplanet/kotlinjiraclient/sdk/SdkJiraCommentOperator.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import com.atlassian.jira.bc.issue.comment.CommentService
2626
import com.atlassian.jira.bc.issue.comment.CommentService.CommentParameters
2727
import com.atlassian.jira.component.ComponentAccessor
2828
import com.atlassian.jira.issue.MutableIssue
29-
import com.atlassian.jira.util.SimpleErrorCollection
3029
import com.linkedplanet.kotlinjiraclient.api.error.JiraClientError
3130
import com.linkedplanet.kotlinjiraclient.api.interfaces.JiraCommentOperator
3231
import com.linkedplanet.kotlinjiraclient.api.model.JiraIssueComment
@@ -69,9 +68,7 @@ object SdkJiraCommentOperator : JiraCommentOperator {
6968

7069
override suspend fun deleteComment(issueKey: String, id: String): Either<JiraClientError, Unit> =
7170
eitherAndCatch {
72-
val simpleErrorCollection = SimpleErrorCollection()
73-
val comment = commentService.getCommentById(user(), id.toLongOrNull()!!, simpleErrorCollection)
74-
simpleErrorCollection.toEither().bind()
71+
val comment = withErrorCollection { commentService.getCommentById(user(), id.toLongOrNull()!!, it) }.bind()
7572
val jiraServiceContextImpl = JiraServiceContextImpl(user())
7673
commentService.delete(jiraServiceContextImpl, comment, DISPATCH_EVENT)
7774
jiraServiceContextImpl.errorCollection.toEither().bind()

kotlin-jira-client/kotlin-jira-client-sdk/src/main/kotlin/com/linkedplanet/kotlinjiraclient/sdk/SdkJiraTransitionOperator.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ import com.linkedplanet.kotlinjiraclient.sdk.util.toEither
3333
object SdkJiraTransitionOperator : JiraTransitionOperator {
3434

3535
private val issueService = ComponentAccessor.getIssueService()
36-
private val workflowManager by lazy { ComponentAccessor.getWorkflowManager() }
37-
private val transitionManager by lazy { ComponentAccessor.getComponent(TransitionManager::class.java) }
38-
private val jiraAuthenticationContext by lazy { ComponentAccessor.getJiraAuthenticationContext() }
36+
private val workflowManager = ComponentAccessor.getWorkflowManager()
37+
private val transitionManager = ComponentAccessor.getComponent(TransitionManager::class.java)
38+
private val jiraAuthenticationContext = ComponentAccessor.getJiraAuthenticationContext()
3939
private fun user() = jiraAuthenticationContext.loggedInUser
4040

4141
override suspend fun doTransition(

kotlin-jira-client/kotlin-jira-client-sdk/src/main/kotlin/com/linkedplanet/kotlinjiraclient/sdk/util/EitherExtension.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import arrow.core.raise.fold
2626
import arrow.core.right
2727
import com.atlassian.jira.bc.ServiceResult
2828
import com.atlassian.jira.util.ErrorCollection
29+
import com.atlassian.jira.util.SimpleErrorCollection
2930
import com.linkedplanet.kotlinjiraclient.api.error.JiraClientError
3031
import org.jetbrains.kotlin.util.removeSuffixIfPresent
3132

@@ -45,6 +46,18 @@ fun ErrorCollection.toEither(errorTitle: String = "SdkError") : Either<JiraClien
4546
else -> Unit.right()
4647
}
4748

49+
fun <T> withErrorCollection(
50+
errorTitle: String = "SdkError",
51+
block: (errors: ErrorCollection) -> T
52+
): Either<JiraClientError, T> {
53+
val simpleErrorCollection = SimpleErrorCollection()
54+
val result = block(simpleErrorCollection)
55+
return when {
56+
simpleErrorCollection.hasAnyErrors() -> jiraClientError(simpleErrorCollection, errorTitle).left()
57+
else -> result.right()
58+
}
59+
}
60+
4861
fun jiraClientError(errorCollection: ErrorCollection, errorTitle: String = "SdkError"): JiraClientError {
4962
val worstReason: ErrorCollection.Reason? = ErrorCollection.Reason.getWorstReason(errorCollection.reasons)
5063
val httpStatusSuffix = worstReason?.let { " (${it.httpStatusCode})" } ?: ""

kotlin-jira-client/kotlin-jira-client-sdk/src/main/kotlin/com/linkedplanet/kotlinjiraclient/sdk/util/IssueJsonConverter.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ import javax.xml.bind.annotation.XmlTransient
4343
class IssueJsonConverter {
4444

4545
private val log = LoggerFactory.getLogger(FieldAccessorImpl::class.java)
46-
private val fieldLayoutManager by lazy { ComponentAccessor.getFieldLayoutManager() }
47-
private val fieldManager by lazy { ComponentAccessor.getFieldManager() }
46+
private val fieldLayoutManager = ComponentAccessor.getFieldLayoutManager()
47+
private val fieldManager = ComponentAccessor.getFieldManager()
4848
private val beanBuilderFactory = ComponentAccessor.getOSGiComponentInstanceOfType(BeanBuilderFactory::class.java)
4949
private val jiraBaseUrls: JiraBaseUrls = ComponentAccessor.getComponent(JiraBaseUrls::class.java)
5050
private val uriBuilder: UriBuilder = UriBuilder.fromPath(jiraBaseUrls.restApi2BaseUrl())
51-
private val gson by lazy { setupGson() }
51+
private val gson = setupGson()
5252

5353

5454
@Throws(FieldException::class)

0 commit comments

Comments
 (0)