func (as *APIServer) existingPatchRequest(w http.ResponseWriter, r *http.Request) { p, err := getPatchFromRequest(r) if err != nil { http.Error(w, err.Error(), http.StatusNotFound) return } if !getGlobalLock(r.RemoteAddr, p.Id.String()) { as.LoggedError(w, r, http.StatusInternalServerError, ErrLockTimeout) return } defer releaseGlobalLock(r.RemoteAddr, p.Id.String()) // dispatch to handlers based on specified action switch r.FormValue("action") { case "update": name := r.FormValue("desc") err := p.SetDescription(name) if err != nil { as.LoggedError(w, r, http.StatusInternalServerError, err) return } as.WriteJSON(w, http.StatusOK, "patch updated") case "finalize": if p.Activated == true { http.Error(w, "patch is already finalized", http.StatusBadRequest) return } patchedProject, err := validator.GetPatchedProject(p, &as.Settings) if err != nil { as.LoggedError(w, r, http.StatusInternalServerError, err) return } projectYamlBytes, err := yaml.Marshal(patchedProject) if err != nil { as.LoggedError(w, r, http.StatusInternalServerError, fmt.Errorf("error marshalling patched config: %v", err)) return } p.PatchedConfig = string(projectYamlBytes) _, err = model.FinalizePatch(p, &as.Settings) if err != nil { as.LoggedError(w, r, http.StatusInternalServerError, err) return } as.WriteJSON(w, http.StatusOK, "patch finalized") case "cancel": err = model.CancelPatch(p) if err != nil { as.LoggedError(w, r, http.StatusInternalServerError, err) return } as.WriteJSON(w, http.StatusOK, "patch deleted") default: http.Error(w, fmt.Sprintf("Unrecognized action: %v", r.FormValue("action")), http.StatusBadRequest) } }
func (as *APIServer) existingPatchRequest(w http.ResponseWriter, r *http.Request) { user := MustHaveUser(r) p, err := getPatchFromRequest(r) if err != nil { http.Error(w, err.Error(), http.StatusNotFound) return } if !getGlobalLock(r.RemoteAddr, p.Id.String()) { as.LoggedError(w, r, http.StatusInternalServerError, ErrLockTimeout) return } defer releaseGlobalLock(r.RemoteAddr, p.Id.String()) var action, desc string if r.Header.Get("Content-Type") == "application/x-www-form-urlencoded" { action = r.FormValue("action") } else { data := struct { PatchId string `json:"patch_id"` Action string `json:"action"` Description string `json:"description"` }{} if err := util.ReadJSONInto(r.Body, &data); err != nil { as.LoggedError(w, r, http.StatusBadRequest, err) return } action, desc = data.Action, data.Description } // dispatch to handlers based on specified action switch action { case "update": err := p.SetDescription(desc) if err != nil { as.LoggedError(w, r, http.StatusInternalServerError, err) return } as.WriteJSON(w, http.StatusOK, "patch updated") case "finalize": if p.Activated == true { http.Error(w, "patch is already finalized", http.StatusBadRequest) return } patchedProject, err := validator.GetPatchedProject(p, &as.Settings) if err != nil { as.LoggedError(w, r, http.StatusInternalServerError, err) return } projectYamlBytes, err := yaml.Marshal(patchedProject) if err != nil { as.LoggedError(w, r, http.StatusInternalServerError, fmt.Errorf("error marshalling patched config: %v", err)) return } p.PatchedConfig = string(projectYamlBytes) _, err = model.FinalizePatch(p, &as.Settings) if err != nil { as.LoggedError(w, r, http.StatusInternalServerError, err) return } as.WriteJSON(w, http.StatusOK, "patch finalized") case "cancel": err = model.CancelPatch(p, user.Id) if err != nil { as.LoggedError(w, r, http.StatusInternalServerError, err) return } as.WriteJSON(w, http.StatusOK, "patch deleted") default: http.Error(w, fmt.Sprintf("Unrecognized action: %v", action), http.StatusBadRequest) } }
func (as *APIServer) submitPatch(w http.ResponseWriter, r *http.Request) { user := MustHaveUser(r) var apiRequest PatchAPIRequest var projId, description string var finalize bool if r.Header.Get("Content-Type") == "application/x-www-form-urlencoded" { patchContent := r.FormValue("patch") if patchContent == "" { as.LoggedError(w, r, http.StatusBadRequest, fmt.Errorf("Error: Patch must not be empty")) return } apiRequest = PatchAPIRequest{ ProjectFileName: r.FormValue("project"), ModuleName: r.FormValue("module"), Githash: r.FormValue("githash"), PatchContent: r.FormValue("patch"), BuildVariants: strings.Split(r.FormValue("buildvariants"), ","), } projId = r.FormValue("project") description = r.FormValue("desc") finalize = strings.ToLower(r.FormValue("finalize")) == "true" } else { data := struct { Description string `json:"desc"` Project string `json:"project"` Patch string `json:"patch"` Githash string `json:"githash"` Variants string `json:"buildvariants"` Tasks []string `json:"tasks"` Finalize bool `json:"finalize"` }{} if err := util.ReadJSONInto(r.Body, &data); err != nil { as.LoggedError(w, r, http.StatusBadRequest, err) return } if len(data.Patch) > patch.SizeLimit { as.LoggedError(w, r, http.StatusBadRequest, fmt.Errorf("Patch is too large.")) } if len(data.Patch) == 0 { as.LoggedError(w, r, http.StatusBadRequest, fmt.Errorf("Error: Patch must not be empty")) return } description = data.Description projId = data.Project finalize = data.Finalize apiRequest = PatchAPIRequest{ ProjectFileName: data.Project, ModuleName: r.FormValue("module"), Githash: data.Githash, PatchContent: data.Patch, BuildVariants: strings.Split(data.Variants, ","), Tasks: data.Tasks, } } projectRef, err := model.FindOneProjectRef(projId) if err != nil { message := fmt.Errorf("Error locating project ref '%v': %v", projId, err) as.LoggedError(w, r, http.StatusInternalServerError, message) return } project, err := model.FindProject("", projectRef) if err != nil { message := fmt.Errorf("Error locating project '%v' from '%v': %v", projId, as.Settings.ConfigDir, err) as.LoggedError(w, r, http.StatusInternalServerError, message) return } if project == nil { as.LoggedError(w, r, http.StatusNotFound, fmt.Errorf("project %v not found", projId)) return } patchMetadata, err := apiRequest.Validate(as.Settings.Credentials["github"]) if err != nil { as.LoggedError(w, r, http.StatusInternalServerError, fmt.Errorf("Invalid patch: %v", err)) return } if patchMetadata == nil { as.LoggedError(w, r, http.StatusBadRequest, fmt.Errorf("patch metadata is empty")) return } if apiRequest.ModuleName != "" { as.WriteJSON(w, http.StatusBadRequest, "module not allowed when creating new patches (must be added in a subsequent request)") return } patchProjectRef, err := model.FindOneProjectRef(patchMetadata.Project.Identifier) if err != nil { as.LoggedError(w, r, http.StatusInternalServerError, fmt.Errorf("Invalid projectRef: %v", err)) return } if patchProjectRef == nil { as.LoggedError(w, r, http.StatusNotFound, fmt.Errorf("Empty patch project Ref")) return } commitInfo, err := thirdparty.GetCommitEvent(as.Settings.Credentials["github"], patchProjectRef.Owner, patchProjectRef.Repo, apiRequest.Githash) if err != nil { as.LoggedError(w, r, http.StatusInternalServerError, err) return } if commitInfo == nil { as.WriteJSON(w, http.StatusBadRequest, "That commit doesn't seem to exist.") return } createTime := time.Now() // create a new object ID to use as reference for the patch data patchFileId := bson.NewObjectId().Hex() patchDoc := &patch.Patch{ Id: bson.NewObjectId(), Description: description, Author: user.Id, Project: apiRequest.ProjectFileName, Githash: apiRequest.Githash, CreateTime: createTime, Status: evergreen.PatchCreated, BuildVariants: apiRequest.BuildVariants, Tasks: apiRequest.Tasks, Patches: []patch.ModulePatch{ patch.ModulePatch{ ModuleName: "", Githash: apiRequest.Githash, PatchSet: patch.PatchSet{ PatchFileId: patchFileId, Summary: patchMetadata.Summaries, }, }, }, } // write the patch content into a GridFS file under a new ObjectId. err = db.WriteGridFile(patch.GridFSPrefix, patchFileId, strings.NewReader(apiRequest.PatchContent)) if err != nil { as.LoggedError(w, r, http.StatusInternalServerError, fmt.Errorf("failed to write patch file to db: %v", err)) return } // Get and validate patched config and add it to the patch document patchedProject, err := validator.GetPatchedProject(patchDoc, &as.Settings) if err != nil { as.LoggedError(w, r, http.StatusInternalServerError, fmt.Errorf("invalid patched config: %v", err)) return } projectYamlBytes, err := yaml.Marshal(patchedProject) if err != nil { as.LoggedError(w, r, http.StatusInternalServerError, fmt.Errorf("error marshalling patched config: %v", err)) return } // set the patch number based on patch author patchDoc.PatchNumber, err = user.IncPatchNumber() if err != nil { as.LoggedError(w, r, http.StatusInternalServerError, fmt.Errorf("error computing patch num %v", err)) return } patchDoc.PatchedConfig = string(projectYamlBytes) patchDoc.ClearPatchData() //expand tasks and build variants and include dependencies buildVariants := patchDoc.BuildVariants if len(patchDoc.BuildVariants) == 1 && patchDoc.BuildVariants[0] == "all" { buildVariants = make([]string, 0) for _, buildVariant := range patchedProject.BuildVariants { if buildVariant.Disabled { continue } buildVariants = append(buildVariants, buildVariant.Name) } } tasks := patchDoc.Tasks if len(patchDoc.Tasks) == 1 && patchDoc.Tasks[0] == "all" { tasks = make([]string, 0) for _, t := range patchedProject.Tasks { tasks = append(tasks, t.Name) } } // update variant and tasks to include dependencies patchDoc.BuildVariants, patchDoc.Tasks = model.IncludePatchDependencies( project, buildVariants, tasks) if err = patchDoc.Insert(); err != nil { as.LoggedError(w, r, http.StatusInternalServerError, fmt.Errorf("error inserting patch: %v", err)) return } if finalize { if _, err = model.FinalizePatch(patchDoc, &as.Settings); err != nil { as.LoggedError(w, r, http.StatusInternalServerError, err) return } } as.WriteJSON(w, http.StatusCreated, PatchAPIResponse{Patch: patchDoc}) }
// CreatePatch checks an API request to see if it is safe and sane. // Returns the relevant patch metadata, the patch document, and any errors that occur. func (pr *PatchAPIRequest) CreatePatch(finalize bool, oauthToken string, dbUser *user.DBUser, settings *evergreen.Settings) (*model.Project, *patch.Patch, error) { var repoOwner, repo string var module *model.Module projectRef, err := model.FindOneProjectRef(pr.ProjectId) if err != nil { return nil, nil, fmt.Errorf("Could not find project ref %v : %v", pr.ProjectId, err) } repoOwner = projectRef.Owner repo = projectRef.Repo if len(pr.Githash) != 40 { return nil, nil, fmt.Errorf("invalid githash") } gitCommit, err := thirdparty.GetCommitEvent(oauthToken, repoOwner, repo, pr.Githash) if err != nil { return nil, nil, fmt.Errorf("could not find base revision %v for project %v: %v", pr.Githash, projectRef.Identifier, err) } if gitCommit == nil { return nil, nil, fmt.Errorf("commit hash %v doesn't seem to exist", pr.Githash) } summaries, err := getSummaries(pr.PatchContent) if err != nil { return nil, nil, err } isEmpty := (pr.PatchContent == "") if finalize && (len(pr.BuildVariants) == 0 || pr.BuildVariants[0] == "") { return nil, nil, fmt.Errorf("no buildvariants specified") } createTime := time.Now() // create a new object ID to use as reference for the patch data patchFileId := bson.NewObjectId().Hex() patchDoc := &patch.Patch{ Id: bson.NewObjectId(), Description: pr.Description, Author: dbUser.Id, Project: pr.ProjectId, Githash: pr.Githash, CreateTime: createTime, Status: evergreen.PatchCreated, BuildVariants: pr.BuildVariants, Tasks: pr.Tasks, IsEmpty: isEmpty, Patches: []patch.ModulePatch{ { ModuleName: "", Githash: pr.Githash, PatchSet: patch.PatchSet{ Patch: pr.PatchContent, PatchFileId: patchFileId, Summary: summaries, }, }, }, } // Get and validate patched config and add it to the patch document project, err := validator.GetPatchedProject(patchDoc, settings) if err != nil { return nil, nil, fmt.Errorf("invalid patched config: %v", err) } if pr.ModuleName != "" { // is there a module? validate it. module, err = project.GetModuleByName(pr.ModuleName) if err != nil { return nil, nil, fmt.Errorf("could not find module %v: %v", pr.ModuleName, err) } if module == nil { return nil, nil, fmt.Errorf("no module named %v", pr.ModuleName) } } // verify that all variants exists for _, buildVariant := range pr.BuildVariants { if buildVariant == "all" || buildVariant == "" { continue } bv := project.FindBuildVariant(buildVariant) if bv == nil { return nil, nil, fmt.Errorf("No such buildvariant: %v", buildVariant) } } // write the patch content into a GridFS file under a new ObjectId after validating. err = db.WriteGridFile(patch.GridFSPrefix, patchFileId, strings.NewReader(pr.PatchContent)) if err != nil { return nil, nil, fmt.Errorf("failed to write patch file to db: %v", err) } // add the project config projectYamlBytes, err := yaml.Marshal(project) if err != nil { return nil, nil, fmt.Errorf("error marshalling patched config: %v", err) } // set the patch number based on patch author patchDoc.PatchNumber, err = dbUser.IncPatchNumber() if err != nil { return nil, nil, fmt.Errorf("error computing patch num %v", err) } patchDoc.PatchedConfig = string(projectYamlBytes) patchDoc.ClearPatchData() return project, patchDoc, nil }
func (as *APIServer) submitPatch(w http.ResponseWriter, r *http.Request) { user := MustHaveUser(r) apiRequest := PatchAPIRequest{ ProjectFileName: r.FormValue("project"), ModuleName: r.FormValue("module"), Githash: r.FormValue("githash"), PatchContent: r.FormValue("patch"), BuildVariants: strings.Split(r.FormValue("buildvariants"), ","), } description := r.FormValue("desc") projId := r.FormValue("project") projectRef, err := model.FindOneProjectRef(projId) if err != nil { message := fmt.Errorf("Error locating project ref '%v': %v", projId, err) as.LoggedError(w, r, http.StatusInternalServerError, message) return } project, err := model.FindProject("", projectRef) if err != nil { message := fmt.Errorf("Error locating project '%v' from '%v': %v", projId, as.Settings.ConfigDir, err) as.LoggedError(w, r, http.StatusInternalServerError, message) return } if project == nil { as.LoggedError(w, r, http.StatusNotFound, fmt.Errorf("project %v not found", projId)) return } patchMetadata, err := apiRequest.Validate(as.Settings.Credentials[projectRef.RepoKind]) if err != nil { as.LoggedError(w, r, http.StatusInternalServerError, fmt.Errorf("Invalid patch: %v", err)) return } if patchMetadata == nil { as.LoggedError(w, r, http.StatusBadRequest, fmt.Errorf("patch metadata is empty")) return } if apiRequest.ModuleName != "" { as.WriteJSON(w, http.StatusBadRequest, "module not allowed when creating new patches (must be added in a subsequent request)") return } patchProjectRef, err := model.FindOneProjectRef(patchMetadata.Project.Identifier) if err != nil { as.LoggedError(w, r, http.StatusInternalServerError, fmt.Errorf("Invalid projectRef: %v", err)) return } if patchProjectRef == nil { as.LoggedError(w, r, http.StatusNotFound, fmt.Errorf("Empty patch project Ref")) return } commitInfo, err := thirdparty.GetCommitEvent(as.Settings.Credentials[projectRef.RepoKind], patchProjectRef.Owner, patchProjectRef.Repo, apiRequest.Githash) if err != nil { as.LoggedError(w, r, http.StatusInternalServerError, err) return } if commitInfo == nil { as.WriteJSON(w, http.StatusBadRequest, "That commit doesn't seem to exist.") return } createTime := time.Now() patchDoc := &patch.Patch{ Id: bson.NewObjectId(), Description: description, Author: user.Id, Project: apiRequest.ProjectFileName, Githash: apiRequest.Githash, CreateTime: createTime, Status: evergreen.PatchCreated, BuildVariants: apiRequest.BuildVariants, Tasks: nil, Patches: []patch.ModulePatch{ patch.ModulePatch{ ModuleName: "", Githash: apiRequest.Githash, PatchSet: patch.PatchSet{ Patch: apiRequest.PatchContent, Summary: patchMetadata.Summaries, }, }, }, } // set the patch number based on patch author patchDoc.PatchNumber, err = user.IncPatchNumber() if err != nil { as.LoggedError(w, r, http.StatusInternalServerError, fmt.Errorf("error computing patch num %v", err)) return } // Get and validate patched config and add it to the patch document patchedProject, err := validator.GetPatchedProject(patchDoc, &as.Settings) if err != nil { as.LoggedError(w, r, http.StatusInternalServerError, fmt.Errorf("invalid patched config: %v", err)) return } projectYamlBytes, err := yaml.Marshal(patchedProject) if err != nil { as.LoggedError(w, r, http.StatusInternalServerError, fmt.Errorf("error marshalling patched config: %v", err)) return } patchDoc.PatchedConfig = string(projectYamlBytes) if err = patchDoc.Insert(); err != nil { as.LoggedError(w, r, http.StatusInternalServerError, fmt.Errorf("error inserting patch: %v", err)) return } if strings.ToLower(r.FormValue("finalize")) == "true" { if _, err = model.FinalizePatch(patchDoc, &as.Settings); err != nil { as.LoggedError(w, r, http.StatusInternalServerError, err) return } } as.WriteJSON(w, http.StatusCreated, PatchAPIResponse{Patch: patchDoc}) }