Skip to content

Commit

Permalink
Generates CSS file for labels based on labels.yaml
Browse files Browse the repository at this point in the history
run gofmt

 make write docs and write css one action

Update path that labels.css is generated into deck/static

typo

update hack/verify-labels script

changes hex pattern
  • Loading branch information
qhuynh96 committed Sep 12, 2018
1 parent 0e27cf2 commit 23ce1ff
Show file tree
Hide file tree
Showing 6 changed files with 797 additions and 9 deletions.
7 changes: 5 additions & 2 deletions hack/update-labels.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ TESTINFRA_ROOT=$(git rev-parse --show-toplevel)
LABELS_CONFIG=${LABELS_CONFIG:-"${TESTINFRA_ROOT}/label_sync/labels.yaml"}
LABELS_DOCS_TEMPLATE=${LABELS_DOCS_TEMPLATE:-"${TESTINFRA_ROOT}/label_sync/labels.md.tmpl"}
LABELS_DOCS_OUTPUT=${LABELS_DOCS_OUTPUT:-"${TESTINFRA_ROOT}/label_sync/labels.md"}
LABELS_CSS_TEMPLATE=${LABELS_CSS_TEMPLATE:-"${TESTINFRA_ROOT}/label_sync/labels.css.tmpl"}
LABELS_CSS_OUTPUT=${LABELS_CSS_OUTPUT:-"${TESTINFRA_ROOT}/prow/cmd/deck/static/labels.css"}

bazel run //label_sync -- \
--config=${LABELS_CONFIG} \
--action=docs \
--docs-template=${LABELS_DOCS_TEMPLATE} \
--docs-output=${LABELS_DOCS_OUTPUT}

--docs-output=${LABELS_DOCS_OUTPUT} \
--css-template=${LABELS_CSS_TEMPLATE} \
--css-output=${LABELS_CSS_OUTPUT}
14 changes: 12 additions & 2 deletions hack/verify-labels.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ set -o pipefail
TESTINFRA_ROOT=$(git rev-parse --show-toplevel)

TMP_LABELS_DOCS=$(mktemp)
trap "rm -f $TMP_LABELS_DOCS" EXIT
LABELS_DOCS_OUTPUT="${TMP_LABELS_DOCS}" ${TESTINFRA_ROOT}/hack/update-labels.sh
TMP_LABELS_CSS=$(mktemp)
trap 'rm -f "${TMP_LABELS_DOCS}" & rm -f "${TMP_LABELS_CSS}"' EXIT
LABELS_DOCS_OUTPUT="${TMP_LABELS_DOCS}" LABELS_CSS_OUTPUT="${TMP_LABELS_CSS}" ${TESTINFRA_ROOT}/hack/update-labels.sh


DIFF=$(diff "${TMP_LABELS_DOCS}" "${TESTINFRA_ROOT}/label_sync/labels.md" || true)
if [ ! -z "$DIFF" ]; then
Expand All @@ -30,3 +32,11 @@ if [ ! -z "$DIFF" ]; then
echo "FAILED: labels.yaml was updated without updating labels.md, please run 'hack/update-labels.sh'"
exit 1
fi

DIFF=$(diff "${TMP_LABELS_CSS}" "${TESTINFRA_ROOT}/prow/cmd/deck/static/labels.css" || true)
if [ ! -z "$DIFF" ]; then
echo "${DIFF}"
echo ""
echo "FAILED: labels.yaml was updated without updating labels.css, please run 'hack/update-labels.sh'"
exit 1
fi
6 changes: 4 additions & 2 deletions label_sync/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,14 @@ bazel run //label_sync -- \
--only kubernetes/community,kubernetes/steering
# see above
# generate docs based on labels.yaml
# generate docs and a css file contains labels styling based on labels.yaml
bazel run //label_sync -- \
--action docs \
--config $(pwd)/label_sync/labels.yaml \
--docs-template $(pwd)/label_sync/labels.md.tmpl \
--docs-output $(pwd)/label_sync/labels.md
--docs-output $(pwd)/label_sync/labels.md \
--css-template $(pwd)/label_sync/labels.css.tmpl \
--css-output $(pwd)/prow/cmd/deck/static/labels.css
```

## Our Deployment
Expand Down
7 changes: 7 additions & 0 deletions label_sync/labels.css.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{{ range $labelCSS := . -}}
.{{- $labelCSS.Name }} {
background-color: #{{ $labelCSS.BackgroundColor -}};
color: #{{ $labelCSS.Color -}};
}

{{ end }}
91 changes: 88 additions & 3 deletions label_sync/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ limitations under the License.
package main

import (
"encoding/hex"
"errors"
"flag"
"fmt"
"io/ioutil"
"math"
"os"
"path/filepath"
"regexp"
Expand All @@ -30,6 +32,7 @@ import (
"sync"
"text/template"
"time"
"unicode"

"github.com/ghodss/yaml"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -121,6 +124,8 @@ var (
skipRepos = flag.String("skip", "", "Comma separated list of org/repos to skip syncing")
token = flag.String("token", "", "Path to github oauth secret")
action = flag.String("action", "sync", "One of: sync, docs")
cssTemplate = flag.String("css-template", "", "Path to template file for label css")
cssOutput = flag.String("css-output", "", "Path to output file for css")
docsTemplate = flag.String("docs-template", "", "Path to template file for label docs")
docsOutput = flag.String("docs-output", "", "Path to output file for docs")
tokens = flag.Int("tokens", defaultTokens, "Throttle hourly token consumption (0 to disable)")
Expand Down Expand Up @@ -649,9 +654,7 @@ func main() {

switch {
case *action == "docs":
if err := writeDocs(*docsTemplate, *docsOutput, *config); err != nil {
logrus.WithError(err).Fatalf("failed to write docs using docs-template %s to docs-output %s", *docsTemplate, *docsOutput)
}
writeDocsAndCSS(*docsTemplate, *docsOutput, *cssTemplate, *cssOutput, *config)
case *action == "sync":
githubClient, err := newClient(*token, *tokens, *tokenBurst, !*confirm, endpoint.Strings()...)
if err != nil {
Expand Down Expand Up @@ -705,6 +708,15 @@ type labelData struct {
Description, Link, Labels interface{}
}

func writeDocsAndCSS(docTmpl, docOut, cssTmpl, cssOut string, config Configuration) {
if err := writeDocs(docTmpl, docOut, config); err != nil {
logrus.WithError(err).Fatalf("failed to write docs using docs-template %s to docs-output %s", docTmpl, docOut)
}
if err := writeCSS(cssTmpl, cssOut, config); err != nil {
logrus.WithError(err).Fatalf("failed to write css file using css-template %s to css-output %s", cssTmpl, cssOut)
}
}

func writeDocs(template string, output string, config Configuration) error {
var desc string
data := []labelData{}
Expand Down Expand Up @@ -785,3 +797,76 @@ func syncOrg(org string, githubClient client, config Configuration, filt filter)
}
return nil
}

type labelCSSData struct {
BackgroundColor, Color, Name string
}

// Returns the CSS escaped label name. Escaped method based on
// https://www.w3.org/International/questions/qa-escapes#cssescapes
func cssEscape(s string) (escaped string) {
var IsAlpha = regexp.MustCompile(`^[a-zA-Z]+$`).MatchString
for i, c := range s {
if (i == 0 && unicode.IsDigit(c)) || !(unicode.IsDigit(c) || IsAlpha(string(c))) {
escaped += fmt.Sprintf("x%0.6x", c)
continue
}
escaped += string(c)
}
return
}

// Returns the text color (whether black or white) given the background color.
// Details: https://www.w3.org/TR/WCAG20/#contrastratio
func getTextColor(backgroundColor string) (string, error) {
d, err := hex.DecodeString(backgroundColor)
if err != nil || len(d) != 3 {
return "", errors.New("expect 6-digit color hex of label")
}

// Calculate the relative luminance (L) of a color
// L = 0.2126 * R + 0.7152 * G + 0.0722 * B
// Formula details at: https://www.w3.org/TR/WCAG20/#relativeluminancedef
color := [3]float64{}
for i, v := range d {
color[i] = float64(v) / 255.0
if color[i] <= 0.03928 {
color[i] = color[i] / 12.92
} else {
color[i] = math.Pow((color[i]+0.055)/1.055, 2.4)
}
}
L := 0.2126*color[0] + 0.7152*color[1] + 0.0722*color[2]

if (L+0.05)/(0.0+0.05) > (1.0+0.05)/(L+0.05) {
return "000000", nil
} else {
return "ffffff", nil
}
}

func writeCSS(tmplPath string, outPath string, config Configuration) error {
var prLabels []Label
prLabels = append(prLabels, LabelsForTarget(config.Default.Labels, bothTarget)...)
prLabels = append(prLabels, LabelsForTarget(config.Default.Labels, prTarget)...)

for repo := range config.Repos {
prLabels = append(prLabels, LabelsForTarget(config.Repos[repo].Labels, bothTarget)...)
prLabels = append(prLabels, LabelsForTarget(config.Repos[repo].Labels, prTarget)...)
}

var labelCSS []labelCSSData
for _, l := range prLabels {
textColor, err := getTextColor(l.Color)
if err != nil {
return err
}

labelCSS = append(labelCSS, labelCSSData{
BackgroundColor: l.Color,
Color: textColor,
Name: cssEscape(l.Name),
})
}
return writeTemplate(tmplPath, outPath, labelCSS)
}
Loading

0 comments on commit 23ce1ff

Please sign in to comment.