|
| 1 | +/* |
| 2 | +Copyright 2020 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 util |
| 18 | + |
| 19 | +import ( |
| 20 | + "bufio" |
| 21 | + "fmt" |
| 22 | + "io/ioutil" |
| 23 | + "net/http" |
| 24 | + "os" |
| 25 | + "regexp" |
| 26 | + "sort" |
| 27 | + |
| 28 | + "sigs.k8s.io/yaml" |
| 29 | +) |
| 30 | + |
| 31 | +var ( |
| 32 | + groups []string |
| 33 | + prrApprovers []string |
| 34 | +) |
| 35 | + |
| 36 | +// Groups returns a list of Kubernetes groups (SIGs, Working Groups, User Groups). |
| 37 | +func Groups() []string { |
| 38 | + return groups |
| 39 | +} |
| 40 | + |
| 41 | +// PRRApprovers returns a list of PRR approvers. |
| 42 | +func PRRApprovers() []string { |
| 43 | + return prrApprovers |
| 44 | +} |
| 45 | + |
| 46 | +func init() { |
| 47 | + var err error |
| 48 | + groups, err = fetchGroups() |
| 49 | + if err != nil { |
| 50 | + fmt.Fprintf(os.Stderr, "%v\n", err) |
| 51 | + os.Exit(1) |
| 52 | + } |
| 53 | + prrApprovers, err = fetchPRRApprovers() |
| 54 | + if err != nil { |
| 55 | + fmt.Fprintf(os.Stderr, "%v\n", err) |
| 56 | + os.Exit(1) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func fetchGroups() ([]string, error) { |
| 61 | + resp, err := http.Get("https://raw.githubusercontent.com/kubernetes/community/master/sigs.yaml") |
| 62 | + if err != nil { |
| 63 | + return nil, fmt.Errorf("unable to fetch list of sigs: %v", err) |
| 64 | + } |
| 65 | + defer resp.Body.Close() |
| 66 | + if resp.StatusCode != http.StatusOK { |
| 67 | + return nil, fmt.Errorf("invalid status code when fetching list of sigs: %d", resp.StatusCode) |
| 68 | + } |
| 69 | + re := regexp.MustCompile(`- dir: (.*)$`) |
| 70 | + |
| 71 | + var result []string |
| 72 | + scanner := bufio.NewScanner(resp.Body) |
| 73 | + for scanner.Scan() { |
| 74 | + match := re.FindStringSubmatch(scanner.Text()) |
| 75 | + if len(match) > 0 { |
| 76 | + result = append(result, match[1]) |
| 77 | + } |
| 78 | + } |
| 79 | + if err := scanner.Err(); err != nil { |
| 80 | + return nil, fmt.Errorf("unable to scan list of sigs: %v", err) |
| 81 | + } |
| 82 | + sort.Strings(result) |
| 83 | + return result, nil |
| 84 | +} |
| 85 | + |
| 86 | +func fetchPRRApprovers() ([]string, error) { |
| 87 | + resp, err := http.Get("https://raw.githubusercontent.com/kubernetes/enhancements/master/OWNERS_ALIASES") |
| 88 | + if err != nil { |
| 89 | + return nil, fmt.Errorf("unable to fetch list of aliases: %v", err) |
| 90 | + } |
| 91 | + defer resp.Body.Close() |
| 92 | + if resp.StatusCode != http.StatusOK { |
| 93 | + return nil, fmt.Errorf("invalid status code when fetching list of aliases: %d", resp.StatusCode) |
| 94 | + } |
| 95 | + |
| 96 | + body, err := ioutil.ReadAll(resp.Body) |
| 97 | + if err != nil { |
| 98 | + return nil, fmt.Errorf("unable to read aliases content: %v", err) |
| 99 | + } |
| 100 | + |
| 101 | + config := &struct { |
| 102 | + Data map[string][]string `json:"aliases,omitempty"` |
| 103 | + }{} |
| 104 | + if err := yaml.Unmarshal(body, config); err != nil { |
| 105 | + return nil, fmt.Errorf("unable to read parse aliases content: %v", err) |
| 106 | + } |
| 107 | + var result []string |
| 108 | + result = append(result, config.Data["prod-readiness-approvers"]...) |
| 109 | + |
| 110 | + sort.Strings(result) |
| 111 | + return result, nil |
| 112 | +} |
0 commit comments