Skip to content

Commit

Permalink
ci: refactor deps bot (#272)
Browse files Browse the repository at this point in the history
* ci: refactor deps bot

* chore: update branch head

* ci: debug

* ci: debug

* chore: fix step id

* chore: validate branch

* ci: debug

* chore: disable bot for forks

* chore: remove PR debug

* chore: reuse dynamic values

* chore: fix env name

* chore: standardize bots

* chore: revert debug
  • Loading branch information
wellwelwel authored May 21, 2024
1 parent 96bc40f commit bd64e81
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 59 deletions.
66 changes: 33 additions & 33 deletions .github/workflows/bot_deps-docs-update.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,22 @@ name: '⏫ Bot (Website) — Dependencies'

on:
schedule:
- cron: '0 15 * * *'
- cron: '0 3 * * 1-5'
workflow_dispatch:

env:
PR_TITLE: 'chore(website): update dependencies'
BRANCH: 'deps-website'
PACKAGE_PATH: 'website'
BODY: '- 🤖 Update website dependencies'
LABEL: |
dependencies
website
jobs:
update-deps:
runs-on: ubuntu-latest
if: github.event.repository.fork == false
name: Update
steps:
- name: ➕ Actions - Checkout
Expand All @@ -19,18 +29,25 @@ jobs:
with:
node-version: '22.x'

- name: Cache Dependencies
- name: Cache dependencies
uses: actions/cache@v4
with:
path: ~/.npm
key: npm-website-${{ hashFiles('website/package-lock.json') }}
restore-keys: npm-website-
key: npm-linux-${{ hashFiles('package-lock.json') }}
restore-keys: npm-linux-

- name: 🔒 Checking if branch is main
run: |
MAIN_BRANCH=$(git remote show origin | grep 'HEAD branch' | awk '{print $NF}')
if [ "${{ env.BRANCH }}" = "$MAIN_BRANCH" ]; then
exit 1
fi
- name: Installing Dependencies
run: cd website && npm ci
- name: 📦 Installing Dependencies
run: cd ${{ env.PACKAGE_PATH }} && npm ci

- name: ⏫ Updating Dependencies
run: cd website && npm run update
run: cd ${{ env.PACKAGE_PATH }} && npm run update

- name: ⚙️ Configuring Git
run: |
Expand All @@ -47,37 +64,21 @@ jobs:
echo "changes=true" >> $GITHUB_OUTPUT
fi
- name: 🔍 Checking if "jq" is installed
if: steps.check_changes.outputs.changes == 'true'
run: sudo apt-get install -y jq

- name: 🔍 Checking if Pull Request exists
if: steps.check_changes.outputs.changes == 'true'
id: check_pr
uses: octokit/[email protected]
with:
route: GET /repos/:owner/:repo/pulls
owner: ${{ github.repository_owner }}
repo: ${{ github.event.repository.name }}
state: open
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: 🔎 Checking if PR exists and is open
- name: 🤖 Checking open PRs
if: steps.check_changes.outputs.changes == 'true'
id: set_pr_exists
run: |
echo "pr_exists=false" >> $GITHUB_OUTPUT
for pr in $(jq -r '.[] | select(.title=="chore(website): update dependencies") | .number' <<< "${{ steps.check_pr.outputs.data }}"); do
if npx tsx tools/workflows/get-prs.ts --title="${{ env.PR_TITLE }}"; then
echo "pr_exists=true" >> $GITHUB_OUTPUT
done
fi
- name: 🚀 Commiting and Pushing Changes
if: steps.check_changes.outputs.changes == 'true'
run: |
git add .
git commit -m 'chore(website): update dependencies'
git push origin HEAD:deps-docs --force
git commit -m "${{ env.PR_TITLE }}"
git push origin HEAD:refs/heads/${{ env.BRANCH }} --force
env:
GITHUB_TOKEN: ${{ secrets.PAT }}

Expand All @@ -86,9 +87,8 @@ jobs:
uses: peter-evans/create-pull-request@v6
with:
token: ${{ secrets.PAT }}
branch: deps-docs
branch: ${{ env.BRANCH }}
title: '${{ env.PR_TITLE }}'
body: ${{ env.BRANCH }}
labels: ${{ env.LABELS }}
delete-branch: true
title: 'chore(website): update dependencies'
body: '- 🤖 Update website dependencies'
labels: |
dependencies
53 changes: 27 additions & 26 deletions .github/workflows/bot_deps-update.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,21 @@ name: '⏫ Bot — Dependencies'

on:
schedule:
- cron: '0 3 * * *'
- cron: '0 15 * * 1-5'
workflow_dispatch:

env:
PR_TITLE: 'chore: update dependencies'
BRANCH: 'deps'
PACKAGE_PATH: '.'
BODY: '- 🤖 Update project dependencies'
LABEL: |
dependencies
jobs:
update-deps:
runs-on: ubuntu-latest
if: github.event.repository.fork == false
name: Update
steps:
- name: ➕ Actions - Checkout
Expand All @@ -26,11 +35,18 @@ jobs:
key: npm-linux-${{ hashFiles('package-lock.json') }}
restore-keys: npm-linux-

- name: 🔒 Checking if branch is main
run: |
MAIN_BRANCH=$(git remote show origin | grep 'HEAD branch' | awk '{print $NF}')
if [ "${{ env.BRANCH }}" = "$MAIN_BRANCH" ]; then
exit 1
fi
- name: 📦 Installing Dependencies
run: npm ci
run: cd ${{ env.PACKAGE_PATH }} && npm ci

- name: ⏫ Updating Dependencies
run: npm run update
run: cd ${{ env.PACKAGE_PATH }} && npm run update

- name: ⚙️ Configuring Git
run: |
Expand All @@ -47,35 +63,21 @@ jobs:
echo "changes=true" >> $GITHUB_OUTPUT
fi
- name: 🤖 Getting PRs
if: steps.check_changes.outputs.changes == 'true'
id: check_pr
uses: octokit/[email protected]
with:
route: GET /repos/:owner/:repo/pulls
owner: ${{ github.repository_owner }}
repo: ${{ github.event.repository.name }}
state: open
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: 🔎 Checking if PR exists
- name: 🤖 Checking open PRs
if: steps.check_changes.outputs.changes == 'true'
id: set_pr_exists
run: |
sudo apt-get install -y jq
echo "pr_exists=false" >> $GITHUB_OUTPUT
pr_titles=$(jq -r '.[] | select(.title=="chore: update dependencies") | .title' <<< "${{ steps.check_pr.outputs.data }}")
if [[ -n "$pr_titles" ]]; then
if npx tsx tools/workflows/get-prs.ts --title="${{ env.PR_TITLE }}"; then
echo "pr_exists=true" >> $GITHUB_OUTPUT
fi
- name: 🚀 Commiting and Pushing Changes
if: steps.check_changes.outputs.changes == 'true'
run: |
git add .
git commit -m 'chore: update dependencies'
git push origin HEAD:deps --force
git commit -m "${{ env.PR_TITLE }}"
git push origin HEAD:refs/heads/${{ env.BRANCH }} --force
env:
GITHUB_TOKEN: ${{ secrets.PAT }}

Expand All @@ -84,9 +86,8 @@ jobs:
uses: peter-evans/create-pull-request@v6
with:
token: ${{ secrets.PAT }}
branch: deps
branch: ${{ env.BRANCH }}
title: '${{ env.PR_TITLE }}'
body: ${{ env.BRANCH }}
labels: ${{ env.LABELS }}
delete-branch: true
title: 'chore: update dependencies'
body: '- 🤖 Update project dependencies'
labels: |
dependencies
18 changes: 18 additions & 0 deletions tools/workflows/get-prs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { getArg } from '../../src/helpers/get-arg.js';

type PR = {
title?: string;
};

(async () => {
const PRs: PR[] = await (
await fetch('https://api.github.com/repos/wellwelwel/poku/pulls?state=open')
)
.clone()
.json();

const titles = PRs.map((PR) => String(PR?.title));
const title = String(getArg('title'));

process.exit(titles.includes(title) ? 0 : 1);
})();

0 comments on commit bd64e81

Please sign in to comment.