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

Added a script for sending commits #259

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
30 changes: 18 additions & 12 deletions workers/release/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,32 @@ This worker is needed to save releases with commits or/and source-maps uploaded

**Current implementation supports only single Rabbit prefetch count (SIMULTANEOUS_TASKS=1)**

## Source maps delivery scheme
## Delivery scheme
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## Delivery scheme
## Release delivery scheme


1. User wants to deploy project
2. He runs deploy script on the server and it runs static builder, for example Webpack.
3. After Webpack finished his job, our [Webpack Plugin](https://github.com/codex-team/hawk.webpack.plugin) gets a source maps for new bundles and sends them to us.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add item about commits

example request:

```bash
curl -F file=@"main.min.js.map" -F 'release=$RANDOM' -H "Authorization: Bearer TOKEN" http://localhost:3000/release
```
Example request:

```bash
curl --request POST \
-F 'release=Verison 1.0'\
-F 'release=Verison 1.0.1'\
-F 'commits=[{"hash":"557940a440352d9d86ad5610f2e366aafb2729e4","title":"Add some stuff","author":"[email protected]","date":"Wed May 6 13:37:00 2021 +0300"}]'\
-F "repository=https://github.com/codex-team/hawk.api.nodejs"\
-F file=@"main.min.js.map"
-H "Authorization: Bearer TOKEN" http://localhost:3000/release
-F file=@"main.min.js.map"\
-H "Authorization: Bearer INTEGRATION_TOKEN" \
http://localhost:3000/release
```

4. Collector accepts file and give a task for ReleaseWorker for saving it to DB
5. ReleaseWorker saves it to DB.
4. Collector accepts files and give a task for ReleaseWorker for saving it to the database.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
4. Collector accepts files and give a task for ReleaseWorker for saving it to the database.
4. Collector accepts commits and source map files and give a task for ReleaseWorker for saving it to the database.

5. ReleaseWorker saves commits and source maps to the database.

## Script for sending comments
To make it easier to send commits, you can use a [shell script](./scripts/commits.sh) that will take the last few commits and send them to the collector
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
To make it easier to send commits, you can use a [shell script](./scripts/commits.sh) that will take the last few commits and send them to the collector
To make it easier to send commits, you can use a [shell script](./scripts/commits.sh) that will take the last few commits and send them to the collector


#### Script arguments
| Argument name | Required | Description |
| -- | -- | -- |
| `-t` \| `--token` | Yes | Hawk integration token for your project. |
| `-r` \| `--release` | Yes | Release name. Any string that will be associated with project events. |
| `-ce` \| `--collectorEndpoint` | No | Endpoint to send release data. |
92 changes: 92 additions & 0 deletions workers/release/scripts/commits.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/bin/sh
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move it to the separate repository (hawk-release for example), because it is not related to workers.


# Script help
if [[ "$1" =~ ^-h|--help$ ]] ; then
echo "Usage: `basename $0` [-h] [-p|--path=<path>] [-t|--token=<string>] [-ce|--collectorEndpoint=<url>]
-t | --token : Hawk integration token for your project
-r | --release : Release name. Any string that will be associated with project events
-ce | --collectorEndpoint : Endpoint to send release data. (optional)
"
exit 0
fi

# Script arguments
for i in "$@"
do
case $i in
-t=*|--token=*)
token="${i#*=}"
;;

-r=*|--release=*)
release="${i#*=}"
;;

-ce=*|--collectorEndpoint=*)
collectorEndpoint="${i#*=}"
;;
esac
done

# Required fields
if [[ $release == "" ]]; then
echo "Please, provide [--]release name so we can attach commits to this release"
exit 0
fi

if [[ $token == "" ]]; then
echo "Please, provide hawk integration [--]token. You can get it in the project integration settings"
exit 0
fi

# Checking the git for availability
git --version 2>&1 >/dev/null
isGitAvailable=$?

# Collecting the last few commits
if [ $isGitAvailable -eq 0 ]; then
commits=$(git --no-pager log --no-color --pretty="{\"hash\":\"%H\",\"title\":\"%s\",\"author\":\"%ae\",\"date\":\"%ad\"}@end@")
lastCommits="[ "
commitsCounter=0
while [[ $commits ]] && [[ $commitsCounter -lt 5 ]] ; do
if [[ $commits = *@end@* ]]; then
first=${commits%%'@end@'*}
rest=${commits#*'@end@'}
else
first=$commits
rest=''
fi

let "commitsCounter+=1"
lastCommits="$lastCommits$first,"
commits=$rest
done
lastCommits="${lastCommits:0:${#lastCommits}-1}]"
else
echo "Could not find the 'git' command on the machine. You have to install it in order to send commits for release"
exit 0
fi

# Create endpoint name
if [[ $collectorEndpoint == "" ]]; then
collectorEndpoint="https://k1.hawk.so/release"
parsedToken=`echo $token | base64 --decode`
integrationId=`echo $parsedToken | awk -F '\"' '{print $4}'`

if [[ $token != "" ]]; then
collectorEndpoint="https://$integrationId.k1.hawk.so/release"
fi
fi

# Colors
CYAN='\033[1;36m'
GREEN='\033[1;32m'
BLUE='\033[1;34m'
NO_COLOR='\033[0m'

echo "${CYAN}Sending ${GREEN}$commitsCounter${CYAN} commits for the ${GREEN}'$release'${CYAN} release to ${BLUE}$collectorEndpoint${NO_COLOR}"

# Send request to the collector
curl --request POST -F release="$release" -F commits="$lastCommits" ${collectorEndpoint} -H "Authorization: Bearer $token"

echo