MCPcopy Create free account
hub / github.com/GoogleChrome/samples / uploadDraftImages

Function uploadDraftImages

trailblazers/public/js/github/github-utils.js:15–58  ·  view source on GitHub ↗
(
  ui,
  draft,
  slug,
  branchName,
  defaultLocale,
  getSha,
)

Source from the content-addressed store, hash-verified

13 * @return {Promise<void>}
14 */
15export async function uploadDraftImages(
16 ui,
17 draft,
18 slug,
19 branchName,
20 defaultLocale,
21 getSha,
22) {
23 for (const img of draft.imageFiles || []) {
24 const data = await getImage(img.id);
25 if (!data) continue;
26
27 const content = await bufferToBase64(data);
28 const imgPath = `content/${defaultLocale}/blog/${slug}/${img.name}`;
29 const existingImgSha = await getSha(imgPath);
30
31 await ghFetch(ui, `/contents/${imgPath}`, {
32 method: 'PUT',
33 body: JSON.stringify({
34 message: `${existingImgSha ? 'Update' : 'Add'} image ${img.name}`,
35 content,
36 branch: branchName,
37 sha: existingImgSha,
38 }),
39 });
40
41 // Redundant copies for each locale
42 if (draft.translations) {
43 for (const locale of Object.keys(draft.translations)) {
44 const localeImgPath = `content/${locale}/blog/${slug}/${img.name}`;
45 const existingLocaleImgSha = await getSha(localeImgPath);
46 await ghFetch(ui, `/contents/${localeImgPath}`, {
47 method: 'PUT',
48 body: JSON.stringify({
49 message: `${existingLocaleImgSha ? 'Update' : 'Add'} localized image ${img.name} for ${locale}`,
50 content,
51 branch: branchName,
52 sha: existingLocaleImgSha,
53 }),
54 });
55 }
56 }
57 }
58}

Callers 1

createPRFunction · 0.90

Calls 4

getImageFunction · 0.90
bufferToBase64Function · 0.90
ghFetchFunction · 0.90
getShaFunction · 0.85

Tested by

no test coverage detected