BranchesApi

interface BranchesApi

Author

sunlulu.tomato

Samples

import com.mato.gitlab.GitlabService
import com.mato.gitlab.response.ProtectedBranch
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.MethodOrderer
import org.junit.jupiter.api.Order
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestMethodOrder
fun main() { 
   //sampleStart 
   /**
 * @author sunlulu.tomato
 * @date 02/19/2024
 */
@TestMethodOrder(MethodOrderer.OrderAnnotation::class)
class BranchesApiTest : BaseTestCase {

    private val branchApi = GitlabService.get<BranchesApi>()
    private val projectId = TestingEnv.GITLAB_TEST_PROJECT_ID.get()
    private val branchName = TestingEnv.GITLAB_TEST_BRANCH.get()
    private val newBranchName = "new_${branchName}_${System.currentTimeMillis()}"
    private val newBranchRegex = "new_${branchName}_*"

    @Test
    @Order(1)
    fun testGetRepoBranches() = runBlocking {
        val repoBranches = branchApi.getRepoBranches(projectId)
        repoBranches.assertSuccess()
        assertTrue(repoBranches.getOrThrow().isNotEmpty())
    }

    @Test
    @Order(2)
    fun testGetSingleBranchInfo() = runBlocking {
        val branch = branchApi.getSingleBranch(projectId, branchName)
        branch.assertSuccess()
        assertEquals(branch.getOrThrow().name, branchName)
    }

    @Test
    @Order(3)
    fun testCreateReoBranch() = runBlocking {
        val createBranch = branchApi.createRepoBranch(projectId, newBranchName, branchName)
        createBranch.assertSuccess()
        assertEquals(createBranch.getOrThrow().name, newBranchName)
    }


    @Test
    @Order(4)
    fun testGetProtectedBranches() = runBlocking {
        val protectedBranches = branchApi.getProtectedBranches(projectId)
        protectedBranches.assertSuccess()
        assertTrue(protectedBranches.getOrThrow().isNotEmpty())
    }

    @Test
    @Order(5)
    fun testProtectBranch() = runBlocking {
        val protectBranch = branchApi.protectBranches(
            projectId,
            newBranchRegex,
            allowForcePush = true,
            pushAccessLevel = ProtectedBranch.AccessLevel.Developer.value
        )
        protectBranch.assertSuccess()
        assertEquals(protectBranch.getOrThrow().name, newBranchRegex)
    }

    @Test
    @Order(6)
    fun testGetSingleProtectedBranch() = runBlocking {
        val protectedBranch = branchApi.getSingleProtectedBranch(projectId, newBranchName)
        protectedBranch.assertSuccess()
        assertEquals(protectedBranch.getOrThrow().name, newBranchName)
    }

    @Test
    @Order(7)
    fun testUpdateProtectedBranch() = runBlocking {
        val updateProtectedBranch = branchApi.updateProtectedBranch(
            projectId, newBranchName, allowForcePush = false, codeOwnerApprovalRequired = false
        )
        updateProtectedBranch.assertSuccess()
        assertEquals(updateProtectedBranch.getOrThrow().name, newBranchName)
    }

    @Test
    @Order(8)
    fun testUnprotectBranches() = runBlocking {
        val unprotectBranch = branchApi.unprotectBranches(projectId, newBranchName)
        unprotectBranch.assertSuccess()
    }

    @Test
    @Order(9)
    fun testDeleteBranch() = runBlocking {
        val createdBranches = branchApi.getRepoBranches(projectId, regex = newBranchRegex)
        createdBranches.assertSuccess()
        createdBranches.getOrThrow().forEach {
                val delete = branchApi.deleteRepoBranch(projectId, it.name)
                delete.assertSuccess()
            }
    }

    @Test
    @Order(10)
    fun testDeleteMergedBranches() = runBlocking {
        val deleteMergedBranches = branchApi.deleteMergedBranches(projectId)
        deleteMergedBranches.assertSuccess()
    }
} 
   //sampleEnd
}

Functions

Link copied to clipboard
@POST(value = "projects/{id}/repository/branches")
abstract suspend fun createRepoBranch(@Path(value = "id") id: String, @Query(value = "branch") branch: String, @Query(value = "ref") ref: String): Result<Branch>

Create a new branch in the repository

Link copied to clipboard
@DELETE(value = "projects/{id}/repository/merged_branches")
abstract suspend fun deleteMergedBranches(@Path(value = "id") id: String): Result<Unit>

Delete all branches that are merged into project's default branch. Protected branches are not deleted as part of this operation.

Link copied to clipboard
@DELETE(value = "projects/{id}/repository/branches/{branch}")
abstract suspend fun deleteRepoBranch(@Path(value = "id") id: String, @Path(value = "branch") branch: String): Result<Unit>

Delete a branch from the repository

Link copied to clipboard
@GET(value = "projects/{id}/protected_branches")
abstract suspend fun getProtectedBranches(@Path(value = "id") id: String, @Query(value = "search") search: String? = null): Result<List<ProtectedBranch>>

Gets a list of protected branches from a project as they are defined in the UI. If a wildcard is set, it is returned instead of the exact name of the branches that match that wildcard.

Link copied to clipboard
@GET(value = "projects/{id}/repository/branches")
abstract suspend fun getRepoBranches(@Path(value = "id") id: String, @Query(value = "search") search: String? = null, @Query(value = "regex") regex: String? = null): Result<List<Branch>>

Get repo branches

Link copied to clipboard
@GET(value = "projects/{id}/repository/branches/{branch}")
abstract suspend fun getSingleBranch(@Path(value = "id") id: String, @Path(value = "branch") branch: String): Result<Branch>

Get single branch

Link copied to clipboard
@GET(value = "projects/{id}/protected_branches/{name}")
abstract suspend fun getSingleProtectedBranch(@Path(value = "id") id: String, @Path(value = "name") name: String): Result<ProtectedBranch>

Get single protected branch

Link copied to clipboard
@POST(value = "projects/{id}/protected_branches")
abstract suspend fun protectBranches(@Path(value = "id") id: String, @Query(value = "name") name: String, @Query(value = "allow_force_push") allowForcePush: Boolean? = null, @Query(value = "code_owner_approval_required") codeOwnerApprovalRequired: Boolean? = null, @Query(value = "push_access_level") pushAccessLevel: Int? = null, @Query(value = "merge_access_level") mergeAccessLevel: Int? = null, @Query(value = "unprotect_access_level") unprotectAccessLevel: Int? = null, @Body option: ProtectBranchOption = ProtectBranchOption()): Result<ProtectedBranch>
Link copied to clipboard
@DELETE(value = "projects/{id}/protected_branches/{name}")
abstract suspend fun unprotectBranches(@Path(value = "id") id: String, @Path(value = "name") name: String): Result<Unit>

Un protects the given protected branch or wildcard protected branch.

Link copied to clipboard
@PATCH(value = "projects/{id}/protected_branches/{name}")
abstract suspend fun updateProtectedBranch(@Path(value = "id") id: String, @Path(value = "name") name: String, @Query(value = "allow_force_push") allowForcePush: Boolean? = null, @Query(value = "code_owner_approval_required") codeOwnerApprovalRequired: Boolean? = null, @Body option: ProtectBranchOption = ProtectBranchOption()): Result<ProtectedBranch>