Skip to content

Instantly share code, notes, and snippets.

@cloudbow
Last active April 14, 2022 06:44
Show Gist options
  • Select an option

  • Save cloudbow/efe0978a48f35ab1b07f93a5fc6dfb43 to your computer and use it in GitHub Desktop.

Select an option

Save cloudbow/efe0978a48f35ab1b07f93a5fc6dfb43 to your computer and use it in GitHub Desktop.

Revisions

  1. cloudbow revised this gist Apr 14, 2022. No changes.
  2. cloudbow revised this gist Apr 14, 2022. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion jira-task-command-line-for-atlassan-jira
    Original file line number Diff line number Diff line change
    @@ -237,4 +237,5 @@ function jira-task () {
    return 1
    fi
    set +x
    }
    }
    #jira-task add "test"
  3. cloudbow revised this gist Apr 11, 2022. 1 changed file with 89 additions and 37 deletions.
    126 changes: 89 additions & 37 deletions jira-task-command-line-for-atlassan-jira
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,13 @@
    #Add the below to your .zshrc and get ability to create jira tasks from commandline. Enjoy!
    #eg; jira-task add create my new jira
    #eg; jira-task 412 comment my comment
    #eg; jira-task 412 start
    #eg; jira-task 412 stop
    #eg; jira-task 412 review
    #eg; jira-task 412 done

    COMPANY_NAME="abc"
    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
    @@ -41,6 +38,26 @@ 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 \
    @@ -71,6 +88,38 @@ function get-mutated-description() {
    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
    @@ -94,6 +143,7 @@ function 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
    @@ -104,10 +154,17 @@ function jira-task () {
    "body": "'$comment'"
    }'
    elif [[ "$2" == "update" ]]; then
    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"
    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)
    @@ -126,7 +183,7 @@ function jira-task () {
    -H "Content-Type: application/json" -H "Authorization: Bearer $jira_token" \
    --data-raw '{
    "transition": {
    "id": "21"
    "id": "'$JIRA_START_TRANSITION_ID'"
    }
    }'; then
    task "$1" start
    @@ -137,21 +194,22 @@ function jira-task () {
    -H "Content-Type: application/json" -H "Authorization: Bearer $jira_token" \
    --data-raw '{
    "transition": {
    "id": "61"
    "id": "'$JIRA_REVIEW_TRANSITION_ID'"
    }
    }'
    elif [[ "$2" == "stop" ]]; 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": "3"
    }
    }'; then
    task "$1" stop
    fi

    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 \
    @@ -164,25 +222,19 @@ function jira-task () {
    -H "Content-Type: application/json" -H "Authorization: Bearer $jira_token" \
    --data-raw '{
    "transition": {
    "id": "151"
    "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
    elif [[ "$2" == "transitions" ]]; then
    jira=$(task _get "$1".jira)
    curl -s --location --request GET "$JIRA_HOME_URL/rest/api/2/issue/$jira/transitions" -X GET \
    -H "Content-Type: application/json" -H "Authorization: Bearer $jira_token" | jq
    -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"x
    echo "The command you are typing is not recogniesd by jira-task"
    return 1
    fi
    set +x
    }
    #jira-task add "test"
    add-task-rc
    }
  4. cloudbow created this gist Apr 9, 2022.
    188 changes: 188 additions & 0 deletions jira-task-command-line-for-atlassan-jira
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,188 @@
    #Add the below to your .zshrc and get ability to create jira tasks from commandline. Enjoy!
    #eg; jira-task add create my new jira
    #eg; jira-task 412 comment my comment
    #eg; jira-task 412 start
    #eg; jira-task 412 stop
    #eg; jira-task 412 review
    #eg; jira-task 412 done

    COMPANY_NAME="abc"
    export JIRA_PROJECT_KEY="abc"
    export JIRA_HOME_URL="https://jira.$COMPANY_NAME.com"
    export JIRA_BROWSER_URL="${JIRA_HOME_URL}/browse"


    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 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"
    }


    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
    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
    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"
    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": "21"
    }
    }'; 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": "61"
    }
    }'
    elif [[ "$2" == "stop" ]]; 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": "3"
    }
    }'; then
    task "$1" stop
    fi

    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": "151"
    }
    }'; 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
    elif [[ "$2" == "transitions" ]]; then
    jira=$(task _get "$1".jira)
    curl -s --location --request GET "$JIRA_HOME_URL/rest/api/2/issue/$jira/transitions" -X GET \
    -H "Content-Type: application/json" -H "Authorization: Bearer $jira_token" | jq
    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"x
    return 1
    fi
    set +x
    }
    #jira-task add "test"
    add-task-rc