Created
July 13, 2016 22:02
-
-
Save jinatonic/1ff77ad5b1f5115d13531cb0ba9ec94c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# To use this file, add the following to your ~/.bashrc or ~/.bash_profile | |
# | |
# export ASANA_TOKEN="ADD YOUR ASANA TOKEN" | |
# if [ -f ~/.arc-helper.bash ] ; then | |
# . ~/.arc-helper.bash | |
# fi | |
# | |
# The ASANA_TOKEN can be found by going into your Asana profile settings, click on "Apps", click | |
# on "Manage Developer Apps", and then "+ Create New Personal Access Token". | |
# | |
# Oh, also don't forget to set $phab_re and $under_review_tag to your custom values! To find this tag id, search for | |
# the tag in asaan and copy the last part of the url. | |
# | |
# | |
# Bash script that exposes three new functions: | |
# 1. ad - arc diff, then performs post processing to parse out all asana urls, comment on the | |
# asana task if the revision isn't already commented, and add the 'Under review' tag. | |
# | |
# Blah | |
# | |
# Summary: closes: https://app.asana.com/0/1234/5678 | |
# | |
# Test Plan: blah | |
# | |
# Reviewers: jin | |
# | |
# Differential Revision: https://phabricator.abc.com/D0000 | |
# | |
# The above example will parse out asana task 5678, check if the task already | |
# has phabricator link https://phabricator.abc.com/D0000 associated with it. If | |
# it doesn't, add the link as a comment. Finally it adds the 'Under review' tag. | |
# | |
# 2. al - arc land, then performs post processing to parse out all of the "closes: " lines | |
# and attempt to parse out the asana task number from it. e.g. | |
# | |
# closes: https://app.asana.com/0/1234/5678 | |
# or | |
# closes: 5678 | |
# | |
# If the revision isn't already commented, it will add the revision as a comment and then | |
# completes the task. | |
# 3. af - arc feature... and that's it. | |
# | |
asana_url_base='https://app.asana.com/api/1.0/' | |
number_re='^[0-9]+$' | |
phab_re='MODIFY THIS TO YOUR PHABRICATOR URL https://phabricator.abc.com/D[0-9]+' | |
asana_task_url_re='https://app.asana.com/[0-9]+/[0-9]+/[0-9]+' | |
under_review_tag='ADD YOUR CUSTOM REVIEW TAG' | |
function _add_comment { | |
text=$3'+'$2 | |
echo "Commenting on task $1 with $text" | |
url=$asana_url_base"tasks/$1/stories" | |
data="task=$1&text=$text" | |
curl -s --data ${data} -H "Authorization: Bearer ${ASANA_TOKEN}" ${url} > /dev/null | |
} | |
function _mark_task_complete { | |
echo "Marking task $1 complete" | |
url=$asana_url_base"tasks/$1" | |
curl -s --request PUT -H "Authorization: Bearer ${ASANA_TOKEN}" ${url} -d 'completed=true' > /dev/null | |
} | |
function _add_under_review_tag { | |
echo "Adding 'Under review' tag to task $1" | |
url=$asana_url_base"tasks/$1/addTag" | |
data="tag=$under_review_tag" | |
curl -s --data ${data} -H "Authorization: Bearer ${ASANA_TOKEN}" ${url} > /dev/null | |
} | |
function _check_for_existing_comment { | |
# get all stories from this task | |
raw_stories=`curl -s -H "Authorization: Bearer ${ASANA_TOKEN}" $asana_url_base"tasks/$1/stories"` | |
# check if we already commented this phabricator revision | |
for phab_match in `echo $raw_stories | egrep -o $phab_re` | |
do | |
if [ "$2" = "$phab_match" ] ; then | |
echo 1 | |
return | |
fi | |
done | |
echo 0 | |
} | |
function ad { | |
# Performs `arc diff`, then do the following: | |
# finds all asana links in the commit and check if those asana tasks have the phabricator link | |
# added in the comment section. If it doesn't, add the phabricator link and add 'Under review' tag. | |
arc diff | |
local summary=`git log -1` | |
# find phabricator link | |
local phab_link=`echo $summary | egrep -o $phab_re` | |
if [[ -z $phab_link ]] ; then | |
echo 'Could not find phabricator link' | |
return | |
fi | |
for task_match in `echo $summary | egrep -o $asana_task_url_re` | |
do | |
local taskId=${task_match##*/} | |
if ! [[ $taskId =~ $number_re ]] ; then | |
continue | |
fi | |
if [ $(_check_for_existing_comment $taskId $phab_link) -eq 0 ] ; then | |
# Only add the phabricator link if it doesn't exist in the comment section yet | |
_add_comment $taskId $phab_link 'Task+tagged+by' | |
fi | |
# Send API request to add the 'Under review' tag | |
_add_under_review_tag $taskId | |
done | |
} | |
function al { | |
# Performs `arc land`, then do the following: | |
# Look for all 'closes: $asana_link' in the commit | |
# for each $asana_link, add a comment with phabricator link if it doesn't already exist | |
# then mark the $asana_link as complete. | |
arc land | |
local summary=`git log -1` | |
# find phabricator link | |
local phab_link=`echo $summary | egrep -o $phab_re` | |
if [[ -z $phab_link ]] ; then | |
echo 'Could not find phabricator link' | |
return | |
fi | |
for match in `echo $summary | egrep -o 'closes:.*[0-9]+'` | |
do | |
local taskId=${match##*/} | |
if ! [[ $taskId =~ $number_re ]] ; then | |
continue | |
fi | |
if [ $(_check_for_existing_comment $taskId $phab_link) -eq 0 ] ; then | |
# Only add the phabricator link if it doesn't exist in the comment section yet | |
_add_comment $taskId $phab_link 'Task+completed+by' | |
fi | |
# Send API request to complete the task | |
_mark_task_complete $taskId | |
done | |
} | |
function af { | |
arc feature $1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment