Issue to PR Automation #27
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: Issue to PR Automation | |
on: | |
issues: | |
types: [opened] | |
jobs: | |
create_pr: | |
if: contains(toJson(github.event.issue.labels), '"prompt"') | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout Repository | |
uses: actions/checkout@v3 | |
- name: Set up Git Config | |
run: | | |
git config --global user.name "github-actions[bot]" | |
git config --global user.email "github-actions[bot]@users.noreply.github.com" | |
- name: Extract Issue Data | |
id: extract_data | |
uses: actions/github-script@v6 | |
with: | |
script: | | |
const issueBody = context.payload.issue.body; | |
const issueTitle = context.payload.issue.title; | |
const lines = issueBody.split('\n'); | |
function extractSection(header) { | |
const sectionLines = []; | |
let inSection = false; | |
for (const line of lines) { | |
if (line.trim().toLowerCase() === `### ${header.toLowerCase()}`) { | |
inSection = true; | |
continue; | |
} | |
if (inSection && line.startsWith('### ')) { | |
break; | |
} | |
if (inSection) { | |
sectionLines.push(line); | |
} | |
} | |
return sectionLines.join('\n').trim(); | |
} | |
const author = extractSection('Author'); | |
const tags = extractSection('Tags'); | |
const promptContent = extractSection('Prompt'); | |
// Base64 encode the prompt content | |
const promptContentB64 = Buffer.from(promptContent, 'utf-8').toString('base64'); | |
// Set outputs | |
core.setOutput('PROMPT_NAME', issueTitle); | |
core.setOutput('AUTHOR', author); | |
core.setOutput('TAGS', tags); | |
core.setOutput('PROMPT_CONTENT_B64', promptContentB64); | |
- name: Debug Extracted Data | |
run: | | |
echo "Prompt Name: ${{ steps.extract_data.outputs.PROMPT_NAME }}" | |
echo "Author: ${{ steps.extract_data.outputs.AUTHOR }}" | |
echo "Tags: ${{ steps.extract_data.outputs.TAGS }}" | |
echo "Prompt Content:" | |
echo "${{ steps.extract_data.outputs.PROMPT_CONTENT_B64 }}" | base64 --decode | |
- name: Create Branch | |
id: create_branch | |
run: | | |
BRANCH_NAME="issue-${{ github.event.issue.number }}" | |
echo "branch=$BRANCH_NAME" >> "$GITHUB_OUTPUT" | |
git checkout -b "$BRANCH_NAME" | |
- name: Create File from Issue | |
run: | | |
# Retrieve variables | |
PROMPT_NAME="${{ steps.extract_data.outputs.PROMPT_NAME }}" | |
AUTHOR="${{ steps.extract_data.outputs.AUTHOR }}" | |
TAGS="${{ steps.extract_data.outputs.TAGS }}" | |
PROMPT_CONTENT_B64="${{ steps.extract_data.outputs.PROMPT_CONTENT_B64 }}" | |
# Decode the base64 content | |
PROMPT_CONTENT=$(echo "$PROMPT_CONTENT_B64" | base64 --decode) | |
# Sanitize prompt name to create a valid file name | |
FILE_NAME="$(echo "$PROMPT_NAME" | tr '[:upper:]' '[:lower:]' \ | |
| sed -E 's/[^a-z0-9]+/-/g; s/^-+|-+$//g')" | |
FILE_PATH="aida-prompts/_prompts/${FILE_NAME}.md" | |
echo "Creating file at $FILE_PATH" | |
mkdir -p "$(dirname "$FILE_PATH")" | |
# Write the content to the .md file using cat | |
{ | |
echo '---' | |
echo "title: \"$PROMPT_NAME\"" | |
echo "author: \"$AUTHOR\"" | |
echo "tags: [${TAGS}]" | |
echo '---' | |
echo | |
echo "$PROMPT_CONTENT" | |
} > "$FILE_PATH" | |
git add "$FILE_PATH" | |
git commit -m "Add prompt '$PROMPT_NAME' from Issue #${{ github.event.issue.number }}" | |
- name: Push Branch | |
run: | | |
git push origin HEAD | |
- name: Create Pull Request | |
uses: peter-evans/create-pull-request@v5 | |
with: | |
token: ${{ secrets.GITHUB_TOKEN }} | |
branch: ${{ steps.create_branch.outputs.branch }} | |
base: main | |
title: "Add Prompt: ${{ steps.extract_data.outputs.PROMPT_NAME }}" | |
body: | | |
Adds a new prompt from Issue #${{ github.event.issue.number }}. | |
Automatically generated by GitHub Actions. |