GitHub - octokit/action.js: GitHub API client for GitHub Actions (original) (raw)
action.js
GitHub API client for GitHub Actions
Usage
Browsers | @octokit/action is not meant for browser usage. |
---|---|
Node | Install with npm install @octokit/action import { Octokit } from "@octokit/action"; |
You can pass secret.GITHUB_TOKEN
or any of your own secrets to a Node.js script. For example
name: My Node Action on:
- pull_request
jobs:
my-action:
runs-on: ubuntu-latest
steps:
Check out code using git
- uses: actions/checkout@v4
Install Node 20
- uses: actions/setup-node@v4 with: node-version: 20
- run: npm install @octokit/action
Node.js script can be anywhere. A good convention is to put local GitHub Actions
into the.github/actions
folder- run: node .github/actions/my-script.js env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Setting GITHUB_TOKEN
on either with: or env: will work.
// .github/actions/my-script.js import { Octokit } from "@octokit/action";
const octokit = new Octokit();
// octokit
is now authenticated using GITHUB_TOKEN
Create an issue using REST API
import { Octokit } from "@octokit/action";
const octokit = new Octokit(); const [owner, repo] = process.env.GITHUB_REPOSITORY.split("/");
// See https://developer.github.com/v3/issues/#create-an-issue const { data } = await octokit.request("POST /repos/{owner}/{repo}/issues", { owner, repo, title: "My test issue", }); console.log("Issue created: %s", data.html_url);
You can also use octokit.issues.create({ owner, repo, title })
. See the REST endpoint methods plugin for a list of all available methods.
Create an issue using GraphQL
import { Octokit } from "@octokit/action";
const octokit = new Octokit(); const eventPayload = require(process.env.GITHUB_EVENT_PATH); const repositoryId = eventPayload.repository.node_id;
const response = await octokit.graphql(
mutation($repositoryId:ID!, $title:String!) { createIssue(input:{repositoryId: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>r</mi><mi>e</mi><mi>p</mi><mi>o</mi><mi>s</mi><mi>i</mi><mi>t</mi><mi>o</mi><mi>r</mi><mi>y</mi><mi>I</mi><mi>d</mi><mo separator="true">,</mo><mi>t</mi><mi>i</mi><mi>t</mi><mi>l</mi><mi>e</mi><mo>:</mo></mrow><annotation encoding="application/x-tex">repositoryId, title: </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">re</span><span class="mord mathnormal">p</span><span class="mord mathnormal">os</span><span class="mord mathnormal">i</span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.03588em;">ory</span><span class="mord mathnormal" style="margin-right:0.07847em;">I</span><span class="mord mathnormal">d</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal">t</span><span class="mord mathnormal">i</span><span class="mord mathnormal" style="margin-right:0.01968em;">tl</span><span class="mord mathnormal">e</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">:</span></span></span></span>title}) { issue { number } } }
,
{
repositoryId,
title: "My test issue",
},
);
Hooks, plugins, and more
@octokit/action
is build upon @octokit/core
. Refer to its README for the full API documentation.
TypeScript: Endpoint method parameters and responses
Types for endpoint method parameters and responses are exported as RestEndpointMethodTypes
. They keys are the same as the endpoint methods. Here is an example to retrieve the parameter and response types for octokit.checks.create()
import { RestEndpointMethodTypes } from @octokit/action
;
type ChecksCreateParams = RestEndpointMethodTypes["checks"]["create"]["parameters"]; type ChecksCreateResponse = RestEndpointMethodTypes["checks"]["create"]["response"];
Proxy Servers
If you use self-hosted runners and require a proxy server to access internet resources then you will need to ensure that you have correctly configured the runner for proxy servers. @octokit/action
will pick up the configured proxy server environment variables and configure @octokit/core
with the correct request.dispatcher
using ProxyAgent. If you need to supply a different request.dispatcher
then you should ensure that it handles proxy servers if needed.
How it works
@octokit/action
is simply a @octokit/core constructor, pre-authenticate using @octokit/auth-action.
The source code is … simple: src/index.ts.