Skip to content

Commit

Permalink
add explicit support to semver
Browse files Browse the repository at this point in the history
Signed-off-by: Matias Pan <[email protected]>
  • Loading branch information
matipan committed Mar 7, 2024
1 parent af9cd6d commit abe237f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ require (
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/sosodev/duration v1.2.0 // indirect
golang.org/x/mod v0.16.0 // indirect
golang.org/x/sys v0.18.0 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ github.com/vektah/gqlparser/v2 v2.5.11 h1:JJxLtXIoN7+3x6MBdtIP59TP1RANnY7pXOaDnA
github.com/vektah/gqlparser/v2 v2.5.11/go.mod h1:1rCcfwB2ekJofmluGWXMSEnPMZgbxzwj6FaZ/4OT8Cc=
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 h1:LfspQV/FYTatPTr/3HzIcmiUFH7PGP+OQ6mgDYo3yuQ=
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225/go.mod h1:CxmFvTBINI24O/j8iY7H1xHzx2i4OsyguNBmN/uPtqc=
golang.org/x/mod v0.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic=
golang.org/x/mod v0.16.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
29 changes: 25 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package main

import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"strings"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/rs/zerolog/pkgerrors"
"golang.org/x/mod/semver"
)

var (
Expand Down Expand Up @@ -51,12 +55,13 @@ func main() {
}

if githubPat == "" {
l.Info().Msg("GITHUB_PATH is not set, private repositories will not be accessible")
l.Info().Msg("GITHUB_PAT is not set. This means that private repositories will not be accessible and there is a maximum of 60 requests per hour before being rate limited by Github")
}

mux := http.NewServeMux()

mux.HandleFunc("/api/v1/getparams.execute", func(w http.ResponseWriter, r *http.Request) {
ctx := l.WithContext(r.Context())
if r.Method != "POST" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
Expand All @@ -76,8 +81,13 @@ func main() {
return
}

if !semver.IsValid(req.Input.Parameters.MinRelease) {
l.Error().Msgf("invalid semver: %s", req.Input.Parameters.MinRelease)
http.Error(w, "invalid semver. Check https://pkg.go.dev/golang.org/x/mod/semver for details on which are valid versions", http.StatusBadRequest)
}

l.Debug().Msgf("fetching releases for %s", req.Input.Parameters.Repository)
releases, err := getReleases(req.Input.Parameters.Repository)
releases, err := getReleases(ctx, req.Input.Parameters.Repository)
if err != nil {
l.Error().Err(err).Msg("failed to fetch releases")
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand Down Expand Up @@ -118,26 +128,37 @@ type Commit struct {
func getFilteredReleases(releases []Release, minRelease string) []Release {
var filteredReleases []Release
for _, r := range releases {
if r.Name > minRelease {
if semver.Compare(r.Name, minRelease) > 0 {
filteredReleases = append(filteredReleases, r)
}
}

return filteredReleases
}

func getReleases(repo string) ([]Release, error) {
func getReleases(ctx context.Context, repo string) ([]Release, error) {
l := log.Ctx(ctx)

rr, err := http.NewRequest(http.MethodGet, "https://api.github.com/repos/"+repo+"/tags", nil)
if err != nil {
return nil, err
}

if githubPat != "" {
rr.Header.Set("Authorization", "Bearer "+githubPat)
}

res, err := http.DefaultClient.Do(rr)
if err != nil {
return nil, err
}
defer res.Body.Close()

if res.StatusCode != http.StatusOK {
l.Error().Int("github_status_code", res.StatusCode).Msg("failed to fetch releases")
return nil, fmt.Errorf("failed to fetch releases, github responded with: %d", res.StatusCode)
}

var releases []Release
if err := json.NewDecoder(res.Body).Decode(&releases); err != nil {
return nil, err
Expand Down

0 comments on commit abe237f

Please sign in to comment.