github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/prow/cmd/deck/templates.go (about)

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"github.com/sirupsen/logrus"
    21  	"html/template"
    22  	"k8s.io/test-infra/prow/cmd/deck/version"
    23  	"k8s.io/test-infra/prow/config"
    24  	"k8s.io/test-infra/prow/deck/jobs"
    25  	"net/http"
    26  	"path"
    27  )
    28  
    29  // This stuff is used in the templates.
    30  type baseTemplateSettings struct {
    31  	MobileFriendly bool
    32  	PageName       string
    33  	Arguments      interface{}
    34  }
    35  
    36  func makeBaseTemplateSettings(mobileFriendly bool, pageName string, arguments interface{}) baseTemplateSettings {
    37  	return baseTemplateSettings{mobileFriendly, pageName, arguments}
    38  }
    39  
    40  func getConcreteBrandingFunction(ca jobs.ConfigAgent) func() config.Branding {
    41  	return func() config.Branding {
    42  		if branding := ca.Config().Deck.Branding; branding != nil {
    43  			return *branding
    44  		}
    45  		return config.Branding{}
    46  	}
    47  }
    48  
    49  type baseTemplateSections struct {
    50  	PR   bool
    51  	Tide bool
    52  }
    53  
    54  func getConcreteSectionFunction(o options) func() baseTemplateSections {
    55  	return func() baseTemplateSections {
    56  		return baseTemplateSections{
    57  			PR:   o.oauthURL != "" || o.pregeneratedData != "",
    58  			Tide: o.tideURL != "" || o.pregeneratedData != "",
    59  		}
    60  	}
    61  }
    62  
    63  func prepareBaseTemplate(o options, ca jobs.ConfigAgent, t *template.Template) (*template.Template, error) {
    64  	return t.Funcs(map[string]interface{}{
    65  		"settings":         makeBaseTemplateSettings,
    66  		"branding":         getConcreteBrandingFunction(ca),
    67  		"sections":         getConcreteSectionFunction(o),
    68  		"mobileFriendly":   func() bool { return true },
    69  		"mobileUnfriendly": func() bool { return false },
    70  		"deckVersion":      func() string { return version.Version },
    71  	}).ParseFiles(path.Join(o.templateFilesLocation, "base.html"))
    72  }
    73  
    74  func handleSimpleTemplate(o options, ca jobs.ConfigAgent, templateName string, param interface{}) http.HandlerFunc {
    75  	return func(w http.ResponseWriter, r *http.Request) {
    76  		t := template.New(templateName) // the name matters, and must match the filename.
    77  		if _, err := prepareBaseTemplate(o, ca, t); err != nil {
    78  			logrus.WithError(err).Error("error preparing base template")
    79  			http.Error(w, "error preparing base template", http.StatusInternalServerError)
    80  			return
    81  		}
    82  		w.Header().Add("Content-Type", "text/html; charset=utf-8")
    83  		if _, err := t.ParseFiles(path.Join(o.templateFilesLocation, templateName)); err != nil {
    84  			logrus.WithError(err).Error("error parsing template " + templateName)
    85  			http.Error(w, "error parsing template", http.StatusInternalServerError)
    86  			return
    87  		}
    88  		if err := t.Execute(w, param); err != nil {
    89  			logrus.WithError(err).Error("error executing template " + templateName)
    90  			http.Error(w, "error executing template", http.StatusInternalServerError)
    91  			return
    92  		}
    93  	}
    94  }