Created
January 24, 2018 06:28
-
-
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
This file contains hidden or 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
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