sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/pkg/config/org/org.go (about)

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package org
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"sigs.k8s.io/prow/pkg/github"
    23  )
    24  
    25  // FullConfig stores the full configuration to be used by the tool, mapping
    26  // orgs to their configuration at the top level under an `orgs` key.
    27  type FullConfig struct {
    28  	Orgs map[string]Config `json:"orgs,omitempty"`
    29  }
    30  
    31  // Metadata declares metadata about the GitHub org.
    32  //
    33  // See https://developer.github.com/v3/orgs/#edit-an-organization
    34  type Metadata struct {
    35  	BillingEmail                 *string                     `json:"billing_email,omitempty"`
    36  	Company                      *string                     `json:"company,omitempty"`
    37  	Email                        *string                     `json:"email,omitempty"`
    38  	Name                         *string                     `json:"name,omitempty"`
    39  	Description                  *string                     `json:"description,omitempty"`
    40  	Location                     *string                     `json:"location,omitempty"`
    41  	HasOrganizationProjects      *bool                       `json:"has_organization_projects,omitempty"`
    42  	HasRepositoryProjects        *bool                       `json:"has_repository_projects,omitempty"`
    43  	DefaultRepositoryPermission  *github.RepoPermissionLevel `json:"default_repository_permission,omitempty"`
    44  	MembersCanCreateRepositories *bool                       `json:"members_can_create_repositories,omitempty"`
    45  }
    46  
    47  // RepoCreateOptions declares options for creating new repos
    48  // See https://developer.github.com/v3/repos/#create
    49  type RepoCreateOptions struct {
    50  	AutoInit          *bool   `json:"auto_init,omitempty"`
    51  	GitignoreTemplate *string `json:"gitignore_template,omitempty"`
    52  	LicenseTemplate   *string `json:"license_template,omitempty"`
    53  }
    54  
    55  // Repo declares metadata about the GitHub repository
    56  //
    57  // See https://developer.github.com/v3/repos/#edit
    58  type Repo struct {
    59  	Description              *string `json:"description,omitempty"`
    60  	HomePage                 *string `json:"homepage,omitempty"`
    61  	Private                  *bool   `json:"private,omitempty"`
    62  	HasIssues                *bool   `json:"has_issues,omitempty"`
    63  	HasProjects              *bool   `json:"has_projects,omitempty"`
    64  	HasWiki                  *bool   `json:"has_wiki,omitempty"`
    65  	AllowSquashMerge         *bool   `json:"allow_squash_merge,omitempty"`
    66  	AllowMergeCommit         *bool   `json:"allow_merge_commit,omitempty"`
    67  	AllowRebaseMerge         *bool   `json:"allow_rebase_merge,omitempty"`
    68  	SquashMergeCommitTitle   *string `json:"squash_merge_commit_title,omitempty"`
    69  	SquashMergeCommitMessage *string `json:"squash_merge_commit_message,omitempty"`
    70  
    71  	DefaultBranch *string `json:"default_branch,omitempty"`
    72  	Archived      *bool   `json:"archived,omitempty"`
    73  
    74  	Previously []string `json:"previously,omitempty"`
    75  
    76  	OnCreate *RepoCreateOptions `json:"on_create,omitempty"`
    77  }
    78  
    79  // Config declares org metadata as well as its people and teams.
    80  type Config struct {
    81  	Metadata
    82  	Teams   map[string]Team `json:"teams,omitempty"`
    83  	Members []string        `json:"members,omitempty"`
    84  	Admins  []string        `json:"admins,omitempty"`
    85  	Repos   map[string]Repo `json:"repos,omitempty"`
    86  }
    87  
    88  // TeamMetadata declares metadata about the github team.
    89  //
    90  // See https://developer.github.com/v3/teams/#edit-team
    91  type TeamMetadata struct {
    92  	Description *string  `json:"description,omitempty"`
    93  	Privacy     *Privacy `json:"privacy,omitempty"`
    94  }
    95  
    96  // Team declares metadata as well as its poeple.
    97  type Team struct {
    98  	TeamMetadata
    99  	Members     []string        `json:"members,omitempty"`
   100  	Maintainers []string        `json:"maintainers,omitempty"`
   101  	Children    map[string]Team `json:"teams,omitempty"`
   102  
   103  	Previously []string `json:"previously,omitempty"`
   104  
   105  	// This is injected to the Team structure by listing privilege
   106  	// levels on dump and if set by users will cause privileges to
   107  	// be added on sync.
   108  	// https://developer.github.com/v3/teams/#list-team-repos
   109  	// https://developer.github.com/v3/teams/#add-or-update-team-repository
   110  	Repos map[string]github.RepoPermissionLevel `json:"repos,omitempty"`
   111  }
   112  
   113  // Privacy is secret or closed.
   114  //
   115  // See https://developer.github.com/v3/teams/#edit-team
   116  type Privacy string
   117  
   118  const (
   119  	// Closed means it is only visible to org members
   120  	Closed Privacy = "closed"
   121  	// Secret means it is only visible to team members.
   122  	Secret Privacy = "secret"
   123  )
   124  
   125  var privacySettings = map[Privacy]bool{
   126  	Closed: true,
   127  	Secret: true,
   128  }
   129  
   130  // MarshalText returns bytes that equal secret or closed
   131  func (p Privacy) MarshalText() ([]byte, error) {
   132  	return []byte(p), nil
   133  }
   134  
   135  // UnmarshalText returns an error if text != secret or closed
   136  func (p *Privacy) UnmarshalText(text []byte) error {
   137  	v := Privacy(text)
   138  	if _, ok := privacySettings[v]; !ok {
   139  		return fmt.Errorf("bad privacy setting: %s", v)
   140  	}
   141  	*p = v
   142  	return nil
   143  }
   144  
   145  // PruneRepoDefaults finds values in org.Repo config that matches the default
   146  // values replaces them with nil pointer. This reduces the size of an org dump
   147  // by omitting the fields that would be set to the same value when not set at all.
   148  // See https://developer.github.com/v3/repos/#edit
   149  func PruneRepoDefaults(repo Repo) Repo {
   150  	pruneString := func(p **string, def string) {
   151  		if *p != nil && **p == def {
   152  			*p = nil
   153  		}
   154  	}
   155  	pruneBool := func(p **bool, def bool) {
   156  		if *p != nil && **p == def {
   157  			*p = nil
   158  		}
   159  	}
   160  
   161  	pruneString(&repo.Description, "")
   162  	pruneString(&repo.HomePage, "")
   163  
   164  	pruneBool(&repo.Private, false)
   165  	pruneBool(&repo.HasIssues, true)
   166  	// Projects' defaults depend on org setting, do not prune
   167  	pruneBool(&repo.HasWiki, true)
   168  	pruneBool(&repo.AllowRebaseMerge, true)
   169  	pruneBool(&repo.AllowSquashMerge, true)
   170  	pruneBool(&repo.AllowMergeCommit, true)
   171  
   172  	pruneBool(&repo.Archived, false)
   173  	pruneString(&repo.DefaultBranch, "master")
   174  
   175  	return repo
   176  }