MergeRequestRepository.kt 4.82 KB
Newer Older
Ilya Gulya's avatar
Ilya Gulya committed
1
package ru.terrakok.gitlabclient.model.repository.mergerequest
Ilya Gulya's avatar
Ilya Gulya committed
2

3 4 5
import io.reactivex.Observable
import io.reactivex.Single
import io.reactivex.functions.BiFunction
6
import org.threeten.bp.LocalDateTime
7
import ru.terrakok.gitlabclient.entity.OrderBy
8
import ru.terrakok.gitlabclient.entity.Project
9
import ru.terrakok.gitlabclient.entity.Sort
10
import ru.terrakok.gitlabclient.entity.app.target.*
11 12
import ru.terrakok.gitlabclient.entity.event.EventAction
import ru.terrakok.gitlabclient.entity.mergerequest.MergeRequest
13 14 15
import ru.terrakok.gitlabclient.entity.mergerequest.MergeRequestScope
import ru.terrakok.gitlabclient.entity.mergerequest.MergeRequestState
import ru.terrakok.gitlabclient.entity.mergerequest.MergeRequestViewType
Ilya Gulya's avatar
Ilya Gulya committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
import ru.terrakok.gitlabclient.model.data.server.GitlabApi
import ru.terrakok.gitlabclient.model.system.SchedulersProvider
import ru.terrakok.gitlabclient.toothpick.PrimitiveWrapper
import ru.terrakok.gitlabclient.toothpick.qualifier.DefaultPageSize
import javax.inject.Inject

class MergeRequestRepository @Inject constructor(
        private val api: GitlabApi,
        private val schedulers: SchedulersProvider,
        @DefaultPageSize private val defaultPageSizeWrapper: PrimitiveWrapper<Int>
) {
    private val defaultPageSize = defaultPageSizeWrapper.value

    fun getMergeRequests(
            state: MergeRequestState? = null,
            milestone: String? = null,
            viewType: MergeRequestViewType? = null,
            labels: String? = null,
34 35
            createdBefore: LocalDateTime? = null,
            createdAfter: LocalDateTime? = null,
Ilya Gulya's avatar
Ilya Gulya committed
36 37 38 39 40 41 42 43 44 45
            scope: MergeRequestScope? = null,
            authorId: Int? = null,
            assigneeId: Int? = null,
            meReactionEmoji: String? = null,
            orderBy: OrderBy? = null,
            sort: Sort? = null,
            page: Int,
            pageSize: Int = defaultPageSize
    ) = api
            .getMergeRequests(
46 47
                    state, milestone, viewType, labels, createdBefore, createdAfter, scope,
                    authorId, assigneeId, meReactionEmoji, orderBy, sort, page, pageSize
Ilya Gulya's avatar
Ilya Gulya committed
48
            )
49 50 51 52 53
            .flatMap { mrs ->
                Single.zip(
                        Single.just(mrs),
                        getDistinctProjects(mrs),
                        BiFunction<List<MergeRequest>, Map<Long, Project>, List<TargetHeader>> { sourceMrs, projects ->
54
                            sourceMrs.map { getTargetHeader(it, projects[it.projectId]!!) }
55 56 57
                        }
                )
            }
Ilya Gulya's avatar
Ilya Gulya committed
58 59 60
            .subscribeOn(schedulers.io())
            .observeOn(schedulers.ui())

61 62 63 64 65 66
    private fun getDistinctProjects(mrs: List<MergeRequest>): Single<Map<Long, Project>> {
        return Observable.fromIterable(mrs)
                .distinct { it.projectId }
                .flatMapSingle { mr -> api.getProject(mr.projectId) }
                .toMap { it.id }
    }
Ilya Gulya's avatar
Ilya Gulya committed
67

68 69
    private fun getTargetHeader(mr: MergeRequest, project: Project): TargetHeader {
        val badges = mutableListOf<TargetBadge>()
70 71 72 73 74
        badges.add(TargetBadge.Status(when(mr.state) {
            MergeRequestState.OPENED -> TargetBadgeStatus.OPENED
            MergeRequestState.CLOSED -> TargetBadgeStatus.CLOSED
            MergeRequestState.MERGED -> TargetBadgeStatus.MERGED
        }))
75 76
        badges.add(TargetBadge.Text(project.name, AppTarget.PROJECT, project.id))
        badges.add(TargetBadge.Text(mr.author.username, AppTarget.USER, mr.author.id))
77 78 79 80
        badges.add(TargetBadge.Icon(TargetBadgeIcon.COMMENTS, mr.userNotesCount))
        badges.add(TargetBadge.Icon(TargetBadgeIcon.UP_VOTES, mr.upvotes))
        badges.add(TargetBadge.Icon(TargetBadgeIcon.DOWN_VOTES, mr.downvotes))
        mr.labels.forEach { label -> badges.add(TargetBadge.Text(label)) }
81 82 83 84 85 86 87 88

        return TargetHeader(
                mr.author,
                TargetHeaderIcon.NONE,
                TargetHeaderTitle.Event(
                        mr.author.name,
                        EventAction.CREATED,
                        "${AppTarget.MERGE_REQUEST} !${mr.iid}",
89
                        project.name
90
                ),
91
                mr.title ?: "",
92
                mr.createdAt,
93
                AppTarget.MERGE_REQUEST,
94
                mr.id,
95
                TargetInternal(mr.projectId, mr.iid),
96 97 98
                badges
        )
    }
99 100 101 102 103 104 105 106

    fun getMergeRequest(
            projectId: Long,
            mergeRequestId: Long
    ) = api
            .getMergeRequest(projectId, mergeRequestId)
            .subscribeOn(schedulers.io())
            .observeOn(schedulers.ui())
107 108 109 110 111

    fun getMergeRequestNotes(
            projectId: Long,
            mergeRequestId: Long,
            orderBy: OrderBy? = null,
112
            sort: Sort? = Sort.ASC
113 114 115 116
    ) = api
            .getMergeRequestNotes(projectId, mergeRequestId, orderBy, sort)
            .subscribeOn(schedulers.io())
            .observeOn(schedulers.ui())
Ilya Gulya's avatar
Ilya Gulya committed
117
}