当前位置: 首页 > 知识库问答 >
问题:

如何使用Rally REST. NET向用户故事添加附件

羊舌源
2023-03-14

我们正在移植我们的产品。NET将代码从SOAP集合到其他程序。NET API。到目前为止,REST API似乎更快、更易于使用,因为在Rally工作区中,每次工作产品自定义字段发生更改时,都没有WSDL可以中断。

不过,当我们试图复制上传附件的能力时,我遇到了一个问题。我们遵循的程序与本文概述的程序非常相似:

Rally SOAP API-如何向分层需求添加附件

将图像读入系统。绘画形象我们使用ImageToByteArray函数将图像转换为字节数组,然后将其分配给首先创建的AttachmentContent。

然后,创建附件,并连接到附件内容和层次要求。

所有创建事件都运行良好。内容对象创建得很好。然后创建了名为“我mage.png”的新附件并链接到故事。但是当我从拉力赛下载结果附件时,我mage.png零字节!我尝试了不同的图像,JPEG的,巴布亚新几内亚的,等等,都有相同的结果。

下面包含了显示我们过程的代码摘录。有什么明显的东西我错过了吗?提前感谢。

    // .... Read content into a System.Drawing.Image called imageObject ....

    // Convert Image to byte array
    byte[] imageBytes = ImageToByteArray(imageObject, System.Drawing.Imaging.ImageFormat.Png);
    var imageLength = imageBytes.Length;

    // AttachmentContent
    DynamicJsonObject attachmentContent = new DynamicJsonObject();
    attachmentContent["Content"] = imageBytes ;

    CreateResult cr = restApi.Create("AttachmentContent", myAttachmentContent);
    String contentRef = cr.Reference;
    Console.WriteLine("Created: " + contentRef);

    // Set up attachment
    DynamicJsonObject newAttachment = new DynamicJsonObject();
    newAttachment["Artifact"] = story;
    newAttachment["Content"] = attachmentContent;
    newAttachment["Name"] = "Image.png";
    newAttachment["ContentType"] = "image/png";
    newAttachment["Size"] = imageLength;
    newAttachment["User"] = user;


    // Create the attachment in Rally
    cr = restApi.Create("Attachment", newAttachment);

    String attachRef = cr.Reference;
    Console.WriteLine("Created: " + attachRef);

}

public static byte[] ImageToByteArray(Image image, System.Drawing.Imaging.ImageFormat format)
{
    using (MemoryStream ms = new MemoryStream())
    {
        image.Save(ms, format);

        // Convert Image to byte[]                
        byte[] imageBytes = ms.ToArray();
        return imageBytes;
    }
}

共有1个答案

闻修筠
2023-03-14

这个问题也让我困惑了一段时间——大约一周前终于解决了。

两点意见:

  1. 虽然Rally的SOAP API会在幕后将字节数组序列化为Base64字符串,但REST不会为您执行此步骤,而是希望将Base64格式的字符串作为AttachmentContent对象的内容属性传递

我提供了一个代码示例来说明:

// System Libraries
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Imaging;
using System.Drawing;
using System.IO;
using System.Web;

// Rally REST API Libraries
using Rally.RestApi;
using Rally.RestApi.Response;

namespace RestExample_CreateAttachment
{
    class Program
    {
        static void Main(string[] args)
        {
            // Set user parameters
            String userName = "user@company.com";
            String userPassword = "password";

            // Set Rally parameters
            String rallyURL = "https://rally1.rallydev.com";
            String rallyWSAPIVersion = "1.36";

            //Initialize the REST API
            RallyRestApi restApi;
            restApi = new RallyRestApi(userName,
                                       userPassword,
                                       rallyURL,
                                       rallyWSAPIVersion);

            // Create Request for User
            Request userRequest = new Request("user");
            userRequest.Fetch = new List<string>()
                {
                    "UserName",
                    "Subscription",
                    "DisplayName",
                };

            // Add a Query to the Request
            userRequest.Query = new Query("UserName",Query.Operator.Equals,userName);

            // Query Rally
            QueryResult queryUserResults = restApi.Query(userRequest);

            // Grab resulting User object and Ref
            DynamicJsonObject myUser = new DynamicJsonObject();
            myUser = queryUserResults.Results.First();
            String myUserRef = myUser["_ref"];

            //Set our Workspace and Project scopings
            String workspaceRef = "/workspace/12345678910";
            String projectRef = "/project/12345678911";
            bool projectScopingUp = false;
            bool projectScopingDown = true;

            // Find User Story that we want to add attachment to

            // Tee up Story Request
            Request storyRequest = new Request("hierarchicalrequirement");
            storyRequest.Workspace = workspaceRef;
            storyRequest.Project = projectRef;
            storyRequest.ProjectScopeDown = projectScopingDown;
            storyRequest.ProjectScopeUp = projectScopingUp;

            // Fields to Fetch
            storyRequest.Fetch = new List<string>()
                {
                    "Name",
                    "FormattedID"
                };

            // Add a query
            storyRequest.Query = new Query("FormattedID", Query.Operator.Equals, "US43");

            // Query Rally for the Story
            QueryResult queryResult = restApi.Query(storyRequest);

            // Pull reference off of Story fetch
            var storyObject = queryResult.Results.First();
            String storyReference = storyObject["_ref"];

            // Read In Image Content
            String imageFilePath = "C:\\Users\\username\\";
            String imageFileName = "image1.png";
            String fullImageFile = imageFilePath + imageFileName;
            Image myImage = Image.FromFile(fullImageFile);

            // Get length from Image.Length attribute - don't use this in REST though
            // REST expects the length of the image in number of bytes as converted to a byte array
            var imageFileLength = new FileInfo(fullImageFile).Length;
            Console.WriteLine("Image File Length from System.Drawing.Image: " + imageFileLength);

            // Convert Image to Base64 format
            string imageBase64String = ImageToBase64(myImage, System.Drawing.Imaging.ImageFormat.Png);           

            // Length calculated from Base64String converted back
            var imageNumberBytes = Convert.FromBase64String(imageBase64String).Length;

            // This differs from just the Length of the Base 64 String!
            Console.WriteLine("Image File Length from Convert.FromBase64String: " + imageNumberBytes);

            // DynamicJSONObject for AttachmentContent
            DynamicJsonObject myAttachmentContent = new DynamicJsonObject();
            myAttachmentContent["Content"] = imageBase64String;

            try
            {
                CreateResult myAttachmentContentCreateResult = restApi.Create("AttachmentContent", myAttachmentContent);
                String myAttachmentContentRef = myAttachmentContentCreateResult.Reference;
                Console.WriteLine("Created: " + myAttachmentContentRef);

                // DynamicJSONObject for Attachment Container
                DynamicJsonObject myAttachment = new DynamicJsonObject();
                myAttachment["Artifact"] = storyReference;
                myAttachment["Content"] = myAttachmentContentRef;
                myAttachment["Name"] = "AttachmentFromREST.png";
                myAttachment["Description"] = "Attachment Desc";
                myAttachment["ContentType"] = "image/png";
                myAttachment["Size"] = imageNumberBytes;
                myAttachment["User"] = myUserRef;

                CreateResult myAttachmentCreateResult = restApi.Create("Attachment", myAttachment);

                List<string> createErrors = myAttachmentContentCreateResult.Errors;
                for (int i = 0; i < createErrors.Count; i++)
                {
                    Console.WriteLine(createErrors[i]);
                }

                String myAttachmentRef = myAttachmentCreateResult.Reference;
                Console.WriteLine("Created: " + myAttachmentRef);

            }
            catch (Exception e)
            {
                Console.WriteLine("Unhandled exception occurred: " + e.StackTrace);
                Console.WriteLine(e.Message);
            }
        }

        // Converts image to Base 64 Encoded string
        public static string ImageToBase64(Image image, System.Drawing.Imaging.ImageFormat format)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                image.Save(ms, format);
                // Convert Image to byte[]                
                byte[] imageBytes = ms.ToArray();

                // Convert byte[] to Base64 String
                string base64String = Convert.ToBase64String(imageBytes);

                return base64String;
            }
        }
    }
}
 类似资料:
  • 我有一个docker容器,里面运行着一些进程(uwsgi和celery)。我想为这些进程创建一个celery用户和一个uwsgi用户,以及一个它们都将属于的worker组,以便分配权限。 我尝试将和添加到我的Dockerfile中,但这会导致问题,因为这些命令提示输入(我已经发布了下面构建的响应)。 将用户添加到Docker容器中以便为在容器中运行的工作者设置权限的最佳方法是什么? 我的Docke

  • 问题内容: 如何使用Dockerfile添加用户-以下内容不起作用。 我完整的Dockerfile: 问题答案: 使用而非交互来添加用户。 以下命令不会创建user。 它将使用用户 请参考Dockerfile用户文档 USER指令设置运行映像时使用的用户名或UID,以及Dockerfile中跟随该映像的所有RUN,CMD和ENTRYPOINT指令。 注意: 确保这是默认外壳程序。 如果使用默认外壳

  • 我使用jira api添加附件的问题 据留档说,我设置了一些东西。 > 包含附件的多部分/表单数据参数的名称必须为“file”。 资源需要一个多部门员额。 我的代码如下 请指出我犯了什么错误

  • 我有一个观察数据表和是和否的模型。为简单起见,我只假设对组。我浪费了一些分类统计数据,我想控制选择哪一个。我知道如何使用eval做到这一点并将其保存在另一个data.table中,但我想添加到现有的data.table中,因为我每个组只有一行。任何人都可以帮我吗? 首先,我为每个组创建列联表。 然后定义统计数据 如果我使用下面几行,它会给我一个新的数据表: 如何在此示例中使用:=将结果添加到我的旧

  • 当我进入我的领域。com:8080默认情况下没有授权机制。我已经查看了配置区域,但找不到添加基本用户名和密码的地方

  • 代码是来自ASP.NET MVC应用程序的操作方法。