github.com/jenkins-x/test-infra@v0.0.7/prow/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  
    23  // Metadata declares metadata about the GitHub org.
    24  //
    25  // See https://developer.github.com/v3/orgs/#edit-an-organization
    26  type Metadata struct {
    27  	BillingEmail                 *string              `json:"billing_email,omitempty"`
    28  	Company                      *string              `json:"company,omitempty"`
    29  	Email                        *string              `json:"email,omitempty"`
    30  	Name                         *string              `json:"name,omitempty"`
    31  	Description                  *string              `json:"description,omitempty"`
    32  	Location                     *string              `json:"location,omitempty"`
    33  	HasOrganizationProjects      *bool                `json:"has_organization_projects,omitempty"`
    34  	HasRepositoryProjects        *bool                `json:"has_repository_projects,omitempty"`
    35  	DefaultRepositoryPermission  *RepoPermissionLevel `json:"default_repository_permission,omitempty"`
    36  	MembersCanCreateRepositories *bool                `json:"members_can_create_repositories,omitempty"`
    37  }
    38  
    39  // Config declares org metadata as well as its people and teams.
    40  type Config struct {
    41  	Metadata
    42  	Teams   map[string]Team `json:"teams,omitempty"`
    43  	Members []string        `json:"members,omitempty"`
    44  	Admins  []string        `json:"admins,omitempty"`
    45  }
    46  
    47  // TeamMetadata declares metadata about the github team.
    48  //
    49  // See https://developer.github.com/v3/teams/#edit-team
    50  type TeamMetadata struct {
    51  	Description *string  `json:"description,omitempty"`
    52  	Privacy     *Privacy `json:"privacy,omitempty"`
    53  }
    54  
    55  // Team declares metadata as well as its poeple.
    56  type Team struct {
    57  	TeamMetadata
    58  	Members     []string        `json:"members,omitempty"`
    59  	Maintainers []string        `json:"maintainers,omitempty"`
    60  	Children    map[string]Team `json:"teams,omitempty"`
    61  
    62  	Previously []string `json:"previously,omitempty"`
    63  }
    64  
    65  // RepoPermissionLevel is admin, write, read or none.
    66  //
    67  // See https://developer.github.com/v3/repos/collaborators/#review-a-users-permission-level
    68  type RepoPermissionLevel string
    69  
    70  const (
    71  	// Read allows pull but not push
    72  	Read RepoPermissionLevel = "read"
    73  	// Write allows Read plus push
    74  	Write RepoPermissionLevel = "write"
    75  	// Admin allows Write plus change others' rights.
    76  	Admin RepoPermissionLevel = "admin"
    77  	// None disallows everything
    78  	None RepoPermissionLevel = "none"
    79  )
    80  
    81  var repoPermissionLevels = map[RepoPermissionLevel]bool{
    82  	Read:  true,
    83  	Write: true,
    84  	Admin: true,
    85  	None:  true,
    86  }
    87  
    88  // MarshalText returns the byte representation of the permission
    89  func (l RepoPermissionLevel) MarshalText() ([]byte, error) {
    90  	return []byte(l), nil
    91  }
    92  
    93  // UnmarshalText validates the text is a valid string
    94  func (l *RepoPermissionLevel) UnmarshalText(text []byte) error {
    95  	v := RepoPermissionLevel(text)
    96  	if _, ok := repoPermissionLevels[v]; !ok {
    97  		return fmt.Errorf("bad repo permission: %s not in %v", v, repoPermissionLevels)
    98  	}
    99  	*l = v
   100  	return nil
   101  }
   102  
   103  // Privacy is secret or closed.
   104  //
   105  // See https://developer.github.com/v3/teams/#edit-team
   106  type Privacy string
   107  
   108  const (
   109  	// Closed means it is only visible to org members
   110  	Closed Privacy = "closed"
   111  	// Secret means it is only visible to team members.
   112  	Secret Privacy = "secret"
   113  )
   114  
   115  var privacySettings = map[Privacy]bool{
   116  	Closed: true,
   117  	Secret: true,
   118  }
   119  
   120  // MarshalText returns bytes that equal secret or closed
   121  func (p Privacy) MarshalText() ([]byte, error) {
   122  	return []byte(p), nil
   123  }
   124  
   125  // UnmarshalText returns an error if text != secret or closed
   126  func (p *Privacy) UnmarshalText(text []byte) error {
   127  	v := Privacy(text)
   128  	if _, ok := privacySettings[v]; !ok {
   129  		return fmt.Errorf("bad privacy setting: %s", v)
   130  	}
   131  	*p = v
   132  	return nil
   133  }