org.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package org
  14. import (
  15. "fmt"
  16. )
  17. // Metadata declares metadata about the GitHub org.
  18. //
  19. // See https://developer.github.com/v3/orgs/#edit-an-organization
  20. type Metadata struct {
  21. BillingEmail *string `json:"billing_email,omitempty"`
  22. Company *string `json:"company,omitempty"`
  23. Email *string `json:"email,omitempty"`
  24. Name *string `json:"name,omitempty"`
  25. Description *string `json:"description,omitempty"`
  26. Location *string `json:"location,omitempty"`
  27. HasOrganizationProjects *bool `json:"has_organization_projects,omitempty"`
  28. HasRepositoryProjects *bool `json:"has_repository_projects,omitempty"`
  29. DefaultRepositoryPermission *RepoPermissionLevel `json:"default_repository_permission,omitempty"`
  30. MembersCanCreateRepositories *bool `json:"members_can_create_repositories,omitempty"`
  31. }
  32. // Config declares org metadata as well as its people and teams.
  33. type Config struct {
  34. Metadata
  35. Teams map[string]Team `json:"teams,omitempty"`
  36. Members []string `json:"members,omitempty"`
  37. Admins []string `json:"admins,omitempty"`
  38. }
  39. // TeamMetadata declares metadata about the github team.
  40. //
  41. // See https://developer.github.com/v3/teams/#edit-team
  42. type TeamMetadata struct {
  43. Description *string `json:"description,omitempty"`
  44. Privacy *Privacy `json:"privacy,omitempty"`
  45. }
  46. // Team declares metadata as well as its poeple.
  47. type Team struct {
  48. TeamMetadata
  49. Members []string `json:"members,omitempty"`
  50. Maintainers []string `json:"maintainers,omitempty"`
  51. Children map[string]Team `json:"teams,omitempty"`
  52. Previously []string `json:"previously,omitempty"`
  53. }
  54. // RepoPermissionLevel is admin, write, read or none.
  55. //
  56. // See https://developer.github.com/v3/repos/collaborators/#review-a-users-permission-level
  57. type RepoPermissionLevel string
  58. const (
  59. // Read allows pull but not push
  60. Read RepoPermissionLevel = "read"
  61. // Write allows Read plus push
  62. Write RepoPermissionLevel = "write"
  63. // Admin allows Write plus change others' rights.
  64. Admin RepoPermissionLevel = "admin"
  65. // None disallows everything
  66. None RepoPermissionLevel = "none"
  67. )
  68. var repoPermissionLevels = map[RepoPermissionLevel]bool{
  69. Read: true,
  70. Write: true,
  71. Admin: true,
  72. None: true,
  73. }
  74. // MarshalText returns the byte representation of the permission
  75. func (l RepoPermissionLevel) MarshalText() ([]byte, error) {
  76. return []byte(l), nil
  77. }
  78. // UnmarshalText validates the text is a valid string
  79. func (l *RepoPermissionLevel) UnmarshalText(text []byte) error {
  80. v := RepoPermissionLevel(text)
  81. if _, ok := repoPermissionLevels[v]; !ok {
  82. return fmt.Errorf("bad repo permission: %s not in %v", v, repoPermissionLevels)
  83. }
  84. *l = v
  85. return nil
  86. }
  87. // Privacy is secret or closed.
  88. //
  89. // See https://developer.github.com/v3/teams/#edit-team
  90. type Privacy string
  91. const (
  92. // Closed means it is only visible to org members
  93. Closed Privacy = "closed"
  94. // Secret means it is only visible to team members.
  95. Secret Privacy = "secret"
  96. )
  97. var privacySettings = map[Privacy]bool{
  98. Closed: true,
  99. Secret: true,
  100. }
  101. // MarshalText returns bytes that equal secret or closed
  102. func (p Privacy) MarshalText() ([]byte, error) {
  103. return []byte(p), nil
  104. }
  105. // UnmarshalText returns an error if text != secret or closed
  106. func (p *Privacy) UnmarshalText(text []byte) error {
  107. v := Privacy(text)
  108. if _, ok := privacySettings[v]; !ok {
  109. return fmt.Errorf("bad privacy setting: %s", v)
  110. }
  111. *p = v
  112. return nil
  113. }