Action

Post Gist to Github

Posted by agiletortoise, Last update 29 days ago

Post the content of the draft to a Gist on Github. After creating the Gist, you are prompted to either place the URL in the clipboard, or open the Gist in Safari. The URL is also logged in the action log for reference.

When first run, you will be prompt to enter a personal access token for API access. Generate a token a your settings page on GitHub.com. The token should have “gist” permissions for this action to work.

Basic configuration options for output content, public/private status can be configured in variables at the beginning of the script.

Steps

  • script

    /* Create public Gist with draft content on GitHub
    *. When first run, you will be prompted for a personal API token
    *  Personal tokens can be generated at: https://github.com/settings/tokens
    *  The token used should have "gist" scope permissions
    */
    
    // BEGIN config
    let content = draft.content; // content of the Gist
    let fileName = "content.md"; // file name
    let isPublic = true; // should this be a public Gist
    // END config 
    
    // do credential auth to get a token from the user.
    let auth = () => {
    	let githubPage = "https://github.com/settings/tokens";
    	let cred = Credential.create("GitHub Gist", "Enter personal access token for the GitHub API generated from your developer page at GitHub.com (https://github.com/settings/tokens). This token should have permissions for the scope \"gist\" to allow creation of Gists.");
    	
    	cred.addTextField("Token", "");
    	if (cred.authorize()) {
    		return cred.getValue("Token");
    	}
    	else {
    		return false
    	}
    }
    
    // 
    let token = auth();
    if (token) {
    	let http = HTTP.create();
    
    	let data = {
    	  "description": "Gist from Drafts",
    	  "public": isPublic,
    	  "files": { }  
    	};
    	data["files"][fileName] = {
    		"content": content
    	}
    
    	let settings = {
    	  "url": "https://api.github.com/gists",
    	  "method": "POST",
    	  "headers": {"Authorization": `Bearer ${token}`},
    	  "data": data
    	};
    
    	let response = http.request(settings);
    	if (response.statusCode == 201) {
    		let responseData = JSON.parse(response.responseText);
    		let gistURL = responseData.html_url;
    
    		console.log(responseData);
    		console.log(gistURL);
    		console.log("Gist created");
    		app.setClipboard(gistURL);
    
    		let p = Prompt.create();
    		p.title = "Gist created"
    		p.message = "Gist created and url placed in the clipboard."
    		p.addButton("Open in Safari");
    
    		if (p.show()) {
    		  if (p.buttonPressed == "Open in Safari") {
    		    app.openURL(gistURL);
    		  }
    		}
    	}
    	else {
    		console.log(response.statusCode);
    		context.fail();
    	}
    }
    else {
    	context.cancel();
    }
    
    

Options

  • After Success Nothing
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.