Skip to content

Instantly share code, notes, and snippets.

@bng44270
Last active April 6, 2023 15:21
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 bng44270/da1b6faca91563512fd646ab078a702b to your computer and use it in GitHub Desktop.
Save bng44270/da1b6faca91563512fd646ab078a702b to your computer and use it in GitHub Desktop.
Update Repository Files using Github API
/*
Update Repository Files using Github API
Requires webrequest.js (https://gist.github.com/bng44270/61122a1947591d50004fcd9ee72d643d)
Usage:
var gh = new GithubFile('API-TOKEN','USERNAME','EMAIL-ADDRESS');
var prFile = gh.getFile('REPOSITORY','FILE-PATH');
prFile.then(file => {
var prUpdate = gh.UpdateFile('REPOSITORY','FILE-PATH',file['sha'],'COMMIT-MESSAGE','NEW FILE CONTENT');
prUpdate.then(resp => {
if (resp.status == 200) {
console.log("Successfully update file");
}
else {
console.log("Error updating file");
console.log(JSON.stringify(resp));
}
});
});
*/
class GithubFile {
constructor(token,username,email) {
this.USER = username;
this.EMAIL = email;
this.TOKEN = token;
}
async getFile(repo,filepath) {
var url = "https://api.github.com/repos/" + this.USER + "/" + repo + "/contents" + filepath;
var req = new WebRequest('GET',url);
var resp = await req.response;
return JSON.parse(resp.body);
}
async UpdateFile(repo,filepath,sha,msg,content) {
var post = {};
post['message'] = msg;
post['committer'] = {};
post['committer']['name'] = this.USER;
post['committer']['email'] = this.EMAIL;
post['content'] = btoa(content);
post['sha'] = sha;
var headers = {};
headers['Accept'] = 'application/vnd.github+json';
headers['Authorization'] = 'Bearer ' + this.TOKEN;
headers['X-GitHub-Api-Version'] = '2022-11-28';
var payload = {
headers : headers,
data : JSON.stringify(post)
};
var url = "https://api.github.com/repos/" + this.USER + "/" + repo + "/contents" + filepath;
var req = new WebRequest('PUT',url,payload);
return await req.response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment