View ProjeQtOr On SourceForge.net
ProjeQtOr - Project Management Tool
Support us on Capterra
OIN - Open Invention Network
ProjeQtOr free project management software - Projeqtor API Bash Script With Curl - ProjeQtOr
 
 

Projeqtor API Bash Script With Curl

More
17 Feb 2022 07:38 #1 by yalovali
Hello,

I just want to share my bash script as another (if exists) example for projeqtor API automations. This script, consists of functions which, according to our specific needs, searchs and updates fields, moves activities to new projects, assigns parent activities, counts child activities etc.

This may be an example to start with, who wants to automate tasks from linux.

Have fun!

Yasin

APIKEY=XYZXXXXXXa08eaXXXXXXXX
APIKEY_LEN=128
SERVER=77.77.77.77
#sample username passwd as defined in .htaccess files in api folder
#this should run for example:
#/usr/bin/curl --silent --user XXXX:YYYYYY 77.77.77.77/api/Activity/all
#you must have jq INSTALLED
CURLOPT="--silent --user ecemtag:YALOVALIWASHERE"
URL="http://$SERVER/api"
CURL="/usr/bin/curl $CURLOPT"

readonly NOW=$(date +"%Y_%m_%d_%H%M")
readonly STARTSECONDS=$(date +'%s')
CURR=`pwd`
#SCRIPTPATH=`dirname $0`

stderr()
{
    echo "$*" 1>&2
}

fatal()
{
   stderr "FATAL: $*"
   exit 1
}

ticket_list_all()
{
        req=Ticket/list/all
        $CURL $URL/$req
}

project_list_id()
{
        $CURL $URL/Project/all  | jq '.items[].id'
}

project_get_name_by_id()
{
        id=`echo ${1} | sed -e 's/^"//' -e 's/"$//'`
        #echo [$id] 1>&2
        $CURL $URL/Project/all  | jq --arg id "$id" '.items[] | select(.id==$id) | .name'
}
project_get_id_by_name()
{
        name=`echo ${1} | sed -e 's/^"//' -e 's/"$//'`
        $CURL $URL/Project/all  | jq --arg arg1 "${name}" '.items[] | select(.name==$arg1) | .id'
}

test_project_id_name_get_test()
{
for i in `project_list_id`;
do
        PNAME=`project_get_name_by_id "$i"`
        ID=`project_get_id_by_name "$PNAME"`
        echo Project ID:$ID NAME:$PNAME
done
}

get_field_by_field()
{
        CLASS="$1"
        KEY=`echo ${2} | sed -e 's/^"//' -e 's/"$//'`
        VALUE=`echo ${3} | sed -e 's/^"//' -e 's/"$//'`
        FIELD=`echo ${4} | sed -e 's/^"//' -e 's/"$//'`

        [ -z $CLASS ] && fatal class not given
        [ -z $KEY ] && fatal id not given
        [ -z $VALUE ] && fatal value not given
        [ -z $FIELD ] && fatal field not given
        X=`$CURL $URL/$CLASS/search/$KEY="$VALUE"  | jq '.items[] | .'$FIELD`
        if [ "$X" = "null" ]
        then
                stderr "field [$FIELD] is not set for class [$CLASS]"
                echo ""
                return 1
        else
                echo "$X"
                return 0
        fi
}

get_field_by_id()
{
        CLASS="$1"
        ID=`echo ${2} | sed -e 's/^"//' -e 's/"$//'`
        FIELD="$3"
        [ -z $CLASS ] && fatal param not given
        [ -z $ID ] && fatal id not given
        [ -z $FIELD ] && fatal field not given
        X=`$CURL $URL/$CLASS/search/id="$ID"  | jq '.items[] | .'$FIELD`
        if [ "$X" = "null" ]
        then
                stderr "field [$FIELD] is not set for class [$CLASS]"
                return
        fi
        echo "$X"
}

get_field_list()
{
        CLASS="$1"
        FIELD="$2"
        SEARCH="$3"
        [ -z $CLASS ] && fatal param not given
        [ -z $FIELD ] && fatal field not given
        [ -z $SEARCH ] && SEARCH="1=1"
        $CURL $URL/$CLASS/search/$SEARCH | jq '.items[].'"$FIELD"
}

get_field_list_by_filter()
{
        CLASS="$1"
        FIELD="$2"
        FILTER="$3"
        [ -z $CLASS ] && fatal param not given
        [ -z $FIELD ] && fatal field not given
        [ -z $FILTER ] && fatal filter not given
        $CURL $URL/$CLASS/filter/$FILTER | jq '.items[].'"$FIELD"
}

curl_post()
{
        class="$1"
        data="$2"
        #stderr "DATA: $data "
        STR='require_once "/var/www/html/external/phpAES/aes.class.php";
require_once "/var/www/html/external/phpAES/aesctr.class.php";
$input="'"$data"'";
$key="'$APIKEY'";
$output=AesCtr::encrypt($input,$key,'$APIKEY_LEN');
print($output);
'
        enc=`php -r "$STR"`
        RESULT=`$CURL --data "$enc" $URL/$class | jq '.items[].apiResult,.items[].apiResultMessage'`
        LINE1=`echo "$RESULT" | head -n1 | tail -n1`
        LINE2=`echo "$RESULT" | head -n2 | tail -n1`
        if [ "$LINE1" != '"OK"' ]; then
                echo " RESULT:$LINE1 MESSAGE:$LINE2"
                return 1
        fi
        return 0
}

set_subProject()
{
        echo -n " set_subProject"
        CLASS=$1
        ID=`echo ${2} | sed -e 's/^"//' -e 's/"$//'`
        [ -z $CLASS ] && fatal class not given
        [ -z $ID ] && fatal id not given
        PNAME=`get_field_by_id $CLASS $ID nameProject`
        [ -z $PNAME ] && fatal proj not set
        #echo "ID:"$ID "nameProject:"$PNAME
        PNAME=`echo ${PNAME} | sed -e 's/^"//' -e 's/"$//'`
        OLDTEAM=`get_field_by_id $CLASS $ID sourceSubProject`
        OLDTEAM=`echo ${OLDTEAM} | sed -e 's/^"//' -e 's/"$//'`
        if [ "$OLDTEAM" = "$PNAME" ]; then
                echo -n " Skip"
                return 0
        fi
        data='{\"id\":'$ID', \"sourceSubProject\":\"'$PNAME'\"}'
        curl_post $CLASS "$data"
        [ $? -eq 0 ] && echo -n " OK" || echo -n " ERR"
}

set_team()
{
        echo -n " team_check"
        CLASS=$1
        ID=`echo ${2} | sed -e 's/^"//' -e 's/"$//'`
        [ -z $CLASS ] && fatal class not given
        [ -z $ID ] && fatal id not given
        PNAME=`get_field_by_id $CLASS $ID nameProject`
        PNAME=`echo ${PNAME} | sed -e 's/^"//' -e 's/"$//'`
        [ -z $PNAME ] && fatal proj not set
        TEAM=0
        OLDTEAM=`get_field_by_id $CLASS $ID idTeam`
        OLDTEAM=`echo ${OLDTEAM} | sed -e 's/^"//' -e 's/"$//'`
        if [ ! -z $OLDTEAM ]
        then
                echo -n " Skip No team"
                return 0
        fi
        if [ "$PNAME" = "yasin" ] || [ "$PNAME" = "yilmaz" ]; then
                TEAM=1
                echo -n " Skip yasin/yilmaz"
                return 0
        fi
        if [ "$PNAME" = "HW-EDM" ] || [ "$PNAME" = "HW-XXX" ] || [ "$PNAME" = "HW-YYY" ]; then
                TEAM=5
        fi
        if      [ "$PNAME" = "CSW-EDM" ] || [ "$PNAME" = "CSW-CCC" ] || [ "$PNAME" = "CSW-XXX" ]; then
                TEAM=3
        fi
        if [ "$PNAME" = "PSW-EDM" ] || [ "$PNAME" = "PSW-XXX" ] || [ "$PNAME" = "PSW-XXX" ]; then
                TEAM=4
        fi
        if [ "$PNAME" = "PO-XXX" ] || [ "$PNAME" = "PO-XXX" ]; then
                TEAM=1
        fi
        if [ "$PNAME" = "VIT-EDM" ] || [ "$PNAME" = "VIT-XXX" ] || [ "$PNAME" = "VIT-CCC" ]; then
                TEAM=7
        fi

        if [ $TEAM -eq 0 ]; then
                stderr "Not listed project name:$PNAME"
                return
        fi

        if [ "$OLDTEAM" = "$TEAM" ]; then
                echo -n " Skip $OLDTEAM = $TEAM"
                return 0
        fi
        #echo "$CLASS ID:"$ID "nameProject:"$PNAME" team:"$TEAM
        TEAM=`echo ${TEAM} | sed -e 's/^"//' -e 's/"$//'`
        data='{\"id\":'$ID', \"idTeam\":\"'$TEAM'\"}'
        curl_post $CLASS "$data"
        [ $? -eq 0 ] && echo -n " OK" || echo -n " ERR"
}

check_field_nonempty()
{
        CLASS=$1
        ID=`echo ${2} | sed -e 's/^"//' -e 's/"$//'`
        FIELD=$3
        DEFAULT=`echo ${4} | sed -e 's/^"//' -e 's/"$//'`
        [ -z $FIELD ] && fatal field not given
        [ -z $CLASS ] && fatal param not given
        [ -z $ID ] && fatal param not given
        DESC=`get_field_by_id $CLASS $ID $FIELD`
        DESC=`echo ${DESC} | sed -e 's/^"//' -e 's/"$//'`
        #[ -z $DESC ] && fatal desc not set
        if [ -z "$DESC" ]; then
                data='{\"id\":'$ID', \"'$FIELD'\":\"'"$DEFAULT"'\"}'
                curl_post $CLASS "$data"
                [ $? -eq 0 ] && echo -n " OK" || echo -n " ERR"
        fi
}

set_proj_from_sub()
{
        echo -n " set_proj_from_sub"
        CLASS=$1
        ID=`echo ${2} | sed -e 's/^"//' -e 's/"$//'`
        [ -z $CLASS ] && fatal param not given
        [ -z $ID ] && fatal param not given
        SUB=`get_field_by_id $CLASS $ID sourceSubProject`
        SUB=`echo ${SUB} | sed -e 's/^"//' -e 's/"$//'`
        if [ -z $SUB ]; then
                echo -n " Skip, not subProject Defined"
                return
        fi
        PROJ=0
        echo "$SUB" | grep -q YASIN
        if [ $? -eq 0 ]
        then
                PROJ=3
        fi
        echo "$SUB" | grep -q YILMAZ
        if [ $? -eq 0 ]
        then
                PROJ=4
        fi
        echo "$SUB" | grep -q EDM
        if [ $? -eq 0 ]
        then
                PROJ=3
        fi
        if [ $PROJ -eq 0 ]; then
                echo -n " Skip, sub=$SUB"
                return 0;
        fi
        OLDTEAM=`get_field_by_id $CLASS $ID idProject`
        OLDTEAM=`echo ${OLDTEAM} | sed -e 's/^"//' -e 's/"$//'`
        if [ "$OLDTEAM" = "$PROJ" ]; then
                echo -n " Skip"
                return
        fi
        data='{\"id\":'$ID', \"idProject\":'$PROJ'}'
        RESULT=`curl_post $CLASS "$data"`
        [ $? -eq 0 ] && echo -n " $OLDTEAM->$PROJ OK" || echo -n " ERR $CLASS $ID $RESULT"
        echo "$RESULT" | grep -q "Parent activity must be in the same project"
        if [ $? -eq 0 ]
        then
                PARENT_ID=`get_field_by_id $CLASS $ID idActivity`
                PARENT_ID=`echo ${PARENT_ID} | sed -e 's/^"//' -e 's/"$//'`
                set_proj_from_sub Activity $PARENT_ID
                #exit
        fi
}

set_parent_activity()
{
        echo -n " set_parent_activity"
        CLASS=$1
        ID=`echo ${2} | sed -e 's/^"//' -e 's/"$//'`
        [ -z $CLASS ] && fatal param not given
        [ -z $ID ] && fatal param not given
        SUB=`get_field_by_id $CLASS $ID idActivity`
        SUB=`echo ${SUB} | sed -e 's/^"//' -e 's/"$//'`
        if [ ! -z $SUB ]; then
                #stderr "Already parented"
                echo -n " Skip Already parented"
                return 0
        fi
        MILESTONE=`get_field_by_id $CLASS $ID idMilestone`
        MILESTONE=`echo ${MILESTONE} | sed -e 's/^"//' -e 's/"$//'`
        MILESTONE_PARENT=""
        if [ -z $MILESTONE ]; then
                SUBPROJ=`get_field_by_id $CLASS $ID sourceSubProject`
                SUBPROJ=`echo ${SUBPROJ} | sed -e 's/^"//' -e 's/"$//'`
                if [ -z $SUBPROJ ]; then
                        echo -n " Skip No sub project assigned"
                        return 0
                fi
                MILESTONE_PARENT=`get_field_by_field Activity name \'TBD-Activities-$SUBPROJ\' id`
                MILESTONE_PARENT=`echo ${MILESTONE_PARENT} | sed -e 's/^"//' -e 's/"$//'`
                if [ -z $MILESTONE_PARENT ]; then
                        echo -n " Skip No team parent activity found $ID"
                        return 0
                fi
        else
                MILESTONE_PARENT=`get_field_by_id Milestone $MILESTONE idActivity`
                MILESTONE_PARENT=`echo ${MILESTONE_PARENT} | sed -e 's/^"//' -e 's/"$//'`
                if [ -z $MILESTONE_PARENT ]; then
                        echo -n " Skip No milestone parent activity found for MS:$MILESTONE !"
                        return 0
                fi
        fi
        if [ -z $MILESTONE_PARENT ]; then
                echo -n " Skip No parent found at all"
                return 0
        fi
        data='{\"id\":'$ID', \"idActivity\":'$MILESTONE_PARENT'}'
        curl_post $CLASS "$data"
        [ $? -eq 0 ] && echo -n " OK" || echo -n " ERR"
}

team_items()
{
        echo "team_items"
        list="ChangeRequest TestCase TestSession Ticket Decision Risk Opportunity Requirement Action Activity Meeting"
        COUNT=0
        for CLASS in $list
        do
                for ID in `get_field_list $CLASS id "idTeam%20is%20null/idProject!=4%20and%20idProject!=3"`
                do
                        COUNT=$(($COUNT + 1))
                        echo -n "$COUNT $CLASS ID:$ID "
                        set_team $CLASS "$ID"
                        echo
                done
        done
}

mark_items()
{
        echo "mark_items"
        list="ChangeRequest TestCase TestSession Ticket Decision Risk Opportunity Requirement Action Activity Meeting"
        COUNT=0
        for CLASS in $list
        do
                for ID in `get_field_list $CLASS id "sourceSubProject%20is%20null"`
                do
                        COUNT=$(($COUNT + 1))
                        echo -n "$COUNT $CLASS ID:$ID "
                        #check_field_nonempty $CLASS "$ID" nameResolution "not resolved"
                        #check_field_nonempty $CLASS "$ID" description "This field will be updated"
                        set_subProject $CLASS "$ID"
                        #set_proj_from_sub $CLASS "$ID"
                        echo
                done
        done
}

#move items to approriate projects, based on subSourceProject tags etc...
move_items()
{
        echo "move_items"
        list="ChangeRequest TestCase TestSession Ticket Decision Risk Opportunity Requirement Action Activity Meeting"
        #list="Activity"
        COUNT=0
        for CLASS in $list
        do
                for ID in `get_field_list $CLASS id "idProject!=4%20and%20idProject!=3"`
                do
                        COUNT=$(($COUNT + 1))
                        echo -n "$COUNT $CLASS ID:$ID "
                        set_proj_from_sub $CLASS "$ID"
                        echo
                done
        done
}

#assign a parent for every activity who has not got a parent, based on any possible field
parent_items()
{
        echo "parent_items"
        list="Activity"
        COUNT=0
        for CLASS in $list
        do
                for ID in `get_field_list $CLASS id "idActivity%20is%20null"`
                do
                        COUNT=$(($COUNT + 1))
                        echo -n "$COUNT $CLASS ID:$ID "
                        set_parent_activity $CLASS "$ID"
                        echo
                done
        done
}

check_if_epic_activity()
{
        echo -n " check_if_epic_activity"
        ID=`echo ${1} | sed -e 's/^"//' -e 's/"$//'`
        CLASS=Activity
        [ -z $ID ] && fatal param not given
        ActivityType=`get_field_by_field Activity id $ID idActivityType`
        ActivityType=`echo ${ActivityType} | sed -e 's/^"//' -e 's/"$//'`
        if [ $ActivityType -eq 387 ] || [ $ActivityType -eq 384 ]
        then
                echo -n " Skip"
                return 0
        fi
        echo -n " check if epic"
        C=`get_field_by_field Activity idActivity $ID id | grep -E '^"[0-9]+"$' | wc -l`
        if [ $C -gt 0 ];
        then
                echo -n " YES"
        else
                echo -n " NO"
                return 0
        fi
        #only developer tasks...
        if [ $ActivityType -eq 19 ]
        then
                data='{\"id\":'$ID', \"idActivity\":'384'}'
                curl_post $CLASS "$data"
                [ $? -eq 0 ] && echo -n " OK" || echo -n " ERR"
        fi
        return 0
}

check_if_epic()
{
        echo "check_if_epic"
        list="Activity"
        COUNT=0
        for CLASS in $list
        do
                for ID in `get_field_list $CLASS id`
                do
                        COUNT=$(($COUNT + 1))
                        echo -n "$COUNT $CLASS ID:$ID "
                        check_if_epic_activity $ID
                        echo
                done
        done
}

#Mark every item, who has at least one child.
count_num_of_children()
{
        echo "count_num_of_children"
        list="Activity"
        COUNT=0
        for CLASS in $list
        do
                for ID in `get_field_list $CLASS idActivity "idActivity%20is%20not%20null"`
                do
                        COUNT=$(($COUNT + 1))
                        ID=`echo ${ID} | sed -e 's/^"//' -e 's/"$//'`
                        echo -n "$COUNT $CLASS ID:$ID "
                        data='{\"id\":'$ID', \"numOfChildren\":'1'}'
                        curl_post $CLASS "$data"
                        [ $? -eq 0 ] && echo -n " OK" || echo -n " ERR"
                        echo
                done
        done
}

echo "
"
echo "             SERVER IS $SERVER"
echo "
"

#mark_items
#team_items
#move_items
#parent_items
#check_if_epic
count_num_of_children

 
The following user(s) said Thank You: Arkhanje, Flou, bgenolini

Please Log in or Create an account to join the conversation.

Moderators: babynusprotion
Time to create page: 0.058 seconds

Cookies settings

×

Functional Cookies

Ce site utilise des cookies pour assurer son bon fonctionnement et ne peuvent pas être désactivés de nos systèmes. Nous ne les utilisons pas à des fins publicitaires. Si ces cookies sont bloqués, certaines parties du site ne pourront pas fonctionner.

Session

Please login to see yours activities!

Other cookies

Ce site web utilise un certain nombre de cookies pour gérer, par exemple, les sessions utilisateurs.