refactor: simplify artifact package and rename some functions to make it more explicit
The following discussion from !787 (merged) should be addressed:
-
@bwill started a discussion: (+3 comments) Reading the rest of the code, I actually don't understand why we're doing all this, because we're essentially cloning this request by pulling the fields out and then rebuilding it again with
http.NewRequestWithContext. Normally I would suggest doingr.Clone(newContext)if we wanted to create a copy with a new context, but it looks like we're even passing the existing request context toNewRequestWithContext, so it's a complete clone. How about we remove all of that?diff --git a/internal/artifact/artifact.go b/internal/artifact/artifact.go index 87ce1450..9e1633a3 100644 --- a/internal/artifact/artifact.go +++ b/internal/artifact/artifact.go @@ -17,7 +17,6 @@ import ( "gitlab.com/gitlab-org/gitlab-pages/internal/httperrors" "gitlab.com/gitlab-org/gitlab-pages/internal/httptransport" "gitlab.com/gitlab-org/gitlab-pages/internal/logging" - "gitlab.com/gitlab-org/gitlab-pages/internal/request" ) const ( @@ -67,27 +66,12 @@ func (a *Artifact) TryMakeRequest(w http.ResponseWriter, r *http.Request, token return false } - host := request.GetHostWithoutPort(r) - - reqURL, ok := a.BuildURL(host, r.URL.Path) - if !ok { - return false - } - - a.makeRequest(w, r, reqURL, token, additionalHandler) + a.makeRequest(w, r, token, additionalHandler) return true } -func (a *Artifact) makeRequest(w http.ResponseWriter, r *http.Request, reqURL *url.URL, token string, additionalHandler func(*http.Response) bool) { - req, err := http.NewRequestWithContext(r.Context(), "GET", reqURL.String(), nil) - if err != nil { - logging.LogRequest(r).WithError(err).Error(createArtifactRequestErrMsg) - errortracking.CaptureErrWithReqAndStackTrace(err, r) - httperrors.Serve500(w) - return - } - +func (a *Artifact) makeRequest(w http.ResponseWriter, req *http.Request, token string, additionalHandler func(*http.Response) bool) { if token != "" { req.Header.Add("Authorization", "Bearer "+token) } @@ -98,8 +82,8 @@ func (a *Artifact) makeRequest(w http.ResponseWriter, r *http.Request, reqURL *u return } - logging.LogRequest(r).WithError(err).Error(artifactRequestErrMsg) - errortracking.CaptureErrWithReqAndStackTrace(err, r) + logging.LogRequest(req).WithError(err).Error(artifactRequestErrMsg) + errortracking.CaptureErrWithReqAndStackTrace(err, req) httperrors.Serve502(w) return } @@ -116,8 +100,8 @@ func (a *Artifact) makeRequest(w http.ResponseWriter, r *http.Request, reqURL *u } if resp.StatusCode == http.StatusInternalServerError { - logging.LogRequest(r).Error(errArtifactResponse) - errortracking.CaptureErrWithReqAndStackTrace(errArtifactResponse, r) + logging.LogRequest(req).Error(errArtifactResponse) + errortracking.CaptureErrWithReqAndStackTrace(errArtifactResponse, req) httperrors.Serve500(w) return }We can use
(*http.Request).Clone()if we really need a copy, but I don't see why we'd need one.