|
| 1 | +# Auto-Label New Issues Task |
| 2 | + |
| 3 | +This example demonstrates how to use the Rightbrain Tasks API to automatically label any new GitHub Issues created within a repository with a matching set of labels. |
| 4 | + |
| 5 | +## Configuration |
| 6 | + |
| 7 | +To run this task, you need to create a Task using the [Rightbrain Tasks API](https://docs.rightbrain.ai/api-reference/tasks/create-task) with the following configuration: |
| 8 | + |
| 9 | +### Task Options |
| 10 | + |
| 11 | + Name: GitHub Issue Auto-Labeller |
| 12 | + Description: Auto-label any newly created issues with matching defined repository labels |
| 13 | + LLM Model: gpt-4o |
| 14 | + Output Format: labels of type str |
| 15 | + Image Required: false |
| 16 | + |
| 17 | +### Prompt |
| 18 | + |
| 19 | +Here we're going to ask the selected LLM to look at all the available labels, then filter them by using the issue title and body. |
| 20 | + |
| 21 | +```` |
| 22 | + Your purpose is to analyse a list of JSON formatted labels and return a filtered list of labels that are deemed relevant to a given GitHub Issue. |
| 23 | +
|
| 24 | + Using the following JSON array of labels: |
| 25 | +
|
| 26 | + labels: |
| 27 | + ```json |
| 28 | + {labels} |
| 29 | + ``` |
| 30 | +
|
| 31 | + I'd like you to use the ``name` and `description` fields of the labels to determine what labels you think are relevant to the following GitHub Issue `title` and `body` supplied below. |
| 32 | +
|
| 33 | + title: |
| 34 | + {title} |
| 35 | +
|
| 36 | + body: |
| 37 | + {body} |
| 38 | +
|
| 39 | + If you are unsure of a given label, please lean towards omitting it rather than including it. |
| 40 | +
|
| 41 | + Return the matching labels as a list of label names. |
| 42 | +```` |
| 43 | + |
| 44 | +### GitHub Actions Workflow |
| 45 | + |
| 46 | +After creating the Task, you will receive a unique Access Token. Store this token as a GitHub Actions Repository Secret named ISSUE_LABELLER_TASK_ACCESS_TOKEN. This token will be used in our Workflow. |
| 47 | + |
| 48 | +The Workflow consists of a single Job with three key Steps: |
| 49 | + |
| 50 | +- Obtain Available Labels: Obtains all the labels defined within the target repository. |
| 51 | +- Obtain Relevant Labels: Calls the Task with the issue title, description, and repository labels for filtering. |
| 52 | +- Apply Relevant Labels: Applies the filtered labels to the newly created issue. |
| 53 | + |
| 54 | +```yaml |
| 55 | +name: auto-label new issues |
| 56 | +on: |
| 57 | + issues: |
| 58 | + types: |
| 59 | + - opened |
| 60 | +permissions: |
| 61 | + issues: write |
| 62 | + contents: read |
| 63 | +jobs: |
| 64 | + auto-label-issue: |
| 65 | + runs-on: ubuntu-latest |
| 66 | + steps: |
| 67 | + - name: Obtain Available Labels |
| 68 | + id: obtain-available-labels |
| 69 | + uses: actions/github-script@v7 |
| 70 | + with: |
| 71 | + script: | |
| 72 | + const res = await github.rest.issues.listLabelsForRepo({ |
| 73 | + owner: context.repo.owner, |
| 74 | + repo: context.repo.repo, |
| 75 | + }) |
| 76 | + return res.data |
| 77 | + - name: Obtain Relevant Labels |
| 78 | + id: obtain-relevant-labels |
| 79 | + uses: RightbrainAI/github-action-tasks@main |
| 80 | + with: |
| 81 | + task-access-token: ${{ secrets.ISSUE_LABELLER_TASK_ACCESS_TOKEN }} |
| 82 | + task-input: | |
| 83 | + { |
| 84 | + "title": ${{ toJSON(github.event.issue.title) }}, |
| 85 | + "body": ${{ toJSON(github.event.issue.body) }}, |
| 86 | + "labels": ${{ toJSON(steps.obtain-available-labels.outputs.result) }} |
| 87 | + } |
| 88 | + - name: Apply Relevant Labels |
| 89 | + uses: actions/github-script@v7 |
| 90 | + with: |
| 91 | + script: |- |
| 92 | + const api = JSON.parse(${{ toJSON(steps.obtain-relevant-labels.outputs.response) }}) |
| 93 | +
|
| 94 | + console.log(`We found ${ api.response.labels.length } matching labels`) |
| 95 | +
|
| 96 | + if (api.response.labels) { |
| 97 | + await github.request('PUT /repos/{owner}/{repo}/issues/{issue_number}/labels', { |
| 98 | + owner: context.repo.owner, |
| 99 | + repo: context.repo.repo, |
| 100 | + issue_number: ${{ github.event.issue.number }}, |
| 101 | + labels: api.response.labels, |
| 102 | + headers: { |
| 103 | + 'X-GitHub-Api-Version': '2022-11-28' |
| 104 | + } |
| 105 | + }) |
| 106 | + } |
| 107 | +``` |
0 commit comments