philschatz / octokat.js

:octocat: Github API Client using Promises or callbacks. Intended for the browser or NodeJS.
http://philschatz.com/2014/05/25/octokat/
MIT License
421 stars 133 forks source link

update README.md to show how to _get_ the blob sha of the previous commit. #269

Open Pomax opened 6 years ago

Pomax commented 6 years ago

Right now under the read/write/update section of the readme, there is the following text:

To update a file you need the blob SHA of the previous commit:

but the code that follows does not show how to get that SHA at all, it just shows a meaningless dummy sha. Can that example be updated to show how to actually get the previous commit sha? (Do we use fetch? do we call something on commits? I can't tell =S)

(Also, the section is titled read/write/update but the examples shown only show off reading and updating. Plain writing does not appear as one of the examples)

dhananjayharel commented 4 years ago

One way you can do this to get the last commit of the specific file and then use that sha

var octo = new Octokat({
    username: 'username',
    password: 'password'
});

var repo = octo.repos('username', 'repoName');
repo.contents('somefile.txt').fetch(function(err, info) {
    if (err) {
        console.log(err);
    } else {
        console.log('somefile.txt's SHA number is: ' + info.sha);
    }
});

Example Using promise =>


var Octokat = require('octokat');
var base64 = require('base-64');

var octo = new Octokat({token: 'your token'})
var repo = octo.repos('username', 'reponame');

repo.contents('somefile.txt').fetch().then((info) =>{
var config = {
  message: 'Updating somefile.txt',
  content: base64.encode('new content'),
  sha:info.sha
}

repo.contents('somefile.txt').add(config)
.then((info) => {
  console.log('File Updated. new sha is ', info);

})}).catch(function(err){console.log("err"+err)});
Pomax commented 4 years ago

Could you turn that into a PR against the README.md?