Skip to content

Commit

Permalink
DRY
Browse files Browse the repository at this point in the history
Signed-off-by: Ian Maddaus <[email protected]>
  • Loading branch information
IanMadd committed Nov 10, 2023
1 parent 8088df6 commit 4a97a90
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 120 deletions.
64 changes: 3 additions & 61 deletions themes/docs-new/layouts/_default/release_notes.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,68 +11,10 @@
{{ if or (hugo.IsProduction) ( eq hugo.Environment "staging") ( eq hugo.Environment "branch-deploy")}}
{{ with $product := $.Param "release_notes" }}

{{- $versionsUrl := "" -}}
{{- if eq $product "automate" -}}
{{- $versionsUrl = "https://packages.chef.io/releases/current/automate.json"}}
{{- else if (eq $product "habitat") -}}
{{- $versionsUrl = "assets/release-notes/habitat/release-versions.json" -}}
{{- else if (hasPrefix $product "inspec-") -}}
{{- $versionsUrl = print "_vendor/github.com/inspec/" $product "/docs-chef-io/assets/release-notes/" $product "/release-dates.json" -}}
{{- else -}}
{{- $versionsUrl = (print "https://omnitruck.chef.io/stable/" $product "/versions/all") -}}
{{- end -}}
{{ $partialData := dict "product" $product "chefVersions" $.Site.Data.releases.chef.current }}
{{ $versionsCorrectOrder := partial "version_numbers.html" $partialData }}

{{ $versions := "" }}

{{ $headers := dict "Cache-Control" "no-cache" "Connection" "keep-alive" }}
{{ $cacheKey := print $versionsUrl (now.Format "20060102") (div now.Hour 6) | md5 }}
{{ $opts := dict "headers" $headers "key" $cacheKey }}

{{- if hasPrefix $versionsUrl "https://" -}}
{{- with resources.GetRemote $versionsUrl $opts -}}
{{ with .Err }}
{{ errorf "%s" . }}
{{ else }}
{{ $unmarshalOpts := dict "delimiter" "," }}
{{ $versions = . | transform.Unmarshal $unmarshalOpts }}
{{ end }}
{{ else }}
{{ errorf "Unable to get remote resource %q" $versionsUrl }}
{{ end }}
{{ else }}
{{ $versions = getJSON $versionsUrl }}
{{ end }}

{{/* We have a "current" version of Infra Client that they want release notes for on the Client release notes page. */}}
{{/* This allows us to add current release version numbers of Client to the list of version numbers */}}
{{/* that we want to include in the Client release notes. */}}
{{- if eq $product "chef" -}}
{{- $current_versions := $.Site.Data.releases.chef.current -}}
{{- $versions = append $current_versions $versions -}}
{{- $paddedVersions := apply $versions "partial" "zero_prefix_pad" "." }}
{{- $sortedVersions := (sort $paddedVersions "value" "desc") }}
{{- $versions = apply $sortedVersions "partial" "zero_prefix_trim" "." }}
{{- end -}}

{{- $versionsCorrectOrder := slice -}}

{{- if eq $product "automate" -}}
{{- $len := len $versions -}}
{{- range seq $len -}}
{{- $versionsCorrectOrder = $versionsCorrectOrder | append (index $versions (sub $len .)) -}}
{{- end -}}
{{- else if eq $product "chef" -}}
{{- $versionsCorrectOrder = $versions -}}
{{ else }}
{{ $len := len $versions }}
{{- range seq $len -}}
{{- $versionsCorrectOrder = $versionsCorrectOrder | append (index $versions (sub $len .)) }}
{{ end }}
{{- end -}}



{{- range $versionsCorrectOrder}}
{{- range $versionsCorrectOrder }}

{{- $version := "" -}}

Expand Down
61 changes: 2 additions & 59 deletions themes/docs-new/layouts/partials/release_notes_toc.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,65 +10,8 @@

{{ with $product := $.Param "release_notes" }}

{{ $versionsUrl := ""}}
{{ if eq $product "automate" }}
{{ $versionsUrl = "https://packages.chef.io/releases/current/automate.json"}}
{{ else if (eq $product "habitat") }}
{{ $versionsUrl = "assets/release-notes/habitat/release-versions.json" }}
{{ else if (hasPrefix $product "inspec-") }}
{{ $versionsUrl = print "_vendor/github.com/inspec/" $product "/docs-chef-io/assets/release-notes/" $product "/release-dates.json" }}
{{ else }}
{{ $versionsUrl = (print "https://omnitruck.chef.io/stable/" $product "/versions/all") }}
{{ end }}

{{ $versions := "" }}

{{ $headers := dict "Cache-Control" "no-cache" "Connection" "keep-alive" }}
{{ $cacheKey := print $versionsUrl (now.Format "20060102") (div now.Hour 6) | md5 }}
{{ $opts := dict "headers" $headers "key" $cacheKey }}

{{- if hasPrefix $versionsUrl "https://" -}}
{{- with resources.GetRemote $versionsUrl $opts -}}
{{ with .Err }}
{{ errorf "%s" . }}
{{ else }}
{{ $unmarshalOpts := dict "delimiter" "," }}
{{ $versions = . | transform.Unmarshal $unmarshalOpts }}
{{ end }}
{{ else }}
{{ errorf "Unable to get remote resource %q" $versionsUrl }}
{{ end }}
{{ else }}
{{ $versions = getJSON $versionsUrl }}
{{ end }}

{{/* We have a current version of Infra Client that they want release notes for on the Client release notes page. */}}
{{/* This allows us to add current release version numbers of Client to the list of version numbers */}}
{{/* that we want to include in the Client release notes. */}}
{{- $current_versions := slice -}}
{{- if eq $product "chef" -}}
{{- $current_versions := $.Site.Data.releases.chef.current -}}
{{- $versions = append $current_versions $versions -}}
{{- $paddedVersions := apply $versions "partial" "zero_prefix_pad" "." }}
{{- $sortedVersions := (sort $paddedVersions "value" "desc") }}
{{- $versions = apply $sortedVersions "partial" "zero_prefix_trim" "." }}
{{- end -}}

{{- $versionsCorrectOrder := slice -}}

{{- if eq $product "automate" -}}
{{- $len := len $versions -}}
{{- range seq $len -}}
{{- $versionsCorrectOrder = $versionsCorrectOrder | append (index $versions (sub $len .)) -}}
{{- end -}}
{{- else if eq $product "chef" -}}
{{- $versionsCorrectOrder = $versions -}}
{{ else }}
{{ $len := len $versions }}
{{- range seq $len -}}
{{- $versionsCorrectOrder = $versionsCorrectOrder | append (index $versions (sub $len .)) }}
{{ end }}
{{- end -}}
{{ $partialData := dict "product" $product "chefVersions" $.Site.Data.releases.chef.current }}
{{ $versionsCorrectOrder := partial "version_numbers.html" $partialData }}

{{ $lastDate := ""}}
{{ if eq $product "automate" }}
Expand Down
64 changes: 64 additions & 0 deletions themes/docs-new/layouts/partials/version_numbers.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{{ $product := .product }}
{{ $chefVersions := .chefVersions }}

{{ $versionsUrl := ""}}
{{ if eq $product "automate" }}
{{ $versionsUrl = "https://packages.chef.io/releases/current/automate.json"}}
{{ else if (eq $product "habitat") }}
{{ $versionsUrl = "assets/release-notes/habitat/release-versions.json" }}
{{ else if (hasPrefix $product "inspec-") }}
{{ $versionsUrl = print "_vendor/github.com/inspec/" $product "/docs-chef-io/assets/release-notes/" $product "/release-dates.json" }}
{{ else }}
{{ $versionsUrl = (print "https://omnitruck.chef.io/stable/" $product "/versions/all") }}
{{ end }}

{{ $versions := "" }}

{{ $headers := dict "Cache-Control" "no-cache" "Connection" "keep-alive" }}
{{ $cacheKey := print $versionsUrl (now.Format "20060102") (div now.Hour 6) | md5 }}
{{ $opts := dict "headers" $headers "key" $cacheKey }}

{{- if hasPrefix $versionsUrl "https://" -}}
{{- with resources.GetRemote $versionsUrl $opts -}}
{{ with .Err }}
{{ errorf "%s" . }}
{{ else }}
{{ $unmarshalOpts := dict "delimiter" "," }}
{{ $versions = . | transform.Unmarshal $unmarshalOpts }}
{{ end }}
{{ else }}
{{ errorf "Unable to get remote resource %q" $versionsUrl }}
{{ end }}
{{ else }}
{{ $versions = getJSON $versionsUrl }}
{{ end }}


{{/* We have a current version of Infra Client that they want release notes for on the Client release notes page. */}}
{{/* This allows us to add current release version numbers of Client to the list of version numbers */}}
{{/* that we want to include in the Client release notes. */}}
{{- $current_versions := slice -}}
{{- if eq $product "chef" -}}
{{- $versions = append $chefVersions $versions -}}
{{- $paddedVersions := apply $versions "partial" "zero_prefix_pad" "." }}
{{- $sortedVersions := (sort $paddedVersions "value" "desc") }}
{{- $versions = apply $sortedVersions "partial" "zero_prefix_trim" "." }}
{{- end -}}

{{- $versionsCorrectOrder := slice -}}

{{- if eq $product "automate" -}}
{{- $len := len $versions -}}
{{- range seq $len -}}
{{- $versionsCorrectOrder = $versionsCorrectOrder | append (index $versions (sub $len .)) -}}
{{- end -}}
{{- else if eq $product "chef" -}}
{{- $versionsCorrectOrder = $versions -}}
{{ else }}
{{ $len := len $versions }}
{{- range seq $len -}}
{{- $versionsCorrectOrder = $versionsCorrectOrder | append (index $versions (sub $len .)) }}
{{ end }}
{{- end -}}

{{ return $versionsCorrectOrder }}

0 comments on commit 4a97a90

Please sign in to comment.