Add protected branch support (#50)

This commit is contained in:
Patrick Stevens
2023-07-24 19:42:14 +01:00
committed by GitHub
parent 3b6b0664ea
commit 8618416a00
4 changed files with 29 additions and 1 deletions

View File

@@ -38,12 +38,14 @@ type ProtectedBranch =
{ {
BranchName : string BranchName : string
BlockOnOutdatedBranch : bool option BlockOnOutdatedBranch : bool option
RequiredStatusChecks : string list option
} }
static member OfSerialised (s : SerialisedProtectedBranch) : ProtectedBranch = static member OfSerialised (s : SerialisedProtectedBranch) : ProtectedBranch =
{ {
BranchName = s.BranchName BranchName = s.BranchName
BlockOnOutdatedBranch = Option.ofNullable s.BlockOnOutdatedBranch BlockOnOutdatedBranch = Option.ofNullable s.BlockOnOutdatedBranch
RequiredStatusChecks = Option.ofObj s.RequiredStatusChecks |> Option.map List.ofArray
} }
type NativeRepo = type NativeRepo =
@@ -185,7 +187,7 @@ type Repo =
elif mirror.Length = 1 then Some mirror.[0] elif mirror.Length = 1 then Some mirror.[0]
else failwith "Multiple mirrors not supported yet" else failwith "Multiple mirrors not supported yet"
let! branchProtections = let! (branchProtections : Gitea.BranchProtection[]) =
client.RepoListBranchProtection (u.Owner.LoginName, u.FullName) client.RepoListBranchProtection (u.Owner.LoginName, u.FullName)
|> Async.AwaitTask |> Async.AwaitTask
@@ -223,6 +225,11 @@ type Repo =
{ {
BranchName = bp.BranchName BranchName = bp.BranchName
BlockOnOutdatedBranch = bp.BlockOnOutdatedBranch BlockOnOutdatedBranch = bp.BlockOnOutdatedBranch
RequiredStatusChecks =
if bp.EnableStatusCheck = Some true then
bp.StatusCheckContexts |> List.ofArray |> Some
else
None
} }
) )
|> Set.ofSeq |> Set.ofSeq

View File

@@ -484,6 +484,8 @@ module Gitea =
do! do!
// TODO: lift this out to a function and then put it into the new-repo flow too // TODO: lift this out to a function and then put it into the new-repo flow too
// The current behaviour is kind of desirable, because it gives you a chance to push to
// the protected branch before it becomes protected.
let extraActualProtected = let extraActualProtected =
Set.difference actual.ProtectedBranches desired.ProtectedBranches Set.difference actual.ProtectedBranches desired.ProtectedBranches
@@ -548,6 +550,12 @@ module Gitea =
let s = Gitea.EditBranchProtectionOption () let s = Gitea.EditBranchProtectionOption ()
s.BlockOnOutdatedBranch <- y.BlockOnOutdatedBranch s.BlockOnOutdatedBranch <- y.BlockOnOutdatedBranch
match y.RequiredStatusChecks with
| None -> s.EnableStatusCheck <- Some false
| Some checks ->
s.EnableStatusCheck <- Some true
s.StatusCheckContexts <- Array.ofList checks
let! _ = let! _ =
client.RepoEditBranchProtection (user, r, y.BranchName, s) client.RepoEditBranchProtection (user, r, y.BranchName, s)
|> Async.AwaitTask |> Async.AwaitTask

View File

@@ -227,6 +227,16 @@
}, },
"blockOnOutdatedBranch": { "blockOnOutdatedBranch": {
"type": "boolean" "type": "boolean"
},
"requiredStatusChecks": {
"type": [
"array",
"null"
],
"description": "A list of status check patterns; merge into this branch will be blocked unless all these checks have run and passed. (Probably go with alphanumeric strings, I can't find any docs.)",
"items": {
"type": "string"
}
} }
} }
} }

View File

@@ -27,6 +27,9 @@ type SerialisedProtectedBranch =
BranchName : string BranchName : string
[<JsonProperty(Required = Required.DisallowNull)>] [<JsonProperty(Required = Required.DisallowNull)>]
BlockOnOutdatedBranch : Nullable<bool> BlockOnOutdatedBranch : Nullable<bool>
[<JsonProperty(Required = Required.Default)>]
[<Description "A list of status check patterns; merge into this branch will be blocked unless all these checks have run and passed. (Probably go with alphanumeric strings, I can't find any docs.)">]
RequiredStatusChecks : string array
} }
[<RequireQualifiedAccess>] [<RequireQualifiedAccess>]