|
| 1 | +# 5 - GitHub API - REST API & GraphQL API |
| 2 | +In this lab you will create a workflow that calls GitHub APIs using REST API and GraphQL API. |
| 3 | +> Duration: 15-20 minutes |
| 4 | +
|
| 5 | +References: |
| 6 | +- [GitHub GraphQL API - GitHub Docs](https://docs.github.com/en/graphql) |
| 7 | +- [GitHub REST API - GitHub Docs](https://docs.github.com/en/rest) |
| 8 | +- [Migrating from REST to GraphQL - GitHub Docs](https://docs.github.com/en/graphql/guides/migrating-from-rest-to-graphql) |
| 9 | +- [actions/github-script](https://github.com/actions/github-script) |
| 10 | +- [octokit/graphql-action](https://github.com/octokit/graphql-action) |
| 11 | +- [See octokit/rest.js for the API client documentation](https://octokit.github.io/rest.js/v18) |
| 12 | + |
| 13 | +## 5.1 Develop a GitHub Action workflow that calls REST APIs |
| 14 | + |
| 15 | +1. Open the workflow file [use-github-apis.yml](/.github/workflows/use-github-apis.yml) |
| 16 | +2. Edit the file and copy the following YAML content at the end of the `rest-api-create-and-close-issue` job: |
| 17 | +```YAML |
| 18 | + - uses: actions/github-script@v6 |
| 19 | + id: close-issue |
| 20 | + with: |
| 21 | + github-token: ${{secrets.GITHUB_TOKEN}} |
| 22 | + script: | |
| 23 | + const result = await github.rest.issues.update({ |
| 24 | + owner: context.repo.owner, |
| 25 | + repo: context.repo.repo, |
| 26 | + issue_number: ${{fromJSON(steps.create-issue.outputs.result).number}}, |
| 27 | + state: 'closed' |
| 28 | + }) |
| 29 | + console.log(result) |
| 30 | +``` |
| 31 | +3. Commit the changes into the `main` branch |
| 32 | +4. Go to `Actions` and see the details of your running workflow |
| 33 | + |
| 34 | +## 5.2 Develop a GitHub Action workflow that calls GraphQL APIs |
| 35 | + |
| 36 | +1. Open the workflow file [use-github-apis.yml](/.github/workflows/use-github-apis.yml) |
| 37 | +2. Edit the file and copy the following YAML content at the end of the file: |
| 38 | +```YAML |
| 39 | + graphql-api-query-labels: |
| 40 | + runs-on: ubuntu-latest |
| 41 | + steps: |
| 42 | + - uses: actions/github-script@v6 |
| 43 | + id: labels-result |
| 44 | + with: |
| 45 | + script: | |
| 46 | + const query = `query($owner:String!, $name:String!) { |
| 47 | + repository(owner:$owner, name:$name){ |
| 48 | + labels (last:100) { |
| 49 | + nodes { |
| 50 | + name, |
| 51 | + color, |
| 52 | + issues(last:100) { |
| 53 | + nodes { |
| 54 | + number |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + }`; |
| 61 | + const variables = { |
| 62 | + owner: context.repo.owner, |
| 63 | + name: context.repo.repo |
| 64 | + } |
| 65 | + const result = await github.graphql(query, variables) |
| 66 | + console.log(result.repository.labels.nodes) |
| 67 | +``` |
| 68 | +3. Commit the changes into the `main` branch |
| 69 | +4. Go to `Actions` and see the details of your running workflow |
0 commit comments