@@ -6,6 +6,9 @@ import "context"
66import "os"
77import "os/exec"
88import "time"
9+ import "errors"
10+ import "bytes"
11+ import "io"
912import "github.com/google/go-github/github"
1013
1114func main () {
@@ -39,6 +42,8 @@ func main() {
3942 }
4043 }
4144
45+ addRepoDokkuRemote (repoPath , * app )
46+
4247 sleepDuration := time .Duration (* sleepInt ) * time .Second
4348 fmt .Printf ("Sleep duration: %v\n " , sleepDuration )
4449
@@ -50,46 +55,53 @@ func main() {
5055
5156 client := github .NewClient (tp .Client ())
5257
53- opt := & github.DeploymentsListOptions {
54- Environment : * env ,
55- ListOptions : github.ListOptions {PerPage : 1 },
56- }
57-
5858 for {
59- deployments , _ , err := client . Repositories . ListDeployments (ctx , * org , * repo , opt )
59+ deployment , _ , err := getDeployment (ctx , client , * org , * repo , * env )
6060
6161 if err != nil {
62- fmt .Printf ("Problem in listing deployments %v\n " , err )
62+ fmt .Printf ("Problem in getting deployment %v\n " , err )
6363 sleep (sleepDuration )
64- continue ;
64+ continue
6565 }
6666
67- deployment := deployments [0 ]
68- if deployment != nil {
69- fmt .Println (* deployment .ID , * deployment .Ref , * deployment .Environment )
67+ fmt .Println (* deployment .ID , * deployment .Ref , * deployment .Environment )
7068
71- fmt .Println ("Gist it" )
69+ err = fetchRepo (repoPath )
70+ if err != nil {
71+ fmt .Printf ("Problem in fetching repo %v\n " , err )
72+ sleep (sleepDuration )
73+ continue
74+ }
75+
76+ gist , _ , err := createDeploymentGist (ctx , client , deployment )
7277
73- title := fmt .Sprintf ("ID: %d, Ref: %s, Environment: %s" , * deployment .ID , * deployment .Ref , * deployment .Environment )
74- input := & github.Gist {
75- Description : github .String (title ),
76- Public : github .Bool (false ),
77- Files : map [github.GistFilename ]github.GistFile {
78- "output.txt" : {Content : github .String ("new file content" )},
79- },
80- }
81- gist , _ , err := client .Gists .Create (ctx , input )
78+ if err != nil {
79+ fmt .Printf ("Problem in creating gist %v\n " , err )
80+ sleep (sleepDuration )
81+ continue
82+ }
8283
83- if err != nil {
84- fmt .Printf ("Problem in creating gist %v\n " , err )
85- sleep (sleepDuration )
86- continue ;
87- }
84+ fmt .Println (* gist .HTMLURL )
8885
89- fmt . Println ( * gist .HTMLURL )
86+ createDeploymentStatus ( ctx , client , deployment , * org , * repo , "pending" , * gist .HTMLURL )
9087
88+ var output bytes.Buffer
89+ cmd := deployToDokku (repoPath , * deployment .Ref )
90+ cmd .Stdout = io .MultiWriter (& output , os .Stdout )
91+ cmd .Stderr = cmd .Stdout
92+ err = cmd .Run ()
93+ if err != nil {
94+ fmt .Printf ("Problem in deploying to dokku %v\n " , err )
95+ createDeploymentStatus (ctx , client , deployment , * org , * repo , "error" , * gist .HTMLURL )
96+ updateDeploymentGist (ctx , client , gist , output .String ())
9197 sleep (sleepDuration )
98+ continue
9299 }
100+
101+ createDeploymentStatus (ctx , client , deployment , * org , * repo , "success" , * gist .HTMLURL )
102+ updateDeploymentGist (ctx , client , gist , output .String ())
103+
104+ sleep (sleepDuration )
93105 }
94106}
95107
@@ -99,10 +111,99 @@ func sleep(duration time.Duration) {
99111}
100112
101113func cloneRepo (repoURL string , repoPath string ) (error ) {
102- fmt .Println ("Cloning " , repoURL )
114+ fmt .Println ("Cloning repo " , repoURL )
103115 cmd := exec .Command ("git" , "clone" , repoURL , repoPath )
104116 cmd .Stdout = os .Stdout
105117 cmd .Stderr = os .Stderr
106118 err := cmd .Run ()
107119 return err
108120}
121+
122+ func addRepoDokkuRemote (repoPath string , app string ) (error ) {
123+ fmt .Println ("Adding repo dokku remote " , repoPath , app )
124+ cmd := exec .Command ("git" , "remote" , "add" , "dokku" , "dokku@localhost:" + app )
125+ cmd .Dir = repoPath
126+ cmd .Stdout = os .Stdout
127+ cmd .Stderr = os .Stderr
128+ err := cmd .Run ()
129+ return err
130+ }
131+
132+ func fetchRepo (repoPath string ) (error ) {
133+ fmt .Println ("Fetchin repo " , repoPath )
134+ cmd := exec .Command ("git" , "fetch" , "origin" , "--force" , "--tags" )
135+ cmd .Dir = repoPath
136+ cmd .Stdout = os .Stdout
137+ cmd .Stderr = os .Stderr
138+ err := cmd .Run ()
139+ return err
140+ }
141+
142+ func deployToDokku (repoPath string , ref string ) (* exec.Cmd ) {
143+ fmt .Println ("Deploying repo " , repoPath )
144+ cmd := exec .Command ("bin/deploy-stub" )
145+ // cmd := exec.Command("git", "push", "-f", "dokku", ref + ":master")
146+ // cmd.Dir = repoPath
147+ // cmd.Stdout = os.Stdout
148+ // cmd.Stderr = os.Stderr
149+ return cmd
150+ }
151+
152+ func getDeployment (ctx context.Context , client * github.Client , org string , repo string , env string ) (* github.Deployment , * github.Response , error ) {
153+ opt := & github.DeploymentsListOptions {
154+ Environment : env ,
155+ ListOptions : github.ListOptions {PerPage : 1 },
156+ }
157+ deployments , resp , err := client .Repositories .ListDeployments (ctx , org , repo , opt )
158+ deployment := deployments [0 ]
159+
160+ if deployment == nil {
161+ err := errors .New ("Deployment list is empty" )
162+ return deployment , resp , err
163+ }
164+
165+ // statuses, resp, err := client.Repositories.ListDeploymentStatuses(ctx, org, repo, *deployment.ID, &github.ListOptions{ PerPage: 1 })
166+ // if err != nil {
167+ // return deployment, resp, err
168+ // }
169+ //
170+ // if len(statuses) > 0 {
171+ // err := errors.New("Deployment statuses already present")
172+ // return deployment, resp, err
173+ // }
174+
175+ return deployment , resp , err
176+ }
177+
178+ func createDeploymentStatus (ctx context.Context , client * github.Client , deployment * github.Deployment , org string , repo string , state string , url string ) (* github.DeploymentStatus , * github.Response , error ) {
179+ fmt .Println ("Deployment status create " , state )
180+ input := & github.DeploymentStatusRequest {
181+ State : github .String (state ),
182+ LogURL : github .String (url ),
183+ }
184+ status , resp , err := client .Repositories .CreateDeploymentStatus (ctx , org , repo , * deployment .ID , input )
185+ return status , resp , err
186+ }
187+
188+ func createDeploymentGist (ctx context.Context , client * github.Client , deployment * github.Deployment ) (* github.Gist , * github.Response , error ) {
189+ fmt .Println ("Gist create" )
190+ title := fmt .Sprintf ("ID: %d, Ref: %s, Environment: %s" , * deployment .ID , * deployment .Ref , * deployment .Environment )
191+ input := & github.Gist {
192+ Description : github .String (title ),
193+ Public : github .Bool (false ),
194+ Files : map [github.GistFilename ]github.GistFile {
195+ "output.txt" : {Content : github .String ("Pending" )},
196+ },
197+ }
198+ gist , resp , err := client .Gists .Create (ctx , input )
199+ return gist , resp , err
200+ }
201+
202+ func updateDeploymentGist (ctx context.Context , client * github.Client , gist * github.Gist , content string ) (* github.Gist , * github.Response , error ) {
203+ fmt .Println ("Gist update" )
204+ file := gist .Files ["output.txt" ]
205+ file .Content = github .String (content )
206+ gist .Files ["output.txt" ] = file
207+ gist , resp , err := client .Gists .Edit (ctx , * gist .ID , gist )
208+ return gist , resp , err
209+ }
0 commit comments