|
| 1 | +name: Auto-Answer Issues |
| 2 | + |
| 3 | +on: |
| 4 | + issues: |
| 5 | + types: [opened, labeled] |
| 6 | + |
| 7 | +permissions: |
| 8 | + issues: write |
| 9 | + contents: read |
| 10 | + |
| 11 | +jobs: |
| 12 | + answer-issue: |
| 13 | + runs-on: ubuntu-latest |
| 14 | + # Only run for issues created by org members or owners (i.e., Microsoft Open Source enterprise members). |
| 15 | + # github.event.issue.author_association is set by GitHub based on the issue author's relationship |
| 16 | + # to this repository. MEMBER = org member, OWNER = repo/org owner. This prevents untrusted |
| 17 | + # external contributors from triggering the Azure OpenAI-backed responder and consuming secrets/tokens. |
| 18 | + if: | |
| 19 | + github.event.issue.author_association == 'MEMBER' || |
| 20 | + github.event.issue.author_association == 'OWNER' || |
| 21 | + github.event.issue.author_association == 'COLLABORATOR' |
| 22 | + steps: |
| 23 | + - name: Checkout repository |
| 24 | + uses: actions/checkout@v4 |
| 25 | + |
| 26 | + - name: Setup Node.js |
| 27 | + uses: actions/setup-node@v4 |
| 28 | + with: |
| 29 | + node-version: '20' |
| 30 | + |
| 31 | + - name: Install dependencies |
| 32 | + run: npm install @octokit/rest openai |
| 33 | + |
| 34 | + - name: Generate and post response |
| 35 | + env: |
| 36 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 37 | + AZURE_OPENAI_API_KEY: ${{ secrets.AZURE_OPENAI_API_KEY }} |
| 38 | + AZURE_OPENAI_ENDPOINT: ${{ secrets.AZURE_OPENAI_ENDPOINT }} |
| 39 | + AZURE_OPENAI_DEPLOYMENT: ${{ secrets.AZURE_OPENAI_DEPLOYMENT }} |
| 40 | + AZURE_OPENAI_API_VERSION: "2024-12-01-preview" |
| 41 | + ISSUE_NUMBER: ${{ github.event.issue.number }} |
| 42 | + ISSUE_TITLE: ${{ github.event.issue.title }} |
| 43 | + ISSUE_BODY: ${{ github.event.issue.body }} |
| 44 | + REPO_OWNER: ${{ github.repository_owner }} |
| 45 | + REPO_NAME: ${{ github.event.repository.name }} |
| 46 | + run: | |
| 47 | + node - << 'EOF' |
| 48 | + async function main() { |
| 49 | + // Use dynamic import() for ESM-only packages (openai v4+ and @octokit/rest v19+ |
| 50 | + // are ESM-only; dynamic import() works from CommonJS in Node 20). |
| 51 | + const { Octokit } = await import("@octokit/rest"); |
| 52 | + const { AzureOpenAI } = await import("openai"); |
| 53 | + const { |
| 54 | + GITHUB_TOKEN, |
| 55 | + AZURE_OPENAI_API_KEY, |
| 56 | + AZURE_OPENAI_ENDPOINT, |
| 57 | + AZURE_OPENAI_DEPLOYMENT, |
| 58 | + AZURE_OPENAI_API_VERSION, |
| 59 | + ISSUE_NUMBER, |
| 60 | + ISSUE_TITLE, |
| 61 | + ISSUE_BODY, |
| 62 | + REPO_OWNER, |
| 63 | + REPO_NAME |
| 64 | + } = process.env; |
| 65 | + |
| 66 | + if (!GITHUB_TOKEN) { |
| 67 | + throw new Error("GITHUB_TOKEN is not set."); |
| 68 | + } |
| 69 | + if (!AZURE_OPENAI_API_KEY) { |
| 70 | + throw new Error("AZURE_OPENAI_API_KEY is not set."); |
| 71 | + } |
| 72 | + if (!AZURE_OPENAI_ENDPOINT) { |
| 73 | + throw new Error("AZURE_OPENAI_ENDPOINT is not set."); |
| 74 | + } |
| 75 | + if (!AZURE_OPENAI_DEPLOYMENT) { |
| 76 | + throw new Error("AZURE_OPENAI_DEPLOYMENT is not set."); |
| 77 | + } |
| 78 | + if (!ISSUE_NUMBER || !REPO_OWNER || !REPO_NAME) { |
| 79 | + throw new Error("Required issue/repository environment variables are missing."); |
| 80 | + } |
| 81 | + |
| 82 | + const issueNumber = parseInt(ISSUE_NUMBER, 10); |
| 83 | + const title = ISSUE_TITLE || ""; |
| 84 | + const body = ISSUE_BODY || ""; |
| 85 | + |
| 86 | + const octokit = new Octokit({ auth: GITHUB_TOKEN }); |
| 87 | +
|
| 88 | + // Check if the bot has already commented on this issue to avoid duplicate responses. |
| 89 | + const comments = await octokit.paginate(octokit.issues.listComments, { |
| 90 | + owner: REPO_OWNER, |
| 91 | + repo: REPO_NAME, |
| 92 | + issue_number: issueNumber, |
| 93 | + per_page: 100 |
| 94 | + }); |
| 95 | + const botAlreadyCommented = comments.some( |
| 96 | + (comment) => comment.user?.login === "github-actions[bot]" |
| 97 | + ); |
| 98 | + if (botAlreadyCommented) { |
| 99 | + console.log("Bot has already commented on this issue. Skipping."); |
| 100 | + return; |
| 101 | + } |
| 102 | +
|
| 103 | + const openai = new AzureOpenAI({ |
| 104 | + apiKey: AZURE_OPENAI_API_KEY, |
| 105 | + endpoint: AZURE_OPENAI_ENDPOINT, |
| 106 | + deployment: AZURE_OPENAI_DEPLOYMENT, |
| 107 | + apiVersion: AZURE_OPENAI_API_VERSION |
| 108 | + }); |
| 109 | + |
| 110 | + const prompt = ` |
| 111 | +You are a helpful GitHub assistant. An issue has been opened in the repository \`${REPO_OWNER}/${REPO_NAME}\`. |
| 112 | + |
| 113 | +Title: |
| 114 | +${title} |
| 115 | + |
| 116 | +Body: |
| 117 | +${body} |
| 118 | + |
| 119 | +Please write a concise, friendly reply that: |
| 120 | +- Acknowledges the question or issue. |
| 121 | +- Asks for any missing information if needed. |
| 122 | +- Suggests next steps or possible causes based only on the information provided. |
| 123 | +- Uses markdown formatting suitable for a GitHub issue comment. |
| 124 | +`; |
| 125 | + |
| 126 | + const completion = await openai.chat.completions.create({ |
| 127 | + model: AZURE_OPENAI_DEPLOYMENT, |
| 128 | + messages: [ |
| 129 | + { role: "system", content: "You are a helpful assistant for triaging GitHub issues." }, |
| 130 | + { role: "user", content: prompt } |
| 131 | + ] |
| 132 | + }); |
| 133 | + |
| 134 | + const reply = (completion.choices[0]?.message?.content || "").trim(); |
| 135 | + if (!reply) { |
| 136 | + throw new Error("Azure OpenAI returned an empty response."); |
| 137 | + } |
| 138 | + |
| 139 | + await octokit.issues.createComment({ |
| 140 | + owner: REPO_OWNER, |
| 141 | + repo: REPO_NAME, |
| 142 | + issue_number: issueNumber, |
| 143 | + body: reply |
| 144 | + }); |
| 145 | + } |
| 146 | + |
| 147 | + main().catch((error) => { |
| 148 | + console.error("Failed to generate or post response:", error); |
| 149 | + process.exit(1); |
| 150 | + }); |
| 151 | + EOF |
0 commit comments