Skip to content

Commit

Permalink
add option to download the changelog content
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderMac committed Dec 4, 2023
1 parent eeed706 commit a8d917b
Showing 1 changed file with 46 additions and 9 deletions.
55 changes: 46 additions & 9 deletions gosu.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ const (
)

type Updater struct {
issuesUrl string
localVersion string
GhAccessToken string
ReleasesUrl string
ChangelogUrl string
LocalVersion string
GhAccessToken string
DownloadChangelog bool
}

type _CheckUpdatesResult struct {
Expand All @@ -58,10 +60,16 @@ type _GhRelease struct {
Body string `json:"body"`
}

type _GhContent struct {
Encoding string `json:"encoding"`
Content string `json:"content"`
}

func New(orgRepoName, ghAccessToken, localVersion string) *Updater {
return &Updater{
issuesUrl: fmt.Sprintf("https://api.github.com/repos/%s/releases/latest", orgRepoName),
localVersion: localVersion,
ReleasesUrl: fmt.Sprintf("https://api.github.com/repos/%s/releases/latest", orgRepoName),
ChangelogUrl: fmt.Sprintf("https://api.github.com/repos/%s/contents/CHANGELOG.md", orgRepoName),
LocalVersion: localVersion,
GhAccessToken: ghAccessToken,
}
}
Expand All @@ -78,7 +86,7 @@ func (updater *Updater) CheckUpdates() (_CheckUpdatesResult, error) {
}

remoteSemver := updater.parseSemVer(lastRelease.TagName)
localSemver := updater.parseSemVer(updater.localVersion)
localSemver := updater.parseSemVer(updater.LocalVersion)

if remoteSemver == nil || localSemver == nil {
return _CheckUpdatesResult{
Expand Down Expand Up @@ -106,15 +114,26 @@ func (updater *Updater) CheckUpdates() (_CheckUpdatesResult, error) {
}

// new version detected
lastReleaseDetails := lastRelease.Body
if updater.DownloadChangelog {
changelog, err := updater.getChangelog()
if err != nil {
logger.Warnf("Unable to download changelog, error: %s", err.Error())
}
if changelog != "" {
lastReleaseDetails = changelog
}
}

logger.Infof("New version detected %s", lastRelease.TagName)
return _CheckUpdatesResult{
Code: CODE_UPGRADE_CONFIRMATION,
Message: fmt.Sprintf(
"Upgrade to a new version? Current version is %s, new version is %s.",
updater.localVersion,
updater.LocalVersion,
lastRelease.TagName,
),
Details: lastRelease.Body,
Details: lastReleaseDetails,
}, nil
}

Expand Down Expand Up @@ -169,7 +188,7 @@ func (updater *Updater) getLastRelease() (_GhRelease, error) {
res, err := updater.getHttpClient().
R().
SetResult(&ghRelease).
Get(updater.issuesUrl)
Get(updater.ReleasesUrl)
if err != nil {
return _GhRelease{}, err
}
Expand All @@ -181,6 +200,24 @@ func (updater *Updater) getLastRelease() (_GhRelease, error) {
return ghRelease, nil
}

func (updater *Updater) getChangelog() (string, error) {
logger.Info("Getting the changelog")

res, err := updater.getHttpClient().
R().
SetHeader("Accept", "application/vnd.github.raw").
Get(updater.ChangelogUrl)
if err != nil {
return "", err
}
if err := updater.checkHttpResponse(res); err != nil {
return "", err
}

logger.Info("Got the changelog successfully")
return string(res.Body()), nil
}

func (updater *Updater) downloadAsset(asset _GhReleaseAsset) error {
logger.Infof("Downloading the asset %s from %s", asset.Name, asset.Url)

Expand Down

0 comments on commit a8d917b

Please sign in to comment.