This repository has been archived by the owner on Feb 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathcreate_github.go
197 lines (164 loc) · 5.23 KB
/
create_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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package cmd
import (
"context"
"fmt"
"net/http"
"net/url"
"os"
"os/exec"
"path"
"runtime"
"strings"
"time"
"github.com/inlets/inletsctl/pkg/names"
"github.com/openfaas/ofc-bootstrap/pkg/github"
"github.com/openfaas/ofc-bootstrap/pkg/types"
"github.com/pkg/errors"
"github.com/spf13/cobra"
yaml "gopkg.in/yaml.v2"
)
var createGitHubAppCommand = &cobra.Command{
Use: "create-github-app",
Short: "Create a GitHub App",
Long: `Creates a GitHub App on GitHub.com. This is required for receiving
webhooks, for checking out code for builds, and updating commit statuses.
With a --root-domain of ofc.example.com, your webhooks will be sent to:
https://system.ofc.example.com/github-event`,
SilenceUsage: true,
RunE: createGitHubAppE,
Example: ` ofc-bootstrap create-github-app --root-domain o6s.io \
--name community-cluster
`,
}
func init() {
rootCommand.AddCommand(createGitHubAppCommand)
createGitHubAppCommand.Flags().String("root-domain", "", "The root domain for your GitHub i.e. ofc.example.com")
createGitHubAppCommand.Flags().String("name", "", "Name your GitHub App, or leave blank to get a generated name.")
createGitHubAppCommand.Flags().Bool("insecure", false, "Use HTTP instead of HTTPS for webhooks")
createGitHubAppCommand.Flags().Int("port", 30010, "HTTP port to listen to for GitHub App automation")
}
func createGitHubAppE(command *cobra.Command, _ []string) error {
name := ""
if nameFlagVal, _ := command.Flags().GetString("name"); len(nameFlagVal) > 0 {
name = nameFlagVal
} else {
name = "OFC " + strings.Replace(names.GetRandomName(10), "_", " ", -1)
}
var rootDomain string
if rootDomain, _ = command.Flags().GetString("root-domain"); len(rootDomain) == 0 {
return fmt.Errorf("give a value for --root-domain")
}
port, portErr := command.Flags().GetInt("port")
if portErr != nil {
return fmt.Errorf("--port error: %s", portErr.Error())
}
scheme := "https"
if insecure, _ := command.Flags().GetBool("insecure"); insecure {
scheme = "http"
}
inputMap := map[string]string{
"AppName": name,
"GitHubEvent": fmt.Sprintf("%s://system.%s/github-event", scheme, rootDomain),
}
if _, err := os.Stat(path.Join("./templates", "github", "index.html")); err != nil {
return fmt.Errorf(`cannot find template "index.html", run this command from the ofc-bootstrap repository`)
}
fmt.Printf("Name: %s\tRoot domain: %s\tScheme: %v\n", name, rootDomain, scheme)
launchBrowser := true
context, cancel := context.WithCancel(context.TODO())
defer cancel()
resCh := make(chan github.AppResult)
go func() {
appRes := <-resCh
fmt.Printf("GitHub App result received.\n")
printResult(rootDomain, appRes)
cancel()
}()
if err := receiveGitHubApp(context, inputMap, resCh, launchBrowser, port); err != nil {
return err
}
return nil
}
func receiveGitHubApp(ctx context.Context, inputMap map[string]string, resCh chan github.AppResult, launchBrowser bool, listenPort int) error {
server := &http.Server{
Addr: fmt.Sprintf(":%d", listenPort),
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20, // Max header of 1MB
Handler: http.HandlerFunc(github.MakeHandler(inputMap, resCh)),
}
go func() {
fmt.Printf("Starting local HTTP server on port %d\n", listenPort)
if err := server.ListenAndServe(); err != nil {
panic(err)
}
}()
defer server.Shutdown(ctx)
localURL, err := url.Parse("http://" + "127.0.0.1" + server.Addr)
if err != nil {
return err
}
fmt.Printf("Launching browser: %s\n", localURL.String())
if launchBrowser {
err := launchURL(localURL.String())
if err != nil {
return errors.Wrap(err, "unable to launch browser")
}
}
fmt.Printf("Please complete the workflow in the browser to create your GitHub App.\n")
<-ctx.Done()
return nil
}
func printResult(rootDomain string, appRes github.AppResult) {
p := types.Plan{
RootDomain: rootDomain,
Github: types.Github{
AppID: fmt.Sprintf("%d", appRes.ID),
},
Secrets: []types.KeyValueNamespaceTuple{
{
Name: "github-webhook-secret",
Literals: []types.KeyValueTuple{
{
Name: "github-webhook-secret",
Value: appRes.WebhookSecret,
},
},
Filters: []string{"scm_github"},
Namespace: "openfaas-fn",
},
{
Name: "private-key",
Literals: []types.KeyValueTuple{
{
Name: "private-key",
Value: appRes.PEM,
},
},
Filters: []string{"scm_github"},
Namespace: "openfaas-fn",
},
},
}
res, _ := yaml.Marshal(p)
fmt.Printf("App: %s\tURL: %s\nYAML file\n\n", appRes.Name, appRes.URL)
fmt.Printf("%s\n", string(res))
}
// launchURL opens a URL with the default browser for Linux, MacOS or Windows.
func launchURL(serverURL string) error {
ctx := context.Background()
var command *exec.Cmd
switch runtime.GOOS {
case "linux":
command = exec.CommandContext(ctx, "sh", "-c", fmt.Sprintf(`xdg-open "%s"`, serverURL))
case "darwin":
command = exec.CommandContext(ctx, "sh", "-c", fmt.Sprintf(`open "%s"`, serverURL))
case "windows":
escaped := strings.Replace(serverURL, "&", "^&", -1)
command = exec.CommandContext(ctx, "cmd", "/c", fmt.Sprintf(`start %s`, escaped))
}
command.Stdout = os.Stdout
command.Stdin = os.Stdin
command.Stderr = os.Stderr
return command.Run()
}