func (ms *MockServer) HandleUpdateTask(w http.ResponseWriter, r *http.Request) {
	updateTaskReq := UpdateTaskReq{Url: r.URL.Path}
	data, err := webhook.AuthenticateRequest(r)
	defer skutil.Close(r.Body)
	if err != nil {
		updateTaskReq.Error = err
		skutil.ReportError(w, r, err, "")
		return
	}
	err = json.Unmarshal(data, &updateTaskReq.Vars)
	if err != nil {
		updateTaskReq.Error = err
		skutil.ReportError(w, r, err, "")
		return
	}
	w.Header().Set("Content-Type", "application/json")
	ms.mutex.Lock()
	defer ms.mutex.Unlock()
	ms.updateTaskReqs = append(ms.updateTaskReqs, updateTaskReq)
}
func UpdateTaskHandler(vars UpdateTaskVars, tableName string, w http.ResponseWriter, r *http.Request) {
	data, err := webhook.AuthenticateRequest(r)
	if err != nil {
		if data == nil {
			skutil.ReportError(w, r, err, "Failed to read update request")
			return
		}
		if !ctfeutil.UserHasAdminRights(r) {
			skutil.ReportError(w, r, err, "Failed authentication")
			return
		}
	}
	w.Header().Set("Content-Type", "application/json")
	if err := json.Unmarshal(data, &vars); err != nil {
		skutil.ReportError(w, r, err, fmt.Sprintf("Failed to parse %T update", vars))
		return
	}
	defer skutil.Close(r.Body)

	if err := UpdateTask(vars, tableName); err != nil {
		skutil.ReportError(w, r, err, fmt.Sprintf("Failed to update %T task", vars))
		return
	}
}
func addTrybotTaskHandler(w http.ResponseWriter, r *http.Request) {
	data, err := webhook.AuthenticateRequest(r)
	if err != nil {
		if data == nil {
			skutil.ReportError(w, r, err, "Failed to read add request")
			return
		}
		if !ctfeutil.UserHasAdminRights(r) {
			skutil.ReportError(w, r, err, "Failed authentication")
			return
		}
	}

	trybotTask := TrybotTask{}
	if err := json.Unmarshal(data, &trybotTask); err != nil {
		skutil.ReportError(w, r, err, fmt.Sprintf("Failed to add %v trybot task", trybotTask))
		return
	}

	task := &trybotTask.TaskVars
	// Add patch data to the task.
	detail, err := getCLDetail(trybotTask.Issue)
	if err != nil {
		skutil.ReportError(w, r, err, "")
		return
	}
	patchsetID, err := strconv.Atoi(trybotTask.PatchsetID)
	if err != nil {
		skutil.ReportError(w, r, err, "")
		return
	}
	patch, err := getCLPatch(detail, patchsetID)
	if err != nil {
		skutil.ReportError(w, r, err, "")
		return
	}
	clData, err := gatherCLData(detail, patch)
	if err != nil {
		skutil.ReportError(w, r, err, "")
		return
	}

	task.Description = fmt.Sprintf("Trybot run for http://codereview.chromium.org/%s#ps%s", clData["cl"], clData["patchset"])
	if val, ok := clData["chromium_patch"]; ok {
		task.ChromiumPatch = val
	}
	if val, ok := clData["skia_patch"]; ok {
		task.SkiaPatch = val
	}

	task.GetAddTaskCommonVars().TsAdded = ctutil.GetCurrentTs()

	taskID, err := task_common.AddTask(task)
	if err != nil {
		skutil.ReportError(w, r, err, fmt.Sprintf("Failed to insert %T task", task))
		return
	}

	w.Header().Set("Content-Type", "application/json")
	jsonResponse := map[string]interface{}{
		"taskID": taskID,
	}
	if err := json.NewEncoder(w).Encode(jsonResponse); err != nil {
		skutil.ReportError(w, r, err, "Failed to encode JSON")
		return
	}
}
Beispiel #4
0
// ingestBuildHandler parses the JSON body as a build and ingests it. The request must be
// authenticated via the protocol implemented in the webhook package. The client should retry this
// request several times, because some errors may be temporary.
func ingestBuildHandler(w http.ResponseWriter, r *http.Request) {
	data, err := webhook.AuthenticateRequest(r)
	if err != nil {
		glog.Errorf("Failed authentication in ingestBuildHandler: %s", err)
		util.ReportError(w, r, fmt.Errorf("Failed authentication."), "")
		return
	}
	vars := map[string]string{}
	if err := json.Unmarshal(data, &vars); err != nil {
		util.ReportError(w, r, err, "Failed to parse request.")
		return
	}
	target := vars["target"]
	commitHash := vars["commitHash"]
	status := vars["status"]
	if target == "" || commitHash == "" || status == "" {
		util.ReportError(w, r, err, "Invalid parameters.")
		return
	}
	cl := vars["changeListNumber"]
	link := vars["testResultsLink"]
	codename := ""
	if noCodenameTargets[target] {
		codename = target
	} else {
		codename = util.StringToCodeName(target)
	}
	buildbotResults, err := buildbot.ParseResultsString(status)
	if err != nil {
		util.ReportError(w, r, err, "Invalid parameters.")
		return
	}
	buildbotTime := time.Now().UTC()
	b := &buildbot.Build{
		Builder:       codename,
		Master:        FAKE_MASTER,
		Number:        0,
		BuildSlave:    FAKE_BUILDSLAVE,
		Branch:        "master",
		Commits:       nil,
		GotRevision:   commitHash,
		PropertiesStr: "",
		Results:       buildbotResults,
		Steps:         nil,
		Started:       buildbotTime,
		Finished:      buildbotTime,
		Comments:      nil,
		Repository:    "https://skia.googlesource.com/skia.git",
	}
	if cl != "" {
		if clNum, err := strconv.Atoi(cl); err == nil {
			b.Properties = append(b.Properties, []interface{}{"changeListNumber", strconv.Itoa(clNum), "datahopper_internal"})
		} else {
			util.ReportError(w, r, err, "Invalid parameters.")
			return
		}
	}
	if link != "" {
		if url, err := url.Parse(link); err == nil {
			b.Properties = append(b.Properties, []interface{}{"testResultsLink", url.String(), "datahopper_internal"})
		} else {
			util.ReportError(w, r, err, "Invalid parameters.")
			return
		}
	}
	// Fill in PropertiesStr based on Properties.
	props, err := json.Marshal(b.Properties)
	if err == nil {
		b.PropertiesStr = string(props)
	} else {
		glog.Errorf("Failed to encode properties: %s", err)
	}
	if err := repos.Update(); err != nil {
		util.ReportError(w, r, err, "Failed to update repos.")
		return
	}
	if err := ingestBuild(b, commitHash, target); err != nil {
		util.ReportError(w, r, err, "Failed to ingest build.")
		return
	}
}