func FetchReleases()

in reference-api/sources/github/releases.go [27:67]


func FetchReleases(repo, gitRef string) (*StandardizedOutput, error) {
	owner, repoName, _ := strings.Cut(repo, "/")
	client := NewGithubClient(repo)
	ctx := context.Background()

	releases, resp, err := client.Repositories.ListReleases(ctx, owner, repoName, nil)

	if err != nil {
		if resp != nil && resp.StatusCode == http.StatusNotFound {
			return nil, fmt.Errorf("404 Not Found: No releases found for repo %s", repo)
		}
		return nil, fmt.Errorf("GitHub API error: %v", err)
	}

	var latestRelease *github.RepositoryRelease
	var matchingRelease *github.RepositoryRelease

	// Iterate through releases to find the latest and the matching release
	for _, release := range releases {
		release := release
		// Identify the latest release (first in the list, assuming GitHub returns them in descending order)
		if latestRelease == nil {
			latestRelease = release
		}

		// Identify the release matching the gitRef
		if *release.TagName == gitRef {
			matchingRelease = release
		}

		// If both latest and matching releases are found, exit the loop early
		if latestRelease != nil && matchingRelease != nil {
			break
		}
	}

	return &StandardizedOutput{
		Latest:  StandardizeRelease(latestRelease),
		Current: StandardizeRelease(matchingRelease),
	}, nil
}