Skip to content

github action, summarize.py 추가 #12

github action, summarize.py 추가

github action, summarize.py 추가 #12

Workflow file for this run

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
# 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 2: Get Changed Files
- 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 }})
# Check if there are any changed files
if [ -z "$CHANGED_FILES" ]; then
echo "No changed files found."
echo "changed_files=" >> $GITHUB_ENV
else
echo "Changed files found:"
echo "$CHANGED_FILES"
# 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 "changed_files=" >> $GITHUB_ENV
else
echo "changed_files=$FILTERED_FILES" >> $GITHUB_ENV
fi
fi
# Step 3: Extract Links and Summarize
- name: Extract Links and Summarize
env:
OPENAI_API_KEY: ${{ secrets.GPT_KEY }}
run: |
if [ -z "${{ env.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 ${{ env.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 the changes
- name: Commit changes
if: github.actor != 'github-actions[bot]' # Bot이 아닐 때만 실행
run: |
git config --local user.email "${{ github.actor }}@users.noreply.github.com"
git config --local user.name "${{ github.actor }}"
git add ${{ env.changed_files }} # 변경된 파일을 추가
git commit -m "Update blog summaries" # 커밋 메시지 작성
git push origin HEAD # 현재 브랜치에 푸시
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}