forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshared.go
More file actions
92 lines (77 loc) · 2.07 KB
/
shared.go
File metadata and controls
92 lines (77 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package shared
import (
"errors"
"fmt"
"time"
"github.com/cli/cli/v2/api"
"github.com/cli/cli/v2/pkg/cmdutil"
)
type Visibility string
const (
All = "all"
Private = "private"
Selected = "selected"
)
type VariableEntity string
const (
Repository = "repository"
Organization = "organization"
Environment = "environment"
)
type Variable struct {
Name string `json:"name"`
Value string `json:"value"`
UpdatedAt time.Time `json:"updated_at"`
CreatedAt time.Time `json:"created_at"`
Visibility Visibility `json:"visibility"`
SelectedReposURL string `json:"selected_repositories_url"`
NumSelectedRepos int `json:"num_selected_repos"`
}
var VariableJSONFields = []string{
"name",
"value",
"visibility",
"updatedAt",
"createdAt",
"numSelectedRepos",
"selectedReposURL",
}
func (v *Variable) ExportData(fields []string) map[string]interface{} {
return cmdutil.StructExportData(v, fields)
}
func GetVariableEntity(orgName, envName string) (VariableEntity, error) {
orgSet := orgName != ""
envSet := envName != ""
if orgSet && envSet {
return "", errors.New("cannot specify multiple variable entities")
}
if orgSet {
return Organization, nil
}
if envSet {
return Environment, nil
}
return Repository, nil
}
func PopulateMultipleSelectedRepositoryInformation(apiClient *api.Client, host string, variables []Variable) error {
for i, variable := range variables {
if err := PopulateSelectedRepositoryInformation(apiClient, host, &variable); err != nil {
return err
}
variables[i] = variable
}
return nil
}
func PopulateSelectedRepositoryInformation(apiClient *api.Client, host string, variable *Variable) error {
if variable.SelectedReposURL == "" {
return nil
}
response := struct {
TotalCount int `json:"total_count"`
}{}
if err := apiClient.REST(host, "GET", variable.SelectedReposURL, nil, &response); err != nil {
return fmt.Errorf("failed determining selected repositories for %s: %w", variable.Name, err)
}
variable.NumSelectedRepos = response.TotalCount
return nil
}