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 update check #41

Merged
merged 1 commit into from
May 8, 2022
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion .github/workflows/changelog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ jobs:
commit-message: "automated changelog generation"
body: "This PR contains an automatically generated changelog."
labels:
"maintenance"
"maintenance"
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,6 @@ skip_entries_without_label: false
# that have been added since the last tag.
# Note: The unreleased section is not created when the --next-version flag is used.
show_unreleased: true
# If set to false, the tool will not check remotely for updates
check_for_updates: true
```
7 changes: 7 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"os"

"github.com/chelnak/gh-changelog/internal/pkg/configuration"
"github.com/chelnak/gh-changelog/internal/pkg/utils"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var version = "dev"
Expand All @@ -21,6 +23,11 @@ var rootCmd = &cobra.Command{
SilenceUsage: true,
SilenceErrors: true,
Run: nil,
PersistentPostRun: func(cmd *cobra.Command, args []string) {
if viper.GetBool("check_for_updates") {
utils.CheckForUpdate(version)
}
},
}

func init() {
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ require (
github.com/charmbracelet/glamour v0.5.0
github.com/charmbracelet/lipgloss v0.5.0
github.com/cli/go-gh v0.0.3
github.com/fatih/color v1.13.0
github.com/jarcoal/httpmock v1.1.0
github.com/shurcooL/githubv4 v0.0.0-20220115235240-a14260e6f8a2
github.com/spf13/cobra v1.4.0
github.com/spf13/viper v1.11.0
github.com/stretchr/testify v1.7.1
gopkg.in/h2non/gock.v1 v1.1.2
)

require (
Expand All @@ -25,9 +27,9 @@ require (
github.com/containerd/console v1.0.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dlclark/regexp2 v1.4.0 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/henvic/httpretty v0.0.6 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5m
github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g=
github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY=
github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
Expand Down Expand Up @@ -210,6 +212,8 @@ github.com/muesli/termenv v0.9.0/go.mod h1:R/LzAKf+suGs4IsO95y7+7DpFHO0KABgnZqtl
github.com/muesli/termenv v0.11.1-0.20220204035834-5ac8409525e0/go.mod h1:Bd5NYQ7pd+SrtBSrSNoBBmXlcY8+Xj4BMJgh8qcZrvs=
github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739 h1:QANkGiGr39l1EESqrE0gZw0/AJNYzIvoGLhIoVYtluI=
github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739/go.mod h1:Bd5NYQ7pd+SrtBSrSNoBBmXlcY8+Xj4BMJgh8qcZrvs=
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 h1:W6apQkHrMkS0Muv8G/TipAy/FJl/rCYT0+EuS8+Z0z4=
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/pelletier/go-toml v1.9.4 h1:tjENF6MfZAg8e4ZmZTeWaWiT2vXtsoO6+iuOjFhECwM=
Expand Down Expand Up @@ -566,6 +570,8 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/h2non/gock.v1 v1.1.2 h1:jBbHXgGBK/AoPVfJh5x4r/WxIrElvbLel8TCZkkZJoY=
gopkg.in/h2non/gock.v1 v1.1.2/go.mod h1:n7UGz/ckNChHiK05rDoiC4MYSunEC/lyaUm2WWaDva0=
gopkg.in/ini.v1 v1.66.4 h1:SsAcf+mM7mRZo2nJNGt8mZCjG8ZRaNGMURJw7BsIST4=
gopkg.in/ini.v1 v1.66.4/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
2 changes: 2 additions & 0 deletions internal/pkg/configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,6 @@ func SetDefaults() {
viper.SetDefault("skip_entries_without_label", false)

viper.SetDefault("show_unreleased", true)

viper.SetDefault("check_for_updates", true)
}
76 changes: 76 additions & 0 deletions internal/pkg/utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package utils

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"

"github.com/Masterminds/semver/v3"
"github.com/fatih/color"
)

func SliceContainsString(s []string, str string) bool {
Expand All @@ -18,3 +25,72 @@ func IsValidSemanticVersion(version string) bool {
_, err := semver.NewVersion(version)
return err == nil
}

type Release struct {
Version string `json:"tag_name"`
}

func CheckForUpdate(currentVersion string) bool {
release, err := requestLatestRelease()
if err != nil {
return false
}

currentVersion = parseLocalVersion(currentVersion)

if versionIsGreaterThan(currentVersion, release.Version) {
color.Yellow("\nVersion %s is available ✨\n\n", release.Version)
fmt.Println("Run", color.GreenString(`gh extension upgrade chelnak/gh-changelog`), "to upgrade.")

fmt.Println("\nAlternatively, you can disable this check by setting", color.GreenString("check_for_updates"), "to", color.RedString("false"), "via the configuration.")
fmt.Println()
return true
}

return false
}

func requestLatestRelease() (Release, error) {
response, err := http.Get("https://api.github.com/repos/chelnak/gh-changelog/releases/latest")
if err != nil {
return Release{}, err
}

body, err := ioutil.ReadAll(response.Body)
if err != nil {
return Release{}, err
}

var release Release
err = json.Unmarshal(body, &release)
if err != nil {
return Release{}, err
}

return release, nil
}

func versionIsGreaterThan(currentVersion, nextVersion string) bool {
constraint, err := semver.NewConstraint(fmt.Sprintf("> %s", currentVersion))
if err != nil {
return false
}

version, err := semver.NewVersion(nextVersion)
if err != nil {
return false
}

return constraint.Check(version)
}

func parseLocalVersion(version string) string {
slice := strings.Split(version, " ")
fmt.Println(slice)

if len(slice) == 1 {
return version
}

return slice[2]
}
38 changes: 38 additions & 0 deletions internal/pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"testing"

"github.com/chelnak/gh-changelog/internal/pkg/utils"
"github.com/stretchr/testify/assert"
"gopkg.in/h2non/gock.v1"
)

func TestSliceContainsString(t *testing.T) {
Expand Down Expand Up @@ -62,3 +64,39 @@ func TestIsValidSemanticVersion(t *testing.T) {
})
}
}

func TestCheckForUpdates(t *testing.T) {
tests := []struct {
name string
currentVersion string
nextVersion string
want bool
}{
{
name: "an update is available",
currentVersion: "changelog version 1.0.0",
nextVersion: "v1.0.1",
want: true,
},
{
name: "no update is available",
currentVersion: "changelog version 1.0.0",
nextVersion: "1.0.0",
want: false,
},
}

defer gock.Off()

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gock.New("https://api.github.com").
Get("/repos/chelnak/gh-changelog/releases/latest").
Reply(200).
JSON(map[string]string{"tag_name": tt.nextVersion})

got := utils.CheckForUpdate(tt.currentVersion)
assert.Equal(t, tt.want, got)
})
}
}