Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore/publish as package #1239

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
195 changes: 195 additions & 0 deletions .github/actions/cleanup-alpha-versions/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
name: 'Cleanup Alpha Versions'
description: 'Remove alpha versions from GitHub Packages based on age or PR number'

inputs:
days:
description: 'Remove alpha versions older than this many days'
required: false
pr-number:
description: 'PR number to remove alpha versions for'
required: false
token:
description: 'GitHub token with packages write permission'
required: true
default: ${{ github.token }}
package-name:
description: 'NPM package name (defaults to package.json name)'
required: false
keep-latest:
description: 'Keep the latest X alpha versions (works alongside days and pr-number filters)'
required: false

runs:
using: "composite"
steps:
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '20'
registry-url: 'https://npm.pkg.github.com'

- name: Cleanup Alpha Versions
shell: bash
run: |
# Get the package name (either from input or package.json)
if [[ -z "${{ inputs.package-name }}" ]]; then
PACKAGE_FULL_NAME=$(node -p "require('./package.json').name")
else
PACKAGE_FULL_NAME="${{ inputs.package-name }}"
fi

echo "Working with package: $PACKAGE_FULL_NAME"

# Extract organization and package name
ORG="${PACKAGE_FULL_NAME#@}" # Remove the '@' from the start
ORG="${ORG%%/*}" # Extract the part before the first '/'
PACKAGE="${PACKAGE_FULL_NAME#*/}" # Remove everything before the first '/' (including the '/')

echo "Organization: $ORG, Package: $PACKAGE"

# Get the alpha versions and their publish dates
ALPHA_INFO=$(npm view $PACKAGE_FULL_NAME time --json | jq 'to_entries | map(select(.key | contains("-alpha"))) | map({version: .key, date: .value})')

# If there's no alpha versions, exit early
if [ -z "$ALPHA_INFO" ] || [ "$ALPHA_INFO" == "[]" ]; then
echo "No alpha versions found for package $PACKAGE_FULL_NAME"
exit 0
fi

# Fetch all versions to have complete information
ALL_VERSIONS=$(curl -s -H "Authorization: Bearer ${{ inputs.token }}" \
"https://api.github.com/orgs/$ORG/packages/npm/$PACKAGE/versions")

if [ -z "$ALL_VERSIONS" ] || [ "$ALL_VERSIONS" == "null" ]; then
echo "Error: Could not fetch versions for $PACKAGE_FULL_NAME"
echo "Response: $ALL_VERSIONS"
exit 1
fi

# Init variables from inputs
REMOVE_DAYS="${{ inputs.days }}"
PR_NUMBER="${{ inputs.pr-number }}"
KEEP_LATEST="${{ inputs.keep-latest }}"
CURRENT_DATE=$(date +%s)

# Log what we're doing
echo "Filter settings:"
[ -n "$REMOVE_DAYS" ] && echo "- Remove versions older than $REMOVE_DAYS days"
[ -n "$PR_NUMBER" ] && echo "- Remove versions for PR #$PR_NUMBER"
[ -n "$KEEP_LATEST" ] && echo "- Keep the latest $KEEP_LATEST versions"
[ -z "$REMOVE_DAYS" ] && [ -z "$PR_NUMBER" ] && [ -z "$KEEP_LATEST" ] && echo "- No filters - will remove ALL alpha versions"

# ---------- Step 1: Create a filtered list of versions to consider ----------
# First, create a file with all alpha versions and their info
echo "$ALPHA_INFO" | jq -c '.[]' > all_versions.json

# If PR filter is active, create filtered list for just that PR
if [ -n "$PR_NUMBER" ]; then
cat all_versions.json | while read -r VERSION_DATA; do
VERSION=$(echo "$VERSION_DATA" | jq -r '.version')

if echo "$VERSION" | grep -q ".pr-$PR_NUMBER-"; then
echo "$VERSION_DATA" >> filtered_versions.json
fi
done
else
# If no PR filter, use all alpha versions
cp all_versions.json filtered_versions.json
fi

# If days filter is active, remove versions newer than cutoff
if [ -n "$REMOVE_DAYS" ]; then
REMOVE_DAYS_IN_SECONDS=$((REMOVE_DAYS * 24 * 60 * 60))

if [ -f "filtered_versions.json" ]; then
rm -f temp_filtered.json

cat filtered_versions.json | while read -r VERSION_DATA; do
VERSION=$(echo "$VERSION_DATA" | jq -r '.version')
PUBLISH_DATE=$(echo "$VERSION_DATA" | jq -r '.date')
PUBLISH_DATE_SECONDS=$(date -d "$PUBLISH_DATE" +%s 2>/dev/null || date -j -f "%Y-%m-%dT%H:%M:%S.000Z" "$PUBLISH_DATE" +%s 2>/dev/null)

DIFF_SECONDS=$((CURRENT_DATE - PUBLISH_DATE_SECONDS))
if [ $DIFF_SECONDS -gt $REMOVE_DAYS_IN_SECONDS ]; then
echo "$VERSION_DATA" >> temp_filtered.json
fi
done

if [ -f "temp_filtered.json" ]; then
mv temp_filtered.json filtered_versions.json
else
# No versions match our date criteria
echo "No versions match the days filter"
rm filtered_versions.json
touch filtered_versions.json
fi
fi
fi

# ---------- Step 2: Handle keep-latest if specified ----------
if [ -n "$KEEP_LATEST" ] && [ -f "filtered_versions.json" ] && [ -s "filtered_versions.json" ]; then
# First, sort filtered versions by date (newest first)
cat filtered_versions.json | jq -s 'sort_by(.date) | reverse' > sorted_versions.json

# Calculate how many to keep vs delete
TOTAL_VERSIONS=$(cat sorted_versions.json | jq 'length')

if [ "$TOTAL_VERSIONS" -le "$KEEP_LATEST" ]; then
echo "Found $TOTAL_VERSIONS versions, which is <= $KEEP_LATEST to keep. Nothing to delete."
# Clear the filtered list since we're keeping all
rm filtered_versions.json
touch filtered_versions.json
else
# Keep only versions beyond the keep-latest count
cat sorted_versions.json | jq ".[${KEEP_LATEST}:]" > to_delete.json

cat to_delete.json
# If we have versions to delete, replace filtered list
if [ -s "to_delete.json" ]; then
rm filtered_versions.json
cat to_delete.json | jq -c '.[]' > filtered_versions.json
fi
fi
fi

# ---------- Step 3: Delete all versions in our final filtered list ----------
if [ -f "filtered_versions.json" ] && [ -s "filtered_versions.json" ]; then
echo "The following versions will be deleted:"
cat filtered_versions.json | while read -r VERSION_DATA; do
VERSION=$(echo "$VERSION_DATA" | jq -r '.version')
PUBLISH_DATE=$(echo "$VERSION_DATA" | jq -r '.date')
echo "- $VERSION (published $PUBLISH_DATE)"

# Get version ID
VERSION_INFO=$(echo "$ALL_VERSIONS" | jq -r ".[] | select(.name == \"$VERSION\")")
VERSION_ID=$(echo "$VERSION_INFO" | jq -r '.id')

if [ -z "$VERSION_ID" ] || [ "$VERSION_ID" == "null" ]; then
echo "+++++++++++++++++++++++++++++++++"
echo $ALL_VERSIONS
echo "+++++++++++++++++++++++++++++++++"
echo " Warning: Could not find ID for version $VERSION"
continue
fi

# Delete the version
DELETE_RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" -X DELETE \
-H "Authorization: Bearer ${{ inputs.token }}" \
"https://api.github.com/orgs/$ORG/packages/npm/$PACKAGE/versions/$VERSION_ID")

# Check for errors based on the HTTP status code
if [ "$DELETE_RESPONSE" -ge 200 ] && [ "$DELETE_RESPONSE" -lt 300 ]; then
echo " ✅ Successfully deleted version $VERSION"
else
echo " ❌ Error deleting version $VERSION: HTTP $DELETE_RESPONSE"
fi
done
else
echo "No versions to delete based on the specified criteria."
fi

# Clean up temp files
rm -f all_versions.json filtered_versions.json sorted_versions.json to_delete.json

env:
NODE_AUTH_TOKEN: ${{ inputs.token }}
33 changes: 33 additions & 0 deletions .github/actions/setup-node-pnpm/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: 'Setup Node and pnpm'
description: 'Setup Node.js and pnpm, install dependencies and build'
inputs:
node-version:
description: 'Node.js version'
required: false
default: '22.x'
registry:
description: 'Registry URL'
required: false
default: 'https://npm.pkg.github.com'
scope:
description: 'Scope'
required: false
default: '@factorialco'

runs:
using: "composite"
steps:
- name: Setup pnpm
uses: pnpm/action-setup@v4

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ inputs.node-version }}
registry-url: ${{ inputs.registry }}
scope: ${{ inputs.scope }}
cache: pnpm

- name: Install dependencies
run: pnpm install
shell: bash
62 changes: 62 additions & 0 deletions .github/workflows/build-and-publish-alpha.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: Build and Publish [ALPHA]

on:
issue_comment:
types: [ created ]
pull_request:
types: [ opened, synchronize ]

jobs:
publish-alpha:
if: |
(github.event_name == 'issue_comment' &&
github.event.issue.pull_request &&
contains(github.event.comment.body, 'build')) ||
github.event_name == 'pull_request'
runs-on: ubuntu-latest
permissions:
contents: write
packages: write

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}

- uses: ./.github/actions/setup-node-pnpm
name: Setup Node and pnpm

- name: Build
run: pnpm build

- name: Create new version
run: |
# Get lastest version from package.json
CURRENT_VERSION=$(node -p "require('./package.json').version")
PR_NUMBER=${{ github.event.pull_request.number }}
COMMIT_SHA=$(git rev-parse --short ${{ github.sha }})

# Create alpha version
ALPHA_VERSION="${CURRENT_VERSION}-alpha.pr-${PR_NUMBER}-$(date +%Y%m%d%H%M%S)-${COMMIT_SHA}"

# Update package.json version
pnpm version $ALPHA_VERSION --no-git-tag-version
env:
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Publish to registry
run: |
# Publish alpha version
pnpm publish --tag alpha --no-git-checks
env:
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Keep only latest 3 alpha versions for this PR
uses: ./.github/actions/cleanup-alpha-versions
with:
token: ${{ github.token }}
pr-number: ${{ github.event.pull_request.number }}
keep-latest: 3


7 changes: 1 addition & 6 deletions .github/workflows/chromatic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,7 @@ jobs:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: pnpm/action-setup@v4
- uses: actions/setup-node@v4
with:
node-version: "22.x"
cache: pnpm
- run: pnpm install
- uses: ./.github/actions/setup-node-pnpm
- name: Run Chromatic
id: chromatic
uses: chromaui/action@latest
Expand Down
6 changes: 1 addition & 5 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
- uses: actions/setup-node@v4
with:
node-version: "22.x"
cache: pnpm
- uses: ./.github/actions/setup-node-pnpm
- uses: bitovi/github-actions-storybook-to-github-pages@v1.0.3
with:
install_command: pnpm install
Expand Down
28 changes: 28 additions & 0 deletions .github/workflows/npm-publish.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Publish factorial-one package to npm

on:
release:
types: [ published ] # Triggers when a new release is published

jobs:
publish:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ github.event.release.tag_name }} # Checkout the tagged release

- uses: ./.github/actions/setup-node-pnpm
name: Setup Node and pnpm

- name: Build
run: pnpm build

- name: Publish to registry
run: pnpm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
18 changes: 3 additions & 15 deletions .github/workflows/quality.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Code Quality checks
on:
pull_request:
branches: [main]
branches: [ main ]
concurrency:
group:
${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
Expand All @@ -12,27 +12,15 @@ jobs:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
- uses: actions/setup-node@v4
with:
node-version: "22.x"
cache: pnpm
- name: Install dependencies
run: pnpm install
- uses: ./.github/actions/setup-node-pnpm
- name: Check formatting with Prettier
run: pnpm run prettier:check:ci
eslint:
name: Eslint
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
- uses: actions/setup-node@v4
with:
node-version: "22.x"
cache: pnpm
- name: Install dependencies
run: pnpm install
- uses: ./.github/actions/setup-node-pnpm
- name: Check linting with eslint
run: pnpm run lint

Loading