Skip to content

Commit

Permalink
Merge pull request #10 from msolomonTMG/configurable-jira-url
Browse files Browse the repository at this point in the history
Configurable jira url
  • Loading branch information
msolomonTMG committed Jan 22, 2016
2 parents 8f9b1e7 + cdda6f9 commit eb88a93
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 17 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
jira_credentials.txt
thankyous.txt
jira_subdomain.txt
thankyous.txt
57 changes: 42 additions & 15 deletions post-commit
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
#!/bin/sh

# Sets JIRA URL
jira_url="https://thrillistmediagroup.atlassian.net"

# Allows us to read user input below, assigns stdin to keyboard
exec < /dev/tty

# Ask for JIRA credentials and store the base64 encoded version of what the user enters
# Ask for JIRA subdomain
parse_jira_subdomain() {
echo "Please enter your JIRA subdomain"
echo "Example: I would type 'thrillistmediagroup' because my JIRA url is: https://thrillistmediagroup.atlassian.net"
read jira_subdomain;
jira_subdomain=$(printf "$jira_subdomain")

old_umask=`umask`
umask 0077
echo "$jira_subdomain" > "$jira_subdomain_file"
umask $old_umask
}

# Ask for JIRA credentials/url and store the base64 encoded version of what the user enters
parse_jira_credentials() {
echo "Please enter your jira username (not your email): ";
read username;

echo "Please enter your jira password";
echo "Please enter your jira password (nothing will show as you type): ";
stty -echo
read password;
stty echo
Expand All @@ -33,14 +43,21 @@ parse_jira_ticket() {
echo $1 | grep -e '[A-Za-z]\+-[0-9]\+' -o
}

# function for checking the existance of jira credentials
check_jira_credentials() {
# function for checking the existence of jira credentials and jira subdomain
check_jira_data() {
# If the user does not have jira credentials stored, we'll need to ask them to enter credentials
if [ -e "$jira_credentials_file" ]; then
jira_credentials=$(cat "$jira_credentials_file")
else
parse_jira_credentials
fi

# If the user does not have jira subdomain stored, we'll need to ask them to enter the subdomain
if [ -e "$jira_subdomain_file" ]; then
jira_subdomain=$(cat "$jira_subdomain_file")
else
parse_jira_subdomain
fi
}

# Sum the total amount of time logged today by the user so we can display it later
Expand All @@ -51,12 +68,12 @@ get_total_time() {
username=${usernamepassword%:*}
# Export these variables so that they can be used in the ruby file
export JIRA_CREDENTIALS=$1
export JIRA_URL=$jira_url
export JIRA_SUBDOMAIN=$2
export USERNAME=$username
# Use a ruby script to query JIRA for all the time that this user has already logged today
total_time=$(ruby `git rev-parse --git-dir`/hooks/timesheet-githook/sum_time.rb)

if [ "$total_time" = "unauthorized" ]; then
if [ "$total_time" = "unauthorized" ] || [ "$total_time" = "bad gateway" ]; then
exit
else
echo "$total_time"
Expand All @@ -69,12 +86,12 @@ log_time() {
if [ "$time" = "" ]; then
exit
else
response=$(eval curl "-D- --write-out %{http_code} --silent --output /dev/null -X POST -d '{\"timeSpent\":\"$time\",\"comment\":\"$commit_message\"}' -H 'Authorization: Basic $1' -H 'Content-Type: application/json' $jira_url/rest/api/2/issue/\"$2\"/worklog")
response=$(eval curl "-D- --write-out %{http_code} --silent --output /dev/null -X POST -d '{\"timeSpent\":\"$time\",\"comment\":\"$commit_message\"}' -H 'Authorization: Basic $1' -H 'Content-Type: application/json' https://$jira_subdomain.atlassian.net/rest/api/2/issue/\"$2\"/worklog")
if [[ $response =~ "201 Created" ]]; then
echo "[post-commit hook] $time successfully logged to $ticket!"
elif [[ $response =~ "401 Unauthorized" ]]; then
read -p "[post-commit hook] $time was not logged successfully to $ticket. If you have updated your credentials in JIRA, press 'u' to update them here so that this hook works on your next commit. Otherwise, hit enter to ignore. " option
if [[ $option == "u" ]]; then
read -p "[post-commit hook] $time was not logged successfully to $ticket. Looks like your JIRA credentials are not proper. Would you like to update them? (y/n) " option
if [[ $option == "y" ]] || [ $option == "Y" ]; then
parse_jira_credentials
else
exit
Expand All @@ -86,6 +103,13 @@ log_time() {
else
exit
fi
elif [[ $response =~ "502 Bad Gateway" ]]; then
read -p "[post-commit hook] $time was not logged successfully to $ticket. Looks like your URL is not proper. Would you like to change it? (y/n) " option
if [ $option == "y" ] || [ $option == "Y" ]; then
parse_jira_subdomain
else
exit
fi
else
read -p "[post-commit hook] $time was not logged successfully to $ticket for an unknown reason. Try again? (y/n) " option
if [ $option == "y" ] || [ $option == "Y" ]; then
Expand All @@ -101,6 +125,9 @@ log_time() {
# Read JIRA credentials if they exist
jira_credentials_file="`git rev-parse --git-dir`/hooks/timesheet-githook/jira_credentials.txt"

# Read JIRA url if it exists
jira_subdomain_file="`git rev-parse --git-dir`/hooks/timesheet-githook/jira_subdomain.txt"

# Get the branch name
git_branch=`parse_git_branch`

Expand Down Expand Up @@ -132,11 +159,11 @@ else
ticket=$ticket_in_branch
fi

# check that the jira credentials file exists
check_jira_credentials
# check that the jira credentials file and url exists
check_jira_data

# The total amount of time logged by the user today
total_time=`get_total_time $jira_credentials`
total_time=`get_total_time $jira_credentials $jira_subdomain`

# If the branch has a ticket number and the commit is not a smart commit, prompt the user for time logging
if [ "$ticket" != "" ] && [ "$is_smart_commit" = false ]; then
Expand Down
5 changes: 4 additions & 1 deletion sum_time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def sum_time(worklogs)
end

# Find all worklogs for this user today
url = "#{ENV['JIRA_URL']}/rest/tempo-timesheets/3/worklogs"
url = "https://#{ENV['JIRA_SUBDOMAIN']}.atlassian.net/rest/tempo-timesheets/3/worklogs"
headers = {
:"Authorization" => "Basic #{ENV['JIRA_CREDENTIALS']}",
:"Content-Type" => "application/json"
Expand All @@ -31,6 +31,9 @@ def sum_time(worklogs)
when 401
puts "unauthorized"
exit
when 502
puts "bad gateway"
exit
else
sum_time response
end
Expand Down

0 comments on commit eb88a93

Please sign in to comment.