-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpublish_gh.go
132 lines (114 loc) · 3.5 KB
/
publish_gh.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
package gontentful
import (
"context"
"encoding/json"
"fmt"
"path/filepath"
"github.com/google/go-github/v48/github"
gh "github.com/moonwalker/moonbase/pkg/github"
)
type GHPublish struct {
Entry *PublishedEntry
RepoName string
FileName string
Locales []*Locale
LocalizedFields map[string]bool
}
func NewGHPublish(entry *PublishedEntry, repoName, fileName string, locales *Locales, localizedFields map[string]bool) *GHPublish {
return &GHPublish{
Entry: entry,
RepoName: repoName,
FileName: fileName,
Locales: locales.Items,
LocalizedFields: localizedFields,
}
}
func getDeleteEntries(ctx context.Context, cfg *Config, s *GHPublish, folderName string) ([]gh.BlobEntry, error) {
path := filepath.Join(cfg.WorkDir, folderName, s.FileName)
return gh.GetDeleteFileEntries(ctx, cfg.Token, owner, s.RepoName, branch, path, "feat(content): delete files")
}
func getAssetImages(ctx context.Context, cfg *Config, repo, url string) (*string, error) {
// download image
imageContent, err := downloadImage(url)
if err != nil {
return nil, err
}
// create the blobs with the image's content (encoding base64)
encoding := "base64"
blob, _, err := gh.CreateBlob(ctx, cfg.Token, owner, repo, branch, &imageContent, &encoding)
if err != nil {
return nil, err
}
return blob.SHA, nil
}
func (s *GHPublish) Exec(fmtVideoURL func(string) string) ([]gh.BlobEntry, error) {
ctx := context.Background()
cfg := getConfig(ctx, owner, s.RepoName, branch)
entryType := s.Entry.Sys.Type
entries := make([]gh.BlobEntry, 0)
folderName := ""
switch entryType {
case DELETED_ASSET:
return getDeleteEntries(ctx, cfg, s, ASSET_TABLE_NAME)
case DELETED_ENTRY:
return getDeleteEntries(ctx, cfg, s, s.Entry.Sys.ContentType.Sys.ID)
case ASSET:
folderName = ASSET_TABLE_NAME
imageURLs := getAssetImageURL(s.Entry)
for fn, url := range imageURLs {
sha, err := getAssetImages(ctx, cfg, s.RepoName, url)
if err != nil {
return nil, err
}
// add image sha to entries array
entries = append(entries, gh.BlobEntry{
Path: fmt.Sprintf("%s/%s", IMAGE_FOLDER_NAME, fn),
SHA: sha,
})
}
default:
folderName = s.Entry.Sys.ContentType.Sys.ID
}
cflId := GetCloudflareImagesID(s.RepoName)
cd := TransformPublishedEntry(s.Locales, s.Entry, s.LocalizedFields, cflId, fmtVideoURL)
// upload to github
for l, c := range cd {
fileName := fmt.Sprintf("%s/%s.json", s.FileName, l)
contentBytes, err := json.Marshal(c)
if err != nil {
return nil, err
}
content := string(contentBytes)
path := filepath.Join(cfg.WorkDir, folderName, fileName)
entries = append(entries, gh.BlobEntry{
Path: path,
Content: &content,
})
}
return entries, nil
}
func PublishCFChanges(repo string, ref string, entries []gh.BlobEntry) (github.Rate, error) {
if len(ref) == 0 {
ref = branch
}
ctx := context.Background()
cfg := getConfig(ctx, owner, repo, ref)
resp, err := gh.CommitBlobs(ctx, cfg.Token, owner, repo, ref, entries, "feat(content): update files")
return resp.Rate, err
}
func getAssetImageURL(entry *PublishedEntry) map[string]string {
imageURLs := make(map[string]string)
for loc, fc := range entry.Fields["file"] {
fileContent, ok := fc.(map[string]interface{})
if ok {
fileName := fileContent["fileName"].(string)
if fileName != "" {
url := fileContent["url"].(string)
if url != "" {
imageURLs[GetImageFileName(fileName, entry.Sys.ID, loc)] = fmt.Sprintf("http:%s", url)
}
}
}
}
return imageURLs
}