Skip to content
GitHub API client for GitHub Actions
TypeScript
Branch: master
Clone or download

Latest commit

Fetching latest commit…
Cannot retrieve the latest commit at this time.

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github
src
test
.gitignore
CODE_OF_CONDUCT.md
LICENSE
README.md
package-lock.json
package.json
tsconfig.json

README.md

action.js

GitHub API client for GitHub Actions

@latest Build Status

Usage

Browsers

@octokit/action is not meant for browser usage.

Node

Install with npm install @octokit/action

const { Octokit } = require("@octokit/action");
// or: 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@v2
      # Install Node 12
      - uses: actions/setup-node@v1
        with:
          version: 12
      # 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
        with:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Setting GITHUB_TOKEN on either with: or env: will work.

// .github/actions/my-script.js
const octokit = new Octokit();

// `octokit` is now authenticated using GITHUB_TOKEN

Create an issue using REST API

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: %d", 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

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: $repositoryId, title: $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"];

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.

License

MIT

You can’t perform that action at this time.