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

     1  // Copyright 2017 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 api
     6  
     7  import (
     8  	"fmt"
     9  	"net/http"
    10  
    11  	"github.com/web-platform-tests/wpt.fyi/shared"
    12  )
    13  
    14  // apiResultsRedirectHandler is responsible for redirecting to the Google Cloud Storage API
    15  // JSON blob for the given SHA (or latest) shared.TestRun for the given browser.
    16  //
    17  // URL format:
    18  // /results
    19  //
    20  // Params:
    21  // product: Browser (and OS) of the run, e.g. "chrome-63.0" or "safari"
    22  // (optional) run: SHA[0:10] of the test run, or "latest" (latest is the default)
    23  // (optional) test: Path of the test, e.g. "/css/css-images-3/gradient-button.html".
    24  func apiResultsRedirectHandler(w http.ResponseWriter, r *http.Request) {
    25  	filters, err := shared.ParseTestRunFilterParams(r.URL.Query())
    26  	if err != nil {
    27  		http.Error(w, err.Error(), http.StatusBadRequest)
    28  
    29  		return
    30  	}
    31  
    32  	ctx := r.Context()
    33  	store := shared.NewAppEngineDatastore(ctx, true)
    34  	one := 1
    35  	testRuns, err := store.TestRunQuery().LoadTestRuns(
    36  		filters.Products, filters.Labels, filters.SHAs, nil, nil, &one, nil)
    37  	if err != nil {
    38  		http.Error(w, err.Error(), http.StatusInternalServerError)
    39  
    40  		return
    41  	}
    42  	allRuns := testRuns.AllRuns()
    43  	if len(allRuns) == 0 {
    44  		http.Error(w, fmt.Sprintf("404 - Test run '%s' not found", filters.SHAs.FirstOrLatest()), http.StatusNotFound)
    45  
    46  		return
    47  	}
    48  
    49  	test := r.URL.Query().Get("test")
    50  	resultsURL := shared.GetResultsURL(allRuns[0], test)
    51  
    52  	http.Redirect(w, r, resultsURL, http.StatusFound)
    53  }