-
Notifications
You must be signed in to change notification settings - Fork 3
/
plugin.go
161 lines (132 loc) · 3.64 KB
/
plugin.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
package main
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"os"
"os/exec"
"regexp"
"strings"
"sync"
)
type Plugin struct {
SurgeToken string
Path string
RepoOwner string
RepoName string
PipelineEvent string
PullRequestID int
ForgeType string
ForgeURL string
ForgeRepoToken string
comment *comment
}
func (p *Plugin) Exec(ctx context.Context) error {
fmt.Println("Surge.sh preview plugin")
if p.RepoName == "" || p.RepoOwner == "" || p.PipelineEvent == "" {
return errors.New("Missing required parameters. Are you running this plugin from within a pipeline?")
}
if p.Path == "" {
return errors.New("Path to the upload folder is not set")
}
if p.SurgeToken == "" {
return errors.New("Surge.sh token is not defined")
}
p.comment = &comment{}
err := p.comment.Load(p.ForgeType, p.ForgeURL, p.ForgeRepoToken)
if err != nil {
return err
}
switch p.PipelineEvent {
case "pull_request":
return p.deploy(ctx)
case "pull_request_closed":
return p.teardown(ctx)
default:
return errors.New("unsupported pipeline event, please only run on pull_request or pull_close")
}
}
func (p *Plugin) deploy(ctx context.Context) error {
url := p.getPreviewURL()
repo := p.RepoOwner + "/" + p.RepoName
comment, err := p.comment.Find(ctx, repo, p.PullRequestID)
if err != nil && err.Error() != "Comment not found" {
return err
}
commentText := fmt.Sprintf("Deploying preview to https://%s", url)
fmt.Println(commentText)
comment, err = p.comment.UpdateOrCreateComment(ctx, repo, p.PullRequestID, comment, commentText)
if err != nil {
return err
}
if err := p.runSurgeCommand(false); err != nil {
return err
}
commentText = fmt.Sprintf("Deployment of preview was successful: https://%s", url)
fmt.Println(commentText)
_, err = p.comment.UpdateOrCreateComment(ctx, repo, p.PullRequestID, comment, commentText)
if err != nil {
return err
}
return nil
}
func (p *Plugin) teardown(ctx context.Context) error {
url := p.getPreviewURL()
repo := p.RepoOwner + "/" + p.RepoName
comment, err := p.comment.Find(ctx, repo, p.PullRequestID)
if err != nil && err.Error() != "Comment not found" {
return err
}
commentText := fmt.Sprintf("Tearing down https://%s\n", url)
fmt.Println(commentText)
comment, err = p.comment.UpdateOrCreateComment(ctx, repo, p.PullRequestID, comment, commentText)
if err != nil {
return err
}
if err := p.runSurgeCommand(true); err != nil {
return err
}
commentText = "Deployment of preview was torn down"
fmt.Println(commentText)
_, err = p.comment.UpdateOrCreateComment(ctx, repo, p.PullRequestID, comment, commentText)
if err != nil {
return err
}
return nil
}
func (p *Plugin) getPreviewURL() string {
pattern := regexp.MustCompile(`[^a-zA-Z0-9\-]`)
owner := pattern.ReplaceAllString(p.RepoOwner, "-")
repo := pattern.ReplaceAllString(p.RepoName, "-")
return fmt.Sprintf("%s-%s-pr-%d.surge.sh", owner, repo, p.PullRequestID)
}
func (p *Plugin) runSurgeCommand(teardown bool) error {
var output bytes.Buffer
var waitGroup sync.WaitGroup
cmdArg := p.Path
if teardown {
cmdArg = "teardown"
}
cmd := exec.Command("surge", cmdArg, p.getPreviewURL(), `--token`, p.SurgeToken)
fmt.Println("#", strings.Join(append(cmd.Args, p.getPreviewURL(), "--token ****"), " "))
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}
writer := io.MultiWriter(os.Stdout, &output)
waitGroup.Add(1)
go func() {
defer waitGroup.Done()
_, _ = io.Copy(writer, stdout)
}()
if err := cmd.Run(); err != nil {
return err
}
waitGroup.Wait()
if !strings.Contains(output.String(), "Success") {
return errors.New("Failed to run surge")
}
return nil
}