-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathREADME.md
143 lines (106 loc) · 4.55 KB
/
README.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# action.js
> GitHub API client for GitHub Actions
[![@latest](https://img.shields.io/npm/v/@octokit/action.svg)](https://www.npmjs.com/package/@octokit/action)
[![Build Status](https://github.com/octokit/action.js/workflows/Test/badge.svg)](https://github.com/octokit/action.js/actions)
## Usage
<table>
<tbody valign=top align=left>
<tr><th>
Browsers
</th><td width=100%>
`@octokit/action` is not meant for browser usage.
</td></tr>
<tr><th>
Node
</th><td>
Install with `npm install @octokit/action`
```js
const { Octokit } = require("@octokit/action");
// or: import { Octokit } from "@octokit/action";
```
</td></tr>
</tbody>
</table>
You can pass `secret.GITHUB_TOKEN` or any of your own secrets to a Node.js script. For example
```yml
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
- 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:`](https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idstepswith) or [`env:`](https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions#env) will work.
```js
// .github/actions/my-script.js
const { Octokit } = require("@octokit/action");
const octokit = new Octokit();
// `octokit` is now authenticated using GITHUB_TOKEN
```
### Create an issue using REST API
```js
const { Octokit } = require("@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](https://github.com/octokit/plugin-rest-endpoint-methods.js/) for a list of all available methods.
### Create an issue using GraphQL
```js
const { Octokit } = require("@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: $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](https://github.com/octokit/core.js#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()`
```ts
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](https://docs.github.com/en/actions/hosting-your-own-runners/about-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](https://docs.github.com/en/actions/hosting-your-own-runners/using-a-proxy-server-with-self-hosted-runners). `@octokit/action` will pick up the configured proxy server environment variables and configure `@octokit/core` with the correct `request.agent` using [proxy-agent](https://github.com/TooTallNate/node-proxy-agent/blob/master/index.js). If you need to supply a different `request.agent` then you should ensure that it handles proxy servers if needed.
## How it works
`@octokit/action` is simply a [`@octokit/core`](https://github.com/octokit/core.js#readme) constructor, pre-authenticate using [`@octokit/auth-action`](https://github.com/octokit/auth-action.js#readme).
The source code is … simple: [`src/index.ts`](src/index.ts).
## License
[MIT](LICENSE)