Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function UserInfo(name, token) {
- this.name = name;
- this.token = token;
- }
- function Gists(userInfo) {
- let commonOptions = {
- accept: "application/vnd.github.v3+json",
- muteHttpExceptions: true,
- headers: {
- "Authorization": `Basic ${Utilities.base64Encode(`${userInfo.name}:${userInfo.token}`)}`
- },
- };
- let GistFileInfo = function(jsonGistFile) {
- this.name = jsonGistFile.filename;
- this.size = jsonGistFile.size;
- }
- let GistInfo = function(jsonGist) {
- this.id = jsonGist.id;
- this.description = jsonGist.description;
- this.url = jsonGist.url;
- this.isPublic = jsonGist.public;
- this.files = Object.getOwnPropertyNames(jsonGist.files).map(fileNode => new GistFileInfo(jsonGist.files[fileNode]));
- }
- let getError = function(response) {
- return JSON.parse(response.getContentText()).message;
- }
- this.list = function(isPublic) {
- result = [];
- let response = UrlFetchApp.fetch(`https://api.github.com/gists`, commonOptions);
- if (response.getResponseCode() !== 200)
- throw new Error(getError(response));
- let allGists = JSON.parse(response.getContentText());
- for (i in allGists)
- if (allGists[i].public === isPublic)
- result.push(new GistInfo(allGists[i]));
- return result;
- }
- this.getGist = function(id) {
- let response = UrlFetchApp.fetch(`https://api.github.com/gists/${id}`, commonOptions);
- if (response.getResponseCode() !== 200)
- throw new Error(getError(response));
- return new GistInfo(JSON.parse(response.getContentText()));
- }
- this.newGist = function(description, fileName, fileContent) {
- let options = Object.assign({}, commonOptions);
- options.method = "post";
- options.payload = `{
- "description": "${description}",
- "files": {
- "${fileName}": {
- "content": "${fileContent}"
- }
- }
- }`;
- let response = UrlFetchApp.fetch(`https://api.github.com/gists`, options);
- if (response.getResponseCode() !== 201)
- throw new Error(getError(response));
- return new GistInfo(JSON.parse(response));
- }
- this.removeGist = function(id) {
- let options = Object.assign({}, commonOptions);
- options.method = "delete";
- let response = UrlFetchApp.fetch(`https://api.github.com/gists/${id}`, options)
- if (response.getResponseCode() !== 204)
- throw new Error(getError(response));
- }
- this.updateGistDescription = function(id, description) {
- let options = Object.assign({}, commonOptions);
- options.method = "patch";
- options.payload = `{
- "description": "${description}"
- }`;
- let response = UrlFetchApp.fetch(`https://api.github.com/gists/${id}`, options)
- if (response.getResponseCode() !== 200)
- throw new Error(getError(response));
- return new GistInfo(JSON.parse(response.getContentText()));
- }
- this.newGistFile = function(id, fileName, fileContent) {
- let gist = this.getGist(id);
- if (gist.files.filter(file => file.name === fileName).length > 0)
- throw new Error(`File ${fileName} already exists.`);
- let options = Object.assign({}, commonOptions);
- options.method = "patch";
- options.payload = `{
- "files": {
- "${fileName}": {
- "content": "${fileContent}"
- }
- }
- }`;
- let response = UrlFetchApp.fetch(`https://api.github.com/gists/${id}`, options)
- if (response.getResponseCode() !== 200)
- throw new Error(getError(response));
- return new GistInfo(JSON.parse(response.getContentText()));
- }
- this.removeGistFile = function(id, fileName) {
- let gist = this.getGist(id);
- if (gist.files.filter(file => file.name === fileName).length === 0)
- throw new Error(`File ${fileName} doesn't exist.`);
- let options = Object.assign({}, commonOptions);
- options.method = "patch";
- options.payload = `{
- "files": {
- "${fileName}": {
- "content": ""
- }
- }
- }`;
- let response = UrlFetchApp.fetch(`https://api.github.com/gists/${id}`, options)
- if (response.getResponseCode() !== 200)
- throw new Error(getError(response));
- return new GistInfo(JSON.parse(response.getContentText()));
- }
- this.updateGistFile = function(id, fileName, fileContent) {
- let gist = this.getGist(id);
- if (gist.files.filter(file => file.name === fileName).length === 0)
- throw new Error(`File ${fileName} doesn't exist.`);
- let options = Object.assign({}, commonOptions);
- options.method = "patch";
- options.payload = `{
- "files": {
- "${fileName}": {
- "content": "${fileContent}"
- }
- }
- }`;
- let response = UrlFetchApp.fetch(`https://api.github.com/gists/${id}`, options)
- if (response.getResponseCode() !== 200)
- throw new Error(getError(response));
- return new GistInfo(JSON.parse(response.getContentText()));
- }
- }
- function main() {
- let userName = ScriptProperties.getProperty("user_name");
- let userToken = ScriptProperties.getProperty("user_token");
- let gists = new Gists(new UserInfo(userName, userToken));
- console.log(gists.removeGistFile("c7b5b0170125f6d42b2106ee0a2ed2c5", "Script.sh"));
- }
Add Comment
Please, Sign In to add comment