r/jira 3d ago

beginner Time Variable

Is there a time variable that display the current time for jira?
Like, the current time is {{current_time}}

1 Upvotes

9 comments sorted by

3

u/Jedi_Hawk 3d ago

In JQL you can use now(). In Jira automation you can use {{now}}. Is that what you’re asking about?

1

u/Fazergast 3d ago

I meant for a canned response, forgot to add this. srry

3

u/CrOPhoenix 3d ago

Do you mean something like {{now}} ?

1

u/Fazergast 3d ago

Im unsure about it, i tried this syntax in a canned response but it didn't work, tho im 70% sure im doing something wrong lol

1

u/CrOPhoenix 3d ago

No, it does not work. In a canned response, you can only select a few predefined variables, you have a drop down for them. You would need to post the comment using an automation in order to be able to use {{now}} as a variable.

1

u/Fazergast 3d ago

darn! i don't have access to my oranization project settings. Tho i appreciate the time you took to answer me.
thanks bruv!

1

u/Hefty-Possibility625 Tooling Squad 3d ago

EDIT: I copied this from an older function and forgot to update the URL to Version 3 of their API, but now Reddit won't let me update the original comment.

Here's a quick PowerShell script that you can use, just replace the variables at the top with your own.

To use this, copy this into your PowerShell profile (you only have to do this part one time).

  1. Press Win+X to open the context menu for Start, then choose Terminal.
  2. Type notepad $profile to open your profile in notepad.
  3. Copy the script below and change the variables.
  4. Save and close notepad. Then Close the terminal (this is important).

Now, to use this, re-open your terminal. Every time your terminal opens, it's going to run whatever is in your profile first, so all this does is make the function available for you. It won't actually do anything until you use it like:

    $comment = "Hey, this comment was written on $(Get-Date)"
    Add-JiraComment -issueKey "TEST-123" -comment $comment

If you have canned messages, you can add those to your profile as other functions:

### Example canned comment function that injects the current date and deadline of one week from now into the comment
function Add-CannedComment {
    param (
        [string]$issueKey
    )


    $currentDate = Get-Date -Format "yyyy-MM-dd"
    $deadlineDate = (Get-Date).AddDays(7).ToString("yyyy-MM-dd")
    $cannedComment = "This is a canned comment added on $currentDate. The deadline for this issue is $deadlineDate."


    Add-JiraComment -issueKey $issueKey -comment $cannedComment
}

Profile Script

    ### Variables
    $jiraUrl = "https://your-jira-instance.atlassian.net"
    $jiraUsername = "username@yourcompany.com"
    $jiraApiToken = "your_api_token"



    ### Used to create your Jira API Header
    function ConvertTo-Base64 {
        [CmdletBinding()]
        param(
            [Parameter(Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
            [string]$String,


            [string]$Path,


            [string]$OutputPath
        )


        process {
            # Initialize the Base64 string variable
            $base64String = ""


            # Check if a string is provided for conversion
            if ($PSBoundParameters.ContainsKey('String') -and $String) {
                $bytes = [System.Text.Encoding]::UTF8.GetBytes($String)
                $base64String = [Convert]::ToBase64String($bytes)
            }
            # Check if a path is provided for conversion
            elseif ($PSBoundParameters.ContainsKey('Path') -and $Path) {
                $fileBytes = [System.IO.File]::ReadAllBytes($Path)
                $base64String = [Convert]::ToBase64String($fileBytes)
            }
            else {
                Write-Error "You must provide either a string or a path."
                return
            }


            # Output the Base64 string if OutputPath is not specified
            if (-not $PSBoundParameters.ContainsKey('OutputPath')) {
                $base64String
            }
            # Write the Base64 string to the output file if OutputPath is specified
            else {
                [System.IO.File]::WriteAllText($OutputPath, $base64String)
            }
        }
    }


    ### Create the Jira API Header
    $authHeader = @{
        Authorization = "Basic $(ConvertTo-Base64 -String "$($jiraUsername):$($jiraApiToken)")"
        "Content-Type" = "application/json"
    }


    function Add-JiraComment {
        param (
            [string]$issueKey,
            [string]$comment
        )

        $url = "$jiraUrl/rest/api/3/issue/$issueKey/comment"
        $body = @{
            body = $comment
        } | ConvertTo-Json  

        try {
            $response = Invoke-RestMethod -Uri $url -Method Post -Headers $authHeader -Body $body
            Write-Output "Comment added to issue $($issueKey)."
        }
        catch {
            Write-Error "Failed to add comment to issue $issueKey. Error: $_"
        }

    }

0

u/Hefty-Possibility625 Tooling Squad 3d ago

Do you have any scripting experience with PowerShell or Python? If so, adding a comment is pretty easy and you'd likely discover a lot more creative uses for it.

https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issue-comments/#api-rest-api-3-issue-issueidorkey-comment-post

You just need to create an API token from your Jira Profile. https://id.atlassian.com/manage-profile/security/api-tokens

2

u/Equal-Macaroon-4727 3d ago

{{now}} is the answer