|
| 1 | +name: Discussion Bot |
| 2 | + |
| 3 | +on: |
| 4 | + discussion: |
| 5 | + types: [created] |
| 6 | + |
| 7 | +env: |
| 8 | + # Must match the model used when building the index.json embeddings. |
| 9 | + OPENAI_EMBEDDING_MODEL: "text-embedding-3-small" |
| 10 | + BOT_REPO: OpenTOPAS/TOPAS-assistant-bot |
| 11 | + |
| 12 | +permissions: |
| 13 | + contents: read |
| 14 | + |
| 15 | +jobs: |
| 16 | + gpt-response: |
| 17 | + runs-on: ubuntu-latest |
| 18 | + |
| 19 | + steps: |
| 20 | + - name: Check user opted in to bot response |
| 21 | + id: optin |
| 22 | + env: |
| 23 | + BODY: ${{ github.event.discussion.body }} |
| 24 | + run: | |
| 25 | + set -euo pipefail |
| 26 | + # Match the exact checked checkbox line produced by the discussion form. |
| 27 | + # Use "--" so grep does not treat leading "-" in the pattern as an option. |
| 28 | + opt_in="- [x] I would like to receive an immediate response from the TOPAS-BOT." |
| 29 | + if printf "%s" "$BODY" | tr -d '\r' | grep -Fqi -- "$opt_in"; then |
| 30 | + echo "opted_in=true" >> "$GITHUB_OUTPUT" |
| 31 | + echo "Opt-in checkbox checked." |
| 32 | + else |
| 33 | + echo "opted_in=false" >> "$GITHUB_OUTPUT" |
| 34 | + echo "Opt-in checkbox not checked; skipping bot response." |
| 35 | + fi |
| 36 | +
|
| 37 | + - name: Checkout OpenTOPAS |
| 38 | + if: steps.optin.outputs.opted_in == 'true' |
| 39 | + uses: actions/checkout@v4 |
| 40 | + |
| 41 | + - name: Validate required secrets |
| 42 | + if: steps.optin.outputs.opted_in == 'true' |
| 43 | + env: |
| 44 | + BOT_APP_ID: ${{ secrets.BOT_APP_ID }} |
| 45 | + BOT_APP_PRIVATE_KEY: ${{ secrets.BOT_APP_PRIVATE_KEY }} |
| 46 | + OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} |
| 47 | + run: | |
| 48 | + set -euo pipefail |
| 49 | + if [ -z "${BOT_APP_ID:-}" ]; then |
| 50 | + echo "Missing required secret: BOT_APP_ID (must be the GitHub App ID)." |
| 51 | + exit 1 |
| 52 | + fi |
| 53 | + if [ -z "${BOT_APP_PRIVATE_KEY:-}" ]; then |
| 54 | + echo "Missing required secret: BOT_APP_PRIVATE_KEY (paste the full .pem contents)." |
| 55 | + exit 1 |
| 56 | + fi |
| 57 | + if [ -z "${OPENAI_API_KEY:-}" ]; then |
| 58 | + echo "Missing required secret: OPENAI_API_KEY." |
| 59 | + exit 1 |
| 60 | + fi |
| 61 | +
|
| 62 | + # Solution using the GitHub App avoids problems with private/public bot/source code repos. It works for both. |
| 63 | + - name: Create GitHub App token |
| 64 | + if: steps.optin.outputs.opted_in == 'true' |
| 65 | + id: app-token |
| 66 | + uses: actions/create-github-app-token@v1 |
| 67 | + with: |
| 68 | + app-id: ${{ secrets.BOT_APP_ID }} |
| 69 | + private-key: ${{ secrets.BOT_APP_PRIVATE_KEY }} |
| 70 | + owner: OpenTOPAS |
| 71 | + # Explicitly scope the token to the repos we need to access. |
| 72 | + # This avoids cases where the token only has access to the current repo. |
| 73 | + repositories: OpenTOPAS,TOPAS-assistant-bot |
| 74 | + |
| 75 | + - name: Debug App token repository access |
| 76 | + if: steps.optin.outputs.opted_in == 'true' |
| 77 | + env: |
| 78 | + GH_TOKEN: ${{ steps.app-token.outputs.token }} |
| 79 | + run: | |
| 80 | + set -euo pipefail |
| 81 | + echo "Token can access these repos:" |
| 82 | + gh api installation/repositories -q '.repositories[].full_name' |
| 83 | +
|
| 84 | + - name: Verify App token can access bot repo |
| 85 | + if: steps.optin.outputs.opted_in == 'true' |
| 86 | + env: |
| 87 | + GH_TOKEN: ${{ steps.app-token.outputs.token }} |
| 88 | + run: | |
| 89 | + set -euo pipefail |
| 90 | + # For private repos, GitHub returns 404 when the token lacks access. |
| 91 | + if ! gh api "repos/${BOT_REPO}" >/dev/null; then |
| 92 | + echo "GitHub App token cannot access ${BOT_REPO}." |
| 93 | + echo "Fix: ensure the GitHub App installation includes ${BOT_REPO} and has Contents: read, then re-run." |
| 94 | + exit 1 |
| 95 | + fi |
| 96 | +
|
| 97 | + - name: Verify App token has Contents read on bot repo |
| 98 | + if: steps.optin.outputs.opted_in == 'true' |
| 99 | + env: |
| 100 | + GH_TOKEN: ${{ steps.app-token.outputs.token }} |
| 101 | + run: | |
| 102 | + set -euo pipefail |
| 103 | + # Cloning requires Contents: read. Metadata-only access can still list repos but cannot read files. |
| 104 | + # This call should succeed if Contents permission has been granted AND re-approved on the installation. |
| 105 | + if ! gh api "repos/${BOT_REPO}/contents/" >/dev/null; then |
| 106 | + echo "GitHub App token can see ${BOT_REPO} but cannot read repository contents." |
| 107 | + echo "Fix: in the GitHub App settings, set Repository permissions -> Contents: Read-only," |
| 108 | + echo "then re-install (or re-configure) the app installation on ${BOT_REPO} to approve new permissions." |
| 109 | + exit 1 |
| 110 | + fi |
| 111 | +
|
| 112 | + - name: Checkout bot repo |
| 113 | + if: steps.optin.outputs.opted_in == 'true' |
| 114 | + id: bot-repo |
| 115 | + uses: actions/checkout@v4 |
| 116 | + with: |
| 117 | + repository: ${{ env.BOT_REPO }} |
| 118 | + token: ${{ steps.app-token.outputs.token }} |
| 119 | + path: topas-bot |
| 120 | + |
| 121 | + - name: Set up Python |
| 122 | + uses: actions/setup-python@v5 |
| 123 | + with: |
| 124 | + python-version: "3.11" |
| 125 | + |
| 126 | + - name: Install bot dependencies |
| 127 | + if: steps.optin.outputs.opted_in == 'true' |
| 128 | + run: pip install -r topas-bot/requirements.txt |
| 129 | + |
| 130 | + - name: Download latest index.json from bot repo releases |
| 131 | + if: steps.optin.outputs.opted_in == 'true' |
| 132 | + env: |
| 133 | + GH_TOKEN: ${{ steps.app-token.outputs.token }} |
| 134 | + run: | |
| 135 | + mkdir -p topas-bot/.bot |
| 136 | +
|
| 137 | + # Since index.json is a release asset we need to first find the API URL. |
| 138 | + # gh api returns an array of releases where [0] is the latest. |
| 139 | + # .assets loops over assets of that release then we select index.json |
| 140 | + asset_api_url="$(gh api repos/${{ env.BOT_REPO }}/releases -q '.[0].assets[] | select(.name=="index.json") | .url')" |
| 141 | + if [ -z "${asset_api_url}" ]; then |
| 142 | + echo "Could not find index.json asset on latest release in ${{ env.BOT_REPO }}" |
| 143 | + exit 1 |
| 144 | + fi |
| 145 | +
|
| 146 | + curl -fsSL \ |
| 147 | + -H "Authorization: Bearer ${GH_TOKEN}" \ |
| 148 | + -H "Accept: application/octet-stream" \ |
| 149 | + "${asset_api_url}" \ |
| 150 | + -o topas-bot/.bot/index.json |
| 151 | +
|
| 152 | + test -s topas-bot/.bot/index.json |
| 153 | +
|
| 154 | + - name: Run GPT Response Script |
| 155 | + if: steps.optin.outputs.opted_in == 'true' |
| 156 | + working-directory: topas-bot |
| 157 | + env: |
| 158 | + OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} |
| 159 | + OPENAI_EMBEDDING_MODEL: ${{ env.OPENAI_EMBEDDING_MODEL }} |
| 160 | + GITHUB_TOKEN: ${{ steps.app-token.outputs.token }} |
| 161 | + run: python .github/actions/get_gpt_response.py |
0 commit comments