Skip to content

Commit

Permalink
Merge pull request #1235 from tubone24/delete-file
Browse files Browse the repository at this point in the history
delete file
  • Loading branch information
tubone24 authored Jan 12, 2023
2 parents e1eb39f + 21078c3 commit bb84282
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 11 deletions.
1 change: 1 addition & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ jobs:
env:
FILE_PATH: screenshot-${{ matrix.os }}-${{ matrix.width }}.png
FILE_NAME: screenshot-${{ matrix.os }}-${{ matrix.width }}.png
HEAD_REF: ${{ github.head_ref }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_REPOSITORY: "tubone24/blog"
GITHUB_PULL_REQUEST_NUMBER: ""
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/previewDeploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@ jobs:
env:
FILE_PATH: screenshot-${{ matrix.os }}-${{ matrix.width }}.png
FILE_NAME: screenshot-${{ matrix.os }}-${{ steps.get-pr-num.outputs.prnum }}-${{ github.head_ref }}-${{ matrix.width }}.png
HEAD_REF: ${{ github.head_ref }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_REPOSITORY: "tubone24/blog"
GITHUB_PULL_REQUEST_NUMBER: ${{ steps.get-pr-num.outputs.prnum }}
Expand Down
58 changes: 47 additions & 11 deletions scripts/uploadScreenShot.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { encode } from "https://deno.land/std/encoding/base64.ts";
import { encode } from "https://deno.land/[email protected]/encoding/base64.ts";
import { sleep } from "https://deno.land/x/[email protected]/mod.ts";

const GITHUB_API_URL = "https://api.github.com";
const AUTHOR_NAME = "tubone24";
Expand All @@ -9,14 +10,54 @@ const gitHubToken = Deno.env.get("GITHUB_TOKEN") as string;
const gitHubRepo = Deno.env.get("GITHUB_REPOSITORY") as string;
const prNumber = Deno.env.get("GITHUB_PULL_REQUEST_NUMBER") as string;
const branchName = Deno.env.get("BRANCH_NAME") as string;
const headRef = Deno.env.get("HEAD_REF") as string;

const readImageData = await Deno.readFile(filePath);
const encodedData = encode(readImageData);

const message = prNumber === "" ? "[file upload] Added file on master" : `[file upload] Added file for PR #${prNumber}`;
const uploadMessage = prNumber === "" ? "[file upload] Added file on master" : `[file upload] Added file for PR #${prNumber}`;

const gitHubPayload = {
message: message,
const deleteMessage = prNumber === "" ? "[file upload] Delete file on master" : `[file upload] Delete file for PR #${prNumber}`;

const gitHubHeaders = {
Accept: "application/vnd.github.v3+json",
Authorization: `Bearer ${gitHubToken}`,
};

const content = await fetch(`${GITHUB_API_URL}/repos/${gitHubRepo}/contents/docs/screenshot/${headRef}/${fileName}?ref=${branchName}`, {
method: "GET",
headers: gitHubHeaders,
});

if (content.ok) {
console.log("already have contents");
const contentJson = await content.json();
console.log(contentJson.sha)
const gitHubDeletePayload = {
message: deleteMessage,
sha: contentJson.sha,
branch: branchName,
author: {
name: AUTHOR_NAME,
email: AUTHOR_EMAIL,
},
committer: {
name: AUTHOR_NAME,
email: AUTHOR_EMAIL,
},
}
const resp = await fetch(`${GITHUB_API_URL}/repos/${gitHubRepo}/contents/docs/screenshot/${headRef}/${fileName}`, {
method: "DELETE",
headers: gitHubHeaders,
body: JSON.stringify(gitHubDeletePayload),
});
console.log(resp);
}

await sleep(10);

const gitHubUploadPayload = {
message: uploadMessage,
content: encodedData.replace(new RegExp("data.*base64,"), ""),
branch: branchName,
author: {
Expand All @@ -29,17 +70,12 @@ const gitHubPayload = {
},
};

const gitHubHeaders = {
Accept: "application/vnd.github.v3+json",
Authorization: `Bearer ${gitHubToken}`,
};

const gitHubUploadurl = `${GITHUB_API_URL}/repos/${gitHubRepo}/contents/docs/screenshot/${fileName}`;
const gitHubUploadurl = `${GITHUB_API_URL}/repos/${gitHubRepo}/contents/docs/screenshot/${headRef}/${fileName}`;

const gitHubRes = await fetch(gitHubUploadurl, {
method: "PUT",
headers: gitHubHeaders,
body: JSON.stringify(gitHubPayload),
body: JSON.stringify(gitHubUploadPayload),
});

console.log(gitHubRes);

0 comments on commit bb84282

Please sign in to comment.