Last active
April 14, 2022 06:44
-
-
Save cloudbow/efe0978a48f35ab1b07f93a5fc6dfb43 to your computer and use it in GitHub Desktop.
Create a bash script for creating jira with personal access token
This file contains hidden or 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
COMPANY_NAME="xyz" | |
export JIRA_PROJECT_KEY="abc" | |
export JIRA_HOME_URL="https://jira.$COMPANY_NAME.com" | |
export JIRA_BROWSER_URL="${JIRA_HOME_URL}/browse" | |
export JIRA_BOARD_ID="596" | |
export JIRA_DONE_TRANSITION_ID="151" | |
export JIRA_START_TRANSITION_ID="21" | |
export JIRA_REVIEW_TRANSITION_ID="61" | |
export OPEN_BROWSER=open | |
function list_tasks_last_week_completed() { | |
task end.after:today-1wk completed | |
} | |
function list_tasks_last_month_completed() { | |
task end.after:today-1mo completed | |
} | |
function list_tasks_last_2_month_completed() { | |
task end.after:today-2mo completed | |
} | |
function connect_timewarrior_to_task_warrior() { | |
if [[ ! -f ~/.task/hooks/on-modify.timewarrior ]]; then | |
cp /usr/local/share/doc/timew/ext/on-modify.timewarrior ~/.task/hooks/ | |
chmod +x ~/.task/hooks/on-modify.timewarrior | |
fi | |
} | |
function write_to_taskrc_if_not_exists() { | |
write_to_file_if_not_exists ~/.taskrc $1 | |
} | |
function add-task-rc() { | |
write_to_taskrc_if_not_exists 'uda.jira.type=string | |
uda.jira.label=JiraID | |
uda.jira.column_width=20' | |
} | |
function get-last-updated-jira() { | |
last_updated_jira="$(curl -s --location --request GET "$JIRA_HOME_URL/rest/agile/1.0/board/$JIRA_BOARD_ID/issue?jql=assignee=currentuser()+and+status=open&maxResults=1" \ | |
-X GET -H "Content-Type: application/json" -H "Authorization: Bearer $jira_token" | jq -r '.issues[0].key')" | |
echo "$last_updated_jira" | |
} | |
function rank-above-latest-issue() { | |
latest_issue=$(get-last-updated-jira) | |
jira_issue="$1" | |
curl -s --location --request PUT "$JIRA_HOME_URL/rest/agile/1.0/issue/rank" -X PUT \ | |
-H "Content-Type: application/json" -H "Authorization: Bearer $jira_token" \ | |
--data-raw '{ | |
"rankBeforeIssue": "'$latest_issue'", | |
"rankCustomFieldId": 11102, | |
"issues": [ | |
"'$jira_issue'" | |
] | |
}' | |
} | |
function create-jira-task() { | |
curl -s --location --request POST "$JIRA_HOME_URL/rest/api/2/issue/" -X POST \ | |
-H "Content-Type: application/json" -H "Authorization: Bearer $jira_token" \ | |
--data-raw '{ | |
"fields": { | |
"project": | |
{ | |
"key": "'$JIRA_PROJECT_KEY'" | |
}, | |
"summary": "'$description'", | |
"description": "'$description'", | |
"issuetype": { | |
"name": "Task" | |
}, | |
"assignee": { | |
"name": "'$USER'" | |
} | |
} | |
} | |
}' | jq -r .key | |
} | |
function get-mutated-description() { | |
description="$1" | |
prefixed="$2" | |
description_new=$description" $prefixed" | |
echo "$description_new" | |
} | |
#eg; jira-task add create my new jira | |
# Add a task to jira | |
#eg; jira-task 412 comment my comment | |
# Add a comment to a jira task | |
#eg; jira-task 412 start | |
# Start a jira task | |
#eg; jira-task 412 review | |
# Review a jira task | |
#eg; jira-task 412 done | |
# Mark a jira task as done | |
#eg; jira-task 412 status | |
# Get the status of a jira task | |
#eg; jira-task 412 assign mukjal | |
# Assign a user to a Jira task | |
#eg; jira-task 412 start-review-done | |
# Does 3 actions in one command - start, review , done | |
#eg; jira-task pull DATALAB-539 | |
# Pulls the jira from jira and add it as a tak for you. | |
#eg; jira-task 412 update | |
# Updates existing tasker task without a jira association to be associated with jira ticket number. | |
#eg; jira-task 412 done | |
# Marks the task as done. | |
function jira-task () { | |
set -x | |
if [[ $# -eq 0 ]]; then | |
echo "Usage: jira-task <task-id> <action> or jira-task <action>" | |
return 1 | |
fi | |
if [[ $USER == "root" ]]; then | |
echo "Must be run as jira user" | |
return 1 | |
fi | |
jira_token="$(cat "$HOME"/.jira_token)" | |
if [[ -z "$jira_token" ]]; then | |
echo "No jira token found" | |
return 1 | |
fi | |
if [[ "$1" == "add" ]]; then | |
shift 1 | |
description="$*" | |
jira=$(description="$description" jira_token="$jira_token" create-jira-task) | |
echo "created task $jira" | |
description_new=$(get-mutated-description "$description" "$JIRA_BROWSER_URL/$jira") | |
task add "$description_new" jira:"$jira" due:monday project:dl | |
rank-above-latest-issue "$jira" | |
elif [[ "$2" == "comment" ]]; then | |
jira=$(task _get "$1".jira) | |
shift 2 | |
comment="$*" | |
curl -s --location --request POST "$JIRA_HOME_URL/rest/api/2/issue/$jira/comment" -X POST \ | |
-H "Content-Type: application/json" -H "Authorization: Bearer $jira_token" \ | |
--data-raw '{ | |
"body": "'$comment'" | |
}' | |
elif [[ "$2" == "update" ]]; then | |
jira=$(task _get "$1".jira) | |
if [[ -z "$jira" ]]; then | |
echo "Jira does not exist for task $1" | |
description=$(task _get "$1".description) | |
jira=$(description="$description" jira_token="$jira_token" create-jira-task) | |
description_new=$(get-mutated-description "$description" "$JIRA_BROWSER_URL/$jira") | |
task "$1" modify description:"$description_new" jira:"$jira" | |
rank-above-latest-issue "$jira" | |
else | |
rank-above-latest-issue "$jira" | |
fi | |
elif [[ "$2" == "assign" ]]; then | |
jira=$(task _get "$1".jira) | |
description=$(task _get "$1".description) | |
user_name="$3" | |
description_new=$(get-mutated-description "$description" "assignee:$user_name") | |
if curl --location --request PUT "$JIRA_HOME_URL/rest/api/2/issue/$jira/assignee" -X PUT \ | |
-H "Content-Type: application/json" -H "Authorization: Bearer $jira_token" \ | |
--data-raw '{ | |
"name": "'$user_name'" | |
}'; then | |
task "$1" modify description:"$description_new" | |
fi | |
elif [[ "$2" == "start" ]]; then | |
jira=$(task _get "$1".jira) | |
if curl --location --request POST "$JIRA_HOME_URL/rest/api/2/issue/$jira/transitions" -X POST \ | |
-H "Content-Type: application/json" -H "Authorization: Bearer $jira_token" \ | |
--data-raw '{ | |
"transition": { | |
"id": "'$JIRA_START_TRANSITION_ID'" | |
} | |
}'; then | |
task "$1" start | |
fi | |
elif [[ "$2" == "review" ]]; then | |
jira=$(task _get "$1".jira) | |
curl --location --request POST "$JIRA_HOME_URL/rest/api/2/issue/$jira/transitions" -X POST \ | |
-H "Content-Type: application/json" -H "Authorization: Bearer $jira_token" \ | |
--data-raw '{ | |
"transition": { | |
"id": "'$JIRA_REVIEW_TRANSITION_ID'" | |
} | |
}' | |
elif [[ "$2" == "start-review-done" ]]; then | |
jira-task "$1" start && \ | |
jira-task "$1" review && \ | |
jira-task "$1" done | |
elif [[ "$1" == "pull" ]]; then | |
jira="$2" | |
# create 2 columns with key and summary from jira rest api | |
curl -s --location --request GET "$JIRA_HOME_URL/rest/api/2/issue/$jira" -X GET -H "Content-Type: application/json" \ | |
-H "Authorization: Bearer $jira_token" | jq -r '[.key, .fields.summary] | @tsv' | while IFS=$'\t' read -r key summary; do | |
echo "Pulling key: $key summary: $summary" | |
description_new=$(get-mutated-description "$summary" "$JIRA_BROWSER_URL/$jira") | |
task add "$description_new" jira:"$key" due:monday project:dl | |
done | |
elif [[ "$2" == "delete" ]]; then | |
jira=$(task _get "$1".jira) | |
if curl --location --request DELETE "$JIRA_HOME_URL/rest/api/2/issue/$jira" -X DELETE \ | |
-H "Content-Type: application/json" -H "Authorization: Bearer $jira_token"; then | |
task "$1" delete | |
fi | |
elif [[ "$2" == "done" ]]; then | |
jira=$(task _get "$1".jira) | |
if curl --location --request POST "$JIRA_HOME_URL/rest/api/2/issue/$jira/transitions" -X POST \ | |
-H "Content-Type: application/json" -H "Authorization: Bearer $jira_token" \ | |
--data-raw '{ | |
"transition": { | |
"id": "'$JIRA_DONE_TRANSITION_ID'" | |
} | |
}'; then | |
task "$1" done | |
fi | |
elif [[ "$2" == "status" ]]; then | |
jira=$(task _get "$1".jira) | |
curl -s --location --request GET "$JIRA_HOME_URL/rest/api/2/issue/$jira" -X GET \ | |
-H "Content-Type: application/json" -H "Authorization: Bearer $jira_token" | jq -r '.fields.status.name' | |
else | |
# command makes it so that we don't recurse into our function | |
echo "The command you are typing is not recogniesd by jira-task" | |
return 1 | |
fi | |
set +x | |
} | |
#jira-task add "test" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment