├── .nvmrc ├── .gitignore ├── .eslintignore ├── package.json ├── README.MD ├── .eslintrc ├── test └── index.js └── index.js /.nvmrc: -------------------------------------------------------------------------------- 1 | 4.2.6 -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | .DS_Store -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | .DS_Store 3 | test/ -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "commit-to-github", 3 | "description": "make commits to github without git, perfect for AWS lambda", 4 | "version": "0.2.3", 5 | "repository": "github.com:Ramshackle-Jamathon/commit-to-github.git", 6 | "author": "Joseph Van Drunen ", 7 | "keywords": [ 8 | "git", 9 | "github", 10 | "api" 11 | ], 12 | "license": "MIT", 13 | "main": "index.js", 14 | "scripts": { 15 | "test": "eslint index.js example/ test/ && mocha test/" 16 | }, 17 | "devDependencies": { 18 | "mocha": "^3.0.2", 19 | "should": "^11.1.0", 20 | "eslint": "^3.10.2" 21 | }, 22 | "dependencies": { 23 | "debug": "^2.3.3", 24 | "github": "^9.0.0" 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /README.MD: -------------------------------------------------------------------------------- 1 | **Deprecated** use https://isomorphic-git.org/ instead 2 | 3 | # commit-to-github 4 | Uses the github API to make commits to github repos. lightweight and doesn't depend on Git. 5 | 6 | ## Installation 7 | 8 | ``` 9 | $ npm install commit-to-github 10 | ``` 11 | 12 | ## Example 13 | 14 | ```javascript 15 | commitToGithub({ 16 | user: "Ramshackle-Jamathon", 17 | repo: "commit-to-github", 18 | token: "", 19 | files: [ 20 | {path: "foo.txt", content: "nice words!"}, 21 | {path: "sick-folder/bar.txt", content: "you're the best!"}, 22 | ], 23 | fullyQualifiedRef : "heads/master", //optional default = "heads/dev" 24 | forceUpdate: true, //optional default = false 25 | commitMessage: "great work!" //option default = "AutoCommit - " + new Date().getTime().toString(); 26 | }).then(function(res){ 27 | // success! 28 | }).catch(function(err){ 29 | // oh no! something went wrong 30 | }) 31 | ``` 32 | 33 | ## Badges 34 | 35 | ![](https://img.shields.io/badge/license-MIT-blue.svg) 36 | ![](https://img.shields.io/badge/status-stable-green.svg) 37 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "parserOptions": { 3 | "ecmaVersion": 6, 4 | "sourceType": "module" 5 | }, 6 | "env": { 7 | "node": true, 8 | "mocha": true, 9 | "es6": true 10 | }, 11 | "rules": { 12 | // warnings 13 | "no-debugger": 1, 14 | "no-empty": 1, 15 | "no-invalid-regexp": 1, 16 | "radix": 1, 17 | "no-warning-comments": 1, 18 | "no-unused-expressions": 1, 19 | "no-native-reassign": 1, 20 | "no-fallthrough": 1, 21 | 22 | // errors 23 | "no-undef": 2, 24 | "no-dupe-keys": 2, 25 | "no-empty-character-class": 2, 26 | "no-self-compare": 2, 27 | "valid-typeof": 2, 28 | "no-unused-vars": 2, 29 | "handle-callback-err": 2, 30 | "no-shadow-restricted-names": 2, 31 | "no-unreachable": 2, 32 | "no-console": 2, 33 | "no-irregular-whitespace": 2, 34 | 35 | // stylistic errors 36 | "no-spaced-func": 2, 37 | "semi-spacing": 2, 38 | "space-unary-ops": 2, 39 | "brace-style": [2, "stroustrup", { "allowSingleLine": false }], 40 | "default-case": 2, 41 | "indent": [2, "tab", { "SwitchCase": 1 }], 42 | "new-cap": 0, // for things like Immutable.Map() 43 | "no-catch-shadow": 2, 44 | "no-eq-null": 2, 45 | "no-floating-decimal": 2, 46 | "no-inline-comments": 0, 47 | "no-nested-ternary": 2, 48 | "no-param-reassign": 0, 49 | "no-shadow": 0, 50 | "no-throw-literal": 2, 51 | "no-undefined": 2, 52 | "no-void": 2, 53 | 54 | // Node.js 55 | "no-restricted-modules": 2, // restrict usage of specified node modules (off by default) 56 | "no-sync": 2, // disallow use of synchronous methods (off by default) 57 | 58 | } 59 | } -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | var should = require("should"); 2 | var commitToGithub = require("..") 3 | 4 | describe("general", function() { 5 | it("should return on invalid params", function() { 6 | console.log(commitToGithub); 7 | var a = commitToGithub(); 8 | a.should.equal(""); 9 | 10 | var b = commitToGithub({}); 11 | b.should.equal(""); 12 | 13 | var c = commitToGithub({ 14 | user : "" 15 | }); 16 | c.should.equal(""); 17 | 18 | var d = commitToGithub({ 19 | repo : "" 20 | }); 21 | d.should.equal(""); 22 | 23 | var d = commitToGithub({ 24 | token : "" 25 | }); 26 | d.should.equal(""); 27 | 28 | var e = commitToGithub({ 29 | files : [] 30 | }); 31 | e.should.equal(""); 32 | 33 | var e = commitToGithub({ 34 | user : "", 35 | repo : "", 36 | token : "", 37 | files : [] 38 | }); 39 | e.should.equal(""); 40 | }); 41 | 42 | it("repo not found", function() { 43 | return commitToGithub({ 44 | user: "Ramshackle-Jamathon", 45 | repo: "repo-that-will-never-exist", 46 | files: [{path: "", content: ""}] 47 | }).catch(error => { 48 | error.should.exist; 49 | }); 50 | }); 51 | 52 | it("ref not found", function() { 53 | return commitToGithub({ 54 | user: "Ramshackle-Jamathon", 55 | repo: "repo-that-will-never-exist", 56 | files: [{path: "", content: ""}], 57 | fullyQualifiedRef : "badbranch" 58 | }).catch(error => { 59 | error.should.exist; 60 | }); 61 | }); 62 | 63 | it("invalid token", function() { 64 | return commitToGithub({ 65 | user: "Ramshackle-Jamathon", 66 | repo: "commit-to-github", 67 | files: [{path: "", content: ""}], 68 | fullyQualifiedRef : "heads/master", 69 | token: "invalid token" 70 | }).catch(error => { 71 | error.should.exist; 72 | }); 73 | }); 74 | }); 75 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var GitHubApi = require("github"); 2 | var debug = require("debug")("commit-to-github"); 3 | 4 | var getReferenceCommit = function (data){ 5 | return new Promise((resolve, reject) => { 6 | data.github.gitdata.getReference({ 7 | user: data.user, 8 | repo: data.repo, 9 | ref: data.fullyQualifiedRef 10 | }, (err, res) => { 11 | if (err) { 12 | debug("getReferenceCommit", JSON.stringify(err, null, " ")); 13 | return reject(err); 14 | } 15 | return resolve(Object.assign(data, { referenceCommitSha : res.object.sha})); 16 | }); 17 | }); 18 | } 19 | 20 | var createTree = function (data){ 21 | return new Promise((resolve, reject) => { 22 | var files = []; 23 | data.files.forEach((file) => { 24 | if(typeof file.path === "string" && typeof file.content === "string"){ 25 | files.push({ 26 | path: file.path, 27 | mode: "100644", 28 | type: "blob", 29 | content: file.content 30 | }); 31 | } 32 | }); 33 | 34 | data.github.gitdata.createTree({ 35 | user: data.user, 36 | repo: data.repo, 37 | tree: files, 38 | base_tree: data.referenceCommitSha 39 | }, (err, res) => { 40 | if (err) { 41 | debug("createTree", JSON.stringify(err, null, " ")); 42 | return reject(err); 43 | } 44 | return resolve(Object.assign(data, { newTreeSha : res.sha})); 45 | }); 46 | }); 47 | } 48 | 49 | var createCommit = function (data){ 50 | return new Promise((resolve, reject) => { 51 | data.github.gitdata.createCommit({ 52 | user: data.user, 53 | repo: data.repo, 54 | message: data.commitMessage, 55 | tree: data.newTreeSha, 56 | parents: [data.referenceCommitSha] 57 | }, (err, res) => { 58 | if (err) { 59 | debug("createCommit", JSON.stringify(err, null, " ")); 60 | return reject(err); 61 | } 62 | return resolve(Object.assign(data, { newCommitSha : res.sha})); 63 | }); 64 | }); 65 | } 66 | 67 | var updateRefrence = function (data){ 68 | return new Promise((resolve, reject) => { 69 | data.github.gitdata.updateReference({ 70 | user: data.user, 71 | repo: data.repo, 72 | ref: data.fullyQualifiedRef, 73 | sha: data.newCommitSha, 74 | force: data.forceUpdate, 75 | }, (err, data) => { 76 | if (err) { 77 | debug("updateRefrence", JSON.stringify(err, null, " ")); 78 | return reject(err); 79 | } 80 | return resolve(data); 81 | }); 82 | }); 83 | } 84 | 85 | module.exports = function(opts) { 86 | opts = opts || {}; 87 | if (!opts.user || !opts.repo || !opts.files || !opts.files.length){ 88 | return ""; 89 | } 90 | var data = {}; 91 | data.github = new GitHubApi(); 92 | if(opts.token){ 93 | data.github.authenticate({ 94 | type: "oauth", 95 | token: opts.token 96 | }); 97 | } 98 | data.user = opts.user; 99 | data.repo = opts.repo; 100 | data.files = opts.files; 101 | data.fullyQualifiedRef = opts.fullyQualifiedRef || "heads/dev"; 102 | data.forceUpdate = opts.forceUpdate || false; 103 | data.commitMessage = opts.commitMessage || 104 | "AutoCommit - " + new Date().getTime().toString(); 105 | 106 | return new Promise((resolve, reject) => { 107 | getReferenceCommit(data) 108 | .then(createTree) 109 | .then(createCommit) 110 | .then(updateRefrence) 111 | .then(resolve) 112 | .catch(error => reject(error)); 113 | }); 114 | }; --------------------------------------------------------------------------------