|
| 1 | +#!/bin/bash |
| 2 | +set -e |
| 3 | + |
| 4 | +function get_version { |
| 5 | + echo $(jq -r .version ./package.json) |
| 6 | +} |
| 7 | + |
| 8 | +function check_tag { |
| 9 | + local tag=$1 |
| 10 | + published_tags=`curl -s https://api.github.com/repos/$REPO_DOMAIN/$REPO_NAME/tags` |
| 11 | + already_published=`echo $published_tags | jq ".[] | select(.name == \"$tag\")"` |
| 12 | + echo $already_published |
| 13 | +} |
| 14 | + |
| 15 | +function release_tag { |
| 16 | + local tag=$1 |
| 17 | + git fetch --tags |
| 18 | + local last_tag=`curl -s https://api.github.com/repos/$REPO_DOMAIN/$REPO_NAME/tags | jq --raw-output '.[0].name'` |
| 19 | + local release_notes=`git log $last_tag..HEAD --oneline` |
| 20 | + # Parse relase notes like an list (e.g): |
| 21 | + # - 12345abcd Merge pull request #1 Title |
| 22 | + # - 12345efgh commit included 1 |
| 23 | + # - 12345hijk commit included 2 |
| 24 | + local parsed_release_notes=$(echo "$release_notes" | sed -n -e 'H;${x;s/\n/\\n - /g;s/^\\n//;p;}') |
| 25 | + parsed_release_notes=`echo "$parsed_release_notes" | sed -e '${s/ \( - [^ ]* Merge pull request\)/\1/g;}'` |
| 26 | + release=`curl -H "Authorization: token $ACCESS_TOKEN" -s --data "{ |
| 27 | + \"tag_name\": \"$tag\", |
| 28 | + \"target_commitish\": \"master\", |
| 29 | + \"name\": \"$REPO_NAME-$tag\", |
| 30 | + \"body\": \"Release $tag includes the following commits: \n$parsed_release_notes\", |
| 31 | + \"draft\": false, |
| 32 | + \"prerelease\": false |
| 33 | + }" https://api.github.com/repos/$REPO_DOMAIN/$REPO_NAME/releases` |
| 34 | + echo $release | jq ".id" |
| 35 | +} |
| 36 | + |
| 37 | +version=`get_version` |
| 38 | + |
| 39 | +if [[ -z $REPO_NAME ]]; then |
| 40 | + echo "Github repository not specified" > /dev/stderr |
| 41 | + exit 1 |
| 42 | +fi |
| 43 | + |
| 44 | +repo_check=`curl -s https://api.github.com/repos/$REPO_DOMAIN/$REPO_NAME` |
| 45 | +if [[ $repo_check == *"Not Found"* ]]; then |
| 46 | + echo "Not found a Github repository for $REPO_DOMAIN/$REPO_NAME, it is not possible to publish it" > /dev/stderr |
| 47 | + exit 1 |
| 48 | +else |
| 49 | + tag=v$version |
| 50 | + already_published=`check_tag $tag` |
| 51 | + if [[ -z $already_published ]]; then |
| 52 | + echo "Releasing $tag in Github" |
| 53 | + release_id=`release_tag $tag` |
| 54 | + if [ "$release_id" == "null" ]; then |
| 55 | + echo "There was an error trying to release $tag" > /dev/stderr |
| 56 | + exit 1 |
| 57 | + else |
| 58 | + echo "Released $tag with ID $release_id" |
| 59 | + fi |
| 60 | + else |
| 61 | + echo "Skipping Github release since $tag was already released" |
| 62 | + fi |
| 63 | +fi |
0 commit comments