Giter VIP home page Giter VIP logo

octokit.js's Introduction

octokit.js

The all-batteries-included GitHub SDK for Browsers, Node.js, and Deno.

The octokit package integrates the three main Octokit libraries

  1. API client (REST API requests, GraphQL API queries, Authentication)
  2. App client (GitHub App & installations, Webhooks, OAuth)
  3. Action client (Pre-authenticated API client for single repository)

Table of contents

Features

  • Complete. All features of GitHub's platform APIs are covered.
  • Prescriptive. All recommended best practices are implemented.
  • Universal. Works in all modern browsers, Node.js, and Deno.
  • Tested. All libraries have a 100% test coverage.
  • Typed. All libraries have extensive TypeScript declarations.
  • Decomposable. Use only the code you need. You can build your own Octokit in only a few lines of code or use the underlying static methods. Make your own tradeoff between functionality and bundle size.
  • Extendable. A feature missing? Add functionalities with plugins, hook into the request or webhook lifecycle or implement your own authentication strategy.

Usage

Browsers Load octokit directly from esm.sh
<script type="module">
import { Octokit, App } from "https://esm.sh/octokit";
</script>
Deno Load octokit directly from esm.sh
import { Octokit, App } from "https://esm.sh/octokit?dts";
Node 12+

Install with npm/pnpm install octokit, or yarn add octokit

import { Octokit, App } from "octokit";
Node 10 and below

Install with npm/pnpm install octokit, or yarn add octokit

const { Octokit, App } = require("octokit");

Octokit API Client

standalone minimal Octokit: @octokit/core.

The Octokit client can be used to send requests to GitHub's REST API and queries to GitHub's GraphQL API.

Example: Get the username for the authenticated user.

// Create a personal access token at https://github.com/settings/tokens/new?scopes=repo
const octokit = new Octokit({ auth: `personal-access-token123` });

// Compare: https://docs.github.com/en/rest/reference/users#get-the-authenticated-user
const {
  data: { login },
} = await octokit.rest.users.getAuthenticated();
console.log("Hello, %s", login);

Constructor options

The most commonly used options are

name type description
userAgent String

Setting a user agent is required for all requests sent to GitHub's Platform APIs. The user agent defaults to something like this: octokit.js/v1.2.3 Node.js/v8.9.4 (macOS High Sierra; x64). It is recommend to set your own user agent, which will prepend the default one.

const octokit = new Octokit({
  userAgent: "my-app/v1.2.3",
});
authStrategy Function

Defaults to @octokit/auth-token.

See Authentication below.

auth String or Object

Set to a personal access token unless you changed the authStrategy option.

See Authentication below.

baseUrl String

When using with GitHub Enterprise Server, set options.baseUrl to the root URL of the API. For example, if your GitHub Enterprise Server's hostname is github.acme-inc.com, then set options.baseUrl to https://github.acme-inc.com/api/v3. Example

const octokit = new Octokit({
  baseUrl: "https://github.acme-inc.com/api/v3",
});

Advanced options

name type description
request Object

Node only

  • request.timeout sets a request timeout, defaults to 0

The request option can also be set on a per-request basis.

timeZone String

Sets the Time-Zone header which defines a timezone according to the list of names from the Olson database.

const octokit = new Octokit({
  timeZone: "America/Los_Angeles",
});

The time zone header will determine the timezone used for generating the timestamp when creating commits. See GitHub's Timezones documentation.

throttle Object

Octokit implements request throttling using @octokit/plugin-throttling

By default, requests are retried once and warnings are logged in case of hitting a rate or secondary rate limit.

{
  onRateLimit: (retryAfter, options, octokit) => {
    octokit.log.warn(
      `Request quota exhausted for request ${options.method} ${options.url}`
    );

    if (options.request.retryCount === 0) {
      // only retries once
      octokit.log.info(`Retrying after ${retryAfter} seconds!`);
      return true;
    }
  },
  onSecondaryRateLimit: (retryAfter, options, octokit) => {
    octokit.log.warn(
      `SecondaryRateLimit detected for request ${options.method} ${options.url}`
    );

    if (options.request.retryCount === 0) {
      // only retries once
      octokit.log.info(`Retrying after ${retryAfter} seconds!`);
      return true;
    }
  },
};

To opt-out of this feature:

new Octokit({ throttle: { enabled: false } });

Throttling in a cluster is supported using a Redis backend. See @octokit/plugin-throttling Clustering

retry Object

Octokit implements request retries using @octokit/plugin-retry

To opt-out of this feature:

new Octokit({ retry: { enabled: false } });

Authentication

By default, the Octokit API client supports authentication using a static token.

There are different means of authentication that are supported by GitHub, that are described in detail at octokit/authentication-strategies.js. You can set each of them as the authStrategy constructor option, and pass the strategy options as the auth constructor option.

For example, in order to authenticate as a GitHub App Installation:

import { createAppAuth } from "@octokit/auth-app";
const octokit = new Octokit({
  authStrategy: createAppAuth,
  auth: {
    appId: 1,
    privateKey: "-----BEGIN PRIVATE KEY-----\n...",
    installationId: 123,
  },
});

// authenticates as app based on request URLs
const {
  data: { slug },
} = await octokit.rest.apps.getAuthenticated();

// creates an installation access token as needed
// assumes that installationId 123 belongs to @octocat, otherwise the request will fail
await octokit.rest.issues.create({
  owner: "octocat",
  repo: "hello-world",
  title: "Hello world from " + slug,
});

You can use the App or OAuthApp SDKs which provide APIs and internal wiring to cover most use cases.

For example, to implement the above using App

const app = new App({ appId, privateKey });
const { data: slug } = await app.octokit.rest.apps.getAuthenticated();
const octokit = await app.getInstallationOctokit(123);
await octokit.rest.issues.create({
  owner: "octocat",
  repo: "hello-world",
  title: "Hello world from " + slug,
});

Learn more about how authentication strategies work or how to create your own.

Proxy Servers (Node.js only)

By default, the Octokit API client does not make use of the standard proxy server environment variables. To add support for proxy servers you will need to provide an https client that supports them such as undici.ProxyAgent().

For example, this would use a ProxyAgent to make requests through a proxy server:

import { fetch as undiciFetch, ProxyAgent } from 'undici';

const myFetch = (url, options) => {
  return undiciFetch(url, {
    ...options,
    dispatcher: new ProxyAgent(<your_proxy_url>)
  })
}

const octokit = new Octokit({
  request: {
     fetch: myFetch
  },
});

If you are writing a module that uses Octokit and is designed to be used by other people, you should ensure that consumers can provide an alternative agent for your Octokit or as a parameter to specific calls such as:

import { fetch as undiciFetch, ProxyAgent } from 'undici';

const myFetch = (url, options) => {
  return undiciFetch(url, {
    ...options,
    dispatcher: new ProxyAgent(<your_proxy_url>)
  })
}

octokit.rest.repos.get({
  owner,
  repo,
  request: {
    fetch: myFetch
  },
});

Fetch missing

If you get the following error:

fetch is not set. Please pass a fetch implementation as new Octokit({ request: { fetch }}).

It probably means you are trying to run Octokit with an unsupported version of NodeJS. Octokit requires Node 18 or higher, which includes a native fetch API.

To bypass this problem you can provide your own fetch implementation (or a built-in version like node-fetch) like this:

import fetch from "node-fetch";

const octokit = new Octokit({
  request: {
    fetch: fetch,
  },
});

REST API

There are two ways of using the GitHub REST API, the octokit.rest.* endpoint methods and octokit.request. Both act the same way, the octokit.rest.* methods are just added for convenience, they use octokit.request internally.

For example

await octokit.rest.issues.create({
  owner: "octocat",
  repo: "hello-world",
  title: "Hello, world!",
  body: "I created this issue using Octokit!",
});

Is the same as

await octokit.request("POST /repos/{owner}/{repo}/issues", {
  owner: "octocat",
  repo: "hello-world",
  title: "Hello, world!",
  body: "I created this issue using Octokit!",
});

In both cases a given request is authenticated, retried, and throttled transparently by the octokit instance which also manages the accept and user-agent headers as needed.

octokit.request can be used to send requests to other domains by passing a full URL and to send requests to endpoints that are not (yet) documented in GitHub's REST API documentation.

octokit.rest endpoint methods

Every GitHub REST API endpoint has an associated octokit.rest endpoint method for better code readability and developer convenience. See @octokit/plugin-rest-endpoint-methods for full details.

Example: Create an issue

await octokit.rest.issues.create({
  owner: "octocat",
  repo: "hello-world",
  title: "Hello, world!",
  body: "I created this issue using Octokit!",
});

The octokit.rest endpoint methods are generated automatically from GitHub's OpenAPI specification. We track operation ID and parameter name changes in order to implement deprecation warnings and reduce the frequency of breaking changes.

Under the covers, every endpoint method is just octokit.request with defaults set, so it supports the same parameters as well as the .endpoint() API.

octokit.request()

You can call the GitHub REST API directly using octokit.request. The request API matches GitHub's REST API documentation 1:1 so anything you see there, you can call using request. See @octokit/request for all the details.

Example: Create an issue

Screenshot of REST API reference documentation for Create an issue

The octokit.request API call corresponding to that issue creation documentation looks like this:

// https://docs.github.com/en/rest/reference/issues#create-an-issue
await octokit.request("POST /repos/{owner}/{repo}/issues", {
  owner: "octocat",
  repo: "hello-world",
  title: "Hello, world!",
  body: "I created this issue using Octokit!",
});

The 1st argument is the REST API route as listed in GitHub's API documentation. The 2nd argument is an object with all parameters, independent of whether they are used in the path, query, or body.

Pagination

All REST API endpoints that paginate return the first 30 items by default. If you want to retrieve all items, you can use the pagination API. The pagination API expects the REST API route as first argument, but you can also pass any of the octokit.rest.*.list* methods for convenience and better code readability.

Example: iterate through all issues in a repository

const iterator = octokit.paginate.iterator(octokit.rest.issues.listForRepo, {
  owner: "octocat",
  repo: "hello-world",
  per_page: 100,
});

// iterate through each response
for await (const { data: issues } of iterator) {
  for (const issue of issues) {
    console.log("Issue #%d: %s", issue.number, issue.title);
  }
}

Using the async iterator is the most memory efficient way to iterate through all items. But you can also retrieve all items in a single call

const issues = await octokit.paginate(octokit.rest.issues.listForRepo, {
  owner: "octocat",
  repo: "hello-world",
  per_page: 100,
});

Media Type formats

Media type formats can be set using mediaType: { format } on every request.

Example: retrieve the raw content of a package.json file

const { data } = await octokit.rest.repos.getContent({
  mediaType: {
    format: "raw",
  },
  owner: "octocat",
  repo: "hello-world",
  path: "package.json",
});
console.log("package name: %s", JSON.parse(data).name);

Learn more about Media type formats.

Request error handling

Standalone module: @octokit/request-error

For request error handling, import RequestError and use try...catch statement.

import { RequestError } from "octokit";
try {
  // your code here that sends at least one Octokit request
  await octokit.request("GET /");
} catch (error) {
  // Octokit errors are instances of RequestError, so they always have an `error.status` property containing the HTTP response code.
  if (error instanceof RequestError) {
    // handle Octokit error
    // error.message; // Oops
    // error.status; // 500
    // error.request; // { method, url, headers, body }
    // error.response; // { url, status, headers, data }
  } else {
    // handle all other errors
    throw error;
  }
}

GraphQL API queries

Octokit also supports GitHub's GraphQL API directly -- you can use the same queries shown in the documentation and available in the GraphQL explorer in your calls with octokit.graphql.

Example: get the login of the authenticated user

const {
  viewer: { login },
} = await octokit.graphql(`{
  viewer {
    login
  }
}`);

Variables can be passed as 2nd argument

const { lastIssues } = await octokit.graphql(
  `
    query lastIssues($owner: String!, $repo: String!, $num: Int = 3) {
      repository(owner: $owner, name: $repo) {
        issues(last: $num) {
          edges {
            node {
              title
            }
          }
        }
      }
    }
  `,
  {
    owner: "octokit",
    repo: "graphql.js",
  },
);

Pagination

GitHub's GraphQL API returns a maximum of 100 items. If you want to retrieve all items, you can use the pagination API.

Example: get all issues

const { allIssues } = await octokit.graphql.paginate(
  `
    query allIssues($owner: String!, $repo: String!, $num: Int = 10, $cursor: String) {
      repository(owner: $owner, name: $repo) {
        issues(first: $num, after: $cursor) {
          edges {
            node {
              title
            }
          }
          pageInfo {
            hasNextPage
            endCursor
          }
        }
      }
    }
  `,
  {
    owner: "octokit",
    repo: "graphql.js",
  },
);

Learn more about GitHub's GraphQL Pagination usage.

Schema previews

Previews can be enabled using the {mediaType: previews: [] } option.

Example: create a label

await octokit.graphql(
  `mutation createLabel($repositoryId:ID!,name:String!,color:String!) {
  createLabel(input:{repositoryId:$repositoryId,name:$name}) {
    label: {
      id
    }
  }
}`,
  {
    repositoryId: 1,
    name: "important",
    color: "cc0000",
    mediaType: {
      previews: ["bane"],
    },
  },
);

Learn more about GitHub's GraphQL schema previews

App client

The App client combines features for GitHub Apps, Webhooks, and OAuth

GitHub App

Standalone module: @octokit/app

For integrators, GitHub Apps are a means of authentication and authorization. A GitHub app can be registered on a GitHub user or organization account. A GitHub App registration defines a set of permissions and webhooks events it wants to receive and provides a set of credentials in return. Users can grant access to repositories by installing them.

Some API endpoints require the GitHub app to authenticate as itself using a JSON Web Token (JWT). For requests affecting an installation, an installation access token has to be created using the app's credentials and the installation ID.

The App client takes care of all that for you.

Example: Dispatch a repository event in every repository the app is installed on

import { App } from "octokit";

const app = new App({ appId, privateKey });

for await (const { octokit, repository } of app.eachRepository.iterator()) {
  // https://docs.github.com/en/rest/reference/repos#create-a-repository-dispatch-event
  await octokit.rest.repos.createDispatchEvent({
    owner: repository.owner.login,
    repo: repository.name,
    event_type: "my_event",
    client_payload: {
      foo: "bar",
    },
  });
  console.log("Event dispatched for %s", repository.full_name);
}

Example: Get an octokit instance authenticated as an installation

const octokit = await app.getInstallationOctokit(123);

Learn more about apps.

Webhooks

Standalone module: @octokit/webhooks

When installing an app, events that the app registration requests will be sent as requests to the webhook URL set in the app's registration.

Webhook event requests are signed using the webhook secret, which is also part of the app's registration. You must verify that secret before handling the request payload.

The app.webhooks.* APIs provide methods to receiving, verifying, and handling webhook events.

Example: create a comment on new issues

import { createServer } from "node:http";
import { App, createNodeMiddleware } from "octokit";

const app = new App({
  appId,
  privateKey,
  webhooks: { secret },
});

app.webhooks.on("issues.opened", ({ octokit, payload }) => {
  return octokit.rest.issues.createComment({
    owner: payload.repository.owner.login,
    repo: payload.repository.name,
    body: "Hello, World!",
  });
});

// Your app can now receive webhook events at `/api/github/webhooks`
createServer(createNodeMiddleware(app)).listen(3000);

For serverless environments, you can explicitly verify and receive an event

await app.webhooks.verifyAndReceive({
  id: request.headers["x-github-delivery"],
  name: request.headers["x-github-event"],
  signature: request.headers["x-hub-signature-256"],
  payload: request.body,
});

Learn more about GitHub webhooks.

OAuth

Standalone module: @octokit/oauth-app

Both OAuth Apps and GitHub Apps support authenticating GitHub users using OAuth, see Authorizing OAuth Apps and Identifying and authorizing users for GitHub Apps.

There are some differences:

  • Only OAuth Apps support scopes. GitHub apps have permissions, and access is granted via installations of the app on repositories.
  • Only GitHub Apps support expiring user tokens
  • Only GitHub Apps support creating a scoped token to reduce the permissions and repository access

App is for GitHub Apps. If you need OAuth App-specific functionality, use OAuthApp instead.

Example: Watch a repository when a user logs in using the OAuth web flow

import { createServer } from "node:http";
import { App, createNodeMiddleware } from "octokit";

const app = new App({
  oauth: { clientId, clientSecret },
});

app.oauth.on("token.created", async ({ token, octokit }) => {
  await octokit.rest.activity.setRepoSubscription({
    owner: "octocat",
    repo: "hello-world",
    subscribed: true,
  });
});

// Your app can receive the OAuth redirect at /api/github/oauth/callback
// Users can initiate the OAuth web flow by opening /api/oauth/login
createServer(createNodeMiddleware(app)).listen(3000);

For serverless environments, you can explicitly exchange the code from the OAuth web flow redirect for an access token. app.oauth.createToken() returns an authentication object and emits the "token.created" event.

const { token } = await app.oauth.createToken({
  code: request.query.code,
});

Example: create a token using the device flow.

const { token } = await app.oauth.createToken({
  async onVerification(verification) {
    await sendMessageToUser(
      request.body.phoneNumber,
      `Your code is ${verification.user_code}. Enter it at ${verification.verification_uri}`,
    );
  },
});

Example: Create an OAuth App Server with default scopes

import { createServer } from "node:http";
import { OAuthApp, createNodeMiddleware } from "octokit";

const app = new OAuthApp({
  clientId,
  clientSecret,
  defaultScopes: ["repo", "gist"],
});

app.oauth.on("token", async ({ token, octokit }) => {
  await octokit.rest.gists.create({
    description: "I created this gist using Octokit!",
    public: true,
    files: {
      "example.js": `/* some code here */`,
    },
  });
});

// Your app can receive the OAuth redirect at /api/github/oauth/callback
// Users can initiate the OAuth web flow by opening /api/oauth/login
createServer(createNodeMiddleware(app)).listen(3000);

App Server

After registering your GitHub app, you need to create and deploy a server which can retrieve the webhook event requests from GitHub as well as accept redirects from the OAuth user web flow.

The simplest way to create such a server is to use createNodeMiddleware(), it works with both, Node's http.createServer() method as well as an Express middleware.

The default routes that the middleware exposes are

Route Route Description
POST /api/github/webhooks Endpoint to receive GitHub Webhook Event requests
GET /api/github/oauth/login Redirects to GitHub's authorization endpoint. Accepts optional ?state and ?scopes query parameters. ?scopes is a comma-separated list of supported OAuth scope names
GET /api/github/oauth/callback The client's redirect endpoint. This is where the token event gets triggered
POST /api/github/oauth/token Exchange an authorization code for an OAuth Access token. If successful, the token event gets triggered.
GET /api/github/oauth/token Check if token is valid. Must authenticate using token in Authorization header. Uses GitHub's POST /applications/{client_id}/token endpoint
PATCH /api/github/oauth/token Resets a token (invalidates current one, returns new token). Must authenticate using token in Authorization header. Uses GitHub's PATCH /applications/{client_id}/token endpoint.
PATCH /api/github/oauth/refresh-token Refreshes an expiring token (invalidates current one, returns new access token and refresh token). Must authenticate using token in Authorization header. Uses GitHub's POST https://github.com/login/oauth/access_token OAuth endpoint.
POST /api/github/oauth/token/scoped Creates a scoped token (does not invalidate the current one). Must authenticate using token in Authorization header. Uses GitHub's POST /applications/{client_id}/token/scoped endpoint.
DELETE /api/github/oauth/token Invalidates current token, basically the equivalent of a logout. Must authenticate using token in Authorization header.
DELETE /api/github/oauth/grant Revokes the user's grant, basically the equivalent of an uninstall. must authenticate using token in Authorization header.

Example: create a GitHub server with express

import express from "express";
import { App, createNodeMiddleware } from "octokit";

const expressApp = express();
const octokitApp = new App({
  appId,
  privateKey,
  webhooks: { secret },
  oauth: { clientId, clientSecret },
});

expressApp.use(createNodeMiddleware(app));

expressApp.listen(3000, () => {
  console.log(`Example app listening at http://localhost:3000`);
});

OAuth for browser apps

You must not expose your app's client secret to the user, so you cannot use the App constructor. Instead, you have to create a server using the App constructor which exposes the /api/github/oauth/* routes, through which you can safely implement an OAuth login for apps running in a web browser.

If you set (User) Authorization callback URL to your own app, than you need to read out the ?code=...&state=... query parameters, compare the state parameter to the value returned by app.oauthLoginUrl() earlier to protect against forgery attacks, then exchange the code for an OAuth Authorization token.

If you run an app server as described above, the default route to do that is POST /api/github/oauth/token.

Once you successfully retrieved the token, it is also recommended to remove the ?code=...&state=... query parameters from the browser's URL

const code = new URL(location.href).searchParams.get("code");
if (code) {
  // remove ?code=... from URL
  const path =
    location.pathname +
    location.search.replace(/\b(code|state)=\w+/g, "").replace(/[?&]+$/, "");
  history.replaceState({}, "", path);

  // exchange the code for a token with your backend.
  // If you use https://github.com/octokit/oauth-app.js
  // the exchange would look something like this
  const response = await fetch("/api/github/oauth/token", {
    method: "POST",
    headers: {
      "content-type": "application/json",
    },
    body: JSON.stringify({ code }),
  });
  const { token } = await response.json();
  // `token` is the OAuth Access Token that can be use

  const { Octokit } = await import("https://esm.sh/@octokit/core");
  const octokit = new Octokit({ auth: token });

  const {
    data: { login },
  } = await octokit.request("GET /user");
  alert("Hi there, " + login);
}

🚧 We are working on @octokit/auth-oauth-user-client to provide a simple API for all methods related to OAuth user tokens.

The plan is to add an new GET /api/github/oauth/octokit.js route to the node middleware which will return a JavaScript file that can be imported into an HTML file. It will make a pre-authenticated octokit Instance available.

Action client

standalone module: @octokit/action

🚧 A fully fledged Action client is pending. You can use @actions/github for the time being

LICENSE

MIT

octokit.js's People

Contributors

bricker avatar bxt avatar c0zen avatar cadienvan avatar christophehurpeau avatar dariuszporowski avatar dependabot[bot] avatar devidw avatar emmanueldemey avatar fetsorn avatar g-rath avatar gimenete avatar gr2m avatar jajargaming avatar jeffmcaffer avatar kfcampbell avatar kylecartmell avatar marcjansen avatar martii avatar mikesurowiec avatar moe82 avatar nickfloyd avatar nimadini avatar octokitbot avatar oscard0m avatar pabhishek09 avatar renovate[bot] avatar tetov avatar unalterable avatar wolfy1339 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

octokit.js's Issues

Can't retrieve multiple pages with same GitHubApi instance (404 error)

Hello! Hello!

When I try to fetch multiple pages from a repo, the second page returns a 404. However, if I get a new instance of GitHubApi for each page, it works spiffy.

Example that fails:

var _      = require('underscore');
var GitHubApi = require('node-github');
var github = new GitHubApi({version: '3.0.0'});

function getRepos(orgname, page) {
   page || (page = 1);
   github.repos.getFromOrg({org: orgname, per_page: 2, page: page}, function(err, data) {
      if( err ) {
         console.error('ERROR', 'page: '+page, err, data);
      }
      else {
         console.log('page: '+page);
         console.log(_.pluck(data, 'name'));
         console.log(data.meta);
         if( data && data.meta && data.length ) {
            getRepos(orgname, page+1);
         }
      }
   });
}
getRepos('Zenovations');

Result:

C:\Users\a93453\Documents\GitHub\git-stats [master +0 ~3 -0]> node .\muck.js
page: 1
[ 'jtype', 'mousey' ]
{ 'x-ratelimit-limit': '5000',
  'x-ratelimit-remaining': '4925',
  link: '<https://api.github.com/orgs/Zenovations/repos?page=2&per_page=2>; rel="next", <https://api.github.com/orgs/Zen
ovations/repos?page=4&per_page=2>; rel="last"' }
[error] { message: '{"message":"Not Found"}', code: 404 } null undefined
ERROR page: 2 { message: '{"message":"Not Found"}', code: 404 } undefined

Example that works

var _      = require('underscore');
var GitHubApi = require('node-github');
//var github = new GitHubApi({version: '3.0.0'});

function getRepos(orgname, page) {
   page || (page = 1);
   new GitHubApi({version: '3.0.0'}).repos.getFromOrg({org: orgname, per_page: 2, page: page}, function(err, data) {
      if( err ) {
         console.error('ERROR', 'page: '+page, err, data);
      }
      else {
         console.log('page: '+page);
         console.log(_.pluck(data, 'name'));
         console.log(data.meta);
         if( data && data.meta && data.length ) {
            getRepos(orgname, page+1);
         }
      }
   });
}
getRepos('Zenovations');

Result:

C:\Users\a93453\Documents\GitHub\git-stats [master +0 ~3 -0]> node .\muck.js
page: 1
[ 'jtype', 'mousey' ]
{ 'x-ratelimit-limit': '5000',
  'x-ratelimit-remaining': '4999',
  link: '<https://api.github.com/orgs/Zenovations/repos?page=2&per_page=2>; rel="next", <https://api.github.com/orgs/Zen
ovations/repos?page=4&per_page=2>; rel="last"' }
page: 2
[ 'jquery-sequence', 'console-normalizer' ]
{ 'x-ratelimit-limit': '5000',
  'x-ratelimit-remaining': '4993',
  link: '<https://api.github.com/orgs/Zenovations/repos?page=3&per_page=2>; rel="next", <https://api.github.com/orgs/Zen
ovations/repos?page=4&per_page=2>; rel="last", <https://api.github.com/orgs/Zenovations/repos?page=1&per_page=2>; rel="f
irst", <https://api.github.com/orgs/Zenovations/repos?page=1&per_page=2>; rel="prev"' }
page: 3
[ 'knockout-sync', 'slidepanels' ]
{ 'x-ratelimit-limit': '5000',
  'x-ratelimit-remaining': '4992',
  link: '<https://api.github.com/orgs/Zenovations/repos?page=4&per_page=2>; rel="next", <https://api.github.com/orgs/Zen
ovations/repos?page=4&per_page=2>; rel="last", <https://api.github.com/orgs/Zenovations/repos?page=1&per_page=2>; rel="f
irst", <https://api.github.com/orgs/Zenovations/repos?page=2&per_page=2>; rel="prev"' }
page: 4
[ 'spritemation' ]
{ 'x-ratelimit-limit': '5000',
  'x-ratelimit-remaining': '4991',
  link: '<https://api.github.com/orgs/Zenovations/repos?page=1&per_page=2>; rel="first", <https://api.github.com/orgs/Ze
novations/repos?page=3&per_page=2>; rel="prev"' }
page: 5
[]
{ 'x-ratelimit-limit': '5000',
  'x-ratelimit-remaining': '4990',
  link: '<https://api.github.com/orgs/Zenovations/repos?page=4&per_page=2>; rel="last", <https://api.github.com/orgs/Zen
ovations/repos?page=1&per_page=2>; rel="first", <https://api.github.com/orgs/Zenovations/repos?page=4&per_page=2>; rel="
prev"' }

my version info

> node -v
v0.6.19

> npm list
[email protected] GitHub\git-stats
├─┬ [email protected]
│ └── [email protected]
├── [email protected]
├── [email protected]  extraneous
├── [email protected]
├── [email protected]
└── [email protected]

fetch stars for a repository?

Hello hello!

I see an entry in the API to fetch stars for a Gist, but nothing for a repo. Am I missing the obvious or has this yet to be added?

Bug in readme docs

github.getUserApi().getFollowers('fjakobs', function(err, followers) {
sys.puts(followers.join('\n');
});

Should be

github.getUserApi().getFollowers('fjakobs', function(err, followers) {
sys.puts(followers.join('\n'));
});

pagination does not work

im doing a simple:

this.githubApi.repos.getCommits({
        user: "balshamali",
        repo: "testing",
        page: 2, // switching this to 1 yields the same commit
        per_page: 1
    }, function(err, data) {
        if (err) console.log(err);
        else console.log(data);
    });

this always returns the same commit regardless of the 'page' number provided. the repo above only has two commits so it is easy to tested. I also tested with a repo with more commits; wasn't able to make it work. Is this a known issue?

Get all pages

I'm trying to get the total number of repos and total number of followers for a user.
Took me a couple of minutes to realize I'm getting 30 because of the default page size :)
My question is: what parameter can I specify to get all pages with one call?
If not possible, what's the recommended approach to getting them all? A loop with getNextPage?

Thanks!

Authentication

Hey,
I can't authenticate with Github client_id and client_secret, the authentication functions only require basic authentication (username and pass), or token authentication (requires token)..

Cheers !

Edit gist : Body should be a JSON Hash

Hi,

First of all, thank you very much for this great library.

Unfortunately I got a error message (body should be a JSON Hash) when I try to update a gist and when the content to update is a source code. If i put content: "test" it's ok but as soon as instead of "test" i put a variable and when the content of this variable is basically a source code, i am always face to this error.

How can i update a gist which is a source code such as a javascript content.

Thanks in advance
Jbx028 from France

Can't run test/oauth.js

I just cloned the repository locally, navigated to node_modules/github ran npm install, then navigated to node_modules/github/test/ and ran node oauth.js and got the following message.

{ statusCode: 400,
  data: '<html>\r\n<head><title>400 The plain HTTP request was sent to HTTPS por
t</title></head>\r\n<body bgcolor="white">\r\n<center><h1>400 Bad Request</h1></
center>\r\n<center>The plain HTTP request was sent to HTTPS port</center>\r\n<hr
><center>nginx/1.0.13</center>\r\n</body>\r\n</html>\r\n' }

Am I missing some kind of setup?

I am running node.js version 0.6.18 on Windows 7 x64.

Have to call GitHubApi.new between each request

Hi,

I'm using this module to access Github through API v3. When I run several successive requests for the same function, the request is always performed using the same URL. The URL does not get updated because the block.url gets updated with the first request, and thus cannot be updated with the values for the requests coming after.

You can have a look at this gist which provides two examples, correct.js and wrong.js showing how reloading the GitHubApi object corrects the issue.

Environment:

  • node v0.6.13
  • node-github installed through npm on 2012-04-15

Node-github does not work with nodejs 0.10.x

I've tried using [email protected] in combination with nodejs 0.10.21, but I'm getting the following exception:

TypeError: Cannot assign to read only property 'watch' of #<Object>
    at Object.<anonymous> (/github/api/v3.0.0/repos.js:2233:16)
    at Object.<anonymous> (/github/api/v3.0.0/repos.js:2566:4)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at GithubHandler (/github/api/v3.0.0/index.js:37:24)
    at Array.forEach (native)
    at Object.<anonymous> (/github/api/v3.0.0/index.js:36:134)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at new module.exports (/github/index.js:180:15)

I've also tried 0.10.0 and I'm getting the same exception. 0.8.26 does not result in a exception. So I'm guessing something changed when they went to 0.10.x

markdown#render tries to parse HTML as JSON

When the method succeeds, the server returns an HTML fragment. Then we try to parse it as JSON, giving an error:

{ defaultMessage: 'Internal Server Error',
  message: 'Unexpected token <',
  code: '500' }

Problem with deleteHook

When I try to delete a hook, I always get an Internal server error. It seems that the function deleteHook in github/api/v3.0.0/repos.js try to parse res.data but res.data is always empty (httpSend end with status code 204).
So I tried to remove the part where you parse res.data, and it worked just fine: the hook is deleted.

Close Issue

Hello, thanks for all the effort put into this library, very helpful indeed.
Could you tell me whether it is possible to close an issue? I've tried via issue edit, but with no success.

thanks

How can I get pull id from github.pullRequests.create

like this
github.pullRequests.create({
repo: 'velocity',
user: 'kissygalleryteam',
title: '13-42', // 注释标题
body: 'test', // 注释内容
base: 'master', // 固定
head: 'sirzxj:master' //自己修改的库名
}, function(e,data) {
console.log(data);
});

callback arguments data is hard to recognize

Use a module for requests ?

I'd like to suggest using a module like node-request to handle requests. That would relieve node-github of connections matters like how to deal with proxies or which protocol to use. It's especially useful when dealing with proxies as it supports tunneling and manages by itself to choose the good settings. (in my case a https-over-http tunnel for github api calls)

Memory leak

This simple test produces a memory leak on each request:

https://gist.github.com/718990

any ideas why? I tried examining the source but couldn't find anything. I guess some context object is being cloned on each request which never gets cleaned by the gc. The leak doesn't seem to be having any relationship with the response body.

Support github enterprise installations too, beside api.github.com

Supporting GHE is easy: instead of hard-coding the API root https://api.github.com, allow us to pass a different API root when calling the github constructor, i e:

new github(
  { version: '3.0.0'
  , apiroot: 'https://github.groupondev.com'
  , debug: true
  });

And, any time a non-github apiroot is provided, prepend the path segment /api/v<version.major>. Or maybe call the apiroot parameter "enterprise" instead, if that adds some clarity.

It would be extra awesome if we could also pass either a cookie storage object that gets probed for extra cookies to add to all requests (and set, when responses tweak them), for GHE installations wrapped in icky layers of corp-ware, or (probably more generically useful, as APIs go) allowing us to pass along a pair of requestCallback and responseCallback functions to run before http requests are sent / responses are processed, at the lower http level, so we can do amazing contortion artist feats to jump such hoops without cluttering up this module with such crap.

repos method requires authentication (is it not just viewing the public repos for a given user?)

Hi Mike,

While trying to use the repos method, I first tried:

var Client = require('github');

var github = new Client({
    debug: true,
    version: "3.0.0"
});

github.repos.getAll({user: "nelsonic"}, function(err, res) {
    console.log("GOT ERR?", err);
    console.log("GOT RES?", res);
});

That gave an error because I was not authenticated.

REQUEST:  { host: 'api.github.com',
  port: 443,
  path: '/user/repos',
  method: 'get',
  headers: 
   { host: 'api.github.com',
     'content-length': '0',
     'user-agent': 'NodeJS HTTP Client' } }
STATUS: 401
HEADERS: {"server":"GitHub.com","date":"Wed, 06 Nov 2013 00:08:45 GMT","content-type":"application/json; charset=utf-8","status":"401 Unauthorized","x-ratelimit-limit":"60","x-ratelimit-remaining":"55","x-ratelimit-reset":"1383697500","x-github-media-type":"github.beta; format=json","x-content-type-options":"nosniff","content-length":"90","access-control-allow-credentials":"true","access-control-expose-headers":"ETag, Link, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes","access-control-allow-origin":"*","x-github-request-id":"56077E62:77B0:13EB9D5:5279888C"}
[error] { message: '{"message":"Requires authentication","documentation_url":"http://developer.github.com/v3"}',
[error]   code: 401 } null alibzafar
GOT ERR? { message: '{"message":"Requires authentication","documentation_url":"http://developer.github.com/v3"}',
  code: 401 }
GOT RES? undefined

Is there a reason why requesting (public) repos for a given user requires Auth when this is easily accessible via web? (guessing this is GitHub's decision rather than yours...?)

Thanks. :-)

I think Github's SSL action broke node-github

Hey there!

I've been using node-github for a while, and I noticed that, since Github switched to SSL, my requests (All with the commit api) have been getting 301 errors. I'm pretty sure that the defaults in /lib/github/Request.js just have to be changed properly (http--> https, port 80 --> whatever it should be), but I couldn't get it to work.

Thanks!

Error with repos.updateKey

Hello,
When i call repos.updateKey, i got an error 404. I checked my parameters, but it seems like everything is right.
Any idea why is it happening ?

Could I get an example of issues.getEvents()

This is the call I'm making

 github.issues.getEvents({
                "user" : res[0].user.login,
                "repo" : res[0].repository.name,
                "number":res[0].id

            }, function (err, res)
            {
                comments = res;
                console.log( err);
            });

And I get back

[error] { message: '{"message":"Not Found"}', code: 404 } null orrinjr
{ message: '{"message":"Not Found"}', code: 404 }

Thanks!

Add an option to retrieve all pages of a request.

I'm not sure if this is already possible, but it would be nice to just to be able to tell node-github to get my all the pages and not have to loop through them all manually and concat them to get them in one place.

NPM repository not up to date

Hi,

I had a problem on deletehook, the same as issue #36 , so I try to update my github module with npm update github command but no new version was available.

Is NPM repository is up to date ?

Thank you for your amazing work !

Eric

getReference error with 404

I getAllReferences and return a list of references, i chose one ref, like this: refs/tags/master

when i used getReference, i got { message: '{"message":"Not Found"}', code: 404 }

any help, thanks

Socket hangup

The following snippet of code, always thrown an error

        var github = new GitHubApi({
            version: "3.0.0",
            debug: true
        });
        github.authenticate({
            type: "oauth",
            token: token
        });
        github.gitdata.createBlob(
            {
                user: username,
                repo: reponame,
                content: "Content of the blob",
                encoding: "utf-8"
            },
            function(err, res) {
                if (err) {
                    console.log(err);
                }
                console.log(res);
            }
        );

Error: socket hang up
at createHangUpError (http.js:1124:15)
at CleartextStream.socketCloseListener (http.js:1173:23)
at CleartextStream.emit (events.js:88:20)
at Array.0 (tls.js:792:22)
at EventEmitter._tickCallback (node.js:190:38)

I am using Node 0.6.20. Is it something related to https implementation ?
Probably is worth to note that others API call (like repos.getBranches or repos.create) works without any issue.

Thanks

Roberto

Is it possible to manually handle error logging?

I'm building a command line interface for node-github and i want to provide my own error handling. Is it possible?

With the following code i expect the output to be only "got an error!" or "got an exception!" (or perhaps a combination thereof):

var GitHubApi = require("github");

var github = new GitHubApi({
    version: "3.0.0"
});

try {
    github.authenticate({
        type: "basic",
        username: "blhaasgdsgh",
        password: "blhaasgdsgh"
    });

    github.repos.fork({
        user: "ajaxorg",
        repo:"node-github"
    }, function(err, res) {
        if(err){
            console.log("got an error!");
        } else {
            console.log("Didn't get an error!");
            console.log(JSON.stringify(res));
        }
    });
} catch (x) {
    console.log("got an exception!");
}

Unfortunately i also get a message like this:

[error] { message: '{"message":"Bad credentials"}', code: 401 } null ajaxorg

that i can't seem to get rid of. Ideas?

repos.getCommits( { user: 'xxxx', repo: 'xxxxx' }) -> NOT FOUND

I am having an issue getting the commits for a private repo. Some work and some do not. Any help would be great. This repos I am trying access are private. The OAuth has a scope: user, repo etc.

var github = new githubAPI({
version: "3.0.0"
});

github.authenticate({
type: "oauth",
token: req.session.user.access_token
});

var since = moment().subtract('months', 24).format('YYYY-MM-DDTHH:mm:ssZ');

github.repos.getCommits({ user: 'd1b1', 'until': since, repo: 'composer' }, function(err, data) {

console.log(err, data);

res.send(data, 200);

});

npm lib

please add this lib to npm

npm test

README.md mentions run all tests by 'npm test', but 'node ./test/all.js' does not exist.

Issues Search API

Do we have the search API implemented? I can't find any reference of it anywhere

github.authenticate - token

There is

github.authenticate({
    type: "oauth",
    token: token
});

i dont know how i can get the token by node-github,

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.