
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
This package has moved to github-client
. Please update references to it.
Github.js provides a minimal higher-level wrapper around git's plumbing commands, exposing an API for manipulating GitHub repositories on the file level. It is being developed in the context of github-book, an EPUB3 editor for GitHub.
This package can also be used in nodejs or as a requirejs module.
Create a Github instance.
var github = new Github({
username: "YOU_USER",
password: "YOUR_PASSWORD",
auth: "basic"
});
Or if you prefer OAuth, it looks like this:
var github = new Github({
token: "OAUTH_TOKEN"
auth: "oauth"
});
define(['github-js'], function(Github) {
github = new Github({
username: "YOU_USER",
password: "YOUR_PASSWORD",
auth: "basic"
});
});
Install instructions:
npm install github-js
var Github = require('github-js');
var github = Github.new({
username: "YOU_USER",
password: "YOUR_PASSWORD",
auth: "basic"
});
var repo = github.getRepo(username, reponame);
Show repository information
repo.show()
.done(function(repo) {})
.fail(function(err) {});
Get contents at a particular path.
repo.contents("path/to/dir")
.done(function(contents) {});
Fork repository. This operation runs asynchronously. You may want to poll for repo.contents
until the forked repo is ready.
repo.fork()
.done(function() {})
.fail(function(err) {});
Create Pull Request.
var pull = {
title: message,
body: "This pull request has been automatically generated.",
base: "gh-pages",
head: "michael" + ":" + "prose-patch",
};
repo.createPullRequest(pull)
.done(function(pullRequest) {})
.fail(function(err) {});
Retrieve all available branches (aka heads) of a repository.
repo.listBranches()
.done(function(branches) {})
.fail(function(err) {});
Store contents at a certain path, where files that don't yet exist are created on the fly.
repo.write('master', 'path/to/file', 'YOUR_NEW_CONTENTS', 'YOUR_COMMIT_MESSAGE')
.done(function() {})
.fail(function(err) {});
Not only can you can write files, you can of course read them.
repo.read('master', 'path/to/file')
.done(function(data) {})
.fail(function(err) {});
You can also read binary files.
repo.readBinary('master', 'path/to/binaryfile')
.done(function(data) {})
.fail(function(err) {});
Move a file from A to B.
repo.move('master', 'path/to/file', 'path/to/new_file')
.done(function() {})
.fail(function(err) {});
Remove a file.
repo.remove('master', 'path/to/file')
.done(function() {})
.fail(function(err) {});
Exploring files of a repository is easy too by accessing the top level tree object.
repo.getTree('master')
.done(function(tree) {})
.fail(function(err) {});
If you want to access all blobs and trees recursively, you can add ?recursive=true
.
repo.getTree('master?recursive=true')
.done(function(tree) {})
.fail(function(err) {});
Given a filepath, retrieve the reference blob or tree sha.
repo.getSha('master', '/path/to/file')
.done(function(sha) {})
.fail(function(err) {});
For a given reference, get the corresponding commit sha.
repo.getRef('heads/master')
.done(function(sha) {})
.fail(function(err) {});
Create a new reference.
var refSpec = {
"ref": "refs/heads/my-new-branch-name",
"sha": "827efc6d56897b048c772eb4087f854f46256132"
};
repo.createRef(refSpec)
.done(function() {})
.fail(function(err) {});
Delete a reference.
repo.deleteRef('heads/gh-pages')
.done(function() {})
.fail(function(err) {});
var user = github.getUser();
List all repositories of the authenticated user.
user.repos(username)
.done(function(repos) {})
.fail(function(err) {});
List organizations the autenticated user belongs to.
user.orgs()
.done(function(orgs) {})
.fail(function(err) {});
List authenticated user's gists.
user.gists(username)
.done(function(gists) {})
.fail(function(err) {});
Show user information for a particular username. Also works for organizations.
user.show(username)
.done(function(user) {})
.fail(function(err) {});
List public repositories for a particular user.
user.userRepos(username)
.done(function(repos) {})
.fail(function(err) {});
List repositories for a particular organization. Includes private repositories if you are authorized.
user.orgRepos(orgname)
.done(function(repos) {})
.fail(function(err) {});
List all gists of a particular user. If username is ommitted gists of the current authenticated user are returned.
user.userGists(username)
.done(function(gists) {})
.fail(function(err) {});
var gist = github.getGist(3165654);
Read the contents of a Gist.
gist.read()
.done(function(gist) {})
.fail(function(err) {});
Updating the contents of a Git. Please consult the documentation on GitHub.
var delta = {
"description": "the description for this gist",
"files": {
"file1.txt": {
"content": "updated file contents"
},
"old_name.txt": {
"filename": "new_name.txt",
"content": "modified contents"
},
"new_file.txt": {
"content": "a new file"
},
"delete_this_file.txt": null
}
};
gist.update(delta)
.done(function(gist) {})
.fail(function(err) {});
##Setup
Github.js has the following dependencies:
Include these before github.js :
<script src="lib/underscore-min.js">
<script src="lib/base64.js">
<script src="github.js">
Switched to a native request
implementation (thanks @mattpass). Adds support for GitHub gists, forks and pull requests.
Adds support for organizations and fixes an encoding issue.
Smart caching of latest commit sha.
Added support for OAuth.
Support for Moving and removing files.
Consider commit messages.
Initial version.
FAQs
Javascript github client for nodejs or a web browser using promises
The npm package github-js receives a total of 1 weekly downloads. As such, github-js popularity was classified as not popular.
We found that github-js demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.