github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/atc/api/pipelineserver/reject_archived_handler_factory.go (about)

     1  package pipelineserver
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  
     7  	"github.com/pf-qiu/concourse/v6/atc"
     8  	"github.com/pf-qiu/concourse/v6/atc/db"
     9  )
    10  
    11  type RejectArchivedHandlerFactory struct {
    12  	teamFactory db.TeamFactory
    13  }
    14  
    15  func NewRejectArchivedHandlerFactory(factory db.TeamFactory) RejectArchivedHandlerFactory {
    16  	return RejectArchivedHandlerFactory{
    17  		teamFactory: factory,
    18  	}
    19  }
    20  
    21  func (f RejectArchivedHandlerFactory) RejectArchived(handler http.Handler) http.Handler {
    22  	return RejectArchivedHandler{
    23  		teamFactory:     f.teamFactory,
    24  		delegateHandler: handler,
    25  	}
    26  }
    27  
    28  type RejectArchivedHandler struct {
    29  	teamFactory     db.TeamFactory
    30  	delegateHandler http.Handler
    31  }
    32  
    33  func (ra RejectArchivedHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    34  	teamName := r.FormValue(":team_name")
    35  	pipelineName := r.FormValue(":pipeline_name")
    36  	pipelineRef := atc.PipelineRef{Name: pipelineName}
    37  	if instanceVars := r.URL.Query().Get("instance_vars"); instanceVars != "" {
    38  		err := json.Unmarshal([]byte(instanceVars), &pipelineRef.InstanceVars)
    39  		if err != nil {
    40  			w.WriteHeader(http.StatusInternalServerError)
    41  			return
    42  		}
    43  	}
    44  
    45  	team, found, err := ra.teamFactory.FindTeam(teamName)
    46  	if err != nil {
    47  		w.WriteHeader(http.StatusInternalServerError)
    48  		return
    49  	}
    50  
    51  	if !found {
    52  		w.WriteHeader(http.StatusNotFound)
    53  		return
    54  	}
    55  
    56  	pipeline, found, err := team.Pipeline(pipelineRef)
    57  	if err != nil {
    58  		w.WriteHeader(http.StatusInternalServerError)
    59  		return
    60  	}
    61  
    62  	if !found {
    63  		w.WriteHeader(http.StatusNotFound)
    64  		return
    65  	}
    66  
    67  	if pipeline.Archived() {
    68  		http.Error(w, "action not allowed for an archived pipeline", http.StatusConflict)
    69  		return
    70  	}
    71  
    72  	ra.delegateHandler.ServeHTTP(w, r)
    73  }