github.com/web-platform-tests/wpt.fyi@v0.0.0-20240530210107-70cf978996f1/api/receiver/update_pending_run.go (about)

     1  // Copyright 2019 The WPT Dashboard Project. All rights reserved.
     2  // Use of this source code is governed by a BSD-style license that can be
     3  // found in the LICENSE file.
     4  
     5  package receiver
     6  
     7  import (
     8  	"encoding/json"
     9  	"fmt"
    10  	"io"
    11  	"net/http"
    12  	"strconv"
    13  
    14  	"github.com/gorilla/mux"
    15  	"github.com/web-platform-tests/wpt.fyi/shared"
    16  )
    17  
    18  // HandleUpdatePendingTestRun handles the PATCH request for updating pending test runs.
    19  func HandleUpdatePendingTestRun(a API, w http.ResponseWriter, r *http.Request) {
    20  	if AuthenticateUploader(a, r) != InternalUsername {
    21  		http.Error(w, "This is a private API.", http.StatusUnauthorized)
    22  
    23  		return
    24  	}
    25  
    26  	body, err := io.ReadAll(r.Body)
    27  	if err != nil {
    28  		http.Error(w, err.Error(), http.StatusInternalServerError)
    29  
    30  		return
    31  	}
    32  	var run shared.PendingTestRun
    33  	if err := json.Unmarshal(body, &run); err != nil {
    34  		http.Error(w, "Failed to parse JSON: "+err.Error(), http.StatusBadRequest)
    35  
    36  		return
    37  	}
    38  
    39  	vars := mux.Vars(r)
    40  	idParam := vars["id"]
    41  	id, err := strconv.ParseInt(idParam, 10, 0)
    42  	if err != nil {
    43  		http.Error(w, "Invalid ID: "+idParam, http.StatusBadRequest)
    44  
    45  		return
    46  	}
    47  	if id != run.ID {
    48  		http.Error(w, fmt.Sprintf("Inconsistent ID: %d != %d", id, run.ID), http.StatusBadRequest)
    49  
    50  		return
    51  	}
    52  
    53  	if err := a.UpdatePendingTestRun(run); err != nil {
    54  		http.Error(w, "Failed to update run: "+err.Error(), http.StatusInternalServerError)
    55  
    56  		return
    57  	}
    58  	w.WriteHeader(http.StatusCreated)
    59  }