pikapika/ci/cmd/check_asset/main.go

103 lines
2.7 KiB
Go
Raw Normal View History

2022-03-22 09:12:48 +00:00
package main
import (
2022-04-15 16:53:37 +00:00
"ci/commons"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
2022-03-22 09:12:48 +00:00
)
const owner = "niuhuan"
const repo = "pikapika"
const ua = "niuhuan pikapika ci"
func main() {
2022-04-15 16:53:37 +00:00
// get ghToken
ghToken := os.Getenv("GH_TOKEN")
if ghToken == "" {
println("Env ${GH_TOKEN} is not set")
os.Exit(1)
}
// get version
var version commons.Version
codeFile, err := ioutil.ReadFile("version.code.txt")
if err != nil {
panic(err)
}
version.Code = strings.TrimSpace(string(codeFile))
infoFile, err := ioutil.ReadFile("version.info.txt")
if err != nil {
panic(err)
}
version.Info = strings.TrimSpace(string(infoFile))
// get target
target := os.Getenv("TARGET")
2022-05-18 03:49:21 +00:00
if target == "" {
2022-04-15 16:53:37 +00:00
println("Env ${TARGET} is not set")
os.Exit(1)
}
2022-05-18 03:49:21 +00:00
// get target
flutterVersion := os.Getenv("flutter_version")
if target == "" {
println("Env ${flutter_version} is not set")
os.Exit(1)
}
2022-04-15 16:53:37 +00:00
//
var releaseFileName string
switch target {
case "macos":
2022-05-18 03:49:21 +00:00
releaseFileName = fmt.Sprintf("pikapika-%v-flutter_%v-macos-intel.dmg", version.Code, flutterVersion)
2022-04-15 16:53:37 +00:00
case "ios":
2022-05-18 03:49:21 +00:00
releaseFileName = fmt.Sprintf("pikapika-%v-flutter_%v-ios-nosign.ipa", version.Code, flutterVersion)
2022-04-15 16:53:37 +00:00
case "windows":
2022-05-18 03:49:21 +00:00
releaseFileName = fmt.Sprintf("pikapika-%v-flutter_%v-windows-x86_64.zip", version.Code, flutterVersion)
2022-04-15 16:53:37 +00:00
case "linux":
2022-05-18 03:49:21 +00:00
releaseFileName = fmt.Sprintf("pikapika-%v-flutter_%v-linux-x86_64.AppImage", version.Code, flutterVersion)
2022-04-15 16:53:37 +00:00
case "android-arm32":
2022-05-18 03:49:21 +00:00
releaseFileName = fmt.Sprintf("pikapika-%v-flutter_%v-android-arm32.apk", version.Code, flutterVersion)
2022-04-15 16:53:37 +00:00
case "android-arm64":
2022-05-18 03:49:21 +00:00
releaseFileName = fmt.Sprintf("pikapika-%v-flutter_%v-android-arm64.apk", version.Code, flutterVersion)
2022-04-15 16:53:37 +00:00
case "android-x86_64":
2022-05-18 03:49:21 +00:00
releaseFileName = fmt.Sprintf("pikapika-%v-flutter_%v-android-x86_64.apk", version.Code, flutterVersion)
2022-04-15 16:53:37 +00:00
}
// get version
getReleaseRequest, err := http.NewRequest(
"GET",
fmt.Sprintf("https://api.github.com/repos/%v/%v/releases/tags/%v", owner, repo, version.Code),
nil,
)
if err != nil {
panic(err)
}
getReleaseRequest.Header.Set("User-Agent", ua)
getReleaseRequest.Header.Set("Authorization", "token "+ghToken)
getReleaseResponse, err := http.DefaultClient.Do(getReleaseRequest)
if err != nil {
panic(err)
}
defer getReleaseResponse.Body.Close()
if getReleaseResponse.StatusCode == 404 {
panic("NOT FOUND RELEASE")
}
buff, err := ioutil.ReadAll(getReleaseResponse.Body)
if err != nil {
panic(err)
}
var release commons.Release
err = json.Unmarshal(buff, &release)
if err != nil {
println(string(buff))
panic(err)
}
for _, asset := range release.Assets {
if asset.Name == releaseFileName {
println("::set-output name=skip_build::true")
os.Exit(0)
}
}
print("::set-output name=skip_build::false")
2022-03-22 09:12:48 +00:00
}