javascript – Google App Script Creating Attachment with Jira API

javascript – Google App Script Creating Attachment with Jira API

Modification points:

  • When I saw the document of How to add an attachment to a JIRA issue using REST API, it seems that the file is sent as multipart/form-data
  • muteHttpExceptions is not used in the header.
  • params of fetch(url, params) has no properties of body and redirect.
  • When the blob is used to the request body, Content-Type is automatically generated by giving the boundary.
  • From your HTML & Javascript, if the uploaded file is the binary file and you are using V8 runtime, var file = data.BugReportFileUpload is not the correct file blob. Ref Im worry about this.

When above points are reflected to your script, it becomes as follows.

Modified script:

HTML&Javascript side:

In this modified script, I modified it for testing. So please modify this for your actual situation.

<form id=bugReportRequest name=bugReportRequest action=#>
<input type=text class=form-control id=BugSubmitterName  name=BugSubmitterName>
<input type=text class=form-control id=BugSubmitterEmail  name=BugSubmitterEmail>
<input type=file class=form-control id=BugReportFileUpload name =BugReportFileUpload>
</form>

<input type=button value=ok onclick=formSubmit()> <!-- Added for testing script. -->
<script src=https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js></script> <!-- Added for testing script. -->

<script>
// I modified this function.
function formSubmit(){
  var BugSubmitterName = $(#BugSubmitterName).val();
  var BugSubmitterEmail = $(#BugSubmitterEmail).val();
  var BugReportFileUpload = $(#BugReportFileUpload).prop(files);

  if (BugReportFileUpload.length == 1) {
    const file = BugReportFileUpload[0];
    const fr = new FileReader();
    fr.onload = function(e) {
      const obj = {
        BugSubmitterName: BugSubmitterName,
        BugSubmitterEmail: BugSubmitterEmail,
        BugReportFileUpload: {
          filename: file.name,
          mimeType: file.type,
          bytes: [...new Int8Array(e.target.result)]
        }
      };
      google.script.run.withSuccessHandler(BugReportSubmitted).submitBugReport(obj);
    };
    fr.readAsArrayBuffer(file);
  } else {
    const obj = {
      BugSubmitterName: BugSubmitterName,
      BugSubmitterEmail: BugSubmitterEmail,
      BugReportFileUpload: null
    };
    google.script.run.withSuccessHandler(BugReportSubmitted).submitBugReport(obj);
  }
}

function BugReportSubmitted(e) {
  console.log(e)
}
</script>

Google Apps Script side:

Please confirm Jira_Email_Address and Jira_API_Token are declared.

function submitBugReport(data){
  if (!data.BugReportFileUpload) return;  // Or, for example, return no file.
  var file = Utilities.newBlob(data.BugReportFileUpload.bytes, data.BugReportFileUpload.mimeType, data.BugReportFileUpload.filename);
  var url = Jira_URL + rest/api/2/issue/ABC-2/attachments;
  var credentials = Utilities.base64Encode(Jira_Email_Address + : + Jira_API_Token);
  let formdata = {file : file};
  var header = { 
    X-Atlassian-Token: no-check,
    Authorization: Basic  + credentials
  } 
  var options = {
    method: POST,
    headers: header,
    payload: formdata,
    muteHttpExceptions: true
  } 
  var resp;
  try {
    resp = UrlFetchApp.fetch(url, options);
    console.error(url);
    console.log(resp.getContentText());
  }  catch (e) {
    console.error(myFunction() yielded an error:  + e);
    console.error(resp.getContentText);
    console.error(resp.getResponseCode);
  }
}

Note:

  • When I saw your script, it seems that the values of BugSubmitterName and BugSubmitterEmail are not used. So in this modification, these values are not used.
  • This modified script supposes that your token and URL can be used for achieving your goal. So please be careful this.

References:

javascript – Google App Script Creating Attachment with Jira API

Leave a Reply

Your email address will not be published.