summaryrefslogtreecommitdiff
path: root/internal/command/receivepack/customaction.go
diff options
context:
space:
mode:
authorNick Thomas <nick@gitlab.com>2019-10-17 12:04:52 +0100
committerNick Thomas <nick@gitlab.com>2019-10-18 11:47:25 +0100
commit83d11f4deeb20b852a0af3433190a0f7250a0027 (patch)
tree1a9df18d6f9f59712c6f5c98e995a4918eb94a11 /internal/command/receivepack/customaction.go
parent7d5229db263a62661653431881bef8b46984d0de (diff)
downloadgitlab-shell-83d11f4deeb20b852a0af3433190a0f7250a0027.tar.gz
Move go code up one level
Diffstat (limited to 'internal/command/receivepack/customaction.go')
-rw-r--r--internal/command/receivepack/customaction.go99
1 files changed, 99 insertions, 0 deletions
diff --git a/internal/command/receivepack/customaction.go b/internal/command/receivepack/customaction.go
new file mode 100644
index 0000000..8623437
--- /dev/null
+++ b/internal/command/receivepack/customaction.go
@@ -0,0 +1,99 @@
+package receivepack
+
+import (
+ "bytes"
+ "errors"
+ "fmt"
+ "io"
+ "io/ioutil"
+ "net/http"
+ "strings"
+
+ "gitlab.com/gitlab-org/gitlab-shell/go/internal/gitlabnet"
+ "gitlab.com/gitlab-org/gitlab-shell/go/internal/gitlabnet/accessverifier"
+)
+
+type Request struct {
+ SecretToken []byte `json:"secret_token"`
+ Data accessverifier.CustomPayloadData `json:"data"`
+ Output []byte `json:"output"`
+}
+
+type Response struct {
+ Result []byte `json:"result"`
+ Message string `json:"message"`
+}
+
+func (c *Command) processCustomAction(response *accessverifier.Response) error {
+ data := response.Payload.Data
+ apiEndpoints := data.ApiEndpoints
+
+ if len(apiEndpoints) == 0 {
+ return errors.New("Custom action error: Empty API endpoints")
+ }
+
+ c.displayInfoMessage(data.InfoMessage)
+
+ return c.processApiEndpoints(response)
+}
+
+func (c *Command) displayInfoMessage(infoMessage string) {
+ messages := strings.Split(infoMessage, "\n")
+
+ for _, msg := range messages {
+ fmt.Fprintf(c.ReadWriter.ErrOut, "> GitLab: %v\n", msg)
+ }
+}
+
+func (c *Command) processApiEndpoints(response *accessverifier.Response) error {
+ client, err := gitlabnet.GetClient(c.Config)
+
+ if err != nil {
+ return err
+ }
+
+ data := response.Payload.Data
+ request := &Request{Data: data}
+ request.Data.UserId = response.Who
+
+ for _, endpoint := range data.ApiEndpoints {
+ response, err := c.performRequest(client, endpoint, request)
+ if err != nil {
+ return err
+ }
+
+ if err = c.displayResult(response.Result); err != nil {
+ return err
+ }
+
+ // In the context of the git push sequence of events, it's necessary to read
+ // stdin in order to capture output to pass onto subsequent commands
+ output, err := ioutil.ReadAll(c.ReadWriter.In)
+ if err != nil {
+ return err
+ }
+ request.Output = output
+ }
+
+ return nil
+}
+
+func (c *Command) performRequest(client *gitlabnet.GitlabClient, endpoint string, request *Request) (*Response, error) {
+ response, err := client.DoRequest(http.MethodPost, endpoint, request)
+ if err != nil {
+ return nil, err
+ }
+ defer response.Body.Close()
+
+ cr := &Response{}
+ if err := gitlabnet.ParseJSON(response, cr); err != nil {
+ return nil, err
+ }
+
+ return cr, nil
+}
+
+func (c *Command) displayResult(result []byte) error {
+ _, err := io.Copy(c.ReadWriter.Out, bytes.NewReader(result))
+ return err
+}