summaryrefslogtreecommitdiff
path: root/bin/createRelease.sh
blob: 5afced8f2ae114cf0d9fbb993a8004d8bf00f05d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/bin/bash
#
# This script is used to publish a new release/version of etherpad on github
#
# Work that is done by this script:
# ETHER_REPO:
# - Add text to CHANGELOG.md
# - Replace version of etherpad in src/package.json
# - Create a release branch and push it to github
# - Merges this release branch into master branch
# - Creating the windows build and the docs
# ETHER_WEB_REPO:
# - Creating a new branch with the docs and the windows build
# - Replacing the version numbers in the index.html
# - Push this branch and merge it to master
# ETHER_REPO:
# - Create a new release on github

ETHER_REPO="https://github.com/ether/etherpad-lite.git"
ETHER_WEB_REPO="https://github.com/ether/ether.github.com.git"
TMP_DIR="/tmp/"

echo "WARNING: You can only run this script if your github api token is allowed to create and merge branches on $ETHER_REPO and $ETHER_WEB_REPO."
echo "This script automatically changes the version number in package.json and adds a text to CHANGELOG.md."
echo "When you use this script you should be in the branch that you want to release (develop probably) on latest version. Any changes that are currently not committed will be committed."
echo "-----"

# Get the latest version
LATEST_GIT_TAG=$(git tag | tail -n 1)

# Current environment
echo "Current environment: "
echo "- branch: $(git branch | grep '* ')"
echo "- last commit date: $(git show --quiet --pretty=format:%ad)"
echo "- current version: $LATEST_GIT_TAG"
echo "- temp dir: $TMP_DIR"

# Get new version number
# format: x.x.x
echo -n "Enter new version (x.x.x): "
read VERSION

# Get the message for the changelogs
read -p "Enter new changelog entries (press enter): "
tmp=$(mktemp)
"${EDITOR:-vi}" $tmp
changelogText=$(<$tmp)
echo "$changelogText"
rm $tmp

if [ "$changelogText" != "" ]; then
  changelogText="# $VERSION\n$changelogText"
fi

# get the token for the github api
echo -n "Enter your github api token: "
read API_TOKEN

function check_api_token {
  echo "Checking if github api token is valid..."
  CURL_RESPONSE=$(curl --silent -i https://api.github.com/user?access_token=$API_TOKEN | iconv -f utf8)
  HTTP_STATUS=$(echo $CURL_RESPONSE | head -1 | sed -r 's/.* ([0-9]{3}) .*/\1/')
  [[ $HTTP_STATUS != "200" ]] && echo "Aborting: Invalid github api token" && exit 1
}

function modify_files {
  # Add changelog text to first line of CHANGELOG.md
  sed -i "1s/^/${changelogText}\n/" CHANGELOG.md
  # Replace version number of etherpad in package.json
  sed -i -r "s/(\"version\"[ ]*: \").*(\")/\1$VERSION\2/" src/package.json
}

function create_release_branch {
  echo "Creating new release branch..."
  git rev-parse --verify release/$VERSION 2>/dev/null
  if [ $? == 0 ]; then
    echo "Aborting: Release branch already present"
    exit 1
  fi
  git checkout -b release/$VERSION
  [[ $? != 0 ]] && echo "Aborting: Error creating release branch" && exit 1

  echo "Committing CHANGELOG.md and package.json"
  git add CHANGELOG.md
  git add src/package.json
  git commit -m "Release version $VERSION"

  echo "Pushing release branch to github..."
  git push -u $ETHER_REPO release/$VERSION
  [[ $? != 0 ]] && echo "Aborting: Error pushing release branch to github" && exit 1
}

function merge_release_branch {
  echo "Merging release to master branch on github..."
  API_JSON=$(printf '{"base": "master","head": "release/%s","commit_message": "Merge new release into master branch!"}' $VERSION)
  CURL_RESPONSE=$(curl --silent -i -N --data "$API_JSON" https://api.github.com/repos/ether/etherpad-lite/merges?access_token=$API_TOKEN  | iconv -f utf8)
  echo $CURL_RESPONSE
  HTTP_STATUS=$(echo $CURL_RESPONSE | head -1 | sed -r 's/.* ([0-9]{3}) .*/\1/')
  [[ $HTTP_STATUS != "200" ]] && echo "Aborting: Error merging release branch on github" && exit 1
}

function create_builds {
  echo "Cloning etherpad-lite repo and ether.github.com repo..."
  cd $TMP_DIR
  rm -rf etherpad-lite ether.github.com
  git clone $ETHER_REPO --branch master
  git clone $ETHER_WEB_REPO
  echo "Creating windows build..."
  cd etherpad-lite
  bin/buildForWindows.sh
  [[ $? != 0 ]] && echo "Aborting: Error creating build for windows" && exit 1
  echo "Creating docs..."
  make docs
  [[ $? != 0 ]] && echo "Aborting: Error generating docs" && exit 1
}

function push_builds {
  cd $TMP_DIR/etherpad-lite/
  echo "Copying windows build and docs to website repo..."
  GIT_SHA=$(git rev-parse HEAD | cut -c1-10)
  mv etherpad-lite-win.zip $TMP_DIR/ether.github.com/downloads/etherpad-lite-win-$VERSION-$GIT_SHA.zip

  mv out/doc $TMP_DIR/ether.github.com/doc/v$VERSION

  cd $TMP_DIR/ether.github.com/
  sed -i "s/etherpad-lite-win.*\.zip/etherpad-lite-win-$VERSION-$GIT_SHA.zip/" index.html
  sed -i "s/$LATEST_GIT_TAG/$VERSION/g" index.html
  git checkout -b release_$VERSION
  [[ $? != 0 ]] && echo "Aborting: Error creating new release branch" && exit 1
  git add doc/
  git add downloads/
  git commit -a -m "Release version $VERSION"
  git push -u $ETHER_WEB_REPO release_$VERSION
  [[ $? != 0 ]] && echo "Aborting: Error pushing release branch to github" && exit 1
}

function merge_web_branch {
        echo "Merging release to master branch on github..."
        API_JSON=$(printf '{"base": "master","head": "release_%s","commit_message": "Release version %s"}' $VERSION $VERSION)
         CURL_RESPONSE=$(curl --silent -i -N --data "$API_JSON" https://api.github.com/repos/ether/ether.github.com/merges?access_token=$API_TOKEN | iconv -f utf8)
  echo $CURL_RESPONSE
  HTTP_STATUS=$(echo $CURL_RESPONSE | head -1 | sed -r 's/.* ([0-9]{3}) .*/\1/')
  [[ $HTTP_STATUS != "200" ]] && echo "Aborting: Error merging release branch" && exit 1
}

function publish_release {
  echo -n "Do you want to publish a new release on github (y/n)? "
  read PUBLISH_RELEASE
  if [ $PUBLISH_RELEASE = "y" ]; then
    # create a new release on github
    API_JSON=$(printf '{"tag_name": "%s","target_commitish": "master","name": "Release %s","body": "%s","draft": false,"prerelease": false}' $VERSION $VERSION $changelogText)
    CURL_RESPONSE=$(curl --silent -i -N --data "$API_JSON" https://api.github.com/repos/ether/etherpad-lite/releases?access_token=$API_TOKEN | iconv -f utf8)
    HTTP_STATUS=$(echo $CURL_RESPONSE | head -1 | sed -r 's/.* ([0-9]{3}) .*/\1/')
    [[ $HTTP_STATUS != "201" ]] && echo "Aborting: Error publishing release on github" && exit 1
  else
    echo "No release published on github!"
  fi
}

function todo_notification {
  echo "Release procedure was successful, but you have to do some steps manually:"
  echo "- Update the wiki at https://github.com/ether/etherpad-lite/wiki"
  echo "- Create a pull request on github to merge the master branch back to develop"
  echo "- Announce the new release on the mailing list, blog.etherpad.org and Twitter"
}

# Call functions
check_api_token
modify_files
create_release_branch
merge_release_branch
create_builds
push_builds
merge_web_branch
publish_release
todo_notification