gitea-tea/modules/print/branch.go

87 lines
2.2 KiB
Go
Raw Normal View History

tea branches list/protect/unprotect (#645) Hello, This is a proposal to support consulting / protecting / unprotecting branches for a specific repository. I copied the existing code for "issues" report and adapted to branches. There is no change of legacy code so I do not expect any impact. Supported commands are "list", "protect", "unprotect": - "List" print the list of branches with some available fields from gitea.Branch type. - "protect" creates a gitea.BranchProtection with some default parameters for some specific branches - "unprotect" destroys gitea.BranchProtection for some specific branches What is printed now could be enriched with additional information gitea datatypes already offer. Could you please evaluate this proposal? I would be happy to receive any comment or remark to take into account. **tea branches unprotect** --login opsi --repo opensky main **tea branches list** --login opsi --repo opensky --fields name,protected,user-can-merge,user-can-push,protection [name protected user-can-merge user-can-push protection] +--------+-----------+----------------+---------------+------------+ | NAME | PROTECTED | USER-CAN-MERGE | USER-CAN-PUSH | PROTECTION | +--------+-----------+----------------+---------------+------------+ | b_test | false | true | true | <None> | | main | false | true | true | <None> | +--------+-----------+----------------+---------------+------------+ **tea branches protect** --login opsi --repo opensky main **tea branches list** --login opsi --repo opensky --fields name,protected,user-can-merge,user-can-push,protection [name protected user-can-merge user-can-push protection] +--------+-----------+----------------+---------------+----------------------+ | NAME | PROTECTED | USER-CAN-MERGE | USER-CAN-PUSH | PROTECTION | +--------+-----------+----------------+---------------+----------------------+ | b_test | false | true | true | <None> | | main | true | true | false | - enable-push: false | | | | | | - approving: - | | | | | | merging: - pushing: | | | | | | | +--------+-----------+----------------+---------------+----------------------+ Following commands run OK: > make test > make fmt > make lint Co-authored-by: Leonard Vimond <leonard.vimond.e@thalesdigital.io> Co-authored-by: techknowlogick <techknowlogick@noreply.gitea.com> Reviewed-on: https://gitea.com/gitea/tea/pulls/645 Reviewed-by: techknowlogick <techknowlogick@noreply.gitea.com> Co-authored-by: leonard.vimond <leonard.vimond@noreply.gitea.com> Co-committed-by: leonard.vimond <leonard.vimond@noreply.gitea.com>
2024-07-26 18:02:07 +02:00
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package print
import (
"fmt"
"code.gitea.io/sdk/gitea"
)
// BranchesList prints a listing of the branches
func BranchesList(branches []*gitea.Branch, protections []*gitea.BranchProtection, output string, fields []string) {
fmt.Println(fields)
printables := make([]printable, len(branches))
for i, branch := range branches {
var protection *gitea.BranchProtection
for _, p := range protections {
if p.BranchName == branch.Name {
protection = p
}
}
printables[i] = &printableBranch{branch, protection}
}
t := tableFromItems(fields, printables, isMachineReadable(output))
t.print(output)
}
type printableBranch struct {
branch *gitea.Branch
protection *gitea.BranchProtection
}
func (x printableBranch) FormatField(field string, machineReadable bool) string {
switch field {
case "name":
return x.branch.Name
case "protected":
return fmt.Sprintf("%t", x.branch.Protected)
case "user-can-merge":
return fmt.Sprintf("%t", x.branch.UserCanMerge)
case "user-can-push":
return fmt.Sprintf("%t", x.branch.UserCanPush)
case "protection":
if x.protection != nil {
approving := ""
for _, entry := range x.protection.ApprovalsWhitelistTeams {
approving += entry + "/"
}
for _, entry := range x.protection.ApprovalsWhitelistUsernames {
approving += entry + "/"
}
merging := ""
for _, entry := range x.protection.MergeWhitelistTeams {
approving += entry + "/"
}
for _, entry := range x.protection.MergeWhitelistUsernames {
approving += entry + "/"
}
pushing := ""
for _, entry := range x.protection.PushWhitelistTeams {
approving += entry + "/"
}
for _, entry := range x.protection.PushWhitelistUsernames {
approving += entry + "/"
}
return fmt.Sprintf(
"- enable-push: %t\n- approving: %s\n- merging: %s\n- pushing: %s\n",
x.protection.EnablePush, approving, merging, pushing,
)
}
return "<None>"
}
return ""
}
// BranchFields are all available fields to print with BranchesList()
var BranchFields = []string{
"name",
"protected",
"user-can-merge",
"user-can-push",
"protection",
}