Avoid upgrade notice for recent release if gh is under Homebrew prefix

Before, when gh detected there was a new release in the `cli/cli` repo,
it would show this notice:

    A new release of gh is available: {V1} → {V2}

Additionally, when the release was more than 24h old, we would show this
to Homebrew users:

    To upgrade, run: brew update && brew upgrade gh

Ref. feb4acc2c0

This change makes it so that the original notice "A new release of gh is
available" is NOT shown to Homebrew users unless the release is older
than 24h. We effectively hide the fact that any release happened until
we're sure that the version bump has made it to `homebrew-core`.
This commit is contained in:
Mislav Marohnić 2021-02-23 12:10:56 +01:00
parent d6798b1852
commit 27aea42d8a

View file

@ -163,12 +163,19 @@ func main() {
newRelease := <-updateMessageChan
if newRelease != nil {
ghExe, _ := os.Executable()
isHomebrew := false
if ghExe, err := os.Executable(); err == nil {
isHomebrew = isUnderHomebrew(ghExe)
}
if isHomebrew && isRecentRelease(newRelease.PublishedAt) {
// do not notify Homebrew users before the version bump had a chance to get merged into homebrew-core
return
}
fmt.Fprintf(stderr, "\n\n%s %s → %s\n",
ansi.Color("A new release of gh is available:", "yellow"),
ansi.Color(buildVersion, "cyan"),
ansi.Color(newRelease.Version, "cyan"))
if suggestBrewUpgrade(newRelease, ghExe) {
if isHomebrew {
fmt.Fprintf(stderr, "To upgrade, run: %s\n", "brew update && brew upgrade gh")
}
fmt.Fprintf(stderr, "%s\n\n",
@ -265,13 +272,12 @@ func apiVerboseLog() api.ClientOption {
return api.VerboseLog(colorable.NewColorable(os.Stderr), logTraffic, colorize)
}
// Suggest to `brew upgrade gh` only if gh was found under homebrew prefix and when the release was
// published over 24h ago, allowing homebrew-core ample time to merge the formula bump.
func suggestBrewUpgrade(rel *update.ReleaseInfo, ghBinary string) bool {
if rel.PublishedAt.IsZero() || time.Since(rel.PublishedAt) < time.Duration(time.Hour*24) {
return false
}
func isRecentRelease(publishedAt time.Time) bool {
return !publishedAt.IsZero() && time.Since(publishedAt) < time.Hour*24
}
// Check whether the gh binary was found under the Homebrew prefix
func isUnderHomebrew(ghBinary string) bool {
brewExe, err := safeexec.LookPath("brew")
if err != nil {
return false