Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: copilot-extensions/skillset-example
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: main
Choose a base ref
...
head repository: brentlaster/gover-skillset
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: main
Choose a head ref
Able to merge. These branches can be automatically merged.
  • 9 commits
  • 6 files changed
  • 1 contributor

Commits on Jan 13, 2025

  1. Copy the full SHA
    c4aaacf View commit details
  2. Copy the full SHA
    b2781e4 View commit details
  3. Copy the full SHA
    b8aed6b View commit details
  4. Update main.go

    brentlaster authored Jan 13, 2025
    Copy the full SHA
    1003e87 View commit details
  5. Copy the full SHA
    c564d94 View commit details
  6. Copy the full SHA
    05d06a9 View commit details
  7. Add supported_versions

    brentlaster committed Jan 13, 2025
    Copy the full SHA
    16cbbe5 View commit details

Commits on Jan 14, 2025

  1. updating for new handlers

    brentlaster committed Jan 14, 2025
    Copy the full SHA
    bb39f64 View commit details
  2. Copy the full SHA
    30edbc3 View commit details
Showing with 255 additions and 152 deletions.
  1. +0 −33 handlers/commit_message.go
  2. +102 −0 handlers/is_supported_or_eol.go
  3. +62 −0 handlers/latest_version.go
  4. +0 −116 handlers/loripsum.go
  5. +88 −0 handlers/supported_versions.go
  6. +3 −3 main.go
33 changes: 0 additions & 33 deletions handlers/commit_message.go

This file was deleted.

102 changes: 102 additions & 0 deletions handlers/is_supported_or_eol.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package handlers

import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"regexp"
"strings"
)

type ReleaseInfo struct {
ReleaseDate string `json:"releaseDate"`
EOL interface{} `json:"eol"` // Can be a string or false
Latest string `json:"latest"`
LatestReleaseDate string `json:"latestReleaseDate"`
LTS bool `json:"lts"`
}

func IsSupportedOrEOL(w http.ResponseWriter, r *http.Request) {
fmt.Println("EOL check called")

params := &struct {
GoVersionOrRelease string `json:"go_version_or_release"`
}{}

err := json.NewDecoder(r.Body).Decode(&params)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}

// Regular expression to match only numbers and periods
validPattern := regexp.MustCompile(`^\d+(\.\d+)*$`)

var url_path string
// Check if GoVersionOrRelease is not empty and matches the pattern
if params.GoVersionOrRelease != "" && validPattern.MatchString(params.GoVersionOrRelease) {
url_path = fmt.Sprintf("https://endoflife.date/api/go/%s.json", params.GoVersionOrRelease)
} else {
http.Error(w, "Invalid version specified", http.StatusNotFound)
}

req, err := http.NewRequestWithContext(r.Context(), http.MethodGet, url_path, nil)
if err != nil {
http.Error(w, "Failed to create request", http.StatusInternalServerError)
return
}

resp, err := (&http.Client{}).Do(req)
if err != nil {
fmt.Println(err)
http.Error(w, "Failed to fetch data", http.StatusInternalServerError)
return
}
defer resp.Body.Close()

if resp.StatusCode == http.StatusNotFound {
fmt.Fprintf(w, "No versions found matching %s.\n", params.GoVersionOrRelease)
return
}

if resp.StatusCode != http.StatusOK {
http.Error(w, fmt.Sprintf("Unexpected status code: %d", resp.StatusCode), http.StatusInternalServerError)
return
}

// Buffer the response body for decoding
var bodyBuffer bytes.Buffer
tee := io.TeeReader(resp.Body, &bodyBuffer)

// Drain the tee to ensure we can parse the body later
if _, err := io.Copy(io.Discard, tee); err != nil {
http.Error(w, "Failed to process response body", http.StatusInternalServerError)
return
}

// Create a Decoder
decoder := json.NewDecoder(strings.NewReader(bodyBuffer.String()))

// Decode JSON into the struct
var releaseInfo ReleaseInfo
if err := decoder.Decode(&releaseInfo); err != nil {
fmt.Println("Error decoding JSON:", err)
return
}

// Handle the "eol" field based on its type
if eolDate, ok := releaseInfo.EOL.(string); ok {
fmt.Fprintf(w, "Version %s is EOL as of %s. The latest release was %s on %s.\n",
params.GoVersionOrRelease, eolDate, releaseInfo.Latest, releaseInfo.LatestReleaseDate)
return
} else if eolBool, ok := releaseInfo.EOL.(bool); ok && !eolBool {
fmt.Fprintf(w, "Version %s is not EOL.\n", params.GoVersionOrRelease)
return
} else {
fmt.Println("EOL: unknown type")
}

http.Error(w, "An unexpected error occurred", http.StatusInternalServerError)
}
62 changes: 62 additions & 0 deletions handlers/latest_version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package handlers

import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
)

type GoVersion struct {
Version string `json:"version"`
}

func LatestVersionGo(w http.ResponseWriter, r *http.Request) {
fmt.Println("Latest Go Version Called")
req, err := http.NewRequestWithContext(r.Context(), http.MethodGet, "https://go.dev/dl/?mode=json", nil)
if err != nil {
http.Error(w, "Failed to create request", http.StatusInternalServerError)
return
}

resp, err := (&http.Client{}).Do(req)
if err != nil {
http.Error(w, "Failed to fetch data", http.StatusInternalServerError)
return
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
http.Error(w, fmt.Sprintf("Unexpected status code: %d", resp.StatusCode), http.StatusInternalServerError)
return
}

// Buffer the response body for decoding
var bodyBuffer bytes.Buffer
tee := io.TeeReader(resp.Body, &bodyBuffer)

// Drain the tee to ensure we can parse the body later
if _, err := io.Copy(io.Discard, tee); err != nil {
http.Error(w, "Failed to process response body", http.StatusInternalServerError)
return
}

// Parse the JSON response
var versions []GoVersion
if err := json.NewDecoder(&bodyBuffer).Decode(&versions); err != nil {
http.Error(w, "Failed to parse JSON response", http.StatusInternalServerError)
fmt.Printf("Failed to decode JSON: %v\n", err)
return
}

// The latest version is the first item in the array
if len(versions) > 0 {
trimmedVersion := strings.TrimPrefix(versions[0].Version, "go")
fmt.Fprintf(w, "%s", trimmedVersion) // Write the version to the HTTP response
return
}

http.Error(w, "No versions found", http.StatusNotFound)
}
116 changes: 0 additions & 116 deletions handlers/loripsum.go

This file was deleted.

88 changes: 88 additions & 0 deletions handlers/supported_versions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package handlers

import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
)

type VersionInfo struct {
Cycle string `json:"cycle"`
ReleaseDate string `json:"releaseDate"`
EOL interface{} `json:"eol"`
}

func SupportedVersionsGo(w http.ResponseWriter, r *http.Request) {
fmt.Println("Supported Go Versions Called")
req, err := http.NewRequestWithContext(r.Context(), http.MethodGet, "https://endoflife.date/api/go.json", nil)
if err != nil {
http.Error(w, "Failed to create request", http.StatusInternalServerError)
return
}

resp, err := (&http.Client{}).Do(req)
if err != nil {
http.Error(w, "Failed to fetch data", http.StatusInternalServerError)
return
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
http.Error(w, fmt.Sprintf("Unexpected status code: %d", resp.StatusCode), http.StatusInternalServerError)
return
}

// Buffer the response body for decoding
var bodyBuffer bytes.Buffer
tee := io.TeeReader(resp.Body, &bodyBuffer)

// Drain the tee to ensure we can parse the body later
if _, err := io.Copy(io.Discard, tee); err != nil {
http.Error(w, "Failed to process response body", http.StatusInternalServerError)
return
}

// Parse the JSON into a slice of VersionInfo
var versions []VersionInfo
if err := json.NewDecoder(&bodyBuffer).Decode(&versions); err != nil {
http.Error(w, "Failed to parse JSON response", http.StatusInternalServerError)
fmt.Printf("Failed to decode JSON: %v\n", err)
return
}

// Filter versions with eol == false and collect the results
result := make([]struct {
Cycle string `json:"cycle"`
ReleaseDate string `json:"releaseDate"`
}, 0)

for _, v := range versions {
// Check if EOL is a boolean and is false
if eolBool, ok := v.EOL.(bool); ok && !eolBool {
result = append(result, struct {
Cycle string `json:"cycle"`
ReleaseDate string `json:"releaseDate"`
}{
Cycle: v.Cycle,
ReleaseDate: v.ReleaseDate,
})
}
}

// Convert the result to a single string
var builder strings.Builder
for _, r := range result {
builder.WriteString(fmt.Sprintf("Version: %s, Release Date: %s\n", r.Cycle, r.ReleaseDate))
}

// The latest version is the first item in the array
if len(builder.String()) > 0 {
fmt.Fprintf(w, "%s", builder.String()) // Write the list of supported versions to the HTTP response
return
}

http.Error(w, "No versions found", http.StatusNotFound)
}
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -16,9 +16,9 @@ func main() {
}

func run() error {
http.HandleFunc("/random-commit-message", handlers.CommitMessage)
http.HandleFunc("/random-lorem-ipsum", handlers.Loripsum)
http.HandleFunc("/random-user", handlers.User)
http.HandleFunc("/latest-version-go", handlers.LatestVersionGo)
http.HandleFunc("/supported-versions-go", handlers.SupportedVersionsGo)
http.HandleFunc("/is-supported-or-eol", handlers.IsSupportedOrEOL)
http.HandleFunc("/_ping", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK"))
})