Skip to content

Instantly share code, notes, and snippets.

@freegroup
Forked from StephanHoyer/github.js
Created April 14, 2022 12:06
Show Gist options
  • Select an option

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

Select an option

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

Revisions

  1. @StephanHoyer StephanHoyer revised this gist Feb 23, 2015. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion usage.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    'use strict';

    var github = require('./github');

    var api = github({
    @@ -12,4 +14,4 @@ api.commit([{
    }, {
    path: 'test/file2.md',
    content: '# File2'
    }], 'test commit via api');
    }], 'test commit via api'); // returns a promise
  2. @StephanHoyer StephanHoyer created this gist Feb 23, 2015.
    74 changes: 74 additions & 0 deletions github.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,74 @@
    'use strict';

    var Octokat = require('octokat');
    var extend = require('lodash/object/assign');

    var defaults = {
    branchName: 'master',
    token: '',
    username: '',
    reponame: ''
    };

    function init(options) {
    options = extend({}, defaults, options);
    var head;

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

    function fetchHead() {
    return repo.git.refs.heads(options.branchName).fetch();
    }

    function fetchTree() {
    return fetchHead().then(function(commit) {
    head = commit;
    return repo.git.trees(commit.object.sha).fetch();
    });
    }

    function commit(files, message) {
    return Promise.all(files.map(function(file) {
    return repo.git.blobs.create({
    content: file.content,
    encoding: 'utf-8'
    });
    })).then(function(blobs) {
    return fetchTree().then(function(tree) {
    return repo.git.trees.create({
    tree: files.map(function(file, index) {
    return {
    path: file.path,
    mode: '100644',
    type: 'blob',
    sha: blobs[index].sha
    };
    }),
    basetree: tree.sha
    });
    });
    }).then(function(tree) {
    return repo.git.commits.create({
    message: message,
    tree: tree.sha,
    parents: [
    head.object.sha
    ]
    });
    }).then(function(commit) {
    return repo.git.refs.heads(options.branchName).update({
    sha: commit.sha
    });
    });

    }

    return {
    commit: commit
    };
    }

    module.exports = init;
    15 changes: 15 additions & 0 deletions usage.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    var github = require('./github');

    var api = github({
    username: 'YOUR_USERNAME',
    token: 'API_TOKEN', // created here https://github.com/settings/applications#personal-access-tokens
    reponame: 'BRANCH_NAME'
    });

    api.commit([{
    path: 'test/file1.md',
    content: '# File1'
    }, {
    path: 'test/file2.md',
    content: '# File2'
    }], 'test commit via api');