Fix branch protection setting API (#2600)

This commit is contained in:
Naoki Takezoe
2020-12-07 02:28:49 +09:00
committed by GitHub
parent 31ace89f43
commit 401728d47f
3 changed files with 50 additions and 13 deletions

View File

@@ -5,7 +5,7 @@ import org.json4s._
/** https://developer.github.com/v3/repos/#enabling-and-disabling-branch-protection */
case class ApiBranchProtection(
url: Option[ApiPath],
url: Option[ApiPath], // for output
enabled: Boolean,
required_status_checks: Option[ApiBranchProtection.Status]
) {
@@ -27,19 +27,28 @@ object ApiBranchProtection {
enabled = info.enabled,
required_status_checks = Some(
Status(
ApiPath(
s"/api/v3/repos/${info.owner}/${info.repository}/branches/${info.branch}/protection/required_status_checks"
Some(
ApiPath(
s"/api/v3/repos/${info.owner}/${info.repository}/branches/${info.branch}/protection/required_status_checks"
)
),
EnforcementLevel(info.enabled && info.contexts.nonEmpty, info.includeAdministrators),
info.contexts,
ApiPath(
s"/api/v3/repos/${info.owner}/${info.repository}/branches/${info.branch}/protection/required_status_checks/contexts"
Some(
ApiPath(
s"/api/v3/repos/${info.owner}/${info.repository}/branches/${info.branch}/protection/required_status_checks/contexts"
)
)
)
)
)
val statusNone = Status(ApiPath(""), Off, Seq.empty, ApiPath(""))
case class Status(url: ApiPath, enforcement_level: EnforcementLevel, contexts: Seq[String], contexts_url: ApiPath)
val statusNone = Status(None, Off, Seq.empty, None)
case class Status(
url: Option[ApiPath], // for output
enforcement_level: EnforcementLevel,
contexts: Seq[String],
contexts_url: Option[ApiPath] // for output
)
sealed class EnforcementLevel(val name: String)
case object Off extends EnforcementLevel("off")
case object NonAdmins extends EnforcementLevel("non_admins")

View File

@@ -2,6 +2,7 @@ package gitbucket.core.api
import java.util.{Calendar, Date, TimeZone}
import gitbucket.core.api.ApiBranchProtection.EnforcementLevel
import gitbucket.core.model._
import gitbucket.core.plugin.PluginInfo
import gitbucket.core.service.ProtectedBranchService.ProtectedBranchInfo
@@ -327,7 +328,7 @@ object ApiSpecModels {
repository = apiRepository
)
val apiBranchProtection = ApiBranchProtection(
val apiBranchProtectionOutput = ApiBranchProtection(
info = ProtectedBranchInfo(
owner = repo1Name.owner,
repository = repo1Name.name,
@@ -338,10 +339,23 @@ object ApiSpecModels {
)
)
val apiBranchProtectionInput = new ApiBranchProtection(
url = None,
enabled = true,
required_status_checks = Some(
ApiBranchProtection.Status(
url = None,
enforcement_level = ApiBranchProtection.Everyone,
contexts = Seq("continuous-integration/travis-ci"),
contexts_url = None
)
)
)
val apiBranch = ApiBranch(
name = "master",
commit = ApiBranchCommit(sha1),
protection = apiBranchProtection
protection = apiBranchProtectionOutput
)(
repositoryName = repo1Name
)
@@ -647,7 +661,7 @@ object ApiSpecModels {
|"url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e/status"
|}""".stripMargin
val jsonBranchProtection =
val jsonBranchProtectionOutput =
"""{
|"url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/branches/master/protection",
|"enabled":true,
@@ -658,10 +672,19 @@ object ApiSpecModels {
|"contexts_url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/branches/master/protection/required_status_checks/contexts"}
|}""".stripMargin
val jsonBranchProtectionInput =
"""{
|"enabled":true,
|"required_status_checks":{
|"enforcement_level":"everyone",
|"contexts":["continuous-integration/travis-ci"]
|}
|}""".stripMargin
val jsonBranch = s"""{
|"name":"master",
|"commit":{"sha":"6dcb09b5b57875f334f61aebed695e2e4193db5e"},
|"protection":$jsonBranchProtection,
|"protection":$jsonBranchProtectionOutput,
|"_links":{
|"self":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/branches/master",
|"html":"http://gitbucket.exmple.com/octocat/Hello-World/tree/master"}

View File

@@ -1,9 +1,11 @@
package gitbucket.core.api
import org.json4s.jackson.JsonMethods
import org.scalatest.funsuite.AnyFunSuite
class JsonFormatSpec extends AnyFunSuite {
import ApiSpecModels._
implicit val format = JsonFormat.jsonFormats
private def expected(json: String) = json.replaceAll("\n", "")
@@ -43,8 +45,11 @@ class JsonFormatSpec extends AnyFunSuite {
test("apiPullRequestReviewComment") {
assert(JsonFormat(apiPullRequestReviewComment) == expected(jsonPullRequestReviewComment))
}
test("apiBranchProtection") {
assert(JsonFormat(apiBranchProtection) == expected(jsonBranchProtection))
test("serialize apiBranchProtection") {
assert(JsonFormat(apiBranchProtectionOutput) == expected(jsonBranchProtectionOutput))
}
test("deserialize apiBranchProtection") {
assert(JsonMethods.parse(jsonBranchProtectionInput).extract[ApiBranchProtection] == apiBranchProtectionInput)
}
test("apiBranch") {
assert(JsonFormat(apiBranch) == expected(jsonBranch))