go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/scheduler/appengine/ui/invocation.go (about)

     1  // Copyright 2015 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package ui
    16  
    17  import (
    18  	"fmt"
    19  	"net/http"
    20  	"strconv"
    21  
    22  	"go.chromium.org/luci/server/router"
    23  	"go.chromium.org/luci/server/templates"
    24  
    25  	"go.chromium.org/luci/scheduler/appengine/engine"
    26  )
    27  
    28  func invocationPage(c *router.Context) {
    29  	projectID := c.Params.ByName("ProjectID")
    30  	jobName := c.Params.ByName("JobName")
    31  	invID, err := strconv.ParseInt(c.Params.ByName("InvID"), 10, 64)
    32  	if err != nil {
    33  		uiErrBadInvocationID.render(c)
    34  		return
    35  	}
    36  
    37  	job := jobFromEngine(c, projectID, jobName)
    38  	if job == nil {
    39  		return
    40  	}
    41  
    42  	inv, err := config(c.Request.Context()).Engine.GetInvocation(c.Request.Context(), job, invID)
    43  	switch {
    44  	case err == engine.ErrNoSuchInvocation:
    45  		uiErrNoInvocation.render(c)
    46  		return
    47  	case err != nil:
    48  		panic(err)
    49  	}
    50  
    51  	jobUI := makeJob(c.Request.Context(), job, nil)
    52  	templates.MustRender(c.Request.Context(), c.Writer, "pages/invocation.html", map[string]any{
    53  		"Job": jobUI,
    54  		"Inv": makeInvocation(jobUI, inv),
    55  	})
    56  }
    57  
    58  ////////////////////////////////////////////////////////////////////////////////
    59  // Actions.
    60  
    61  func abortInvocationAction(c *router.Context) {
    62  	handleInvAction(c, func(job *engine.Job, invID int64) error {
    63  		return config(c.Request.Context()).Engine.AbortInvocation(c.Request.Context(), job, invID)
    64  	})
    65  }
    66  
    67  func handleInvAction(c *router.Context, cb func(*engine.Job, int64) error) {
    68  	projectID := c.Params.ByName("ProjectID")
    69  	jobName := c.Params.ByName("JobName")
    70  	invID := c.Params.ByName("InvID")
    71  	invIDAsInt, err := strconv.ParseInt(invID, 10, 64)
    72  	if err != nil {
    73  		uiErrBadInvocationID.render(c)
    74  		return
    75  	}
    76  
    77  	job := jobFromEngine(c, projectID, jobName)
    78  	if job == nil {
    79  		return
    80  	}
    81  
    82  	switch err := cb(job, invIDAsInt); {
    83  	case err == engine.ErrNoPermission:
    84  		uiErrActionForbidden.render(c)
    85  		return
    86  	case err != nil:
    87  		panic(err)
    88  	default:
    89  		http.Redirect(c.Writer, c.Request, fmt.Sprintf("/jobs/%s/%s/%s", projectID, jobName, invID), http.StatusFound)
    90  	}
    91  }