-
Notifications
You must be signed in to change notification settings - Fork 0
/
repos.go
134 lines (122 loc) · 3.7 KB
/
repos.go
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"github.com/google/go-github/v43/github"
"golang.org/x/net/context"
"log"
"strings"
)
type repositoryInfo struct {
owner string
name string
description string
teams []string
templateRepo string
}
// checkRepository - check if a repository exists
func checkRepository(ctx context.Context,
client *github.Client, owner, repositoryName string) (*repositoryInfo, error) {
repository, _, err := client.Repositories.Get(ctx, owner, repositoryName)
if err != nil {
log.Println("Unable to detect whether repository exists:", err)
return nil, err
}
info := repositoryInfo{
owner: *repository.Owner.Login,
name: *repository.Name,
teams: nil,
templateRepo: "",
}
if repository.Description != nil {
info.description = *repository.Description
}
return &info, nil
}
// isRepository - confirms that the input is the name of a repository within the org
func isRepository(ctx context.Context, client *github.Client, org, entitySlug string) bool {
// repo will never have a @ in it
if strings.Contains(entitySlug, "@") {
return false
}
_, resp, err := client.Organizations.Get(ctx, org)
if err != nil || resp.StatusCode == 404 {
return false
}
_, resp, err = client.Repositories.Get(ctx, org, entitySlug)
if resp.StatusCode == 200 {
return true
}
return false
}
// createRepository - create a new repository within the org
func createRepository(ctx context.Context,
client *github.Client,
info repositoryInfo) (*github.Repository, error) {
exists, _, err := client.Repositories.Get(ctx, info.owner, info.name)
if err != nil {
log.Println("Unable to detect whether repository exists:", err)
return nil, err
}
if exists != nil {
log.Println("Repository exists")
return nil, nil
}
var repository *github.Repository
repository = &github.Repository{
Name: github.String(info.name),
Private: github.Bool(true),
Description: github.String(info.description)}
if info.templateRepo != "" {
template, _, err := client.Repositories.Get(ctx, info.owner, info.templateRepo)
if err != nil {
log.Println("Unable to locate template dir ", info.templateRepo)
} else {
repository.TemplateRepository = template
}
}
repo, _, err := client.Repositories.Create(ctx, info.owner, repository)
if err != nil {
log.Fatal("Creating repo failed:", err)
}
return repo, nil
}
func enableVulnerabilityAlerts(ctx context.Context, client *github.Client, owner, repository string) (bool, error) {
enabled, _, err := client.Repositories.GetVulnerabilityAlerts(ctx, owner, repository)
if err != nil {
log.Println("Unable to find repository", err)
return false, err
}
if enabled {
log.Println("Repository ", repository, "already enabled")
return false, nil
}
_, err = client.Repositories.EnableVulnerabilityAlerts(ctx, owner, repository)
if err != nil {
log.Println("Unable to enable vulnerability alerts for repository", err)
return false, err
}
return true, nil
}
// getRepositoryTeams - get the teams associated with a repository
func getRepositoryTeams(ctx context.Context, client *github.Client, owner, repositoryName string) ([]teamInfo, error) {
// Check the repo exists
_, _, err := client.Repositories.Get(ctx, owner, repositoryName)
if err != nil {
return nil, err
}
var listOptions = github.ListOptions{PerPage: 100}
repoTeams, _, err := client.Repositories.ListTeams(ctx, owner, repositoryName, &listOptions)
if err != nil {
return nil, err
}
var teams []teamInfo
for _, team := range repoTeams {
teams = append(teams, teamInfo{
name: *team.Name,
description: *team.Description,
slug: *team.Slug,
url: *team.HTMLURL,
access: *team.Permission,
})
}
return teams, nil
}