From 02a42e99f8f82c36d3065aed83d1bbfbc963d304 Mon Sep 17 00:00:00 2001 From: lwch Date: Tue, 18 Oct 2022 11:25:01 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9release.go?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- release.go | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/release.go b/release.go index 7bc9768..913d9fc 100644 --- a/release.go +++ b/release.go @@ -9,6 +9,7 @@ import ( "path/filepath" "strconv" "strings" + "time" "github.com/gomarkdown/markdown" "github.com/gomarkdown/markdown/ast" @@ -21,6 +22,8 @@ import ( "golang.org/x/oauth2" ) +const retry = 5 + func main() { token, ok := os.LookupEnv("GITHUB_TOKEN") if !ok { @@ -67,7 +70,13 @@ func main() { if fi.IsDir() { continue } - upload(gcli, owner, repo, releaseID, file) + for i := 0; i < retry; i++ { + deleteIfExists(gcli, owner, repo, releaseID, filepath.Base(file)) + if upload(gcli, owner, repo, releaseID, file) { + break + } + time.Sleep(time.Second) + } } } @@ -96,7 +105,7 @@ func createOrDrop(cli *github.Client, owner, repo, version, body string) int64 { return ret.GetID() } -func upload(cli *github.Client, owner, repo string, id int64, dir string) { +func upload(cli *github.Client, owner, repo string, id int64, dir string) bool { log.Printf("upload file %s...", dir) f, err := os.Open(dir) runtime.Assert(err) @@ -106,8 +115,32 @@ func upload(cli *github.Client, owner, repo string, id int64, dir string) { var rep *github.Response _, rep, err = cli.Repositories.UploadReleaseAsset( context.Background(), owner, repo, id, &opt, f) - runtime.Assert(err) + if err != nil { + return false + } defer rep.Body.Close() + return true +} + +func deleteIfExists(cli *github.Client, owner, repo string, id int64, name string) bool { + var opt github.ListOptions + opt.PerPage = 1000 + assets, rep, err := cli.Repositories.ListReleaseAssets(context.Background(), owner, repo, id, &opt) + if err != nil { + return false + } + defer rep.Body.Close() + for _, asset := range assets { + if *asset.Name == name { + log.Printf("delete file %s...", name) + rep, err = cli.Repositories.DeleteReleaseAsset(context.Background(), owner, repo, asset.GetID()) + if err != nil { + return false + } + defer rep.Body.Close() + } + } + return true } func getChangeLog(version string) string {