-
-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add new release check
- Loading branch information
Showing
10 changed files
with
267 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package update | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
// ReleaseInfo stores information about a release | ||
// nolint:tagliatelle | ||
type ReleaseInfo struct { | ||
Version string `json:"tag_name"` | ||
URL string `json:"html_url"` | ||
PublishedAt time.Time `json:"published_at"` | ||
} | ||
|
||
// CheckForUpdate checks whether this software has had a newer release on GitHub | ||
func CheckForUpdate(repo, currentVersion string) (*ReleaseInfo, error) { | ||
releaseInfo, err := getLatestReleaseInfo(repo) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if releaseInfo.Version != currentVersion { | ||
return releaseInfo, nil | ||
} | ||
return nil, nil | ||
} | ||
|
||
func getLatestReleaseInfo(repo string) (*ReleaseInfo, error) { | ||
var latestRelease ReleaseInfo | ||
resp, err := http.Get(fmt.Sprintf("https://api.github.com/repos/%s/releases/latest", repo)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if resp.StatusCode != 200 { | ||
return nil, fmt.Errorf("unexpected response status code: %v", resp.StatusCode) | ||
} | ||
|
||
defer resp.Body.Close() | ||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = json.Unmarshal(body, &latestRelease) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &latestRelease, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package update | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func Test_getLatestReleaseInfo(t *testing.T) { | ||
type args struct { | ||
repo string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want *ReleaseInfo | ||
wantErr bool | ||
}{ | ||
// TODO: Add test cases. | ||
{ | ||
name: "test lastest release info", | ||
args: args{ | ||
repo: "sunny0826/kubectl-pod-lens", | ||
}, | ||
want: &ReleaseInfo{ | ||
Version: "v0.2.2", | ||
URL: "https://github.com/sunny0826/kubectl-pod-lens/releases/tag/v0.2.2", | ||
PublishedAt: time.Date(2021, 8, 29, 10, 19, 42, 0, time.UTC), | ||
}, | ||
}, | ||
{ | ||
name: "repo not found", | ||
args: args{ | ||
repo: "bar/foo", | ||
}, | ||
want: nil, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := getLatestReleaseInfo(tt.args.repo) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("getLatestReleaseInfo() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("getLatestReleaseInfo() got = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestCheckForUpdate(t *testing.T) { | ||
type args struct { | ||
repo string | ||
currentVersion string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want *ReleaseInfo | ||
wantErr bool | ||
}{ | ||
// TODO: Add test cases. | ||
{ | ||
name: "need update", | ||
args: args{ | ||
repo: "sunny0826/kubectl-pod-lens", | ||
currentVersion: "v0.2.1", | ||
}, | ||
want: &ReleaseInfo{ | ||
Version: "v0.2.2", | ||
URL: "https://github.com/sunny0826/kubectl-pod-lens/releases/tag/v0.2.2", | ||
PublishedAt: time.Date(2021, 8, 29, 10, 19, 42, 0, time.UTC), | ||
}, | ||
}, | ||
{ | ||
name: "do not need update", | ||
args: args{ | ||
repo: "sunny0826/kubectl-pod-lens", | ||
currentVersion: "v0.2.2", | ||
}, | ||
want: nil, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := CheckForUpdate(tt.args.repo, tt.args.currentVersion) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("CheckForUpdate() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("CheckForUpdate() got = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.