@@ -2,12 +2,16 @@ package appliance
22
33import (
44 "context"
5+ "fmt"
56 "html/template"
67 "net/http"
78
9+ "github.com/life4/genesis/slices"
10+
811 "github.com/sourcegraph/log"
912
1013 "github.com/sourcegraph/sourcegraph/internal/appliance/config"
14+ "github.com/sourcegraph/sourcegraph/internal/releaseregistry"
1115)
1216
1317const (
@@ -29,11 +33,47 @@ func (a *Appliance) applianceHandler(w http.ResponseWriter, r *http.Request) {
2933}
3034
3135func (a * Appliance ) getSetupHandler (w http.ResponseWriter , r * http.Request ) {
32- err := setupTmpl . Execute ( w , "" )
36+ versions , err := a . getVersions ( r . Context () )
3337 if err != nil {
34- a .logger .Error ("failed to execute templating" , log .Error (err ))
35- // Handle err
38+ a .handleError (w , err , "getting versions" )
39+ return
40+ }
41+ versions , err = NMinorVersions (versions , a .latestSupportedVersion , 2 )
42+ if err != nil {
43+ a .handleError (w , err , "filtering versions to 2 minor points" )
44+ return
45+ }
46+
47+ err = setupTmpl .Execute (w , struct {
48+ Versions []string
49+ }{
50+ Versions : versions ,
51+ })
52+ if err != nil {
53+ a .handleError (w , err , "executing template" )
54+ return
55+ }
56+ }
57+
58+ func (a * Appliance ) handleError (w http.ResponseWriter , err error , msg string ) {
59+ a .logger .Error (msg , log .Error (err ))
60+
61+ // TODO we should probably look twice at this and decide whether it's in
62+ // line with existing standards.
63+ // Don't leak details of internal errors to users - that's why we have
64+ // logging above.
65+ w .WriteHeader (http .StatusInternalServerError )
66+ fmt .Fprintln (w , "Something went wrong - please contact support." )
67+ }
68+
69+ func (a * Appliance ) getVersions (ctx context.Context ) ([]string , error ) {
70+ versions , err := a .releaseRegistryClient .ListVersions (ctx , "sourcegraph" )
71+ if err != nil {
72+ return nil , err
3673 }
74+ return slices .MapFilter (versions , func (version releaseregistry.ReleaseInfo ) (string , bool ) {
75+ return version .Version , version .Public
76+ }), nil
3777}
3878
3979func (a * Appliance ) postSetupHandler (w http.ResponseWriter , r * http.Request ) {
0 commit comments