diff --git a/src/main/scala/gitbucket/core/api/ApiBranchProtection.scala b/src/main/scala/gitbucket/core/api/ApiBranchProtection.scala index acb4885..1c3eac5 100644 --- a/src/main/scala/gitbucket/core/api/ApiBranchProtection.scala +++ b/src/main/scala/gitbucket/core/api/ApiBranchProtection.scala @@ -5,7 +5,7 @@ /** 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 @@ 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") diff --git a/src/test/scala/gitbucket/core/api/ApiSpecModels.scala b/src/test/scala/gitbucket/core/api/ApiSpecModels.scala index 6c9205d..36c8811 100644 --- a/src/test/scala/gitbucket/core/api/ApiSpecModels.scala +++ b/src/test/scala/gitbucket/core/api/ApiSpecModels.scala @@ -2,6 +2,7 @@ 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 @@ repository = apiRepository ) - val apiBranchProtection = ApiBranchProtection( + val apiBranchProtectionOutput = ApiBranchProtection( info = ProtectedBranchInfo( owner = repo1Name.owner, repository = repo1Name.name, @@ -338,10 +339,23 @@ ) ) + 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 @@ |"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 @@ |"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"} diff --git a/src/test/scala/gitbucket/core/api/JsonFormatSpec.scala b/src/test/scala/gitbucket/core/api/JsonFormatSpec.scala index bc6d63f..484ce7c 100644 --- a/src/test/scala/gitbucket/core/api/JsonFormatSpec.scala +++ b/src/test/scala/gitbucket/core/api/JsonFormatSpec.scala @@ -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 @@ 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))