@@ -226,226 +226,3 @@ external function.
226
226
Additionally, you'll want to use the [ checkout
227
227
action] ( https://github.com/actions/checkout ) to make sure your script file is
228
228
available.
229
-
230
- This action makes it easy to quickly write a script in your workflow that
231
- uses the GitHub API and the workflow run context.
232
-
233
- In order to use this action, a ` script ` input is provided. The value of that
234
- input should be the body of an asynchronous function call. The following
235
- arguments will be provided:
236
-
237
- - ` github ` A pre-authenticated
238
- [ octokit/rest.js] ( https://github.com/octokit/rest.js ) client
239
- - ` context ` An object containing the [ context of the workflow
240
- run] ( https://github.com/actions/toolkit/blob/main/packages/github/src/context.ts )
241
- - ` core ` A reference to the [ @actions/core ] ( https://github.com/actions/toolkit/tree/main/packages/core ) package
242
- - ` io ` A reference to the [ @actions/io ] ( https://github.com/actions/toolkit/tree/main/packages/io ) package
243
-
244
- Since the ` script ` is just a function body, these values will already be
245
- defined, so you don't have to (see examples below).
246
-
247
- See [ octokit/rest.js] ( https://octokit.github.io/rest.js/ ) for the API client
248
- documentation.
249
-
250
- ** Note** This action is still a bit of an experiment—the API may change in
251
- future versions. 🙂
252
-
253
- ## Development
254
-
255
- See [ development.md] ( /docs/development.md ) .
256
-
257
- ## Reading step results
258
-
259
- The return value of the script will be in the step's outputs under the
260
- "result" key.
261
-
262
- ``` yaml
263
- - uses : actions/github-script@v3
264
- id : set-result
265
- with :
266
- script : return "Hello!"
267
- result-encoding : string
268
- - name : Get result
269
- run : echo "${{steps.set-result.outputs.result}}"
270
- ` ` `
271
-
272
- See ["Result encoding"](#result-encoding) for details on how the encoding of
273
- these outputs can be changed.
274
-
275
- ## Result encoding
276
-
277
- By default, the JSON-encoded return value of the function is set as the "result" in the
278
- output of a github-script step. For some workflows, string encoding is preferred. This option can be set using the
279
- ` result-encoding` input:
280
-
281
- ` ` ` yaml
282
- - uses: actions/github-script@v3
283
- id: my-script
284
- with:
285
- github-token: ${{secrets.GITHUB_TOKEN}}
286
- result-encoding: string
287
- script: return "I will be string (not JSON) encoded!"
288
- ` ` `
289
-
290
- # # Examples
291
-
292
- Note that `github-token` is optional in this action, and the input is there
293
- in case you need to use a non-default token.
294
-
295
- By default, github-script will use the token provided to your workflow.
296
-
297
- # ## Comment on an issue
298
-
299
- ` ` ` yaml
300
- on:
301
- issues:
302
- types: [opened]
303
-
304
- jobs:
305
- comment:
306
- runs-on: ubuntu-latest
307
- steps:
308
- - uses: actions/github-script@v3
309
- with:
310
- github-token: ${{secrets.GITHUB_TOKEN}}
311
- script: |
312
- github.issues.createComment({
313
- issue_number: context.issue.number,
314
- owner: context.repo.owner,
315
- repo: context.repo.repo,
316
- body: '👋 Thanks for reporting!'
317
- })
318
- ` ` `
319
-
320
- # ## Apply a label to an issue
321
-
322
- ` ` ` yaml
323
- on:
324
- issues:
325
- types: [opened]
326
-
327
- jobs:
328
- apply-label:
329
- runs-on: ubuntu-latest
330
- steps:
331
- - uses: actions/github-script@v3
332
- with:
333
- github-token: ${{secrets.GITHUB_TOKEN}}
334
- script: |
335
- github.issues.addLabels({
336
- issue_number: context.issue.number,
337
- owner: context.repo.owner,
338
- repo: context.repo.repo,
339
- labels: ['Triage']
340
- })
341
- ` ` `
342
-
343
- # ## Welcome a first-time contributor
344
-
345
- ` ` ` yaml
346
- on: pull_request
347
-
348
- jobs:
349
- welcome:
350
- runs-on: ubuntu-latest
351
- steps:
352
- - uses: actions/github-script@v3
353
- with:
354
- github-token: ${{secrets.GITHUB_TOKEN}}
355
- script: |
356
- // Get a list of all issues created by the PR opener
357
- // See: https://octokit.github.io/rest.js/#pagination
358
- const creator = context.payload.sender.login
359
- const opts = github.issues.listForRepo.endpoint.merge({
360
- ...context.issue,
361
- creator,
362
- state: 'all'
363
- })
364
- const issues = await github.paginate(opts)
365
-
366
- for (const issue of issues) {
367
- if (issue.number === context.issue.number) {
368
- continue
369
- }
370
-
371
- if (issue.pull_request) {
372
- return // Creator is already a contributor.
373
- }
374
- }
375
-
376
- await github.issues.createComment({
377
- issue_number: context.issue.number,
378
- owner: context.repo.owner,
379
- repo: context.repo.repo,
380
- body: 'Welcome, new contributor!'
381
- })
382
- ` ` `
383
-
384
- # ## Download data from a URL
385
-
386
- You can use the `github` object to access the Octokit API. For
387
- instance, `github.request`
388
-
389
- ` ` ` yaml
390
- on: pull_request
391
-
392
- jobs:
393
- diff:
394
- runs-on: ubuntu-latest
395
- steps:
396
- - uses: actions/github-script@v3
397
- with:
398
- github-token: ${{secrets.GITHUB_TOKEN}}
399
- script: |
400
- const diff_url = context.payload.pull_request.diff_url
401
- const result = await github.request(diff_url)
402
- console.log(result)
403
- ` ` `
404
-
405
- _(Note that this particular example only works for a public URL, where the
406
- diff URL is publicly accessible. Getting the diff for a private URL requires
407
- using the API.)_
408
-
409
- This will print the full diff object in the screen; `result.data` will
410
- contain the actual diff text.
411
-
412
- # ## Run a separate file
413
-
414
- If you don't want to inline your entire script that you want to run, you can
415
- use a separate JavaScript module in your repository like so :
416
-
417
- ` ` ` yaml
418
- on: push
419
-
420
- jobs:
421
- echo-input:
422
- runs-on: ubuntu-latest
423
- steps:
424
- - uses: actions/checkout@v2
425
- - uses: actions/github-script@v2
426
- with:
427
- script: |
428
- const script = require(` ${process.env.GITHUB_WORKSPACE}/path/to/script.js`)
429
- console.log(script({github, context}))
430
- ```
431
-
432
- _ Note that the script path given to ` require() ` must be an ** absolute path** in this case, hence using [ ` GITHUB_WORKSPACE ` ] ( https://docs.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables#default-environment-variables ) ._
433
-
434
- And then export a function from your module:
435
-
436
- ``` javascript
437
- module .exports = (github , context ) => {
438
- return context .payload .client_payload .value
439
- }
440
- ```
441
-
442
- You can also use async functions in this manner, as long as you ` await ` it in
443
- the inline script.
444
-
445
- Note that because you can't ` require ` things like the GitHub context or
446
- Actions Toolkit libraries, you'll want to pass them as arguments to your
447
- external function.
448
-
449
- Additionally, you'll want to use the [ checkout
450
- action] ( https://github.com/actions/checkout ) to make sure your script file is
451
- available.
0 commit comments