Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

github-data

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github-data - npm Package Compare versions

Comparing version
0.0.1
to
0.0.2
+5
-0
CHANGELOG.md
# 0.0.1
- First release, containing all plumbing functionality, but little higher-level interfaces.
# 0.0.2
- Added branch.createBranch()
- Added branch.createPullRequest()

@@ -66,2 +66,30 @@ var Commit = require('./commit')

Branch.prototype.createBranch = function(name, callback) {
var me = this;
// This function requires a commit, so get one if we haven't yet.
me.getCommit(function(error, commit) {
me.gh.gitdata.createReference({
user: me.gh.user
, repo: me.gh.repo
, ref: 'refs/heads/' + name
, sha: commit.sha
}, function(error, referenceData) {
console.log(error);
var newBranch = new Branch(referenceData, me.gh);
callback(error, newBranch);
});
});
};
Branch.prototype.createPullRequest = function(base, title, body, callback) {
this.gh.pullRequests.create({
user: this.gh.user
, repo: this.gh.repo
, title: title
, body: body
, head: this.ref.split('/').pop()
, base: base.ref.split('/').pop()
}, callback);
};
/*

@@ -68,0 +96,0 @@ * Work in Progress

+1
-1
{
"name": "github-data",
"version": "0.0.1",
"version": "0.0.2",
"description": "Tools for running git commands without git.",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -1,82 +0,101 @@

/*
* This is just a brainstorm of what I want the API to look like. It doesn't
* actually work.
*/
var GitData = require('./index');
var username = process.env['GH_USERNAME'];
var authToken = process.env['GH_PASSWORD'];
var gdata = new GitData('username', 'password', 'organization', 'repository');
var gdata = new GitData(username, authToken, "numenta", "experiments");
/**
* Manually committing a file change.
*/
gdata.getBranch('master', function(error, master) {
master.getCommit(function(error, commit) {
commit.getTree(function(error, tree) {
tree.getBlob(path, function(error, blob) {
blob.setContents('new contents');
//commit.commit(blob, 'Commit message.', function(err, commit) {
//
//});
gdata.getBranch("master", function(err, master) {
console.log('branch: ' + master.ref);
// 1. Create new blob through api.
// 2. Given the new blob sha, walk up the chain of parents of
// old blob, which should be all trees up to a commit. For
// each parent tree:
// - create new tree with same tree objects except for the
// updated child sha, which should point to the new child
// object sha
// - check to see if current parent's parent is a tree
// - if tree: repeat #2 sub-tasks
// - if commit, proceed to #3
// 3. Create a new commit pointing to the topmost parent tree
// in the parent chain.
/* The short way to do it: */
master.getFile('temp.txt', function(err, file) {
console.log(file);
file.blob.setContents(file.blob.getContents() + '\nUpdated on: ' + new Date());
file.commit('Updated through GitFile interface.', function(err, commit) {
// Still have to push the new commit.
master.push(commit, function(err) {
console.log('push done!');
master.getCommit(function(error, latestCommit) {
console.log(latestCommit);
});
});
});
});
});
/**
* Changing a file by committing directly to the master branch.
*/
/**
* Same file change, but putting the changes in a pull request against the
* master branch.
*/
gdata.getBranch('master', function(error, master) {
master.getFile('path/to/file', function(error, file) {
// Update something within the file.
file.blob.setContents(file.blob.getContents().replace('foo', 'bar'));
// Commit the changes to the file.
file.commit('Commit message', function(error, commit) {
console.log('Created commit with SHA "%s"', commit.sha);
master.push(commit, function(error) {
console.log('Changes pushed.');
master.createBranch('feature-branch5', function(error, featureBranch) {
featureBranch.getFile('temp.txt', function(error, file) {
// Update something within the file.
file.blob.setContents(file.blob.getContents() + '\nUpdated on: ' + new Date());
// Commit the changes to the file.
file.commit('Updated through GitFile interface.', function(err, commit) {
console.log('Created commit with SHA "%s"', commit.sha);
featureBranch.push(commit, function(error) {
console.log(error);
featureBranch.createPullRequest(master, 'Automated PR test', 'Some hot body here.', function(error, pr) {
console.log('Created PR #%s', pr.number);
});
});
});
});
});
});
/**
* Same file change, but putting the changes in a pull request against the
* master branch.
*/
//
//gdata.getBranch('master', function(error, master) {
// master.createBranch('feature-branch', function(error, featureBranch) {
// featureBranch.getFile('path/to/file', function(error, file) {
// // Update something within the file.
// file.contents = fileObject.contents.replace('foo', 'bar');
// // Commit the changes to the file.
// file.commit('Commit message', function(error, commit) {
// console.log('Created commit with SHA "%s"', commit.sha);
// featureBranch.push(commit, function(error) {
// featureBranch.createPullRequest({
// title: 'PR title',
// body: 'optional',
// base: master
// }, function(error, pr) {
// console.log('Created PR #%s', pr.number);
// });
// });
// });
// });
// });
//});
/* The long manual way to do it: */
master.getCommit(function(err, commit) {
console.log('commit: ' + commit.sha);
commit.getTree(function(err, tree) {
console.log(tree);
tree.getBlob('temp.txt', function(err, blob) {
console.log(('old blob contents:\n' +
'========================================================' +
'\n%s\n' +
'========================================================'),
blob.getContents());
blob.setContents(blob.getContents() + '\nUpdated on: ' + new Date());
console.log(('new blob contents:\n' +
'========================================================' +
'\n%s\n' +
'========================================================'),
blob.getContents());
blob.update(function(err, blobData) {
console.log('new blob sha: ', blobData.sha);
tree.setBlob('temp.txt', blobData.sha);
tree.update(function(err, newTree) {
console.log('New Tree:');
console.log(newTree);
var message = 'This commit is being created through the GitHub Git Data API.';
commit.commitTree(newTree, message, function(err, newCommit) {
console.log('Commit created:');
console.log(newCommit);
master.push(newCommit, function(err) {
console.log('push done!');
master.getCommit(function(error, latestCommit) {
console.log(latestCommit);
});
});
});
});
});
});
});
});
});

@@ -5,2 +5,4 @@ # GitHub Data

[![NPM](https://nodei.co/npm/github-data.png?mini=true)](https://nodei.co/npm/github-data/)
This is essentially a Node.js interface to make low-level git calls through the [GitHub Git Data API](https://developer.github.com/v3/git/).

@@ -7,0 +9,0 @@

@@ -216,2 +216,75 @@ var fs = require('fs')

describe('when creating a new branch', function() {
it('uses current sha to create a new reference through API', function(done) {
var mockRefData = {
ref: 'refs/heads/branch-name'
, object: { sha: 'a075829d6b803ce74acf407b6d19e8434f1cf653'}
}
, mockCommit = {
sha: 'master-branch-commit-sha'
}
, mockGetCommit = function(callback) {
callback(null, mockCommit);
}
, mockClient = {
user: 'my-organization'
, repo: 'my-repository'
, gitdata: {
createReference: function(params, callback) {
expect(params.user).to.equal('my-organization');
expect(params.repo).to.equal('my-repository');
expect(params.ref).to.equal('refs/heads/branch-name');
expect(params.sha).to.equal('master-branch-commit-sha');
callback(null, mockRefData);
}
}
}
;
var branch = new Branch(mockRefMaster, mockClient);
branch.getCommit = mockGetCommit;
branch.createBranch('branch-name', function(error, newBranch) {
assert.notOk(error);
expect(newBranch).to.instanceOf(Branch);
expect(newBranch.ref).to.equal('refs/heads/branch-name');
expect(newBranch.sha).to.equal('a075829d6b803ce74acf407b6d19e8434f1cf653');
done();
});
});
});
describe('when creating a pull request', function() {
it('calls the github client properly', function(done) {
var mockBaseBranch = {
ref: 'refs/heads/feature'
}
, mockClient = {
user: 'my-organization'
, repo: 'my-repository'
, pullRequests: {
create: function(params, callback) {
expect(params.user).to.equal('my-organization');
expect(params.repo).to.equal('my-repository');
expect(params.title).to.equal('pr-title');
expect(params.body).to.equal('pr-body');
expect(params.head).to.equal('master');
expect(params.base).to.equal('feature');
callback(null, 'mock-pr-data');
}
}
}
, branch = new Branch(mockRefMaster, mockClient)
;
branch.createPullRequest(mockBaseBranch, 'pr-title', 'pr-body', function(error, prData) {
assert.notOk(error);
expect(prData).to.equal('mock-pr-data');
done();
});
});
});
});

@@ -26,2 +26,24 @@ var GitData = require('./index');

/**
* Same file change, but putting the changes in a pull request against the
* master branch.
*/
master.createBranch('feature-branch5', function(error, featureBranch) {
featureBranch.getFile('temp.txt', function(error, file) {
// Update something within the file.
file.blob.setContents(file.blob.getContents() + '\nUpdated on: ' + new Date());
// Commit the changes to the file.
file.commit('Updated through GitFile interface.', function(err, commit) {
console.log('Created commit with SHA "%s"', commit.sha);
featureBranch.push(commit, function(error) {
console.log(error);
featureBranch.createPullRequest(master, 'Automated PR test', 'Some hot body here.', function(error, pr) {
console.log('Created PR #%s', pr.number);
});
});
});
});
});
/* The long manual way to do it: */

@@ -73,3 +95,3 @@ master.getCommit(function(err, commit) {

});
});

@@ -76,0 +98,0 @@