github.com/jfrog/frogbot@v1.1.1-0.20231221090046-821a26f50338/action/node_modules/@actions/github/README.md (about) 1 # `@actions/github` 2 3 > A hydrated Octokit client. 4 5 ## Usage 6 7 Returns an authenticated Octokit client that follows the machine [proxy settings](https://help.github.com/en/actions/hosting-your-own-runners/using-a-proxy-server-with-self-hosted-runners) and correctly sets GHES base urls. See https://octokit.github.io/rest.js for the API. 8 9 ```js 10 const github = require('@actions/github'); 11 const core = require('@actions/core'); 12 13 async function run() { 14 // This should be a token with access to your repository scoped in as a secret. 15 // The YML workflow will need to set myToken with the GitHub Secret Token 16 // myToken: ${{ secrets.GITHUB_TOKEN }} 17 // https://help.github.com/en/actions/automating-your-workflow-with-github-actions/authenticating-with-the-github_token#about-the-github_token-secret 18 const myToken = core.getInput('myToken'); 19 20 const octokit = github.getOctokit(myToken) 21 22 // You can also pass in additional options as a second parameter to getOctokit 23 // const octokit = github.getOctokit(myToken, {userAgent: "MyActionVersion1"}); 24 25 const { data: pullRequest } = await octokit.rest.pulls.get({ 26 owner: 'octokit', 27 repo: 'rest.js', 28 pull_number: 123, 29 mediaType: { 30 format: 'diff' 31 } 32 }); 33 34 console.log(pullRequest); 35 } 36 37 run(); 38 ``` 39 40 You can also make GraphQL requests. See https://github.com/octokit/graphql.js for the API. 41 42 ```js 43 const result = await octokit.graphql(query, variables); 44 ``` 45 46 Finally, you can get the context of the current action: 47 48 ```js 49 const github = require('@actions/github'); 50 51 const context = github.context; 52 53 const newIssue = await octokit.rest.issues.create({ 54 ...context.repo, 55 title: 'New issue!', 56 body: 'Hello Universe!' 57 }); 58 ``` 59 60 ## Webhook payload typescript definitions 61 62 The npm module `@octokit/webhooks-definitions` provides type definitions for the response payloads. You can cast the payload to these types for better type information. 63 64 First, install the npm module `npm install @octokit/webhooks-definitions` 65 66 Then, assert the type based on the eventName 67 ```ts 68 import * as core from '@actions/core' 69 import * as github from '@actions/github' 70 import {PushEvent} from '@octokit/webhooks-definitions/schema' 71 72 if (github.context.eventName === 'push') { 73 const pushPayload = github.context.payload as PushEvent 74 core.info(`The head commit is: ${pushPayload.head_commit}`) 75 } 76 ``` 77 78 ## Extending the Octokit instance 79 `@octokit/core` now supports the [plugin architecture](https://github.com/octokit/core.js#plugins). You can extend the GitHub instance using plugins. 80 81 For example, using the `@octokit/plugin-enterprise-server` you can now access enterprise admin apis on GHES instances. 82 83 ```ts 84 import { GitHub, getOctokitOptions } from '@actions/github/lib/utils' 85 import { enterpriseServer220Admin } from '@octokit/plugin-enterprise-server' 86 87 const octokit = GitHub.plugin(enterpriseServer220Admin) 88 // or override some of the default values as well 89 // const octokit = GitHub.plugin(enterpriseServer220Admin).defaults({userAgent: "MyNewUserAgent"}) 90 91 const myToken = core.getInput('myToken'); 92 const myOctokit = new octokit(getOctokitOptions(token)) 93 // Create a new user 94 myOctokit.rest.enterpriseAdmin.createUser({ 95 login: "testuser", 96 email: "testuser@test.com", 97 }); 98 ```