Git Product home page Git Product logo

Comments (3)

github-learning-lab avatar github-learning-lab commented on June 16, 2024

Create an issue comment

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/[email protected]
  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!'
    })

Open a pull request

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/[email protected]
  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 🔥"
    })

from write-github-script.

github-learning-lab avatar github-learning-lab commented on June 16, 2024

That's not much different, so why might I want to use GitHub Script?

With GitHub Script you don't have to worry about

  • Building a custom action to create this issue comment.
  • No Octokit dependencies to worry about.
  • No additional packages to manage.
  • No need to write action metadata.
  • No need to write source code to handle this Octokit method.

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

from write-github-script.

github-learning-lab avatar github-learning-lab commented on June 16, 2024

Using GitHub Script in a workflow

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.

Brand new to GitHub Actions? Click here to learn about workflows!

What is a workflow file?

A workflow file can be thought of as the recipe for automating a task. They house the start to finish instructions, in the form of jobs and steps, for what should happen based on specific triggers.

Your repository can contain multiple workflow files that carry out a wide variety of tasks. It is important to consider this when deciding on a name for your workflow. The name you choose should reflect the tasks being performed.


📖 Read more about workflows

⌨️ Activity: Respond to an issue when it gets opened

  1. 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/[email protected]
            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!!!"
                })
  2. Commit the workflow to a new branch.

  3. Create a pull request. I suggest a title like Automate issue responses.

  4. Supply the pull request body content and click Create pull request.

About pull pull request titles and content

It is important to place meaningful content into the body of the pull requests you create. This repository will stay with you long after you complete the course. We recommend you use the body of your pull requests as a way to take long lived notes about thing you want to remember.

In practice, good pull request titles and content convey information efficiently to your collaborators.

You can fill the body of this pull request with the following recommended content:

Workflow files are the recipe for task automation. This is where actions are placed if I want to use them for a task.


I am waiting for you to create a new pull request before moving on.

from write-github-script.

Related Issues (1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.