Allow setting collaborators (#51)

This commit is contained in:
Patrick Stevens
2023-07-25 20:04:28 +01:00
committed by GitHub
parent 8618416a00
commit f4c1e5d400
5 changed files with 80 additions and 7 deletions

View File

@@ -66,6 +66,7 @@ type NativeRepo =
AllowMergeCommits : bool option
Mirror : PushMirror option
ProtectedBranches : ProtectedBranch Set
Collaborators : string Set
}
static member Default : NativeRepo =
@@ -86,6 +87,7 @@ type NativeRepo =
AllowMergeCommits = Some false
Mirror = None
ProtectedBranches = Set.empty
Collaborators = Set.empty
}
member this.OverrideDefaults () =
@@ -110,6 +112,7 @@ type NativeRepo =
AllowMergeCommits = this.AllowMergeCommits |> Option.orElse NativeRepo.Default.AllowMergeCommits
Mirror = this.Mirror
ProtectedBranches = this.ProtectedBranches // TODO should this replace null with empty?
Collaborators = this.Collaborators
}
static member internal OfSerialised (s : SerialisedNativeRepo) =
@@ -133,6 +136,10 @@ type NativeRepo =
match s.ProtectedBranches with
| null -> Set.empty
| a -> a |> Seq.map ProtectedBranch.OfSerialised |> Set.ofSeq
Collaborators =
match s.Collaborators with
| null -> Set.empty
| l -> Set.ofArray l
}
type GitHubRepo =
@@ -180,7 +187,10 @@ type Repo =
|> async.Return
else
async {
let! mirror = getAllPushMirrors client u.Owner.LoginName u.FullName
let! mirror =
getAllPaginated (fun page count ->
client.RepoListPushMirrors (u.Owner.LoginName, u.FullName, Some page, Some count)
)
let mirror =
if mirror.Length = 0 then None
@@ -191,6 +201,11 @@ type Repo =
client.RepoListBranchProtection (u.Owner.LoginName, u.FullName)
|> Async.AwaitTask
let! (collaborators : Gitea.User[]) =
getAllPaginated (fun page count ->
client.RepoListCollaborators (u.Owner.LoginName, u.FullName, Some page, Some count)
)
return
{
@@ -233,6 +248,7 @@ type Repo =
}
)
|> Set.ofSeq
Collaborators = collaborators |> Seq.map (fun user -> user.LoginName) |> Set.ofSeq
}
|> Some
}

View File

@@ -182,7 +182,10 @@ module Gitea =
options.RemotePassword <- token
options.Interval <- "8h0m0s"
let! mirrors = getAllPushMirrors client user r
let! mirrors =
getAllPaginated (fun page count ->
client.RepoListPushMirrors (user, r, Some page, Some count)
)
match mirrors |> Array.tryFind (fun m -> m.RemoteAddress = options.RemoteAddress) with
| None ->
@@ -482,6 +485,46 @@ module Gitea =
else
async.Return ()
do!
let desiredButNotPresent = Set.difference desired.Collaborators actual.Collaborators
let presentButNotDesired = Set.difference actual.Collaborators desired.Collaborators
[|
desiredButNotPresent
|> Seq.map (fun desired ->
async {
logger.LogTrace (
"Setting collaborator {Collaborator} on repo {User}:{Repo}",
desired,
user,
r
)
do! client.RepoAddCollaborator (user, r, desired) |> Async.AwaitTask
}
)
|> Async.Parallel
|> Async.map (Array.iter id)
presentButNotDesired
|> Seq.map (fun desired ->
async {
logger.LogTrace (
"Deleting collaborator {Collaborator} on repo {User}:{Repo}",
desired,
user,
r
)
do! client.RepoDeleteCollaborator (user, r, desired) |> Async.AwaitTask
}
)
|> Async.Parallel
|> Async.map (Array.iter id)
|]
|> Async.Parallel
|> Async.map (Array.iter id)
do!
// 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

View File

@@ -1,5 +1,6 @@
namespace Gitea.Declarative
open System.Threading.Tasks
open SwaggerProvider
[<AutoOpen>]
@@ -10,12 +11,12 @@ module GiteaClient =
type Gitea = SwaggerClientProvider<Host>
let getAllPushMirrors (client : Gitea.Client) (owner : string) (repoName : string) : Gitea.PushMirror array Async =
let rec go (page : int64) (soFar : Gitea.PushMirror array) =
/// The input function takes page first, then count.
/// Repeatedly calls `f` with increasing page numbers until all results are returned.
let getAllPaginated (f : int64 -> int64 -> 'ret array Task) : 'ret array Async =
let rec go (page : int64) (soFar : 'ret array) =
async {
let! newPage =
client.RepoListPushMirrors (owner, repoName, Some page, Some 100L)
|> Async.AwaitTask
let! newPage = f page 100L |> Async.AwaitTask
let soFar = Array.append soFar newPage

View File

@@ -198,6 +198,16 @@
"items": {
"$ref": "#/definitions/SerialisedProtectedBranch"
}
},
"collaborators": {
"type": [
"array",
"null"
],
"description": "Usernames on this Gitea instance who are collaborators on this repo",
"items": {
"type": "string"
}
}
}
},

View File

@@ -86,6 +86,9 @@ type internal SerialisedNativeRepo =
[<JsonProperty(Required = Required.Default)>]
[<Description "Protected branch configuration">]
ProtectedBranches : SerialisedProtectedBranch array
[<JsonProperty(Required = Required.Default)>]
[<Description "Usernames on this Gitea instance who are collaborators on this repo">]
Collaborators : string array
}
[<Struct>]