-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
artifact.go
94 lines (85 loc) · 2.24 KB
/
artifact.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
package artifact
import (
"context"
"errors"
"fmt"
"io/fs"
"log"
"strings"
"testing/fstest"
"github.com/k1LoW/octocov/gh"
"github.com/k1LoW/octocov/report"
)
const defaultArtifactName = "octocov-report"
const reportFilename = "report.json"
var keyRep = strings.NewReplacer(`"`, "_", ":", "_", "<", "_", ">", "_", "|", "_", "*", "_", "?", "_", "\r", "_", "\n", "_", "\\", "_", "/", "_")
type Artifact struct {
gh *gh.Gh
repository string
name string
r *report.Report
}
func New(gh *gh.Gh, repo, name string, r *report.Report) (*Artifact, error) {
if name == "" {
name = defaultArtifactName
}
return &Artifact{
gh: gh,
repository: repo,
name: name,
r: r,
}, nil
}
func (a *Artifact) StoreReport(ctx context.Context, r *report.Report) error {
switch {
case a.repository == r.Repository:
return a.Put(ctx, reportFilename, r.Bytes())
case strings.HasPrefix(r.Repository, fmt.Sprintf("%s/", a.repository)):
a.name = fmt.Sprintf("%s-%s", a.name, keyRep.Replace(r.Key()))
return a.Put(ctx, reportFilename, r.Bytes())
default:
return errors.New("reporting to the artifact can only be sent from the GitHub Actions of the same repository")
}
}
func (a *Artifact) Put(ctx context.Context, path string, content []byte) error {
return a.gh.PutArtifact(ctx, a.name, path, content)
}
func (a *Artifact) FS() (fs.FS, error) {
ctx := context.Background()
var (
path, name string
r *gh.Repository
err error
)
if a.r == nil {
r, err = gh.Parse(a.repository)
if err != nil {
return nil, err
}
path = fmt.Sprintf("%s/%s/%s", r.Owner, r.Repo, reportFilename)
name = a.name
} else {
r, err = gh.Parse(a.r.Repository)
if err != nil {
return nil, err
}
path = fmt.Sprintf("%s/%s/%s", r.Owner, r.Reponame(), reportFilename)
key := keyRep.Replace(a.r.Key())
if key == "" {
name = a.name
} else {
name = fmt.Sprintf("%s-%s", a.name, keyRep.Replace(a.r.Key()))
}
}
log.Printf("artifact name: %s", name)
af, err := a.gh.GetLatestArtifact(ctx, r.Owner, r.Repo, name, reportFilename)
fsys := fstest.MapFS{}
if err == nil {
fsys[path] = &fstest.MapFile{
Data: af.Content,
Mode: fs.ModePerm,
ModTime: af.CreatedAt,
}
}
return &fsys, nil
}