Create ReleasedIn endpoints /findreleasedbuild, /findreleasedbuildv2

/findreleasedbuildv2 is going to be used in the changelog-webapp
/findreleasedbuild is a lighter weight version to be called from Gerrit UI

The endpoint returns a json response:
eg: {"versions" : "16619.0.0"}

B=b/239224440

Change-Id: I3279a2824792c0e7d61d07b2cd5b82745340b887
Reviewed-on: https://cos-review.googlesource.com/c/cos/tools/+/35753
Tested-by: Arnav Kansal <rnv@google.com>
Cloud-Build: GCB Service account <228075978874@cloudbuild.gserviceaccount.com>
Reviewed-by: Emmanuel Ketsri <emmank@google.com>
diff --git a/src/cmd/changelog-webapp/controllers/pageHandlers.go b/src/cmd/changelog-webapp/controllers/pageHandlers.go
index c1f8c48..d421f8c 100644
--- a/src/cmd/changelog-webapp/controllers/pageHandlers.go
+++ b/src/cmd/changelog-webapp/controllers/pageHandlers.go
@@ -16,6 +16,7 @@
 
 import (
 	"context"
+	"encoding/json"
 	"fmt"
 	"net/http"
 	"os"
@@ -550,3 +551,30 @@
 		http.Error(w, err.Error(), http.StatusInternalServerError)
 	}
 }
+
+// HandleFindReleasedBuildGerrit returns the released build number in JSON
+func HandleFindReleasedBuildGerrit(w http.ResponseWriter, r *http.Request) {
+	var err error
+	if err = r.ParseForm(); err != nil {
+		log.Errorf("error parsing form: %v", err)
+		http.Error(w, err.Error(), http.StatusInternalServerError)
+		return
+	}
+	cl := r.FormValue("cl")
+	// If no CL value specified in request, display empty CL form
+	if cl == "" {
+		http.Error(w, "empty cl provided", http.StatusInternalServerError)
+		return
+	}
+	internal, gerrit, gob, repo := false, externalGerritInstance, externalGoBInstance, externalManifestRepo
+	if r.FormValue("cos-internal") == "true" {
+		internal, gerrit, gob, repo = true, internalGerritInstance, internalGoBInstance, internalManifestRepo
+	}
+	buildData, utilErr := findReleaseBuild(nil, gerrit, gob, repo, cl, internal)
+	if utilErr != nil {
+		log.Errorf("error retrieving build for CL %s with internal set to %t\n%v", cl, internal, utilErr)
+		http.Error(w, err.Error(), http.StatusInternalServerError)
+		return
+	}
+	json.NewEncoder(w).Encode(map[string]string{"versions": buildData.BuildNum})
+}
diff --git a/src/cmd/changelog-webapp/main.go b/src/cmd/changelog-webapp/main.go
index 24a7900..4805973 100644
--- a/src/cmd/changelog-webapp/main.go
+++ b/src/cmd/changelog-webapp/main.go
@@ -41,7 +41,8 @@
 	http.HandleFunc("/readme/", controllers.HandleReadme)
 	http.HandleFunc("/changelog/", controllers.HandleChangelog)
 	http.HandleFunc("/findbuild/", controllers.HandleFindBuild)
-	http.HandleFunc("/findreleasedbuild/", controllers.HandleFindReleasedBuild)
+	http.HandleFunc("/findreleasedbuildv2/", controllers.HandleFindReleasedBuild)
+	http.HandleFunc("/findreleasedbuild/", controllers.HandleFindReleasedBuildGerrit)
 	http.HandleFunc("/login/", controllers.HandleLogin)
 	http.HandleFunc("/oauth2callback/", controllers.HandleCallback)
 	http.HandleFunc("/signout/", controllers.HandleSignOut)
diff --git a/src/cmd/changelog-webapp/static/templates/changelog.html b/src/cmd/changelog-webapp/static/templates/changelog.html
index a77c3b4..261d869 100644
--- a/src/cmd/changelog-webapp/static/templates/changelog.html
+++ b/src/cmd/changelog-webapp/static/templates/changelog.html
@@ -14,7 +14,7 @@
     <a href="/">Home</a>
     <a class="active" href="/changelog/">Changelog</a>
     <a href="/findbuild/">Find Build</a>
-    <a href="/findreleasedbuild/">Find Released Build</a>
+    <a href="/findreleasedbuildv2/">Find Released Build</a>
     <a href="/readme/">Readme</a>
   </div>
   <div class="main">
@@ -197,4 +197,4 @@
   </table>
   </div>
 </body>
-</html>
\ No newline at end of file
+</html>
diff --git a/src/cmd/changelog-webapp/static/templates/error.html b/src/cmd/changelog-webapp/static/templates/error.html
index 8521e29..4ed3715 100644
--- a/src/cmd/changelog-webapp/static/templates/error.html
+++ b/src/cmd/changelog-webapp/static/templates/error.html
@@ -29,10 +29,10 @@
     {{else}}
       <a href="/findbuild/">Find Build</a>
     {{end}}
-    {{if (eq .ActivePage "/findreleasedbuild/")}}
-      <a class="active" href="/findreleasedbuild/">Find Released Build</a>
+    {{if (eq .ActivePage "/findreleasedbuildv2/")}}
+      <a class="active" href="/findreleasedbuildv2/">Find Released Build</a>
     {{else}}
-      <a href="/findreleasedbuild/">Find Released Build</a>
+      <a href="/findreleasedbuildv2/">Find Released Build</a>
     {{end}}
     <a href="/readme/">Readme</a>
   </div>
@@ -45,4 +45,4 @@
   </div>
 </body>
 
-</html>
\ No newline at end of file
+</html>
diff --git a/src/cmd/changelog-webapp/static/templates/findBuild.html b/src/cmd/changelog-webapp/static/templates/findBuild.html
index 484d96f..a3f1609 100644
--- a/src/cmd/changelog-webapp/static/templates/findBuild.html
+++ b/src/cmd/changelog-webapp/static/templates/findBuild.html
@@ -14,7 +14,7 @@
     <a href="/">Home</a>
     <a href="/changelog/">Changelog</a>
     <a class="active" href="/findbuild/">Find Build</a>
-    <a href="/findreleasedbuild/">Find Released Build</a>
+    <a href="/findreleasedbuildv2/">Find Released Build</a>
     <a href="/readme/">Readme</a>
   </div>
   <div class="main">
@@ -76,4 +76,4 @@
     </div>
   </div>
 </body>
-</html>
\ No newline at end of file
+</html>
diff --git a/src/cmd/changelog-webapp/static/templates/findReleasedBuild.html b/src/cmd/changelog-webapp/static/templates/findReleasedBuild.html
index d215ae6..3b88a41 100644
--- a/src/cmd/changelog-webapp/static/templates/findReleasedBuild.html
+++ b/src/cmd/changelog-webapp/static/templates/findReleasedBuild.html
@@ -8,13 +8,13 @@
 <body>
   <div class="navbar">
     <p class="navbar-title">Container Optimized OS</p>
-    <a class="signout" href="/signout/?redirect=/findreleasedbuild/">Sign Out</a>
+    <a class="signout" href="/signout/?redirect=/findreleasedbuildv2/">Sign Out</a>
   </div>
   <div class="sidenav">
     <a href="/">Home</a>
     <a href="/changelog/">Changelog</a>
     <a href="/findbuild/">Find Build</a>
-    <a class="active" href="/findreleasedbuild/">Find Released Build</a>
+    <a class="active" href="/findreleasedbuildv2/">Find Released Build</a>
     <a href="/readme/">Readme</a>
   </div>
   <div class="main">
@@ -32,7 +32,7 @@
       this tool. Commits directly submitted to a Git repository are not
       supported.
     </p>
-    <form class="changelog-form" action="/findreleasedbuild">
+    <form class="changelog-form" action="/findreleasedbuildv2">
       <div class="text">
         {{if (ne .CL "")}}
           <input type="text" class="cl-input" name="cl" placeholder="CL-Number" value={{.CL}} required>
@@ -73,4 +73,4 @@
     </div>
   </div>
 </body>
-</html>
\ No newline at end of file
+</html>
diff --git a/src/cmd/changelog-webapp/static/templates/index.html b/src/cmd/changelog-webapp/static/templates/index.html
index c18c461..30761eb 100644
--- a/src/cmd/changelog-webapp/static/templates/index.html
+++ b/src/cmd/changelog-webapp/static/templates/index.html
@@ -17,7 +17,7 @@
     <a class="active" href="/">Home</a>
     <a href="/changelog/">Changelog</a>
     <a href="/findbuild/">Find Build</a>
-    <a href="/findreleasedbuild/">Find Released Build</a>
+    <a href="/findreleasedbuildv2/">Find Released Build</a>
     <a href="/readme/">Readme</a>
   </div>
   <div class="main">
@@ -34,4 +34,4 @@
     </div>
   </div>
 </body>
-</html>
\ No newline at end of file
+</html>
diff --git a/src/cmd/changelog-webapp/static/templates/promptLogin.html b/src/cmd/changelog-webapp/static/templates/promptLogin.html
index 66bff89..05937a9 100644
--- a/src/cmd/changelog-webapp/static/templates/promptLogin.html
+++ b/src/cmd/changelog-webapp/static/templates/promptLogin.html
@@ -21,10 +21,10 @@
     {{else}}
       <a href="/findbuild/">Find Build</a>
     {{end}}
-    {{if (eq .ActivePage "/findreleasedbuild/")}}
-      <a class="active" href="/findreleasedbuild/">Find Released Build</a>
+    {{if (eq .ActivePage "/findreleasedbuildv2/")}}
+      <a class="active" href="/findreleasedbuildv2/">Find Released Build</a>
     {{else}}
-      <a href="/findreleasedbuild/">Find Released Build</a>
+      <a href="/findreleasedbuildv2/">Find Released Build</a>
     {{end}}
     <a href="/readme/">Readme</a>
   </div>
@@ -39,4 +39,4 @@
     </div>
   </div>
 </body>
-</html>
\ No newline at end of file
+</html>
diff --git a/src/cmd/changelog-webapp/static/templates/readme.html b/src/cmd/changelog-webapp/static/templates/readme.html
index 0f6ae80..dd4deab 100644
--- a/src/cmd/changelog-webapp/static/templates/readme.html
+++ b/src/cmd/changelog-webapp/static/templates/readme.html
@@ -18,7 +18,7 @@
     <a href="/">Home</a>
     <a href="/changelog/">Changelog</a>
     <a href="/findbuild/">Find Build</a>
-    <a href="/findreleasedbuild/">Find Released Build</a>
+    <a href="/findreleasedbuildv2/">Find Released Build</a>
     <a class="active" href="/readme/?redirect=/readme/">Readme</a>
   </div>
   <div class="main">
@@ -92,4 +92,4 @@
     </div>
   </div>
 </body>
-</html>
\ No newline at end of file
+</html>