84 lines
1.8 KiB
Go
84 lines
1.8 KiB
Go
package github
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/google/go-github/v72/github"
|
|
)
|
|
|
|
func (g *Github) Upload(releaseID int64) error {
|
|
var assets []*github.ReleaseAsset
|
|
listOpts := &github.ListOptions{}
|
|
|
|
for {
|
|
asset, resp, err := g.client.Repositories.ListReleaseAssets(
|
|
g.app.Context(), g.app.Settings().Owner, g.app.Settings().Repo,
|
|
releaseID, listOpts,
|
|
)
|
|
if err != nil {
|
|
g.app.Logger().WithError(err).Error("Failed to fetch existing assets")
|
|
|
|
return err
|
|
}
|
|
|
|
assets = append(assets, asset...)
|
|
|
|
if resp.NextPage == 0 {
|
|
break
|
|
}
|
|
|
|
listOpts.Page = resp.NextPage
|
|
}
|
|
|
|
uploadFiles, err := os.ReadDir(g.app.Settings().Storage)
|
|
if err != nil {
|
|
g.app.Logger().WithError(err).Error("Failed to read assets storage")
|
|
}
|
|
|
|
for _, file := range uploadFiles {
|
|
if !file.IsDir() {
|
|
for _, asset := range assets {
|
|
if file.Name() == *asset.Name {
|
|
g.app.Logger().WithField("name", file.Name()).Error("File exists")
|
|
|
|
return fmt.Errorf("file exists: %s", file.Name())
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
for _, file := range uploadFiles {
|
|
if file.IsDir() {
|
|
continue
|
|
}
|
|
|
|
fileReader, err := os.Open(
|
|
fmt.Sprintf("%s/%s", g.app.Settings().Storage, file.Name()),
|
|
)
|
|
if err != nil {
|
|
g.app.Logger().
|
|
WithError(err).WithField("name", file.Name()).
|
|
Error("Failed to read artifact")
|
|
|
|
return err
|
|
}
|
|
|
|
uploadOptions := &github.UploadOptions{Name: file.Name()}
|
|
|
|
if _, _, err = g.client.Repositories.UploadReleaseAsset(
|
|
g.app.Context(), g.app.Settings().Owner, g.app.Settings().Repo,
|
|
releaseID, uploadOptions, fileReader); err != nil {
|
|
g.app.Logger().
|
|
WithError(err).WithField("name", file.Name()).
|
|
Error("Failed to upload artifact")
|
|
|
|
return err
|
|
}
|
|
|
|
g.app.Logger().WithField("name", file.Name()).Info("Successfully uploaded artifact")
|
|
}
|
|
|
|
return nil
|
|
}
|