-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1235 from tubone24/delete-file
delete file
- Loading branch information
Showing
3 changed files
with
49 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
|
@@ -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: { | ||
|
@@ -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); |