Update JSON API (recurring job) #387
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: Update JSON API (recurring job) | |
on: | |
push: | |
branches: | |
- master | |
pull_request: | |
branches: | |
- master | |
schedule: | |
- cron: '0 0 * * *' # Runs every day at midnight | |
workflow_dispatch: | |
jobs: | |
update-api: | |
defaults: | |
run: | |
shell: bash -l {0} | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- uses: conda-incubator/setup-miniconda@v2 | |
with: | |
python-version: "3.11" | |
mamba-version: "*" | |
channels: conda-forge,alchem0x2a,defaults | |
channel-priority: true | |
activate-environment: sparc-api-test | |
- name: Install package | |
run: | | |
pip install -e ".[test]" | |
- name: Generate json api from github master | |
run: | | |
python -m sparc.docparser --git --include-subdirs | |
- name: Env variable for api version | |
run: | | |
python -c "from sparc.api import SparcAPI; import os; ver=SparcAPI('parameters.json').sparc_version; os.system(f'echo API_VERSION={ver} >> $GITHUB_ENV')" | |
- name: Test if json api is newer than current | |
id: probe | |
run: | | |
# exit 0 --> no version change | |
if python .github/workflows/api_version_probe.py; then | |
echo "UPDATE_NEEDED=false" >> $GITHUB_ENV | |
else | |
echo "UPDATE_NEEDED=true" >> $GITHUB_ENV | |
fi | |
echo UPDATE_NEEDED is "${UPDATE_NEEDED}" | |
echo API version is "${API_VERSION}" | |
continue-on-error: true | |
- name: Create PR body text | |
if: env.UPDATE_NEEDED == 'true' | |
run: | | |
touch pr_body.txt | |
echo "## Automated JSON API Update" >> pr_body.txt | |
echo "Hello! This is an automatic pull request to merge the new JSON API version **${API_VERSION}** into the master branch." >> pr_body.txt | |
echo "### Details" >> pr_body.txt | |
echo "- **Workflow Trigger**: This update is triggered by the **Update JSON API (recurring job)** workflow." >> pr_body.txt | |
echo "- **Reason for Update**: A new JSON schema version was generated that differs from the existing one in the repository." >> pr_body.txt | |
echo "### Notes" >> pr_body.txt | |
echo "- If you notice any issues or have questions regarding this update, please contact @alchem0x2a or other maintainers of the repository." >> pr_body.txt | |
echo "Thank you for keeping the SPARC-X-API project up-to-date! 🚀" >> pr_body.txt | |
cat pr_body.txt | |
- name: Create Pull Request | |
# Do not create PR from another PR | |
if: (env.UPDATE_NEEDED == 'true') && (github.event_name != 'pull_request') | |
run: | | |
git config --global user.email "[email protected]" | |
git config --global user.name "Github Action Bot" | |
echo "New version is ${API_VERSION}" | |
BRANCH_NAME="update-api-${GITHUB_RUN_ID}" | |
echo "Checking new branch ${BRANCH_NAME}" | |
git checkout -b $BRANCH_NAME | |
mv parameters.json sparc/sparc_json_api/ | |
git add sparc/sparc_json_api/parameters.json | |
git commit -m "Add new json api version ${API_VERSION}" | |
git push --force --set-upstream origin ${BRANCH_NAME} | |
gh pr create --base master \ | |
--title "[PR Bot] New JSON API version ${API_VERSION}" \ | |
--body-file pr_body.txt \ | |
-R ${{ github.repository_owner }}/SPARC-X-API | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |