github action, summarize.py 추가 #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: PR Blog Summarizer | |
on: | |
pull_request: | |
types: [opened, synchronize] | |
jobs: | |
summarize: | |
runs-on: ubuntu-latest | |
steps: | |
# Step 1: Check out the code | |
- name: Check out code | |
uses: actions/checkout@v3 | |
with: | |
fetch-depth: 0 # Fetch all history for all branches | |
# Step 2: Set up Git to fetch all branches | |
- name: Set up Git | |
run: | | |
git fetch origin | |
# Set up Python | |
- name: Set up Python | |
uses: actions/setup-python@v4 | |
with: | |
python-version: "3.8" | |
# Install dependencies | |
- name: Install dependencies | |
run: | | |
python -m pip install --upgrade pip | |
pip install openai requests | |
# Step 3: Get the base branch from the PR context | |
- name: Get Changed Files | |
id: get_files | |
run: | | |
# Get the changed files between the PR branch and the base branch | |
CHANGED_FILES=$(git diff --name-only ${{ github.event.before }} ${{ github.sha }}) | |
# Filter the changed files for the desired pattern | |
FILTERED_FILES=$(echo "$CHANGED_FILES" | grep -E 'week[0-9]+/.*_link\.md$') | |
echo "Filtered changed files:" | |
echo "$FILTERED_FILES" | |
# Save filtered files to an output variable | |
if [ -z "$FILTERED_FILES" ]; then | |
echo "No matching files found." | |
echo "::set-output name=changed_files::" | |
else | |
echo "::set-output name=changed_files::$FILTERED_FILES" | |
fi | |
# Step 4: Extract links and summarize | |
- name: Extract Links and Summarize | |
run: | | |
if [ -z "${{ steps.get_files.outputs.changed_files }}" ]; then | |
echo "No *_link.md files found in week directories. Exiting." | |
exit 0 | |
fi | |
# Loop through each filtered file | |
IFS=$'\n' # Set the Internal Field Separator to newline | |
for file_path in ${{ steps.get_files.outputs.changed_files }}; do | |
echo "Processing file: $file_path" | |
# Extract the blog link from the file using grep and sed | |
link=$(grep 'link: ' "$file_path" | sed 's/link: //') | |
if [ -z "$link" ]; then | |
echo "No link found in the file $file_path. Skipping." | |
continue | |
fi | |
echo "Fetched link: $link" | |
# Run the Python script with the link and save the summary | |
python summarize.py "$link" > summary.txt | |
# Append the summary to the Markdown file | |
echo -e "\n$(cat summary.txt)" >> "$file_path" | |
echo "Summary appended to $file_path" | |
done | |
# Clean up the summary file | |
rm -f summary.txt | |
echo "Removed summary.txt file." | |
# Commit and push the changes | |
- name: Commit and Push Changes | |
run: | | |
git config --local user.name "github-actions[bot]" | |
git config --local user.email "github-actions[bot]@users.noreply.github.com" | |
git add "$file_path" | |
git commit -m "Add blog summary to file" | |
git push |