-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
348 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,260 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"os/exec" | ||
"os/signal" | ||
"slices" | ||
"strings" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/cenkalti/backoff" | ||
"github.com/google/go-github/v57/github" | ||
"github.com/hashicorp/go-slug" | ||
"golang.ngrok.com/ngrok" | ||
"golang.ngrok.com/ngrok/config" | ||
) | ||
|
||
type LocalContent struct { | ||
dir string | ||
} | ||
|
||
func (c *LocalContent) ServeHTTP(rw http.ResponseWriter, req *http.Request) { | ||
rw.Header().Set("Content-Type", "application/octet-stream") | ||
|
||
_, err := slug.Pack(c.dir, rw, true) | ||
if err != nil { | ||
fmt.Printf("failed to pack contents: %+v\n", err) | ||
return | ||
} | ||
|
||
fmt.Println("workspace was downloaded") | ||
} | ||
|
||
func startServer(ctx context.Context) (string, error) { | ||
listenerCtx, cancelListener := context.WithCancel(context.Background()) | ||
|
||
connected := make(chan struct{}) | ||
go func() { | ||
select { | ||
case <-connected: | ||
case <-time.After(10 * time.Second): | ||
cancelListener() | ||
} | ||
}() | ||
|
||
listener, err := ngrok.Listen(listenerCtx, config.HTTPEndpoint(), ngrok.WithAuthtokenFromEnv()) | ||
if err != nil { | ||
return "", err | ||
} | ||
close(connected) | ||
|
||
cwd, err := os.Getwd() | ||
if err != nil { | ||
listener.Close() | ||
return "", err | ||
} | ||
handler := &LocalContent{ | ||
dir: cwd, | ||
} | ||
|
||
server := &http.Server{ | ||
Handler: handler, | ||
} | ||
|
||
go func() { | ||
<-ctx.Done() | ||
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
defer cancel() | ||
if err := server.Shutdown(shutdownCtx); err != nil { | ||
fmt.Printf("failed to shutdown server: %+v\n", err) | ||
} | ||
}() | ||
|
||
go func() { | ||
if err := server.Serve(listener); err != http.ErrServerClosed { | ||
fmt.Printf("server failed: %+v\n", err) | ||
} | ||
}() | ||
|
||
return listener.URL(), nil | ||
} | ||
|
||
type countingReader struct { | ||
io.Reader | ||
readBytes int | ||
} | ||
|
||
func (c *countingReader) Read(dst []byte) (int, error) { | ||
n, err := c.Reader.Read(dst) | ||
c.readBytes += n | ||
return n, err | ||
} | ||
|
||
var ignoredGroupNames = []string{ | ||
"Operating System", | ||
"Runner Image", | ||
"Runner Image Provisioner", | ||
"GITHUB_TOKEN Permissions", | ||
} | ||
|
||
func streamLogs(logsURL *url.URL, skip int64) (int64, error) { | ||
logs, err := http.Get(logsURL.String()) | ||
if err != nil { | ||
return 0, err | ||
} | ||
if logs.StatusCode != http.StatusOK { | ||
return 0, fmt.Errorf("invalid status for logs: %d", logs.StatusCode) | ||
} | ||
defer logs.Body.Close() | ||
|
||
if _, err := io.Copy(io.Discard, io.LimitReader(logs.Body, skip)); err != nil { | ||
return 0, err | ||
} | ||
|
||
r := &countingReader{Reader: logs.Body} | ||
scanner := bufio.NewScanner(r) | ||
groupDepth := 0 | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
ts, rest, ok := strings.Cut(line, " ") | ||
if !ok { | ||
rest = ts | ||
} | ||
if groupName, ok := strings.CutPrefix(rest, "##[group]"); ok { | ||
groupDepth++ | ||
if !slices.Contains(ignoredGroupNames, groupName) { | ||
fmt.Printf("\n# %s\n", groupName) | ||
} | ||
} | ||
if groupDepth == 0 { | ||
fmt.Println(rest) | ||
} | ||
if strings.HasPrefix(rest, "##[endgroup]") { | ||
groupDepth-- | ||
} | ||
} | ||
if err := scanner.Err(); err != nil { | ||
return int64(r.readBytes), err | ||
} | ||
|
||
return int64(r.readBytes), err | ||
} | ||
|
||
const ( | ||
owner = "ffddorf" | ||
repo = "terraform-playground" | ||
workflowFilename = "preview.yaml" | ||
) | ||
|
||
func main() { | ||
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) | ||
defer cancel() | ||
|
||
serverURL, err := startServer(ctx) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// steal token from GH CLI | ||
cmd := exec.CommandContext(ctx, "gh", "auth", "token") | ||
out, err := cmd.Output() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
token := strings.TrimSpace(string(out)) | ||
gh := github.NewClient(nil).WithAuthToken(token) | ||
|
||
startedAt := time.Now().UTC() | ||
|
||
// start workflow | ||
_, err = gh.Actions.CreateWorkflowDispatchEventByFileName(ctx, | ||
owner, repo, workflowFilename, | ||
github.CreateWorkflowDispatchEventRequest{ | ||
Ref: "main", | ||
Inputs: map[string]interface{}{ | ||
"workspace_transfer_url": serverURL, | ||
}, | ||
}, | ||
) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println("Waiting for run to start...") | ||
|
||
// find workflow run | ||
var run *github.WorkflowRun | ||
err = backoff.Retry(func() error { | ||
workflows, _, err := gh.Actions.ListWorkflowRunsByFileName( | ||
ctx, owner, repo, workflowFilename, | ||
&github.ListWorkflowRunsOptions{ | ||
Created: fmt.Sprintf(">=%s", startedAt.Format("2006-01-02T15:04")), | ||
}, | ||
) | ||
if err != nil { | ||
return backoff.Permanent(err) | ||
} | ||
if len(workflows.WorkflowRuns) == 0 { | ||
return fmt.Errorf("no workflow runs found") | ||
} | ||
|
||
run = workflows.WorkflowRuns[0] | ||
return nil | ||
}, backoff.NewExponentialBackOff()) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
var jobID int64 | ||
err = backoff.Retry(func() error { | ||
jobs, _, err := gh.Actions.ListWorkflowJobs(ctx, | ||
owner, repo, *run.ID, | ||
&github.ListWorkflowJobsOptions{}, | ||
) | ||
if err != nil { | ||
return backoff.Permanent(err) | ||
} | ||
if len(jobs.Jobs) == 0 { | ||
return fmt.Errorf("no jobs found") | ||
} | ||
|
||
jobID = *jobs.Jobs[0].ID | ||
return nil | ||
}, backoff.NewExponentialBackOff()) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
logsURL, _, err := gh.Actions.GetWorkflowJobLogs(ctx, owner, repo, jobID, 2) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
var readBytes int64 | ||
for { | ||
n, err := streamLogs(logsURL, readBytes) | ||
if err != nil { | ||
panic(err) | ||
} | ||
readBytes += n | ||
|
||
// check if job is done | ||
job, _, err := gh.Actions.GetWorkflowJobByID(ctx, owner, repo, jobID) | ||
if err != nil { | ||
panic(err) | ||
} | ||
if job.CompletedAt != nil { | ||
fmt.Println("Job complete.") | ||
break | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
module github.com/ffddorf/tf-preview-github | ||
|
||
go 1.21.5 | ||
|
||
require ( | ||
github.com/cenkalti/backoff v2.2.1+incompatible | ||
github.com/google/go-github/v57 v57.0.0 | ||
github.com/hashicorp/go-slug v0.13.3 | ||
github.com/stretchr/testify v1.8.4 | ||
golang.ngrok.com/ngrok v1.7.0 | ||
) | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/go-stack/stack v1.8.1 // indirect | ||
github.com/google/go-querystring v1.1.0 // indirect | ||
github.com/inconshreveable/log15 v3.0.0-testing.5+incompatible // indirect | ||
github.com/inconshreveable/log15/v3 v3.0.0-testing.5 // indirect | ||
github.com/jpillora/backoff v1.0.0 // indirect | ||
github.com/mattn/go-colorable v0.1.13 // indirect | ||
github.com/mattn/go-isatty v0.0.20 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
go.uber.org/multierr v1.11.0 // indirect | ||
golang.ngrok.com/muxado/v2 v2.0.0 // indirect | ||
golang.org/x/net v0.19.0 // indirect | ||
golang.org/x/sync v0.6.0 // indirect | ||
golang.org/x/sys v0.16.0 // indirect | ||
golang.org/x/term v0.16.0 // indirect | ||
google.golang.org/protobuf v1.32.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4= | ||
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/go-stack/stack v1.8.1 h1:ntEHSVwIt7PNXNpgPmVfMrNhLtgjlmnZha2kOpuRiDw= | ||
github.com/go-stack/stack v1.8.1/go.mod h1:dcoOX6HbPZSZptuspn9bctJ+N/CnF5gGygcUP3XYfe4= | ||
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= | ||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= | ||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= | ||
github.com/google/go-github/v57 v57.0.0 h1:L+Y3UPTY8ALM8x+TV0lg+IEBI+upibemtBD8Q9u7zHs= | ||
github.com/google/go-github/v57 v57.0.0/go.mod h1:s0omdnye0hvK/ecLvpsGfJMiRt85PimQh4oygmLIxHw= | ||
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= | ||
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= | ||
github.com/hashicorp/go-slug v0.13.3 h1:JiYNpOkD0HmMWw/lNYiBAUD6+WIBIV7UftKiqIbpNqM= | ||
github.com/hashicorp/go-slug v0.13.3/go.mod h1:RA4C+ezyC2nDsiPM5+1djqagveBBJdSN/fM2QCUziYQ= | ||
github.com/hashicorp/yamux v0.1.1 h1:yrQxtgseBDrq9Y652vSRDvsKCJKOUD+GzTS4Y0Y8pvE= | ||
github.com/hashicorp/yamux v0.1.1/go.mod h1:CtWFDAQgb7dxtzFs4tWbplKIe2jSi3+5vKbgIO0SLnQ= | ||
github.com/inconshreveable/log15 v3.0.0-testing.5+incompatible h1:VryeOTiaZfAzwx8xBcID1KlJCeoWSIpsNbSk+/D2LNk= | ||
github.com/inconshreveable/log15 v3.0.0-testing.5+incompatible/go.mod h1:cOaXtrgN4ScfRrD9Bre7U1thNq5RtJ8ZoP4iXVGRj6o= | ||
github.com/inconshreveable/log15/v3 v3.0.0-testing.5 h1:h4e0f3kjgg+RJBlKOabrohjHe47D3bbAB9BgMrc3DYA= | ||
github.com/inconshreveable/log15/v3 v3.0.0-testing.5/go.mod h1:3GQg1SVrLoWGfRv/kAZMsdyU5cp8eFc1P3cw+Wwku94= | ||
github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA= | ||
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= | ||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= | ||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= | ||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= | ||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= | ||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= | ||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= | ||
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= | ||
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= | ||
golang.ngrok.com/muxado/v2 v2.0.0 h1:bu9eIDhRdYNtIXNnqat/HyMeHYOAbUH55ebD7gTvW6c= | ||
golang.ngrok.com/muxado/v2 v2.0.0/go.mod h1:wzxJYX4xiAtmwumzL+QsukVwFRXmPNv86vB8RPpOxyM= | ||
golang.ngrok.com/ngrok v1.7.0 h1:xwcr8QWue+ehgn54hdQwTya4B6A1qXg6+IRim6WINmA= | ||
golang.ngrok.com/ngrok v1.7.0/go.mod h1:ruVcXZ7Rre5O9oeqqa8uZCB3Xtkt2PoyjF3eW9b7t6A= | ||
golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY= | ||
golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= | ||
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c= | ||
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U= | ||
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ= | ||
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= | ||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU= | ||
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= | ||
golang.org/x/term v0.16.0 h1:m+B6fahuftsE9qjo0VWp2FW0mB3MTJvR0BaMQrq0pmE= | ||
golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY= | ||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= | ||
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I= | ||
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |