Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

GitHub Apps toolset for Node.js

License

NotificationsYou must be signed in to change notification settings

octokit/app.js

Repository files navigation

GitHub App toolset for Node.js

@latestBuild Status

Usage

Browsers

@octokit/app is not meant for browser usage.

Node

Install withnpm install @octokit/app

const{ App, createNodeMiddleware}=require("@octokit/app");

Important

As we useconditional exports, you will need to adapt yourtsconfig.json by setting"moduleResolution": "node16", "module": "node16".

See the TypeScript docs onpackage.json "exports".
See thishelpful guide on transitioning to ESM from@sindresorhus

constapp=newApp({appId:123,privateKey:"-----BEGIN PRIVATE KEY-----\n...",oauth:{clientId:"0123",clientSecret:"0123secret",},webhooks:{secret:"secret",},});const{ data}=awaitapp.octokit.request("/app");console.log("authenticated as %s",data.name);forawait(const{ installation}ofapp.eachInstallation.iterator()){forawait(const{ octokit, repository}ofapp.eachRepository.iterator({installationId:installation.id,})){awaitoctokit.request("POST /repos/{owner}/{repo}/dispatches",{owner:repository.owner.login,repo:repository.name,event_type:"my_event",});}}app.webhooks.on("issues.opened",async({ octokit, payload})=>{awaitoctokit.request("POST /repos/{owner}/{repo}/issues/{issue_number}/comments",{owner:payload.repository.owner.login,repo:payload.repository.name,issue_number:payload.issue.number,body:"Hello World!",},);});app.oauth.on("token",async({ token, octokit})=>{const{ data}=awaitoctokit.request("GET /user");console.log(`Token retrieved for${data.login}`);});require("http").createServer(createNodeMiddleware(app)).listen(3000);// can now receive requests at /api/github/*

App.defaults(options)

Create a newApp with custom defaults for theconstructor options

constMyApp=App.defaults({Octokit:MyOctokit,});constapp=newMyApp({ clientId, clientSecret});// app.octokit is now an instance of MyOctokit

Constructor

name type description
appIdnumberRequired. Find theApp ID on the app’s about page in settings.
privateKeystringRequired. Content of the*.pem file you downloaded from the app’s about page. You can generate a new private key if needed.
OctokitConstructor

You can pass in your own Octokit constructor with custom defaults and plugins. Note thatauthStrategy will be always be set tocreateAppAuth from@octokit/auth-app.

For usage with enterprise, setbaseUrl to the hostname +/api/v3. Example:

const{ Octokit}=require("@octokit/core");newApp({appId:123,privateKey:"-----BEGIN PRIVATE KEY-----\n...",oauth:{clientId:123,clientSecret:"secret",},webhooks:{secret:"secret",},Octokit:Octokit.defaults({baseUrl:"https://ghe.my-company.com/api/v3",}),});

Defaults to@octokit/core.

logobject Used for internal logging. Defaults toconsole.
webhooks.secretstringRequired. Secret as configured in the GitHub App's settings.
webhooks.transformfunction Only relevant for `app.webhooks.on`. Transform emitted event before calling handlers. Can be asynchronous.
oauth.clientIdnumber Find the OAuthClient ID on the app’s about page in settings.
oauth.clientSecretnumber Find the OAuthClient Secret on the app’s about page in settings.
oauth.allowSignupboolean Sets the default value forapp.oauth.getAuthorizationUrl(options).

API

app.octokit

Octokit instance. Uses theOctokit constructor option if passed.

app.log

Seehttps://github.com/octokit/core.js#logging. Customize using thelog constructor option.

app.getInstallationOctokit

constoctokit=awaitapp.getInstallationOctokit(123);

app.eachInstallation

forawait(const{ octokit, installation}ofapp.eachInstallation.iterator()){/* ... */}awaitapp.eachInstallation(({ octokit, installation})=>/* ... */)

app.eachRepository

forawait(const{ octokit, repository}ofapp.eachRepository.iterator()){/* ... */}awaitapp.eachRepository(({ octokit, repository})=>/* ... */)

Optionally pass installation ID to iterate through all repositories in one installation

forawait(const{ octokit, repository}ofapp.eachRepository.iterator({ installationId})){/* ... */}awaitapp.eachRepository({ installationId},({ octokit, repository})=>/* ... */)

app.getInstallationUrl

constinstallationUrl=awaitapp.getInstallationUrl();returnres.redirect(installationUrl);

Optionally pass the ID of a GitHub organization or user to request installation on that specific target.

If the user will be sent to a redirect URL after installation (such as if you request user authorization during installation), you can also supply astate string that will be included in the query of the post-install redirect.

constinstallationUrl=awaitapp.getInstallationUrl({ state, target_id});returnres.redirect(installationUrl);

app.webhooks

An@octokit/webhooks instance

app.oauth

An@octokit/oauth-app instance

Middlewares

A middleware is a method or set of methods to handle requests for common environments.

By default, all middlewares expose the following routes

RouteRoute Description
POST /api/github/webhooksEndpoint to receive GitHub Webhook Event requests
GET /api/github/oauth/loginRedirects to GitHub's authorization endpoint. Accepts optional?state query parameter.
GET /api/github/oauth/callbackThe client's redirect endpoint. This is where thetoken event gets triggered
POST /api/github/oauth/tokenExchange an authorization code for an OAuth Access token. If successful, thetoken event gets triggered.
GET /api/github/oauth/tokenCheck if token is valid. Must authenticate using token inAuthorization header. Uses GitHub'sPOST /applications/{client_id}/token endpoint
PATCH /api/github/oauth/tokenResets a token (invalidates current one, returns new token). Must authenticate using token inAuthorization header. Uses GitHub'sPATCH /applications/{client_id}/token endpoint.
DELETE /api/github/oauth/tokenInvalidates current token, basically the equivalent of a logout. Must authenticate using token inAuthorization header.
DELETE /api/github/oauth/grantRevokes the user's grant, basically the equivalent of an uninstall. must authenticate using token inAuthorization header.

createNodeMiddleware(app, options)

Middleware for Node's built in http server orexpress.

const{ App, createNodeMiddleware}=require("@octokit/app");constapp=newApp({appId:123,privateKey:"-----BEGIN PRIVATE KEY-----\n...",oauth:{clientId:"0123",clientSecret:"0123secret",},webhooks:{secret:"secret",},});constmiddleware=createNodeMiddleware(app);require("http").createServer(async(req,res)=>{// `middleware` returns `false` when `req` is unhandled (beyond `/api/github`)if(awaitmiddleware(req,res))return;res.writeHead(404);res.end();}).listen(3000);// can now receive user authorization callbacks at /api/github/*

The middleware returned fromcreateNodeMiddleware can also serve as anExpress.js middleware directly.

name type description
appApp instanceRequired.
options.pathPrefixstring

All exposed paths will be prefixed with the provided prefix. Defaults to"/api/github"

log object

Used for internal logging. Defaults toconsole withdebug andinfo doing nothing.

  </td></tr>

Contributing

SeeCONTRIBUTING.md

License

MIT

About

GitHub Apps toolset for Node.js

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors33


[8]ページ先頭

©2009-2025 Movatter.jp