Files
academy-scripts/createJCAppLink
2024-02-13 17:03:06 +00:00

197 lines
4.8 KiB
Bash
Executable File

#!/bin/bash
#############################################################################
###
### this script creates a bi-directional application link between a Jira
### and a Confluence instance.
###
### Simply provide base URLs for both applications as well as a user account
### with admin privileges on both systems.
###
### (c) 2018 anaron GmbH
### Author(s): Korbinian Weidinger
### Heinz Jürgen Letsch
###
#############################################################################
########## DEFAULTS/PARAMETERS #########
JIRAAPPLABEL="Jira Schulungsumgebung"
CONFAPPLABEL="Confluence Schulungsumgebung"
CHEADER=/tmp/cheader
CURLVERBOSITY="-s -S -w %{http_code}"
CURLVERBOSITY="-s -S "
for i in "$@"
do
case $i in
-j=*|--JIRA=*)
JIRA="${i#*=}"
;;
-l=*|--JIRAlabel=*)
JIRAAPPLABEL="${i#*=}"
;;
-c=*|--CONFLUENCE=*)
CONFLUENCE="${i#*=}"
;;
-k=*|--CONFLUENCElabel=*)
CONFAPPLABEL="${i#*=}"
;;
-u=*|--username=*)
USERNAME="${i#*=}"
;;
-p=*|--password=*)
PASSWORD="${i#*=}"
;;
-d|--debug)
debug=true
CURLVERBOSITY="-D /tmp/header"
;;
*)
# unknown option
;;
esac
done
if [[ "$debug" == "true" ]]
then
echo "jira: $JIRA"
echo "confluence: $CONFLUENCE"
echo "username: $USERNAME"
echo "password: $PASSWORD"
echo "jiraLabel: $JIRAAPPLABEL"
echo "confLabel: $CONFAPPLABEL"
echo
curlverbosity="-D ${CHEADER} -w %{http_code}" # save response header in debug mode
curlverbosity="-D ${CHEADER} " # save response header in debug mode
fi
#################### FUNCTIONS ###################
###
# show brief parameter summary
###
usage() {
echo -e "\nUsage:\n$0 \n\
-u|--username=<username> \n\
-p|--password=<password> \n\
-j|--jira=<jiraURL> \n\
-c|--confluence=<confluenceURL> \n\
[-l|--jiralabel=<jiraAppLabel>] \n\
[-k|--confluencelabel=<confluenceAppLabel>]\n"
}
###
# perform oauth authentication for given token/id
# params: baseUrl id
###
doOauth()
{
response=$(curl ${CURLVERBOSITY} -u ${USERNAME}:${PASSWORD} -X PUT $1/rest/applinks/3.0/status/$2/oauth \
-H 'content-type: application/json' \
-H "origin: $1" \
-d '{"incoming":{"enabled":true,"twoLoEnabled":true,"twoLoImpersonationEnabled":true},"outgoing":{"enabled":true,"twoLoEnabled":true,"twoLoImpersonationEnabled":true}}')
if [[ "${debug}" == "true" ]]; then
cat ${CHEADER}
echo ${response}
fi
}
###
# create an application link entity
# params: baseUrl schema
###
createAppLink()
{
response=$(curl ${CURLVERBOSITY} \
-u ${USERNAME}:${PASSWORD} \
-H "X-Atlassian-Token: no-check" \
-H "Origin: $1" \
-H "Content-Type: application/json" \
-X POST $1/rest/applinks/3.0/applicationlinkForm/createAppLink \
-d "$2"
)
if [[ "${debug}" == "true" ]]; then
echo ${CHEADER}
echo ${response}
fi
id=$(echo ${response} | grep -oP '(?<=<id>).*?(?=</id>)')
if [[ -z ${id} ]]; then
$(echo {$response} | grep -oPq '502 Bad Gateway')
badGW=$?;
$(echo {$response} | grep -oPq 'error status=\"400\"')
exists=$?;
if [ "$badGW" == "0" ]; then
echo " error: instance not running!"
else
if [ "$exists" == "0" ]; then
echo " link already exists"
else
echo "ERROR: could not create app link on $1: ${response}"
fi
fi
exit 1;
fi
#echo "got applink ID: ${id}"
doOauth $1 ${id}
}
###
# prepare required data for applink creation
# params: application appLabel baseUrlLink
# application: jira|confluence
# appLabel: label of applink shown in admin GUI
# baseUrlLink: baseURL of system to be linked to
###
getApplinkSchema(){
echo "{
\"applicationLink\":
{
\"typeId\":\"$1\",
\"name\":\"$2\",
\"displayUrl\":\"$3\",
\"rpcUrl\":\"$3\",
\"isPrimary\":true,
\"isSystem\":false
},
\"username\":\"$username\",
\"password\":\"$password\",
\"customRpcURL\":false,
\"rpcUrl\":\"null\",
\"createTwoWayLink\":false,
\"orphanedTrust\":null,
\"configFormValues\":
{
\"trustEachOther\":true,
\"shareUserbase\":true
}
}"
}
#################### BODY ###################
###
# check if required parameters are set
###
if [[ -z "${JIRA}" ]] || [[ -z "${CONFLUENCE}" ]] || [[ -z "${USERNAME}" ]] || [[ -z "${PASSWORD}" ]]
then
usage
exit 1
fi
###
# create link on both systems
###
createAppLink ${JIRA} "$(getApplinkSchema "confluence" "${CONFAPPLABEL}" "${CONFLUENCE}")"
createAppLink ${CONFLUENCE} "$(getApplinkSchema "jira" "${JIRAAPPLABEL}" "${JIRA}")"