/
github.go
145 lines (118 loc) · 3.59 KB
/
github.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
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package github includes all of the client calls to github for building
// automation tools for deploystack
package github
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/google/go-github/github"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
)
// GITHUBHOST is the hostname of where to find github on the web
const GITHUBHOST = "github.com"
// Repo contains the details of a github repo for the purpose of downloading
type Repo struct {
Name string `json:"name" yaml:"name"`
Owner string `json:"owner" yaml:"owner"`
Branch string `json:"branch" yaml:"branch"`
Description string `json:"description" yaml:"description"`
}
// URL returns the github url of this repo
func (r Repo) URL() string {
return fmt.Sprintf("https://%s/%s/%s", GITHUBHOST, r.Owner, r.Name)
}
// ReferenceName is a shortcut to the reference for the current branch
func (r Repo) ReferenceName() string {
return fmt.Sprintf("refs/heads/%s", r.Branch)
}
// NewRepo generates Github from a url that might contain branch information
//
// Deprecated: Use New() Instead
func NewRepo(repo string) Repo {
return New("", SiteURL(repo))
}
// Populate gets metadata from github
func (r *Repo) Populate() error {
client := github.NewClient(nil)
repo, _, err := client.Repositories.Get(context.Background(), r.Owner, r.Name)
if err != nil {
return err
}
r.Description = repo.GetDescription()
return nil
}
// Path returns where this repo should exist locally given the input path
func (r Repo) Path(path string) string {
result := filepath.Base(r.Name)
result = fmt.Sprintf("%s/%s", path, result)
return result
}
// Clone performs a git clone to the directory of our choosing
func (r Repo) Clone(path string) error {
if _, err := os.Stat(path); err == nil {
return fmt.Errorf("directory (%s) already exists", path)
}
options := &git.CloneOptions{
URL: r.URL(),
ReferenceName: plumbing.ReferenceName(r.ReferenceName()),
}
if _, err := git.PlainClone(path, false, options); err != nil {
return fmt.Errorf("cannot clone repo (%s) : %s", r.URL(), err)
}
return nil
}
// Option defines option functions that can be passed in to modify repos on New
type Option func(*Repo)
// New generates Github from a series of options
func New(name string, options ...Option) Repo {
r := Repo{
Name: name,
Owner: "GoogleCloudPlatform",
Branch: "main",
}
for _, f := range options {
f(&r)
}
return r
}
// Owner sets the owner on a repo
func Owner(o string) Option {
return func(r *Repo) {
r.Owner = o
}
}
// Branch sets the branch on a repo
func Branch(b string) Option {
return func(r *Repo) {
r.Branch = b
}
}
// SiteURL sets the owner, name, and branch of a repo based on a URL
func SiteURL(u string) Option {
return func(r *Repo) {
input := strings.ReplaceAll(u, fmt.Sprintf("https://%s/", GITHUBHOST), "")
sl := strings.Split(input, "/")
r.Owner = sl[0]
r.Name = sl[1]
if strings.Contains(u, "/tree/") {
end := strings.Index(u, "/tree/")
r.Branch = u[end+6:]
}
}
}