Auto Respond to GitHub Issues #5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Auto Respond to GitHub Issues | |
on: | |
issues: | |
types: [opened] | |
jobs: | |
respond: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout Repository | |
uses: actions/checkout@v3 | |
- name: Fetch Issue Data | |
id: issue | |
uses: actions/github-script@v6 | |
with: | |
script: | | |
const issue = context.payload.issue; | |
core.setOutput("number", 593); // Ensure it's a string | |
core.setOutput("body", issue.body); | |
- name: Generate Response with Gemini Pro API | |
id: generate_response | |
env: | |
GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }} | |
run: | | |
ISSUE_NUMBER="${{ steps.issue.outputs.number }}" | |
ISSUE_TEXT="${{ steps.issue.outputs.body }}" | |
echo "ISSUE_NUMBER=$ISSUE_NUMBER" >> $GITHUB_ENV | |
echo "ISSUE_TEXT=$ISSUE_TEXT" >> $GITHUB_ENV | |
RESPONSE=$(curl -s -X POST \ | |
"https://generativelanguage.googleapis.com/v1/models/gemini-pro:generateContent?key=$GEMINI_API_KEY" \ | |
-H "Content-Type: application/json" \ | |
-d '{ | |
"contents": [ | |
{ | |
"parts": [ | |
{"text": "Respond to this GitHub issue in a helpful and professional manner:\n\n'"$ISSUE_TEXT"'"} | |
] | |
} | |
] | |
}') | |
RESPONSE_TEXT=$(echo "$RESPONSE" | jq -r '.candidates[0].content.parts[0].text' | sed 's/"/\\"/g' | sed ':a;N;$!ba;s/\n/\\n/g') | |
echo "response=$RESPONSE_TEXT" >> $GITHUB_ENV | |
- name: Post Response as Comment | |
uses: actions/github-script@v6 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const issueNumber = process.env.ISSUE_NUMBER; // Correct way to get env variables | |
const response = process.env.response; | |
console.log("Issue Number:", issueNumber); // Debugging log | |
if (!issueNumber) { | |
throw new Error("Issue number is missing!"); | |
} | |
await github.rest.issues.createComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: parseInt(issueNumber), // Ensure it's a number | |
body: response | |
}); |