Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Help on VBA program to add an attachment to a Jira issue using api/2/issue/{issueIdOrKey}/attachments

Joe Gigliotti
June 10, 2016

I have successfully created an issue however if I want to attach a file to an existing issue I cannot do so using VBA.

I have successfully been able to add a file using the "curl" example here: https://docs.atlassian.com/jira/REST/latest/#api/2/issue/{issueIdOrKey}/attachments-addAttachment as per the example:
"curl -D- -u admin:admin -X POST -H "X-Atlassian-Token: no-check" -F "file=@myfile.txt" http://myhost/rest/api/2/issue/TEST-123/attachments"

It was quite easy using "curl" however I need to do it using Microsoft VB.

I get an error "FileUploadException: the request was rejected because no multipart boundary was found

Now I am new to this and know nothing about what is meant by "multipart boundary" means and I assume "curl" does some of this magic internally?

Any assistance would be greatly appreciated, here is my sample code:

 

Sub JIRA_PostAttachment()

Dim oHttp As Object
Set oHttp = CreateObject("Microsoft.XMLHTTP")

' initialize variables that we will set and pass as parameters
Dim pHtml As String
Dim strResponse As String

pHtml = "https://jira.ae.sda.corp.test.com/rest/api/2/issue/IS-163/attachments"

    '-- prepare the HTTP POST message
    Call oHttp.Open("POST", pHtml, False)
    
    ' Set headers
    oHttp.SetRequestHeader "X-Atlassian-Token", "nocheck"
    oHttp.SetRequestHeader "Content-Type", "multipart/form-data"

    oHttp.SetRequestHeader "Authorization", "Basic Yzc3NjQ2OTpHaWxpdDIwMTY/"
    
    '-- send the message
    Call oHttp.Send("file=C:\Users\c776469\FORM1.msg")
    
    strResponse = oHttp.ResponseText
    MsgBox strResponse
    
    Set oHttp = Nothing

End Sub

3 answers

0 votes
JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Champions.
June 24, 2014

Calculated fields are not really suitable for values which use "now" as an input, they will only get indexed when the issue is changed, so you can't usefully query on them.

I think this is discussed here... https://jamieechlin.atlassian.net/wiki/display/GRV/Scripted%20Fields- there are also examples similar to what you're trying to do.

Jeannette Lamb
Contributor
June 24, 2014

thanks Jamie, I actually already found that link and followed the example for "Total time this issue has been In Progress". I got it working to calculate how long the issue has been in "Acknowledged" but I can't get it to work for "Open". My workflow is Open - Acknowledged - Resolved - Closed. Any thoughts?

Thank you!

Jeannette Lamb
Contributor
June 25, 2014

Since I have been unsuccessful at calculating Time since Created. I am using a custom field called "Reported" which defaults to the current date/time or can be overwritten by the user.

Can you help me with the syntax for calculating the difference between Reported and now?

The script I am using, below, works for calculating Time since Acknowledged. Acknowledged is a workflow state, Reported is not. How can I modify this to not look for "status"?

import com.atlassian.core.util.DateUtils

import com.atlassian.jira.ComponentManager

import com.atlassian.jira.issue.history.ChangeItemBean

def componentManager = ComponentManager.getInstance()

def changeHistoryManager = componentManager.getChangeHistoryManager()

def OpenName = "Acknowledged"

def rt = [0]

changeHistoryManager.getChangeItemsForField (issue, "status").reverse().each {ChangeItemBean item ->

def timeDiff = System.currentTimeMillis() - item.created.getTime()

if (item.fromString == OpenName) {

rt << -timeDiff

}

if (item.toString == OpenName){

rt << timeDiff

}

}

// NOTE: doesn't show anything if less than 60 seconds

DateUtils.getDurationString(Math.round(rt.sum() / 1000))

Kevin Dalton
Contributor
February 18, 2016

We love this but would like to know is it possible to do this without status or is there a way to include all of our status's except close.

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Champions.
February 22, 2016

Kevin - could you ask a new question with a bit more detail... I have lost the context here. thanks.

Kevin Dalton
Contributor
February 22, 2016

We love the format of this showing weeks, days, hours, etc but we need it to be able to show total time from since open no matter the status as well as exclude weekends.  We are currently using the following

 

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.sql.Timestamp
import java.text.SimpleDateFormat

// Grab current date, and wipe out time component
Calendar cal = Calendar.getInstance()
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.set(Calendar.HOUR_OF_DAY,0);
cal.set(Calendar.MINUTE,0);
cal.set(Calendar.SECOND,0);
cal.set(Calendar.MILLISECOND,0);

// Convert to GregorianCalendar
GregorianCalendar gcal = (GregorianCalendar) cal

// Get the Values of the Start date, End Date and Half Day fields and their values
def StartDate = issue.getCreated()
def EndDate = new Timestamp(gcal.getTimeInMillis())


//initialise some variables
float NumberOfDays = 0;
float TotalNumberOfDays = 0;

// Calculate the number of days that a user has requested
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.setTime(StartDate);
cal2.setTime(EndDate);

while (cal1.before(cal2)) {
// If the days include weekends skip those days and do not count them
if ((Calendar.SATURDAY != cal1.get(Calendar.DAY_OF_WEEK))
&&(Calendar.SUNDAY != cal1.get(Calendar.DAY_OF_WEEK))) {
NumberOfDays++;
cal1.add(Calendar.DATE,1);
}else {
cal1.add(Calendar.DATE,1);
}
}

// Add on 1 day to include the start date which the calculation discards
TotalNumberOfDays = NumberOfDays + 0

return TotalNumberOfDays.toString()

federico schultz
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
June 6, 2019

Hi @Jeannette Lamb , i m sorry i m late now hahaha but... maybe it can help you or somebody: SCRIPT TO CALCULATE TOTAL DAYS IN A STATUS. IN THIS CASE "CREADO" ( CREATED)

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.history.ChangeItemBean

def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()

def inProgressName = "Creado"

List<Long> rt = [0L]
def changeItems = changeHistoryManager.getChangeItemsForField(issue, "status")

changeItems.reverse().each { ChangeItemBean item ->
def timeDiff = System.currentTimeMillis() - item.created.getTime()
if (item.fromString == inProgressName) {
rt << -timeDiff
}
if (item.toString == inProgressName) {
rt << timeDiff
}
}

def total = rt.sum() as Long
return (total / 1000/86400) as long ?: 0L

 

best wishes

0 votes
Andriy Zhdanov
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Champions.
June 24, 2014

On JIRA OnDemand it might be possible to achive something similar using Lambda plugin, e.g. expression:

issue.fields.status.name == 'Open' ? (issue.fields.created | sinceNow) : ''

Please let me know if you'd like to make it work.

0 votes
Jeannette Lamb
Contributor
June 24, 2014

I found an older post https://answers.atlassian.com/questions/95556/how-to-get-the-time-spent-report-of-issues-in-a-particular-status?page=1#96116

which partially solves my problem. I used the answer to calculate how long an issue has been in the Acknowledged state. I would also like to use it to calculate how long an issue has been in the Open state. The problem is, I think it is calculating time between created and Open which is 0m. Is there any way to modify this?

Thanks.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events