-
Notifications
You must be signed in to change notification settings - Fork 22
/
assets_fetcher.go
148 lines (124 loc) · 4 KB
/
assets_fetcher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package github
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"os"
"path"
"path/filepath"
"time"
"code.vegaprotocol.io/vega/visor/utils"
"github.com/google/go-github/v50/github"
"golang.org/x/sync/errgroup"
)
const zipMimeType = "application/zip"
type AssetsFetcher struct {
repositoryOwner string
repository string
assetNames map[string]struct{}
*github.Client
}
func NewAssetsFetcher(
repositoryOwner, repository string,
assetsNames []string,
) *AssetsFetcher {
return &AssetsFetcher{
repositoryOwner: repositoryOwner,
repository: repository,
assetNames: utils.ToLookupMap(assetsNames),
Client: github.NewClient(nil),
}
}
func (af *AssetsFetcher) GetReleaseID(ctx context.Context, releaseTag string) (int64, error) {
nextPage := 1
opts := &github.ListOptions{
PerPage: 100,
Page: nextPage,
}
for nextPage > 0 {
opts.Page = nextPage
releases, resp, err := af.Client.Repositories.ListReleases(ctx, af.repositoryOwner, af.repository, opts)
if err != nil {
return 0, err
}
nextPage = resp.NextPage
for _, r := range releases {
if *r.TagName == releaseTag {
return r.GetID(), nil
}
}
}
return 0, fmt.Errorf("release tag %q not found", releaseTag)
}
func (af *AssetsFetcher) GetAssets(ctx context.Context, releaseID int64) ([]*github.ReleaseAsset, error) {
assets, _, err := af.Client.Repositories.ListReleaseAssets(ctx, af.repositoryOwner, af.repository, releaseID, nil)
if err != nil {
return nil, err
}
var filteredAssets []*github.ReleaseAsset
for _, asset := range assets {
if _, ok := af.assetNames[asset.GetName()]; ok {
filteredAssets = append(filteredAssets, asset)
}
}
return filteredAssets, nil
}
func (af *AssetsFetcher) DownloadAsset(ctx context.Context, assetID int64, assetType, path string) error {
followClient := &http.Client{Timeout: time.Second * 120}
ra, _, err := af.Client.Repositories.DownloadReleaseAsset(ctx, af.repositoryOwner, af.repository, assetID, followClient)
if err != nil {
return fmt.Errorf("failed to download release asset: %w", err)
}
defer ra.Close()
all, err := ioutil.ReadAll(ra)
if err != nil {
return fmt.Errorf("failed to read %q: %w", path, err)
}
if err := os.WriteFile(path, all, 0o770); err != nil {
return fmt.Errorf("failed to write to %q: %w", path, err)
}
if assetType == zipMimeType {
if err := utils.UnzipSource(path, filepath.Dir(path)); err != nil {
return fmt.Errorf("failed to unzip asset: %w", err)
}
}
return nil
}
func (af *AssetsFetcher) Download(ctx context.Context, releaseTag, downloadDir string) error {
releaseID, err := af.GetReleaseID(ctx, releaseTag)
if err != nil {
return fmt.Errorf("failed to get release ID for tag %q: %q", releaseTag, err)
}
assetIDs, err := af.GetAssets(ctx, releaseID)
if err != nil {
return fmt.Errorf("failed to get assets ID for tag %q: %q", releaseTag, err)
}
eg, ctx := errgroup.WithContext(ctx)
for _, asset := range assetIDs {
assetID := asset.GetID()
assetName := asset.GetName()
assetType := asset.GetContentType()
eg.Go(func() error {
if err := af.DownloadAsset(ctx, assetID, assetType, path.Join(downloadDir, assetName)); err != nil {
return fmt.Errorf("failed to download asset %q of type %q for tag %q: %w", assetName, releaseTag, assetType, err)
}
return nil
})
}
return eg.Wait()
}