hook: get push options and pass them #340

closed
opened by ptr.pet targeting master from [deleted fork]: push-options
Changed files
+35 -3
hook
knotserver
+10
hook/hook.go
··· 36 Usage: "endpoint for the internal API", 37 Value: "http://localhost:5444", 38 }, 39 }, 40 Commands: []*cli.Command{ 41 { ··· 52 userDid := cmd.String("user-did") 53 userHandle := cmd.String("user-handle") 54 endpoint := cmd.String("internal-api") 55 56 payloadReader := bufio.NewReader(os.Stdin) 57 payload, _ := payloadReader.ReadString('\n') ··· 67 req.Header.Set("X-Git-Dir", gitDir) 68 req.Header.Set("X-Git-User-Did", userDid) 69 req.Header.Set("X-Git-User-Handle", userHandle) 70 71 resp, err := client.Do(req) 72 if err != nil {
··· 36 Usage: "endpoint for the internal API", 37 Value: "http://localhost:5444", 38 }, 39 + &cli.StringSliceFlag{ 40 + Name: "push-option", 41 + Usage: "any push option from git", 42 + }, 43 }, 44 Commands: []*cli.Command{ 45 { ··· 56 userDid := cmd.String("user-did") 57 userHandle := cmd.String("user-handle") 58 endpoint := cmd.String("internal-api") 59 + pushOptions := cmd.StringSlice("push-option") 60 61 payloadReader := bufio.NewReader(os.Stdin) 62 payload, _ := payloadReader.ReadString('\n') ··· 72 req.Header.Set("X-Git-Dir", gitDir) 73 req.Header.Set("X-Git-User-Did", userDid) 74 req.Header.Set("X-Git-User-Handle", userHandle) 75 + if pushOptions != nil { 76 + for _, option := range pushOptions { 77 + req.Header.Add("X-Git-Push-Option", option) 78 + } 79 + } 80 81 resp, err := client.Do(req) 82 if err != nil {
+6 -1
hook/setup.go
··· 133 134 hookContent := fmt.Sprintf(`#!/usr/bin/env bash 135 # AUTO GENERATED BY KNOT, DO NOT MODIFY 136 - %s hook -git-dir "$GIT_DIR" -user-did "$GIT_USER_DID" -user-handle "$GIT_USER_HANDLE" -internal-api "%s" post-recieve 137 `, executablePath, config.internalApi) 138 139 return os.WriteFile(hookPath, []byte(hookContent), 0755)
··· 133 134 hookContent := fmt.Sprintf(`#!/usr/bin/env bash 135 # AUTO GENERATED BY KNOT, DO NOT MODIFY 136 + push_options=() 137 + for ((i=0; i<GIT_PUSH_OPTION_COUNT; i++)); do 138 + option_var="GIT_PUSH_OPTION_$i" 139 + push_options+=(-push-option "${!option_var}") 140 + done 141 + %s hook -git-dir "$GIT_DIR" -user-did "$GIT_USER_DID" -user-handle "$GIT_USER_HANDLE" -internal-api "%s" "${push_options[@]}" post-recieve 142 `, executablePath, config.internalApi) 143 144 return os.WriteFile(hookPath, []byte(hookContent), 0755)
+19 -2
knotserver/internal.go
··· 64 return 65 } 66 67 func (h *InternalHandle) PostReceiveHook(w http.ResponseWriter, r *http.Request) { 68 l := h.l.With("handler", "PostReceiveHook") 69 ··· 90 // non-fatal 91 } 92 93 for _, line := range lines { 94 err := h.insertRefUpdate(line, gitUserDid, repoDid, repoName) 95 if err != nil { ··· 97 // non-fatal 98 } 99 100 - err = h.triggerPipeline(line, gitUserDid, repoDid, repoName) 101 if err != nil { 102 l.Error("failed to trigger pipeline", "err", err, "line", line, "did", gitUserDid, "repo", gitRelativeDir) 103 // non-fatal ··· 148 return h.db.InsertEvent(event, h.n) 149 } 150 151 - func (h *InternalHandle) triggerPipeline(line git.PostReceiveLine, gitUserDid, repoDid, repoName string) error { 152 didSlashRepo, err := securejoin.SecureJoin(repoDid, repoName) 153 if err != nil { 154 return err
··· 64 return 65 } 66 67 + type PushOptions struct { 68 + skipCi bool 69 + } 70 + 71 func (h *InternalHandle) PostReceiveHook(w http.ResponseWriter, r *http.Request) { 72 l := h.l.With("handler", "PostReceiveHook") 73 ··· 94 // non-fatal 95 } 96 97 + // extract any push options 98 + pushOptionsRaw := r.Header.Values("X-Git-Push-Option") 99 + pushOptions := PushOptions{} 100 + for _, option := range pushOptionsRaw { 101 + if option == "skip-ci" || option == "ci-skip" { 102 + pushOptions.skipCi = true 103 + } 104 + } 105 + 106 for _, line := range lines { 107 err := h.insertRefUpdate(line, gitUserDid, repoDid, repoName) 108 if err != nil { ··· 110 // non-fatal 111 } 112 113 + err = h.triggerPipeline(line, gitUserDid, repoDid, repoName, pushOptions) 114 if err != nil { 115 l.Error("failed to trigger pipeline", "err", err, "line", line, "did", gitUserDid, "repo", gitRelativeDir) 116 // non-fatal ··· 161 return h.db.InsertEvent(event, h.n) 162 } 163 164 + func (h *InternalHandle) triggerPipeline(line git.PostReceiveLine, gitUserDid, repoDid, repoName string, pushOptions PushOptions) error { 165 + if pushOptions.skipCi { 166 + return nil 167 + } 168 + 169 didSlashRepo, err := securejoin.SecureJoin(repoDid, repoName) 170 if err != nil { 171 return err