Skip to content

Commit

Permalink
* switch to Releases Github API endpoint, add latest Release URL to UI
Browse files Browse the repository at this point in the history
  • Loading branch information
bnkai committed Dec 31, 2019
1 parent fc1cae6 commit 23e4b33
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 25 deletions.
1 change: 1 addition & 0 deletions graphql/documents/queries/misc.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,6 @@ query Version {
query LatestVersion {
latestversion {
shorthash
url
}
}
1 change: 1 addition & 0 deletions graphql/schema/types/version.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ type Version {

type ShortVersion {
shorthash: String!
url: String!
}
114 changes: 96 additions & 18 deletions pkg/api/check_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,24 @@ import (
"github.com/stashapp/stash/pkg/logger"
"io/ioutil"
"net/http"
"runtime"
"time"
)

//we use the github REST V3 API as no login is required
const apiURL string = "https://api.github.com/repos/stashapp/stash/tags"
const apiReleases string = "https://api.github.com/repos/stashapp/stash/releases"
const apiAcceptHeader string = "application/vnd.github.v3+json"

var stashReleases = func() map[string]string {
return map[string]string{
"windows/amd64": "stash-win.exe",
"linux/amd64": "stash-linux",
"darwin/amd64": "stash-osx",
"linux/arm": "stash-pi",
}
}

type githubTagResponse struct {
Name string
Zipball_url string
Expand All @@ -24,70 +35,137 @@ type githubTagResponse struct {
Node_id string
}

type githubReleasesResponse struct {
Url string
Assets_url string
Upload_url string
Html_url string
Id int64
Node_id string
Tag_name string
Target_commitish string
Name string
Draft bool
Author githubAuthor
Prerelease bool
Created_at string
Published_at string
Assets []githubAsset
Tarball_url string
Zipball_url string
Body string
}

type githubAuthor struct {
Login string
Id int64
Node_id string
Avatar_url string
Gravatar_id string
Url string
Html_url string
Followers_url string
Following_url string
Gists_url string
Starred_url string
Subscriptions_url string
Organizations_url string
Repos_url string
Events_url string
Received_events_url string
Type string
Site_admin bool
}

type githubAsset struct {
Url string
Id int64
Node_id string
Name string
Label string
Uploader githubAuthor
Content_type string
State string
Size int64
Download_count int64
Created_at string
Updated_at string
Browser_download_url string
}

//gets latest version (git commit hash) from github API
//the repo's tags are used to find the latest version
//of the "master" or "develop" branch
func GetLatestVersion(shortHash bool) (string, error) {
func GetLatestVersion(shortHash bool) (latestVersion string, latestRelease string, err error) {

platform := fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH)
wantedRelease := stashReleases()[platform]

branch, _, _ := GetVersion()
if branch == "" {
return "", fmt.Errorf("Stash doesn't have a version. Version check not supported.")
return "", "", fmt.Errorf("Stash doesn't have a version. Version check not supported.")
}

latestVersion := ""

client := &http.Client{
Timeout: 3 * time.Second,
}

req, _ := http.NewRequest("GET", apiURL, nil)
req, _ := http.NewRequest("GET", apiReleases, nil)

req.Header.Add("Accept", apiAcceptHeader) // gh api recommendation , send header with api version
response, err := client.Do(req)

input := make([]githubTagResponse, 0)
input := make([]githubReleasesResponse, 0)

if err != nil {
return "", fmt.Errorf("Github API request failed: %s", err)
return "", "", fmt.Errorf("Github API request failed: %s", err)
} else {

defer response.Body.Close()

data, err := ioutil.ReadAll(response.Body)
if err != nil {
return "", fmt.Errorf("Github API read response failed: %s", err)
return "", "", fmt.Errorf("Github API read response failed: %s", err)
} else {
err := json.Unmarshal(data, &input)
err = json.Unmarshal(data, &input)
if err != nil {
return "", fmt.Errorf("Unmarshalling Github API response failed: %s", err)
return "", "", fmt.Errorf("Unmarshalling Github API response failed: %s", err)
} else {

for _, ghApi := range input {
if ghApi.Name == branch {
if ghApi.Tag_name == branch {

if shortHash {
latestVersion = ghApi.Commit.Sha[0:7] //shorthash is first 7 digits of git commit hash
latestVersion = ghApi.Target_commitish[0:7] //shorthash is first 7 digits of git commit hash
} else {
latestVersion = ghApi.Commit.Sha
latestVersion = ghApi.Target_commitish
}

if wantedRelease != "" {
for _, asset := range ghApi.Assets {
if asset.Name == wantedRelease {
latestRelease = asset.Browser_download_url
break
}

}
}
break
}
}

}
}
if latestVersion == "" {
return "", fmt.Errorf("No version found for \"%s\"", branch)
return "", "", fmt.Errorf("No version found for \"%s\"", branch)
}

}
return latestVersion, nil
return latestVersion, latestRelease, nil

}

func printLatestVersion() {
_, githash, _ = GetVersion()
latest, err := GetLatestVersion(true)
latest, _, err := GetLatestVersion(true)
if err != nil {
logger.Errorf("Couldn't find latest version :%s\n", err)
} else {
Expand Down
3 changes: 2 additions & 1 deletion pkg/api/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (r *queryResolver) Version(ctx context.Context) (*models.Version, error) {

//Gets latest version (git shorthash commit for now)
func (r *queryResolver) Latestversion(ctx context.Context) (*models.ShortVersion, error) {
ver, err := GetLatestVersion(true)
ver, url, err := GetLatestVersion(true)
if err == nil {
logger.Infof("Retrieved latest hash: %s", ver)
} else {
Expand All @@ -127,6 +127,7 @@ func (r *queryResolver) Latestversion(ctx context.Context) (*models.ShortVersion

return &models.ShortVersion{
Shorthash: ver,
URL: url,
}, err
}

Expand Down
11 changes: 5 additions & 6 deletions ui/v2/src/components/Settings/SettingsAboutPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface IProps { }
export const SettingsAboutPanel: FunctionComponent<IProps> = (props: IProps) => {
const { data, error, loading } = StashService.useVersion();
const { data: dt, error: er, loading: ld, refetch, networkStatus } = StashService.useLatestVersion();

function maybeRenderTag() {
if (!data || !data.version || !data.version.version) { return; }
return (
Expand All @@ -29,7 +29,7 @@ export const SettingsAboutPanel: FunctionComponent<IProps> = (props: IProps) =>
}

function maybeRenderLatestVersion() {
if (!dt || !dt.latestversion || !dt.latestversion.shorthash) { return; }
if (!dt || !dt.latestversion || !dt.latestversion.shorthash || !dt.latestversion.url) { return; }
if (!data || !data.version || !data.version.hash) {
return (
<>{dt.latestversion.shorthash}</>
Expand All @@ -38,7 +38,9 @@ export const SettingsAboutPanel: FunctionComponent<IProps> = (props: IProps) =>

if (data.version.hash !== dt.latestversion.shorthash) {
return (
<strong>{dt.latestversion.shorthash} (NEW)</strong>
<>
<strong>{dt.latestversion.shorthash} [NEW] </strong><a href={dt.latestversion.url}>Download</a>
</>
);
}

Expand All @@ -55,9 +57,6 @@ export const SettingsAboutPanel: FunctionComponent<IProps> = (props: IProps) =>
< td>Latest Version Build Hash: </td>
< td>{maybeRenderLatestVersion()} </td>
</tr>
<tr>
<td>Get latest releases on <a href="https://github.com/stashapp/stash/releases">Github</a></td>
</tr>
<tr>
<td><Button minimal={true} onClick={() => refetch()} text="Check for new version!" /></td>
</tr>
Expand Down

0 comments on commit 23e4b33

Please sign in to comment.