summaryrefslogtreecommitdiffstats
path: root/pkg/artifactcache/model.go
blob: 32b8ce51e754bc227d5deb7efcb1dc743e8bd79d (plain)
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
package artifactcache

import (
	"crypto/sha256"
	"fmt"
)

type Request struct {
	Key     string `json:"key" `
	Version string `json:"version"`
	Size    int64  `json:"cacheSize"`
}

func (c *Request) ToCache() *Cache {
	if c == nil {
		return nil
	}
	ret := &Cache{
		Key:     c.Key,
		Version: c.Version,
		Size:    c.Size,
	}
	if c.Size == 0 {
		// So the request comes from old versions of actions, like `actions/cache@v2`.
		// It doesn't send cache size. Set it to -1 to indicate that.
		ret.Size = -1
	}
	return ret
}

type Cache struct {
	ID             uint64 `json:"id" boltholdKey:"ID"`
	Key            string `json:"key" boltholdIndex:"Key"`
	Version        string `json:"version" boltholdIndex:"Version"`
	KeyVersionHash string `json:"keyVersionHash" boltholdUnique:"KeyVersionHash"`
	Size           int64  `json:"cacheSize"`
	Complete       bool   `json:"complete"`
	UsedAt         int64  `json:"usedAt" boltholdIndex:"UsedAt"`
	CreatedAt      int64  `json:"createdAt" boltholdIndex:"CreatedAt"`
}

func (c *Cache) FillKeyVersionHash() {
	c.KeyVersionHash = fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprintf("%s:%s", c.Key, c.Version))))
}