Skip to content

Instantly share code, notes, and snippets.

@itaditya
Created January 24, 2018 06:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itaditya/751e1aa41671986c24521404399e8905 to your computer and use it in GitHub Desktop.
Save itaditya/751e1aa41671986c24521404399e8905 to your computer and use it in GitHub Desktop.
Code in Nodejs to create a file on github using Github REST API
const request = require('superagent');
const base64 = require('base-64');
const BASE_URL = 'https://api.github.com';
const TOKEN = 'BOT_ACCESS_TOKEN';
(async() => {
const COMMIT_MSG = process.argv[2];
const FILE_CONTENT = process.argv[3];
const OWNER = 'YOUR_NAME';
const REPO_NAME = 'NAME_OF_YOUR_REPO'; //file will be created in this repo.
const FILE_PATH = 'folder-1/test.txt';
const API_URL = `${BASE_URL}/repos/${OWNER}/${REPO_NAME}/contents/${FILE_PATH}`;
try {
const res = await request
.put(API_URL)
.send({
message: COMMIT_MSG,
content: base64.encode(FILE_CONTENT)
})
.set('Authorization', `token ${TOKEN}`);
console.log('File Created, see: ', res.body.content.html_url);
} catch (err) {
if(err) {
console.log('OOPS !! Some Error Occured ....')
}
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment