This repository has been archived by the owner on Jul 16, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract finding private Github download URL to library
Signed-off-by: Knut Ahlers <[email protected]>
- Loading branch information
Showing
2 changed files
with
92 additions
and
74 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package privatehub | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
const ( | ||
httpRequestTimeout = 5 * time.Second | ||
) | ||
|
||
var ( | ||
AssetNotFound = errors.New("Did not find given asset in release") | ||
ReleaseNotFound = errors.New("Did not find given release") | ||
) | ||
|
||
type ghRelease struct { | ||
Assets []struct { | ||
Name string `json:"name"` | ||
URL string `json:"url"` | ||
} `json:"assets"` | ||
} | ||
|
||
func GetDownloadURL(repo, version, filename, ghToken string) (string, error) { | ||
path := fmt.Sprintf("/repos/%s/releases/tags/%s", repo, version) | ||
if version == "latest" { | ||
path = fmt.Sprintf("/repos/%s/releases/latest", repo) | ||
} | ||
u := fmt.Sprintf("https://api.github.com%s", path) | ||
|
||
req, _ := http.NewRequest("GET", u, nil) | ||
req.SetBasicAuth("auth", ghToken) | ||
ctx, cancel := context.WithTimeout(context.Background(), httpRequestTimeout) | ||
defer cancel() | ||
|
||
httpClient := &http.Client{ | ||
CheckRedirect: func(req *http.Request, via []*http.Request) error { | ||
return http.ErrUseLastResponse | ||
}, | ||
} | ||
|
||
resp, err := httpClient.Do(req.WithContext(ctx)) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != 200 { | ||
return "", ReleaseNotFound | ||
} | ||
|
||
r := &ghRelease{} | ||
if err := json.NewDecoder(resp.Body).Decode(r); err != nil { | ||
return "", err | ||
} | ||
|
||
var dlURL string | ||
for _, ass := range r.Assets { | ||
if ass.Name != filename { | ||
continue | ||
} | ||
|
||
dlURL = ass.URL | ||
} | ||
|
||
if dlURL == "" { | ||
return "", AssetNotFound | ||
} | ||
|
||
req, _ = http.NewRequest("HEAD", dlURL, nil) | ||
req.Header.Set("Accept", "application/octet-stream") | ||
req.SetBasicAuth("auth", ghToken) | ||
|
||
ctx, cancel = context.WithTimeout(context.Background(), httpRequestTimeout) | ||
defer cancel() | ||
|
||
resp, err = httpClient.Do(req.WithContext(ctx)) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer resp.Body.Close() | ||
|
||
return resp.Header.Get("Location"), nil | ||
} |