<commit_before>package resource

import (
	"sort"

	"github.com/Masterminds/semver"
	"github.com/cppforlife/go-semi-semantic/version"
	"github.com/google/go-github/v32/github"
)

type CheckCommand struct {
	github GitHub
}

func NewCheckCommand(github GitHub) *CheckCommand {
	return &CheckCommand{
		github: github,
	}
}

func SortByVersion(releases []*github.RepositoryRelease, versionParser *versionParser) {
	sort.Slice(releases, func(i, j int) bool {
		first, err := version.NewVersionFromString(versionParser.parse(*releases[i].TagName))
		if err != nil {
			return true
		}

		second, err := version.NewVersionFromString(versionParser.parse(*releases[j].TagName))
		if err != nil {
			return false
		}

		return first.IsLt(second)
	})
}

func SortByTimestamp(releases []*github.RepositoryRelease) {
	sort.Slice(releases, func(i, j int) bool {
		a := releases[i]
		b := releases[j]
		return getTimestamp(a).Before(getTimestamp(b))
	})
}

func (c *CheckCommand) Run(request CheckRequest) ([]Version, error) {
	releases, err := c.github.ListReleases()
	if err != nil {
		return []Version{}, err
	}

	if len(releases) == 0 {
		return []Version{}, nil
	}

	orderByTime := false
	if request.Source.OrderBy == "time" {
		orderByTime = true
	}

	var filteredReleases []*github.RepositoryRelease

	versionParser, err := newVersionParser(request.Source.TagFilter)
	if err != nil {
		return []Version{}, err
	}

	for _, release := range releases {

		if request.Source.Drafts != *release.Draft {
			continue
		}

		// Should we skip this release
		//   a- prerelease condition dont match our source config
		//   b- release condition match  prerealse in github since github has true/false to describe release/prerelase
		if request.Source.PreRelease != *release.Prerelease && request.Source.Release == *release.Prerelease {
			continue
		}

		if request.Source.SemverConstraint != "" {
			constraint, err := semver.NewConstraint(request.Source.SemverConstraint)
			if err != nil {
				return []Version{}, err
			}

			if release.TagName == nil {
				// Release has no tag, so certainly isn't a valid semver
				continue
			}<commit_msg>move constraint construction out of loop<commit_after>package resource

import (
	"sort"

	"github.com/Masterminds/semver"
	"github.com/cppforlife/go-semi-semantic/version"
	"github.com/google/go-github/v32/github"
)

type CheckCommand struct {
	github GitHub
}

func NewCheckCommand(github GitHub) *CheckCommand {
	return &CheckCommand{
		github: github,
	}
}

func SortByVersion(releases []*github.RepositoryRelease, versionParser *versionParser) {
	sort.Slice(releases, func(i, j int) bool {
		first, err := version.NewVersionFromString(versionParser.parse(*releases[i].TagName))
		if err != nil {
			return true
		}

		second, err := version.NewVersionFromString(versionParser.parse(*releases[j].TagName))
		if err != nil {
			return false
		}

		return first.IsLt(second)
	})
}

func SortByTimestamp(releases []*github.RepositoryRelease) {
	sort.Slice(releases, func(i, j int) bool {
		a := releases[i]
		b := releases[j]
		return getTimestamp(a).Before(getTimestamp(b))
	})
}

func (c *CheckCommand) Run(request CheckRequest) ([]Version, error) {
	releases, err := c.github.ListReleases()
	if err != nil {
		return []Version{}, err
	}

	if len(releases) == 0 {
		return []Version{}, nil
	}

	orderByTime := false
	if request.Source.OrderBy == "time" {
		orderByTime = true
	}

	var filteredReleases []*github.RepositoryRelease

	versionParser, err := newVersionParser(request.Source.TagFilter)
	if err != nil {
		return []Version{}, err
	}

	var constraint *semver.Constraints
	if request.Source.SemverConstraint != "" {
		constraint, err = semver.NewConstraint(request.Source.SemverConstraint)
		if err != nil {
			return []Version{}, err
		}
	}

	for _, release := range releases {
		if request.Source.Drafts != *release.Draft {
			continue
		}

		// Should we skip this release
		//   a- prerelease condition dont match our source config
		//   b- release condition match  prerealse in github since github has true/false to describe release/prerelase
		if request.Source.PreRelease != *release.Prerelease && request.Source.Release == *release.Prerelease {
			continue
		}

		if constraint != nil {
			if release.TagName == nil {
				// Release has no tag, so certainly isn't a valid semver
				continue
			}
{'fraction_non_alphanumeric': 0.12442502299908004, 'fraction_numerical': 0.0013799448022079118, 'mean_word_length': 3.9988505747126437, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '181015', 'n_tokens_mistral': 1540, 'n_tokens_neox': 1359, 'n_words': 349}