Skip to content

Commit

Permalink
Merge pull request #19 from peaqnetwork/feature/1207277221740229_type…
Browse files Browse the repository at this point in the history
…s-fix

Fix: add github action for peaq-js and update types version to 0.2.13
  • Loading branch information
lavish0000 authored Jul 30, 2024
2 parents 5698339 + 3f4d344 commit e1cf39a
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 4 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Release
on:
workflow_dispatch:

permissions:
contents: write
pull-requests: write

jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
- uses: actions/setup-node@v4
with:
node-version: 20
- run: |
npm ci
bash bin/github-release.sh --verbose
env:
GH_TOKEN: ${{ github.token }}
142 changes: 142 additions & 0 deletions bin/github-release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
#!/usr/bin/env bash

set -euo pipefail

usage() {
cat <<EOF
Script for managing the GitHub release process.
It provides functionality for automatically:
• Creating GitHub pull requests for bumping version numbers
• Drafting new GitHub releases
• Publishing GitHub releases once the version lands on NPM
USAGE
bash bin/github-release.sh [-v|--verbose] [-n|--dry-run]
OPTIONS
-v | --verbose Verbose output
-n | --dry-run Dry run; don't create any commits, tags, or draft a new
release GitHub.
EXAMPLES
bash bin/github-release.sh
EOF
}

verbose="n"
dryrun="n"
while [[ $# -gt 0 ]]; do
case "$1" in
-v|--verbose)
verbose="y"
;;
-n|--dry-run)
dryrun="y"
;;
*)
usage
exit 1
;;
esac
shift
done

log() {
if [[ "$verbose" == "y" ]]; then
echo "$@"
fi
}

if [[ -n "$(git status --porcelain)" ]]; then
echo "ERROR: Dirty Git index, please commit all changes before continuing" 1>&2
if [[ "$dryrun" == "n" ]]; then
exit 1
fi
fi
if ! command -v gh &> /dev/null; then
echo "ERROR: Please install the 'gh' GitHub CLI" 1>&2
exit 1
fi

name="$(jq -r '.name' package.json)"
current="$(jq -r '.version' package.json)"
latest="$(npm view "$name" version)"

log "current: v$current"
log "latest: v$latest"

if ! printf "%s\n" "$latest" "$current" | sort -C -V; then
echo "ERROR: Latest NPM version is newer than current version" 1>&2
exit 1
fi

tag="v$current"
commit="$(git rev-parse HEAD)"
draft="$(gh release view "$tag" --json isDraft,targetCommitish --jq 'if(.isDraft) then .targetCommitish else "" end' 2>/dev/null || true)"

log "commit: ${commit}"
log "draft: ${draft:-none}"

if [[ -z "$draft" ]] && [[ "$current" == "$latest" ]]; then
# In this case, we have no existing draft, and the current version is
# the same as the latest release, so we need to create a new PR to bump
# the package version.
log "==> Bumping Version"

git fetch --tags --force --quiet
if [[ -n "$(git tag -l --points-at HEAD | awk '$1 == "'$tag'"')" ]]; then
log "no changes since latest release"
exit 0
fi

newtag="$(npm version patch --no-git-tag-version)"
log "bumping to $newtag"

branch="bump/$newtag"
if git ls-remote --heads origin | grep "refs/heads/$branch\$" >/dev/null; then
log "version bump PR already exists"
exit 0
fi
if [[ "$dryrun" == "n" ]]; then
log "creating PR bumping version"
git checkout -b "$branch"
git commit -am "Bump Version to $newtag"
git push -u origin "$branch"
gh pr create --fill
fi
elif [[ "$current" != "$latest" ]]; then
# In this case, the current version is newer that the latest released
# version on NPM, so we create or update the draft release to ensure it
# includes the latest version.
log "==> Drafting Release"

if [[ "$commit" == "$draft" ]]; then
log "draft is already at latest commit"
exit 0
fi

log "generating NPM package"
npm pack
package="${name#@}-$current.tgz"
package="${package//\//-}"

if [[ "$dryrun" == "n" ]]; then
log "drafting release with NPM tarball"
if [[ -n "$draft" ]]; then
log "cleaning up existing draft"
gh release delete "$tag" --yes
fi
gh release create "$tag" --draft --generate-notes --target "$commit" --title "$tag" "$package"
fi
else # if [[ -n "$draft" ]] && [[ "$current" == "$latest" ]]; then
# In this case, there is an existing draft, and the latest version is
# equal to it. Publish the draft release!
log "==> Publishing Release"

tag="v$current"
if [[ "$dryrun" == "n" ]]; then
log "publishing draft release"
gh release edit "$tag" --draft=false
fi
fi
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "peaq-network",
"version": "0.2.12",
"name": "peaq-js",
"version": "0.2.13",
"license": "MIT",
"scripts": {
"prepare": "ts-patch install -s"
Expand Down

0 comments on commit e1cf39a

Please sign in to comment.