Rally C#:如何上传附件集合并与用户故事相关联?

时间:2017-01-09 18:16:24

标签: c# api rally

我已经对本网站上的其他示例进行了评审,但发现我的方法存在重大差异。 (请耐心等待)

我正在尝试遍历文件目录并将每个文件作为附件上传并关联到用户素材。 到目前为止,我只能为用户故事添加1个文件。 我看到每个附件都必须编码为base 64字符串,并且必须具有以字节为单位的大小。

到目前为止,这是我的代码:

 public void createUsWithAttachmentList(string workspace, string project, string userStoryName, string userStoryDescription)
    {

        //authentication
        this.EnsureRallyIsAuthenticated();

        //DynamicJSONObject for AttachmentContent
        DynamicJsonObject myAttachmentContent = new DynamicJsonObject();

        //Length calculated from Base64String converted back
        int imageNumberBytes = 0;

        //Userstory setup
        DynamicJsonObject toCreate = new DynamicJsonObject();
        toCreate["Workspace"] = workspace;
        toCreate["Project"] = project;
        toCreate["Name"] = userStoryName;
        toCreate["Description"] = userStoryDescription;

        //Trying to get a list of all the file paths within a given directory, this directory would contain .png files that need to be associated to a user story.
        string[] attachmentPath = Directory.GetFiles("C:\\Users\\user\\Desktop\\RallyAttachments");

这个foreach循环令人困惑。我试图遍历目录中的每个文件,以便将其转换为base64字符串,同时获取每个文件的字节数作为int。

        foreach (string fileName in attachmentPath)
        {
            Image myImage = Image.FromFile(fileName);
            string imageBase64String = imageToBase64(myImage, System.Drawing.Imaging.ImageFormat.Png);
            imageNumberBytes = Convert.FromBase64String(imageBase64String).Length;

            //I am stuck here to be exact because there are multiple imageBase64Strings due to the collection of files located inside the directory. AND the below line is wrong because I have a list of imageBase64Strings that were generated from iterating through the string[] attachmentPath.
            myAttachmentContent[RallyField.content] = imageBase64String;
        }

        try
        {
            //create user story
            CreateResult createUserStory = _api.Create(RallyField.attachmentContent, myAttachmentContent);
            //create attachment
            CreateResult myAttachmentContentCreateResult = _api.Create(RallyField.attachmentContent, myAttachmentContent);
            String myAttachmentContentRef = myAttachmentContentCreateResult.Reference;

            //DynamicJSONObject for Attachment Container
            //I assume I would need a separate container for each file in my directory containing the attachments.
            DynamicJsonObject myAttachment = new DynamicJsonObject();
            myAttachment["Artifact"] = createUserStory.Reference;
            myAttachment["Content"] = myAttachmentContentRef;
            myAttachment["Name"] = "AttachmentFromREST.png";
            myAttachment["Description"] = "Email Attachment";
            myAttachment["ContentType"] = "image/png"; 
            myAttachment["Size"] = imageNumberBytes;

            //create & associate the attachment
            CreateResult myAttachmentCreateResult = _api.Create(RallyField.attachment, myAttachment);
            Console.WriteLine("Created User Story: " + createUserStory.Reference);
        }
        catch (WebException e)
        {
            Console.WriteLine(e.Message);
        }
    }

注意:我正计划扩展此方法以支持多种文件类型,我需要获取目录中每个文件的文件类型并相应地继续。 关于如何写这个的任何想法?

1 个答案:

答案 0 :(得分:1)

您已经实施了所有部件 - 我们只需稍微移动一下即可。在开头创建一次故事,然后每次循环为每个文件创建一个新的AttachmentContent和一个新的附件。

public void createUsWithAttachmentList(string workspace, string project, string userStoryName, string userStoryDescription)
{

    //authentication
    this.EnsureRallyIsAuthenticated();

    //Userstory setup
    DynamicJsonObject toCreate = new DynamicJsonObject();
    toCreate["Workspace"] = workspace;
    toCreate["Project"] = project;
    toCreate["Name"] = userStoryName;
    toCreate["Description"] = userStoryDescription;

    //Create the story first
    try
    {
        //create user story
        CreateResult createUserStory = _api.Create(RallyField.userStory, toCreate);


        //now loop over each file 
        string[] attachmentPath = Directory.GetFiles("C:\\Users\\user\\Desktop\\RallyAttachments");

        foreach (string fileName in attachmentPath)
        {
            //DynamicJSONObject for AttachmentContent
            DynamicJsonObject myAttachmentContent = new DynamicJsonObject();
            Image myImage = Image.FromFile(fileName);
            string imageBase64String = imageToBase64(myImage, System.Drawing.Imaging.ImageFormat.Png);
            int imageNumberBytes = Convert.FromBase64String(imageBase64String).Length;
            myAttachmentContent[RallyField.content] = imageBase64String;

            //create the AttachmentConent
            CreateResult myAttachmentContentCreateResult = _api.Create(RallyField.attachmentContent, myAttachmentContent);
            String myAttachmentContentRef = myAttachmentContentCreateResult.Reference;

            //create an Attachment to associate to story
            DynamicJsonObject myAttachment = new DynamicJsonObject();
            myAttachment["Artifact"] = createUserStory.Reference;
            myAttachment["Content"] = myAttachmentContentRef;
            myAttachment["Name"] = "AttachmentFromREST.png";
            myAttachment["Description"] = "Email Attachment";
            myAttachment["ContentType"] = "image/png"; 
            myAttachment["Size"] = imageNumberBytes;

            //create & associate the attachment
            CreateResult myAttachmentCreateResult = _api.Create(RallyField.attachment, myAttachment);
        }     
    }
    catch (WebException e)
    {
        Console.WriteLine(e.Message);
    }
}