Google 使用 Jira 创建附件的应用程序脚本 API
Google App Script Creating Attachment with Jira API
我正在编写一个 Google 应用程序脚本代码,它将向 Jira 的 API 提交附件。用户上传文件并提交,此时我的代码应向 API 发送请求以发送附件。我的代码没有引发任何错误,但附件没有添加到 Jira 问题中。我想这可能是我格式化有效负载的方式?我在 PostMan 中使用了相同的设置,并且 API 调用工作正常。我的代码如下:
index.html
function formSubmit(){
var form = $("#bugReportRequest")[0];
google.script.run.withSuccessHandler(BugReportSubmitted).submitBugReport(form)
//where form is:
//<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>
}
Code.gs
function submitBugReport(data){
var file = data.BugReportFileUpload;
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 = {
"Content-Type": "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
"X-Atlassian-Token": "no-check",
"muteHttpExceptions": true,
"Authorization": "Basic " + credentials
}
var options = {
"method": "POST",
"headers": header,
"body": formdata,
"redirect": "follow",
"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);
}
}
我得到的响应代码是 200,但 resp.getContentText() 只打印“[]”。当我检查 ABC-2 时,没有添加附件。关于如何格式化数据有效负载的任何建议?或者,还有其他原因会发生这种情况吗?
修改点:
- 当我看到How to add an attachment to a JIRA issue using REST API的文档时,好像是
multipart/form-data
发送的文件
muteHttpExceptions
没有用在header.
fetch(url, params)
的 params
没有 body
和 redirect
的属性。
- 当blob用于请求body时,
Content-Type
通过给定边界自动生成
- 从您的 HTML & Javascript,如果上传的文件是二进制文件并且您使用的是 V8 运行时,则
var file = data.BugReportFileUpload
不是正确的文件 blob。 Ref我很担心这个。
当以上几点反映到你的脚本中,就会变成下面这样。
修改后的脚本:
HTML&Javascript 边:
在这个修改脚本中,我修改了它以进行测试。所以请根据您的实际情况进行修改。
<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 脚本端:
请确认 Jira_Email_Address
和 Jira_API_Token
已声明。
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);
}
}
注:
- 当我看到你的脚本时,似乎没有使用“BugSubmitterName”和“BugSubmitterEmail”的值。所以在这次修改中,没有使用这些值。
- 此修改后的脚本假定您的令牌和 URL 可用于实现您的目标。所以请注意这一点。
参考文献:
我正在编写一个 Google 应用程序脚本代码,它将向 Jira 的 API 提交附件。用户上传文件并提交,此时我的代码应向 API 发送请求以发送附件。我的代码没有引发任何错误,但附件没有添加到 Jira 问题中。我想这可能是我格式化有效负载的方式?我在 PostMan 中使用了相同的设置,并且 API 调用工作正常。我的代码如下:
index.html
function formSubmit(){
var form = $("#bugReportRequest")[0];
google.script.run.withSuccessHandler(BugReportSubmitted).submitBugReport(form)
//where form is:
//<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>
}
Code.gs
function submitBugReport(data){
var file = data.BugReportFileUpload;
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 = {
"Content-Type": "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
"X-Atlassian-Token": "no-check",
"muteHttpExceptions": true,
"Authorization": "Basic " + credentials
}
var options = {
"method": "POST",
"headers": header,
"body": formdata,
"redirect": "follow",
"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);
}
}
我得到的响应代码是 200,但 resp.getContentText() 只打印“[]”。当我检查 ABC-2 时,没有添加附件。关于如何格式化数据有效负载的任何建议?或者,还有其他原因会发生这种情况吗?
修改点:
- 当我看到How to add an attachment to a JIRA issue using REST API的文档时,好像是
multipart/form-data
发送的文件
muteHttpExceptions
没有用在header.params
没有body
和redirect
的属性。- 当blob用于请求body时,
Content-Type
通过给定边界自动生成 - 从您的 HTML & Javascript,如果上传的文件是二进制文件并且您使用的是 V8 运行时,则
var file = data.BugReportFileUpload
不是正确的文件 blob。 Ref我很担心这个。
fetch(url, params)
的 当以上几点反映到你的脚本中,就会变成下面这样。
修改后的脚本:
HTML&Javascript 边:
在这个修改脚本中,我修改了它以进行测试。所以请根据您的实际情况进行修改。
<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 脚本端:
请确认 Jira_Email_Address
和 Jira_API_Token
已声明。
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);
}
}
注:
- 当我看到你的脚本时,似乎没有使用“BugSubmitterName”和“BugSubmitterEmail”的值。所以在这次修改中,没有使用这些值。
- 此修改后的脚本假定您的令牌和 URL 可用于实现您的目标。所以请注意这一点。