5 - GitHub API - REST API & GraphQL API
In this lab you will create a workflow that calls GitHub APIs using REST API and GraphQL API.
Duration: 15-20 minutes
References:
- GitHub GraphQL API - GitHub Docs
- GitHub REST API - GitHub Docs
- Migrating from REST to GraphQL - GitHub Docs
- actions/github-script
- octokit/graphql-action
- See octokit/rest.js for the API client documentation
5.1 Develop a GitHub Action workflow that calls REST APIs
- Open the workflow file use-github-apis.yml
- Edit the file and copy the following YAML content at the end of the
rest-api-create-and-close-issue
job: ```YAML- uses: actions/github-script@v6
id: close-issue
with:
github-token: $
script: |
const result = await github.rest.issues.update({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: $,
state: ‘closed’
})
console.log(result) ```
- uses: actions/github-script@v6
id: close-issue
with:
github-token: $
script: |
const result = await github.rest.issues.update({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: $,
state: ‘closed’
})
- Commit the changes into the
main
branch - Go to
Actions
and see the details of your running workflow
5.2 Develop a GitHub Action workflow that calls GraphQL APIs
- Open the workflow file use-github-apis.yml
- Edit the file and copy the following YAML content at the end of the file:
```YAML
graphql-api-query-labels:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v6
id: labels-result
with:
script: |
const query =
query($owner:String!, $name:String!) { repository(owner:$owner, name:$name){ labels (last:100) { nodes { name, color, issues(last:100) { nodes { number } } } } } }
; const variables = { owner: context.repo.owner, name: context.repo.repo } const result = await github.graphql(query, variables) console.log(result.repository.labels.nodes) ```
- uses: actions/github-script@v6
id: labels-result
with:
script: |
const query =
- Commit the changes into the
main
branch - Go to
Actions
and see the details of your running workflow