create_article_pr #60
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: Generate article from Qiita with hexiita | |
on: | |
repository_dispatch: | |
types: [create_article_pr] | |
# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages | |
permissions: | |
contents: write | |
id-token: write | |
pages: write | |
pull-requests: write | |
jobs: | |
create-article-pr: | |
runs-on: ubuntu-latest | |
env: | |
TZ: Asia/Tokyo | |
GH_TOKEN: ${{ github.token }} | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- uses: actions/setup-python@v5 | |
with: | |
python-version: '3.12' | |
- name: Install Python tool | |
run: pip install matter-edit@git+https://github.com/yknz/matter-edit.git | |
- name: Install Go tool | |
run: | | |
mkdir ~/bin | |
export GOBIN=~/bin | |
go install github.com/ma91n/hexiita@latest | |
- name: Add Mask | |
run: | | |
echo -n '::add-mask::' > cmd.txt | |
jq < ${GITHUB_EVENT_PATH} -r .client_payload.article_url >> cmd.txt | |
echo -n '::add-mask::' >> cmd.txt | |
jq < ${GITHUB_EVENT_PATH} -r .client_payload.thumbnail_url >> cmd.txt | |
cat cmd.txt | |
rm cmd.txt | |
- name: Generate article | |
run: | | |
mkdir -p ~/tech-blog/source/_posts ~/tech-blog/source/images | |
cd ~/tech-blog | |
~/bin/hexiita ${{ github.event.client_payload.article_url }} | |
cd ${{ github.workspace }} | |
cp -rT ~/tech-blog . | |
- name: Update author | |
run: ls -rt source/_posts | tail -n 1 | xargs -I {} python3 -m matteredit update source/_posts/{} --params author=${{ github.event.client_payload.author }} | |
- name: Download & Update thumbnail | |
if: ${{ github.event.client_payload.thumbnail_url != '' }} | |
run: | | |
articlefilepath=`ls -rt source/_posts | tail -n 1 | xargs -I {} echo source/_posts/{}` | |
thumbnailpath=`python3 -m matteredit show $articlefilepath --param thumbnail | sed -e 's/^/\source/g'` | |
filename=`curl -JLO -w '%{filename_effective}' "${{ github.event.client_payload.thumbnail_url }}"` | |
rm $thumbnailpath | |
newthumbnailpath="${thumbnailpath%.*}.${filename##*.}" | |
mv $filename $newthumbnailpath | |
echo $newthumbnailpath | sed -e 's/source//g' | xargs -I {} python3 -m matteredit update $articlefilepath --params thumbnail={} | |
- name: Get article title | |
id: get-article-title | |
run: | | |
title=`curl -s "${{ github.event.client_payload.article_url }}" | grep -oP '(?<=<title>).*(?=\s\#.*</title>)'` | |
echo "title=$title" >> $GITHUB_OUTPUT | |
- name: Commit and push article to new branch | |
id: commit-and-push | |
run: | | |
branch=actions/run_id/${{ github.run_id }} | |
echo branch=$branch >> $GITHUB_OUTPUT | |
git config --local user.name "${{ github.actor }}" | |
git config --local user.email "${{ github.actor_id }}+${{ github.actor }}@users.noreply.github.com" | |
git checkout -b $branch | |
git add . | |
git commit -m "Add article: ${{ steps.get-article-title.outputs.title }}" | |
git push -u origin $branch | |
- name: Open pull request | |
run: | | |
gh pr create --base main --head ${{ steps.commit-and-push.outputs.branch }} --fill --body "This PR is created by GitHub Actions workflow." |