Branches Api
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
Link copied to clipboard
@DELETE(value = "projects/{id}/repository/merged_branches" )
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}" )
Delete a branch from the repository
Link copied to clipboard
@GET(value = "projects/{id}/protected_branches" )
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
Link copied to clipboard
@GET(value = "projects/{id}/repository/branches/{branch}" )
Get single branch
Link copied to clipboard
@GET(value = "projects/{id}/protected_branches/{name}" )
Get single protected branch
Link copied to clipboard
@POST(value = "projects/{id}/protected_branches" )
Link copied to clipboard
@DELETE(value = "projects/{id}/protected_branches/{name}" )
Un protects the given protected branch or wildcard protected branch.
Link copied to clipboard
@PATCH(value = "projects/{id}/protected_branches/{name}" )