如何使用RAlly API向RallyDev添加屏幕截图
如何使用rally rest api将屏幕截图上传到rally dev。我能够记录缺陷。我需要记录屏幕截图以下是一个使用创建缺陷然后在缺陷上创建附件的示例:如何使用RAlly API向RallyDev添加屏幕截图,rally,Rally,如何使用rally rest api将屏幕截图上传到rally dev。我能够记录缺陷。我需要记录屏幕截图以下是一个使用创建缺陷然后在缺陷上创建附件的示例: public class CreateDefectAddAttachment{ public static void main(String[] args) throws URISyntaxException, IOException { String host = "https://rally1.rall
public class CreateDefectAddAttachment{
public static void main(String[] args) throws URISyntaxException, IOException {
String host = "https://rally1.rallydev.com";
String apiKey = "_abc123";
String projectRef = "/project/12352608219";
String applicationName = "RestExample_createDefectAttachScreenshot";
RallyRestApi restApi = new RallyRestApi(new URI(host),apiKey);
restApi.setApplicationName(applicationName);
//Read User
QueryRequest userRequest = new QueryRequest("User");
userRequest.setQueryFilter(new QueryFilter("UserName", "=", "user@company.com"));
QueryResponse userQueryResponse = restApi.query(userRequest);
JsonArray userQueryResults = userQueryResponse.getResults();
JsonElement userQueryElement = userQueryResults.get(0);
JsonObject userQueryObject = userQueryElement.getAsJsonObject();
String userRef = userQueryObject.get("_ref").getAsString();
try {
for (int i=0; i<1; i++) {
//Create a defect
JsonObject newDefect = new JsonObject();
newDefect.addProperty("Name", "bug1234");
newDefect.addProperty("Project", projectRef);
CreateRequest createRequest = new CreateRequest("defect", newDefect);
CreateResponse createResponse = restApi.create(createRequest);
if (createResponse.wasSuccessful()) {
System.out.println(String.format("Created %s", createResponse.getObject().get("_ref").getAsString()));
//Read defect
String ref = Ref.getRelativeRef(createResponse.getObject().get("_ref").getAsString());
System.out.println(String.format("\nReading Defect %s...", ref));
String imageFilePath = "C:/";
String imageFileName = "pic.png";
String fullImageFile = imageFilePath + imageFileName;
String imageBase64String;
long attachmentSize;
// Open file
RandomAccessFile myImageFileHandle = new RandomAccessFile(fullImageFile, "r");
try {
long longLength = myImageFileHandle.length();
long maxLength = 5000000;
if (longLength >= maxLength) throw new IOException("File size >= 5 MB Upper limit for Rally.");
int fileLength = (int) longLength;
// Read file and return data
byte[] fileBytes = new byte[fileLength];
myImageFileHandle.readFully(fileBytes);
imageBase64String = Base64.encodeBase64String(fileBytes);
attachmentSize = fileLength;
// First create AttachmentContent from image string
JsonObject myAttachmentContent = new JsonObject();
myAttachmentContent.addProperty("Content", imageBase64String);
CreateRequest attachmentContentCreateRequest = new CreateRequest("AttachmentContent", myAttachmentContent);
CreateResponse attachmentContentResponse = restApi.create(attachmentContentCreateRequest);
String myAttachmentContentRef = attachmentContentResponse.getObject().get("_ref").getAsString();
System.out.println("Attachment Content created: " + myAttachmentContentRef);
//Create the Attachment
JsonObject myAttachment = new JsonObject();
myAttachment.addProperty("Artifact", ref);
myAttachment.addProperty("Content", myAttachmentContentRef);
myAttachment.addProperty("Name", "AttachmentFromREST.png");
myAttachment.addProperty("Description", "Attachment From REST");
myAttachment.addProperty("ContentType","image/png");
myAttachment.addProperty("Size", attachmentSize);
myAttachment.addProperty("User", userRef);
CreateRequest attachmentCreateRequest = new CreateRequest("Attachment", myAttachment);
CreateResponse attachmentResponse = restApi.create(attachmentCreateRequest);
String myAttachmentRef = attachmentResponse.getObject().get("_ref").getAsString();
System.out.println("Attachment created: " + myAttachmentRef);
if (attachmentResponse.wasSuccessful()) {
System.out.println("Successfully created Attachment");
} else {
String[] attachmentContentErrors;
attachmentContentErrors = attachmentResponse.getErrors();
System.out.println("Error occurred creating Attachment: ");
for (int j=0; j<attachmentContentErrors.length;j++) {
System.out.println(attachmentContentErrors[j]);
}
}
}catch (Exception e) {
System.out.println("Exception occurred while attempting to create Content and/or Attachment: ");
e.printStackTrace();
}
} else {
String[] createErrors;
createErrors = createResponse.getErrors();
System.out.println("Error occurred creating a defect: ");
for (int j=0; i<createErrors.length;j++) {
System.out.println(createErrors[j]);
}
}
}
} finally {
//Release all resources
restApi.close();
}
}
}
public类CreateDefectAddAttachment{
公共静态void main(字符串[]args)抛出URISyntaxException、IOException{
字符串主机=”https://rally1.rallydev.com";
字符串apiKey=“_abc123”;
字符串projectRef=“/project/12352608219”;
String applicationName=“RestExample\u createDefectAttachScreenshot”;
RallyRestApi restApi=新的RallyRestApi(新URI(主机),apiKey);
restApi.setApplicationName(applicationName);
//读用户
QueryRequest userRequest=新的QueryRequest(“用户”);
setQueryFilter(新的QueryFilter(“用户名”、“=”、”user@company.com"));
QueryResponse userQueryResponse=restApi.query(userRequest);
JsonArray userQueryResults=userQueryResponse.getResults();
JsonElement userquerylement=userQueryResults.get(0);
JsonObject userQueryObject=userQueryElement.getAsJsonObject();
字符串userRef=userQueryObject.get(“_ref”).getAsString();
试一试{
对于(inti=0;i=maxLength)抛出新IOException(“Rally的文件大小>=5MB上限”);
int fileLength=(int)longlegth;
//读取文件并返回数据
byte[]fileBytes=新字节[fileLength];
myImageFileHandle.readFully(fileBytes);
imageBase64String=Base64.encodeBase64String(fileBytes);
attachmentSize=文件长度;
//首先从图像字符串创建AttachmentContent
JsonObject myAttachmentContent=新JsonObject();
myAttachmentContent.addProperty(“内容”,imageBase64String);
CreateRequest attachmentContentCreateRequest=新建CreateRequest(“AttachmentContent”,myAttachmentContent);
CreateResponse attachmentContentResponse=restApi.create(attachmentContentCreateRequest);
字符串myAttachmentContentRef=attachmentContentResponse.getObject().get(“_ref”).getAsString();
System.out.println(“创建的附件内容:“+myAttachmentContentRef”);
//创建附件
JsonObject myAttachment=新的JsonObject();
myAttachment.addProperty(“工件”,参考);
myAttachment.addProperty(“内容”,myAttachmentContentRef);
myAttachment.addProperty(“名称”、“AttachmentFromREST.png”);
myAttachment.addProperty(“说明”、“来自REST的附件”);
myAttachment.addProperty(“ContentType”、“image/png”);
myAttachment.addProperty(“大小”,attachmentSize);
myAttachment.addProperty(“用户”,userRef);
CreateRequest attachmentCreateRequest=新建CreateRequest(“附件”,myAttachment);
CreateResponse attachmentResponse=restApi.create(attachmentCreateRequest);
字符串myAttachmentRef=attachmentResponse.getObject().get(“_ref”).getAsString();
System.out.println(“创建的附件:“+myAttachmentRef”);
if(attachmentResponse.wasSuccessful()){
System.out.println(“成功创建附件”);
}否则{
字符串[]attachmentContentErrors;
attachmentContentErrors=attachmentResponse.getErrors();
System.out.println(“创建附件时出错:”);
对于(int j=0;j