Closed github-learning-lab[bot] closed 2 years ago
If we take a look at the Octokit documentation on how to create issue comments we are greeted with the following method:
someFile.js
octokit.issues.createComment({
owner,
repo,
issue_number,
body
});
Okay, that doesn't seem so hard. Now that we know how to do it with Octokit let's take a look at how to use GitHub Script to create an issue comment:
my-workflow.yml
- uses: actions/github-script@0.8.0
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
github.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: '👋 Thanks for reporting!'
})
Now let's examine what it's like to open a pull request with octokit/rest.js:
someFile.js
octokit.pulls.create({
owner,
repo,
title,
head,
base
});
Again, that's not too hard at all. Now let's do the same thing, only using the GitHub Script action:
- uses: actions/github-script@0.8.0
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
github.pull.create({
repo: github.context.repo.repo,
owner: github.context.repo.owner,
head: github.context.ref,
base: "main",
title: "from my action",
body: "## I totally used GitHub Script to pull this off 🔥"
})
With GitHub Script you don't have to worry about
Using GitHub Script instead of writing custom actions for repository related tasks can prove to be a much more light-weight solution in most cases.
Anything you can do with Octokit can be accomplished with GitHub Script 🎉!
You may have noticed that when using GitHub Script the method call starts with github
and not octokit
. This is because GitHub Script provides you with a pre-authenticated octokit/rest.js client stored in a variable named github
.
context
is a reference to an object containing the context of the current workflow run
Actions are enabled on your repository by default, but we still have to tell our repository to use them. We do this by creating a workflow file in our repository.
If you've been following the learning path for GitHub Actions then this task is quite familiar to you.
📖 Read more about workflows
Create a new workflow file titled .github/workflows/my-workflow.yml
with the following contents:
You can use this quicklink to easily create this file
name: Learning GitHub Script
on:
issues:
types: [opened]
jobs:
comment:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@0.8.0
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
github.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: "🎉 You've created this issue comment using GitHub Script!!!"
})
Commit the workflow to a new branch.
Create a pull request. I suggest a title like Automate issue responses.
Supply the pull request body content and click Create pull request.
I am waiting for you to create a new pull request before moving on.
I have created a new pull request where we will continue this lesson. Click the link to meet me over there.
Hi there 👋!
Hello @ManishOjwani, I'm so excited to teach you how to use GitHub Script in your workflows 😄
What is GitHub Script?
GitHub Script is a really awesome action that allows you to quickly interact with the GitHub API directly in your workflow!
This allows you to use the workflow context to script any API usage that is available through the octokit/rest.js library without the need to build a bulky custom action to do so.
📖 See octokit/rest.js for the API client documentation.
Let's take a closer look at how this action compares to Octokit.