This repository has been archived by the owner on Aug 12, 2020. 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.
- Loading branch information
1 parent
80a9f04
commit 4c0eb46
Showing
137 changed files
with
180,769 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,29 +1,128 @@ | ||
package actions | ||
|
||
import ( | ||
"crypto/sha256" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
"strings" | ||
|
||
"github.com/go-resty/resty" | ||
"github.com/google/go-github/github" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type ReleaseInfo struct { | ||
Tag string | ||
Assets []Asset | ||
} | ||
|
||
type Asset struct { | ||
DownloadURL string | ||
Sha256 string | ||
Platform string | ||
OS string | ||
} | ||
|
||
//GetReleaseInfo gets the release info | ||
func GetReleaseInfo() error { | ||
func GetReleaseInfo() (*ReleaseInfo, error) { | ||
payload, err := GetPayload() | ||
if err != nil { | ||
return err | ||
return nil, err | ||
} | ||
|
||
e, err := github.ParseWebHook("release", payload) | ||
if err != nil { | ||
return err | ||
return nil, err | ||
} | ||
|
||
event, ok := e.(*github.ReleaseEvent) | ||
if !ok { | ||
return fmt.Errorf("invalid event data") | ||
return nil, fmt.Errorf("invalid event data") | ||
} | ||
|
||
if len(event.Release.Assets) == 0 { | ||
return nil, fmt.Errorf("no assets found") | ||
} | ||
|
||
client := resty.New() | ||
for _, releaseAsset := range event.Release.Assets { | ||
_, err = getAssetInfo(client, releaseAsset) | ||
} | ||
|
||
logrus.Info(event) | ||
return nil | ||
return nil, nil | ||
} | ||
|
||
func getAssetInfo(client *resty.Client, releaseAsset github.ReleaseAsset) (*Asset, error) { | ||
file, err := ioutil.TempFile("", "") | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer os.Remove(file.Name()) | ||
|
||
resp, err := client.R().SetOutput(file.Name()).Get(releaseAsset.GetBrowserDownloadURL()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if resp.IsError() { | ||
return nil, fmt.Errorf("received response-code %d from %s", resp.StatusCode(), releaseAsset.GetBrowserDownloadURL()) | ||
} | ||
|
||
sha256, err := getSha256(file.Name()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
platformOS, platformArch, err := getPlatformInfo(releaseAsset.GetBrowserDownloadURL()) | ||
logrus.Infof("platformOS: %s, platformArch: %s, sha256: %s", platformOS, platformArch, sha256) | ||
return nil, nil | ||
} | ||
|
||
func stringValue(s *string) string { | ||
if s == nil { | ||
return "" | ||
} | ||
|
||
return *s | ||
} | ||
|
||
func getSha256(filename string) ([]byte, error) { | ||
f, err := os.Open(filename) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer f.Close() | ||
|
||
h := sha256.New() | ||
if _, err := io.Copy(h, f); err != nil { | ||
return nil, err | ||
} | ||
|
||
return h.Sum(nil), nil | ||
} | ||
|
||
func getPlatformInfo(u string) (string, string, error) { | ||
platformOS := "" | ||
platformArch := "" | ||
|
||
switch { | ||
case strings.Contains(u, "darwin"): | ||
platformOS = "darwin" | ||
case strings.Contains(u, "linux"): | ||
platformOS = "linux" | ||
case strings.Contains(u, "windows"): | ||
platformOS = "windows" | ||
} | ||
|
||
switch { | ||
case strings.Contains(strings.ToLower(u), "x86-64") || strings.Contains(strings.ToLower(u), "x86_64"): | ||
platformArch = "X86_64" | ||
case strings.Contains(strings.ToLower(u), "arm"): | ||
platformArch = "arm" | ||
} | ||
|
||
return platformOS, platformArch, nil | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.