Skip to content

Instantly share code, notes, and snippets.

@freegroup
Forked from lilmuckers/commit.js
Created February 9, 2023 19:25
Show Gist options
  • Select an option

  • Save freegroup/e484baffc7d6d0e2e6d3e089e8a04b96 to your computer and use it in GitHub Desktop.

Select an option

Save freegroup/e484baffc7d6d0e2e6d3e089e8a04b96 to your computer and use it in GitHub Desktop.

Revisions

  1. @lilmuckers lilmuckers revised this gist Apr 8, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions example.js
    Original file line number Diff line number Diff line change
    @@ -2,8 +2,8 @@ var GitHub = require('githubber'),
    Commit = require('commit.js')

    //instantiate the Githubber github objects
    var ghObj = new GitHub.GitHub();
    var github = new GitHub.GitHubAPI(ghObj, 'accessToken');
    var ghObj = new GitHub.GitHub()
    var github = new GitHub.GitHubAPI(ghObj, 'accessToken')

    //instantiate the Commit object
    var commit = new Commit(github, 'lilmuckers', 'api-commits', 'heads/master')
  2. @lilmuckers lilmuckers revised this gist Apr 8, 2013. 1 changed file with 21 additions and 0 deletions.
    21 changes: 21 additions & 0 deletions example.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    var GitHub = require('githubber'),
    Commit = require('commit.js')

    //instantiate the Githubber github objects
    var ghObj = new GitHub.GitHub();
    var github = new GitHub.GitHubAPI(ghObj, 'accessToken');

    //instantiate the Commit object
    var commit = new Commit(github, 'lilmuckers', 'api-commits', 'heads/master')

    //I want to commit something!
    commit.commit(
    [{
    path: 'path/to/file.txt',
    content: 'Some content for this file'
    }],
    'The commit message',
    function(err, data){
    //data is the response from the reference update.
    }
    )
  3. @lilmuckers lilmuckers revised this gist Apr 7, 2013. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions commit.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@

    //set up the object with the api details
    function GitHubCommit(api, owner, repo, ref)
    {
    @@ -16,7 +15,7 @@ GitHubCommit.prototype.commit = function(fileData, commitMessage, callback)
    fileData is an array of objects of format:
    {
    path: 'path/to/file/in/repo.ext',
    contents: 'here is the file contents as a single string'
    content: 'here is the file contents as a single string'
    [ -- optional information that will be defaulted to the following --
    mode: '100644',
    type: 'blob'
  4. @lilmuckers lilmuckers created this gist Apr 7, 2013.
    174 changes: 174 additions & 0 deletions commit.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,174 @@

    //set up the object with the api details
    function GitHubCommit(api, owner, repo, ref)
    {
    this.api = api;
    this.repo = {
    owner: owner,
    repo: repo,
    ref: ref
    }
    }

    GitHubCommit.prototype.commit = function(fileData, commitMessage, callback)
    {
    /*
    fileData is an array of objects of format:
    {
    path: 'path/to/file/in/repo.ext',
    contents: 'here is the file contents as a single string'
    [ -- optional information that will be defaulted to the following --
    mode: '100644',
    type: 'blob'
    ]
    }
    */
    //set defaults on the file data
    fileData = this._cleanFileData(fileData);

    //step 1 - cut a hole in the box
    //and by that i mean get the latest commit information
    this.api.git.refs.info(
    this.repo.owner,
    this.repo.repo,
    this.repo.ref,
    this._initialRefsResponse(fileData, commitMessage, callback)
    );
    }

    GitHubCommit.prototype._initialRefsResponse = function(fileData, commitMessage, callback)
    {
    //the response handler from step 1
    return function(err, data){
    //handle any errors sanely
    if(err){
    callback(err);
    return;
    }

    //step 1.5 - keep cutting the hole smooth like
    //and by that i mean get the tree information
    this.api.git.commits.info(
    this.repo.owner,
    this.repo.repo,
    data.object.sha,
    this._initialTreeResponse(fileData, commitMessage, data.object.sha, callback)
    );
    }.bind(this);
    }

    GitHubCommit.prototype._initialTreeResponse = function(fileData, commitMessage, parentCommitSha, callback)
    {
    //the response handler from step 1.5
    return function(err, data){
    //handle any errors sanely
    if(err){
    callback(err);
    return;
    }
    //build the blob information object
    var blobInfo = {
    base_tree: data.tree.sha,
    tree: fileData
    }

    //step 2 - put your junk in the box
    //and by that i mean squirt the changes into a new tree in github
    this.api.git.trees.create(
    this.repo.owner,
    this.repo.repo,
    blobInfo,
    this._createTreeResponse(commitMessage, parentCommitSha, callback)
    );

    }.bind(this);
    }

    GitHubCommit.prototype._createTreeResponse = function(commitMessage, parentCommitSha, callback)
    {
    //the response handler from step 2
    return function(err, data){
    //handle any errors sanely
    if(err){
    callback(err);
    return;
    }

    //build the commit information
    var commitInfo = {
    message: commitMessage,
    tree: data.sha,
    parents: [parentCommitSha]
    }

    //step 3 - have her open the box
    //and by that i mean create the new commit for this tree
    this.api.git.commits.create(
    this.repo.owner,
    this.repo.repo,
    commitInfo,
    this._createCommitResponse(callback)
    );

    }.bind(this);
    }

    GitHubCommit.prototype._createCommitResponse = function(callback)
    {
    //the response handler from step 3
    return function(err, data){
    //handle any errors sanely
    if(err){
    callback(err);
    return;
    }

    //set up the reference data for the change to the tree
    var refInfo = {
    sha: data.sha
    }

    //and that's the way we do it
    this.api.git.refs.update(
    this.repo.owner,
    this.repo.repo,
    this.repo.ref,
    refInfo,
    callback
    );

    }.bind(this);
    }

    GitHubCommit.prototype._cleanFileData = function(fileData)
    {
    //initialise the array
    var returnArray = [];

    //set the blob type and the file type for the blob if none are set
    for( var k in fileData){
    var file = fileData[k];
    var newFile = {}

    if(file.content && file.path){
    newFile.content = file.content;
    newFile.path = file.path;
    }

    if(!file.blob){
    newFile.blob = '100644';
    } else {
    newFile.blob = file.blob;
    }
    if(!file.type){
    newFile.type = 'blob';
    } else {
    newFile.type = file.type;
    }
    returnArray.push(newFile);
    }

    return returnArray;
    }

    module.exports = GitHubCommit;