|
| 1 | +--- |
| 2 | +title: "Exercise 2: Working with Issues in the GitHub API" |
| 3 | +execute: |
| 4 | + echo: true |
| 5 | +format: |
| 6 | + html: |
| 7 | + code-fold: true |
| 8 | +lightbox: true |
| 9 | +--- |
| 10 | + |
| 11 | +Now it’s time to interact with one of the most common features of GitHub: **issues**. You’ll create, update, list, and (sort of) delete an issue — all using the GitHub API. |
| 12 | + |
| 13 | +## Preparing Your Repository |
| 14 | + |
| 15 | +To complete this exercise, you’ll need a repository that **you own** (not one you just contribute to). If you don’t already have one, go to GitHub and create a new, empty repository. |
| 16 | + |
| 17 | +You can name it something like `api-test-repo` or `postman-playground`. |
| 18 | + |
| 19 | +## Setting Environment Variables |
| 20 | + |
| 21 | +Let’s make this exercise easier to repeat and reuse. In your **GitHub Postman environment**, add two **default** variables: |
| 22 | + |
| 23 | +- **`owner`** → your GitHub username |
| 24 | +- **`repo`** → the name of your target repository |
| 25 | + |
| 26 | +Now you can reuse them like this: |
| 27 | +`{{gh_URL}}/repos/{{owner}}/{{repo}}/issues` |
| 28 | + |
| 29 | +## Create a New Issue |
| 30 | + |
| 31 | +- **Method**: `POST` |
| 32 | +- **Endpoint**: |
| 33 | + ``` |
| 34 | + {{gh_URL}}/repos/{{owner}}/{{repo}}/issues |
| 35 | + ``` |
| 36 | +- **Headers**: |
| 37 | + - `Authorization`: `Bearer {{gh_token}}` |
| 38 | + - `accept`: `application/vnd.github+json` |
| 39 | +- **Body (raw, JSON)**: |
| 40 | + ```json |
| 41 | + { |
| 42 | + "title": "API-created issue", |
| 43 | + "body": "This issue was created using Postman!" |
| 44 | + } |
| 45 | + ``` |
| 46 | + |
| 47 | +Click **Send**. If successful, you’ll receive a `201 Created` response with the new issue’s details. |
| 48 | + |
| 49 | +📌 **Save the `number` of the issue** from the response — you'll need it in the next step. |
| 50 | + |
| 51 | +## Update the Issue |
| 52 | + |
| 53 | +- **Method**: `PATCH` |
| 54 | +- **Endpoint**: |
| 55 | + ``` |
| 56 | + {{gh_URL}}/repos/{{owner}}/{{repo}}/issues/{issue_number} |
| 57 | + ``` |
| 58 | + Replace `{issue_number}` with the number from Step 1. |
| 59 | + |
| 60 | +- **Headers**: same as before |
| 61 | +- **Body (raw, JSON)**: |
| 62 | + ```json |
| 63 | + { |
| 64 | + "title": "Updated title from Postman", |
| 65 | + "body": "This issue has been updated via PATCH request!" |
| 66 | + } |
| 67 | + ``` |
| 68 | + |
| 69 | +You should receive a `200 OK` response with the updated issue content. |
| 70 | + |
| 71 | +## List Issues in Your Repository |
| 72 | + |
| 73 | +- **Method**: `GET` |
| 74 | +- **Endpoint**: |
| 75 | + ``` |
| 76 | + {{gh_URL}}/repos/{{owner}}/{{repo}}/issues |
| 77 | + ``` |
| 78 | + |
| 79 | +This request returns all open issues in your repository, including the one you just created. |
| 80 | + |
| 81 | +## "Delete" the Issue (Unlock) |
| 82 | + |
| 83 | +GitHub doesn’t allow true issue deletion via the API. However, you can simulate deletion in two ways: |
| 84 | +- **Close** the issue (optional) |
| 85 | +- **Unlock** it — which removes any restriction on further edits |
| 86 | + |
| 87 | +Let’s unlock it: |
| 88 | + |
| 89 | +- **Method**: `DELETE` |
| 90 | +- **Endpoint**: |
| 91 | + ``` |
| 92 | + {{gh_URL}}/repos/{{owner}}/{{repo}}/issues/{issue_number}/lock |
| 93 | + ``` |
| 94 | + |
| 95 | +If successful, you’ll receive a `204 No Content` response. |
| 96 | + |
| 97 | +## Summary |
| 98 | + |
| 99 | +By completing these steps, you've now: |
| 100 | + |
| 101 | +- Created and updated GitHub content using `POST` and `PATCH` |
| 102 | +- Retrieved structured issue data with `GET` |
| 103 | +- Performed a partial deletion (unlock) with `DELETE` |
| 104 | + |
| 105 | +You're now ready to apply what you’ve learned to real project workflows, automation tasks, or your next API-powered integration. |
0 commit comments