Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add latest_by_creation_time option to the github-release-dependency #744

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 36 additions & 14 deletions actions/github-release-dependency/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"net/http"
"os"
"regexp"
"sort"
"strconv"

"github.com/google/go-github/v43/github"
"golang.org/x/oauth2"
Expand All @@ -42,10 +44,16 @@ func main() {
panic(fmt.Errorf("repository must be specified"))
}

var (
err error
g *regexp.Regexp
)
var err error
useCreationTime := false
if b, ok := inputs["latest_by_creation_time"]; ok {
useCreationTime, err = strconv.ParseBool(b)
if err != nil {
panic(fmt.Errorf("unable to parse %s as a bool\n%w", b, err))
}
}

var g *regexp.Regexp
if s, ok := inputs["glob"]; ok {
g, err = regexp.Compile(s)
if err != nil {
Expand Down Expand Up @@ -73,6 +81,7 @@ func main() {
panic(fmt.Errorf("%s is not a valid regex", t))
}

var releases []*github.RepositoryRelease
opt := &github.ListOptions{PerPage: 100}
for {
rel, rsp, err := gh.Repositories.ListReleases(context.Background(), o, r, opt)
Expand All @@ -86,17 +95,13 @@ func main() {
}

if p := re.FindStringSubmatch(*r.TagName); p != nil {
for _, a := range r.Assets {
if g.MatchString(*a.Name) {
n, err := actions.NormalizeVersion(p[1])
if err != nil {
panic(err)
}

versions[n] = *a.BrowserDownloadURL
break
}
n, err := actions.NormalizeVersion(p[1])
if err != nil {
panic(err)
}
r.TagName = github.String(n)

releases = append(releases, r)
}
}

Expand All @@ -106,6 +111,23 @@ func main() {
opt.Page = rsp.NextPage
}

if useCreationTime {
sort.Slice(releases, func(i, j int) bool {
return releases[i].CreatedAt.Before(releases[j].CreatedAt.Time)
})

releases = releases[len(releases)-1:]
}

for _, r := range releases {
for _, a := range r.Assets {
if g.MatchString(*a.Name) {
versions[*r.TagName] = *a.BrowserDownloadURL
break
}
}
}

if o, err := versions.GetLatest(inputs); err != nil {
panic(err)
} else {
Expand Down