Simplify Go Endpoints + Add Tests (#120)
This MR represents a major refactor of the Go codebase, as well as introducing tests for the handlers. The MR also introduces an endpoint to shutdown or restart the Go server, which may be useful for clients who want to refresh the state of the plugin after checking out branches. Finally, this MR adds a contributing document for users who want to make feature changes.
This commit is contained in:
committed by
GitHub
parent
10b0b596ae
commit
93fe3e8bd6
84
cmd/main.go
84
cmd/main.go
@@ -1,96 +1,24 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
g, err := ExtractGitInfo(RefreshProjectInfo, GetProjectUrlFromNativeGitCmd, GetCurrentBranchNameFromNativeGitCmd)
|
||||
gitInfo, err := extractGitInfo(RefreshProjectInfo, GetProjectUrlFromNativeGitCmd, GetCurrentBranchNameFromNativeGitCmd)
|
||||
if err != nil {
|
||||
log.Fatalf("Failure initializing plugin with `git` commands: %v", err)
|
||||
}
|
||||
|
||||
var c Client
|
||||
if err := c.initGitlabClient(); err != nil {
|
||||
err, client := initGitlabClient()
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to initialize Gitlab client: %v", err)
|
||||
}
|
||||
|
||||
if err := c.initProjectSettings(g); err != nil {
|
||||
err, projectInfo := initProjectSettings(client, gitInfo)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to initialize project settings: %v", err)
|
||||
}
|
||||
|
||||
m := http.NewServeMux()
|
||||
m.Handle("/ping", http.HandlerFunc(PingHandler))
|
||||
m.Handle("/mr/summary", withGitlabContext(http.HandlerFunc(SummaryHandler), c))
|
||||
m.Handle("/mr/attachment", withGitlabContext(http.HandlerFunc(AttachmentHandler), c))
|
||||
m.Handle("/mr/reviewer", withGitlabContext(http.HandlerFunc(ReviewersHandler), c))
|
||||
m.Handle("/mr/revisions", withGitlabContext(http.HandlerFunc(RevisionsHandler), c))
|
||||
m.Handle("/mr/assignee", withGitlabContext(http.HandlerFunc(AssigneesHandler), c))
|
||||
m.Handle("/approve", withGitlabContext(http.HandlerFunc(ApproveHandler), c))
|
||||
m.Handle("/revoke", withGitlabContext(http.HandlerFunc(RevokeHandler), c))
|
||||
m.Handle("/info", withGitlabContext(http.HandlerFunc(InfoHandler), c))
|
||||
m.Handle("/discussions", withGitlabContext(http.HandlerFunc(ListDiscussionsHandler), c))
|
||||
m.Handle("/discussion/resolve", withGitlabContext(http.HandlerFunc(DiscussionResolveHandler), c))
|
||||
m.Handle("/comment", withGitlabContext(http.HandlerFunc(CommentHandler), c))
|
||||
m.Handle("/reply", withGitlabContext(http.HandlerFunc(ReplyHandler), c))
|
||||
m.Handle("/members", withGitlabContext(http.HandlerFunc(ProjectMembersHandler), c))
|
||||
m.Handle("/pipeline", withGitlabContext(http.HandlerFunc(PipelineHandler), c))
|
||||
m.Handle("/job", withGitlabContext(http.HandlerFunc(JobHandler), c))
|
||||
|
||||
port := os.Args[2]
|
||||
if port == "" {
|
||||
// port was not specified
|
||||
port = "0"
|
||||
}
|
||||
addr := fmt.Sprintf("localhost:%s", port)
|
||||
listener, err := net.Listen("tcp", addr)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
fmt.Fprintf(os.Stderr, "Error starting server: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
listenerPort := listener.Addr().(*net.TCPAddr).Port
|
||||
|
||||
errCh := make(chan error)
|
||||
go func() {
|
||||
err := http.Serve(listener, m)
|
||||
errCh <- err
|
||||
}()
|
||||
|
||||
go func() {
|
||||
for i := 0; i < 10; i++ {
|
||||
resp, err := http.Get("http://localhost:" + fmt.Sprintf("%d", listenerPort) + "/ping")
|
||||
if resp.StatusCode == 200 && err == nil {
|
||||
/* This print is detected by the Lua code and used to fetch project information */
|
||||
fmt.Println("Server started on port: ", listenerPort)
|
||||
return
|
||||
}
|
||||
// Wait for healthcheck to pass - at most 1 sec.
|
||||
time.Sleep(100 * time.Microsecond)
|
||||
}
|
||||
errCh <- err
|
||||
}()
|
||||
|
||||
if err := <-errCh; err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error starting server: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func PingHandler(w http.ResponseWriter, _ *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
fmt.Fprintln(w, "pong")
|
||||
}
|
||||
|
||||
func withGitlabContext(next http.HandlerFunc, c Client) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := context.WithValue(context.Background(), "client", c) //nolint:all
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
startServer(client, projectInfo)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user