-
Notifications
You must be signed in to change notification settings - Fork 12
/
add.go
111 lines (95 loc) · 3.15 KB
/
add.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
// Copyright (c) 2022 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.
// nolint: dupl // ignore similar code among actions
package repo
import (
"fmt"
"github.com/go-vela/cli/internal/output"
"github.com/go-vela/sdk-go/vela"
"github.com/go-vela/types/constants"
"github.com/go-vela/types/library"
"github.com/sirupsen/logrus"
)
// Add creates a repository based off the provided configuration.
func (c *Config) Add(client *vela.Client) error {
logrus.Debug("executing add for repo configuration")
// create the repository object
//
// https://pkg.go.dev/github.com/go-vela/types/library?tab=doc#Repo
r := &library.Repo{
Org: vela.String(c.Org),
Name: vela.String(c.Name),
FullName: vela.String(fmt.Sprintf("%s/%s", c.Org, c.Name)),
Link: vela.String(c.Link),
Clone: vela.String(c.Clone),
Branch: vela.String(c.Branch),
BuildLimit: vela.Int64(c.BuildLimit),
Timeout: vela.Int64(c.Timeout),
Counter: vela.Int(c.Counter),
Visibility: vela.String(c.Visibility),
Private: vela.Bool(c.Private),
Trusted: vela.Bool(c.Trusted),
Active: vela.Bool(c.Active),
PipelineType: vela.String(c.PipelineType),
}
// iterate through all events provided
for _, event := range c.Events {
// check if the repository should allow push events
if event == constants.EventPush {
r.AllowPush = vela.Bool(true)
}
// check if the repository should allow pull_request events
if event == constants.EventPull {
r.AllowPull = vela.Bool(true)
}
// check if the repository should allow tag events
if event == constants.EventTag {
r.AllowTag = vela.Bool(true)
}
// check if the repository should allow deployment events
if event == constants.EventDeploy {
r.AllowDeploy = vela.Bool(true)
}
// check if the repository should allow comment events
if event == constants.EventComment {
r.AllowComment = vela.Bool(true)
}
}
logrus.Tracef("adding repo %s/%s", c.Org, c.Name)
// send API call to add a repository
//
// https://pkg.go.dev/github.com/go-vela/sdk-go/vela?tab=doc#RepoService.Add
repo, _, err := client.Repo.Add(r)
if err != nil {
return err
}
// handle the output based off the provided configuration
switch c.Output {
case output.DriverDump:
// output the repository in dump format
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Dump
return output.Dump(repo)
case output.DriverJSON:
// output the repository in JSON format
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#JSON
return output.JSON(repo)
case output.DriverSpew:
// output the repository in spew format
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Spew
return output.Spew(repo)
case output.DriverYAML:
// output the repository in YAML format
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#YAML
return output.YAML(repo)
default:
// output the repository in stdout format
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/output?tab=doc#Stdout
return output.Stdout(repo)
}
}