Skip to content

Merge pull request #299 from farid-zare/master #9

Merge pull request #299 from farid-zare/master

Merge pull request #299 from farid-zare/master #9

name: Remove Deleted Tutorials
on:
push:
branches: [ master ]
paths:
- '**.mlx'
permissions:
contents: write
jobs:
remove-deleted-files:
runs-on: self-hosted
steps:
- name: Checkout Source Repo
uses: actions/checkout@v4
with:
repository: '${{ github.repository_owner }}/COBRA.tutorials'
token: ${{ secrets.GITHUB_TOKEN }}
fetch-depth: 0
- name: Clone the destination repository
shell: bash
run: |
rm -rf cobratoolbox
echo "Cloning the destination repository: git@github.com:opencobra/cobratoolbox.git"
git clone --depth 1 --branch gh-pages "https://x-access-token:${{ secrets.DEST_REPO_TOKEN }}@github.com/opencobra/cobratoolbox.git"
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.x'
- name: Install Python dependencies
shell: bash
run: |
python -m pip install --upgrade pip
pip install beautifulsoup4
- name: Detect Deleted mlx Files
id: getDeletedFiles
shell: bash
run: |
echo "Looking for last sync commit..."
last_sync_commit=$(git log --grep="removed .pdf, .html and .m files" -n 1 --pretty=format:%H)
if [[ -z "$last_sync_commit" ]]; then
echo "No previous deletion sync commit found. Checking all history for deletions."
# Look back further for any sync commit
last_sync_commit=$(git log --grep="created .pdf, .mlx and .m files" -n 1 --pretty=format:%H)
fi
if [[ -z "$last_sync_commit" ]]; then
echo "No sync commit found. Cannot determine deletions. Exiting."
exit 0
fi
echo "Last sync commit: $last_sync_commit"
deleted_files=$(git diff --name-only --diff-filter=D "$last_sync_commit"..HEAD | grep '\.mlx' || true)
if [[ -z "$deleted_files" ]]; then
echo "No .mlx files deleted since last sync. Exiting."
exit 0
fi
deleted_files=$(echo "$deleted_files" | tr '\n' ' ')
echo "Found deleted files: $deleted_files"
echo "deleted_files=$deleted_files" >> "$GITHUB_ENV"
- name: Remove Deleted Files from Website
shell: bash
run: |
if [[ -z "${deleted_files:-}" ]]; then
echo "No files to delete. Skipping."
exit 0
fi
cd cobratoolbox
for file in $deleted_files; do
if [[ $file != "" ]]; then
echo "Processing deletion: $file"
# Remove only HTML file from website
HTML_FILE_NAME=$(basename "$file" .mlx).html
HTML_FILE_PATH="stable/tutorials/$(dirname "$file")/$HTML_FILE_NAME"
if [[ -f "$HTML_FILE_PATH" ]]; then
echo "Removing $HTML_FILE_PATH from website"
git rm "$HTML_FILE_PATH" || rm -f "$HTML_FILE_PATH"
else
echo "HTML file not found: $HTML_FILE_PATH"
fi
# Update index to remove entry and remove tutorial wrapper file
echo "Updating cobratoolbox index to remove deleted tutorial..."
python stable/remove_from_index.py "stable/tutorials/$(dirname "$file")/$HTML_FILE_NAME" || echo "Index update failed or script not found"
fi
done
cd ..
- name: Pushing the changes to website
shell: bash
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
cd cobratoolbox
# Add any remaining untracked deletions
git add -A
if git diff --cached --quiet && git diff --quiet; then
echo "No changes to commit in cobratoolbox"
else
git commit -m "Remove deleted tutorials from source repo" || echo "Commit failed"
git push "https://x-access-token:${{ secrets.DEST_REPO_TOKEN }}@github.com/opencobra/cobratoolbox.git" gh-pages
fi
cd ..
rm -rf cobratoolbox
echo "Deletion workflow completed."