Skip to content

Instantly share code, notes, and snippets.

@neretin-trike
Last active February 14, 2018 08:51
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 neretin-trike/bff28315e7016a2a1d152bfcf714a72c to your computer and use it in GitHub Desktop.
Save neretin-trike/bff28315e7016a2a1d152bfcf714a72c to your computer and use it in GitHub Desktop.
JS-сниппеты
/*
Создание коммита на обновление файла с помощью Github API
*/
var dataJson = "";
$.ajax({
url: 'https://api.github.com/repos/neretin-trike/test_repo1/contents/7.txt',
type: 'GET',
}).done(function(response) {
var filecontent = "The data of the file";
var basecontent = Base64.encode(filecontent);
var commit = {
message: "Update file",
commiter:{
name: "neretin-trike",
email: "hawktrike@gmail.com"
},
content: basecontent,
sha: response.sha // Запрашивается хэш редактируемого файла
}
dataJson = JSON.stringify(commit);
UpdateFile();
});
function UpdateFile(){
$.ajax({
url: 'https://api.github.com/repos/neretin-trike/test_repo1/contents/7.txt',
type: 'PUT',
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "token YOUR-PRIVATE-TOKEN");
},
data: dataJson
}).done(function(response) {
console.log(response);
});
}
/*
Создание репозитория с помощью Github API
http://techslides.com/create-repositories-with-github-api-and-html5
*/
$.ajax({
url: 'https://api.github.com/authorizations',
type: 'POST',
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "Basic " + btoa("YOUR-USERNAME:YOUR-PASSWORD"));
},
data: '{"scopes":["repo"],"note":"create repo with ajax"}'
}).done(function(response) {
console.log(response);
});
$.ajax({
url: 'https://api.github.com/user/repos',
type: 'POST',
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "token TOKEN-FROM-PREVIOUS-CALL");
},
data: '{"name": "repo test","description":"repo create from ajax test","homepage": "https://sample.com","auto_init":true}'
}).done(function(response) {
console.log(response);
});
var filename = "firstfile.txt";
var filemessage = "uploading a file";
var filecontent = "The data of the file."
var basecontent = btoa(filecontent);
var apiurl = contents_url.replace('{+path}',filename);
var filedata = '{"message":"'+filemessage+'","content":"'+basecontent+'"}';
$.ajax({
url: apiurl,
type: 'PUT',
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "token TOKEN-FROM-PREVIOUS-CALL");
},
data: filedata
}).done(function(response) {
console.log(response);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment