如何使用Http从Jira 4.4删除附件 [英] How to remove an attachment from Jira 4.4 using Http

查看:120
本文介绍了如何使用Http从Jira 4.4删除附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一种使用SOAP Api从Jira删除附件的方法,但是看来这是不可能的,并且我宁愿不必像已接受的建议那样为Jira实现新的插件回答这个问题,或者重新编译现有插件以支持该问题,如

I have been looking for a way to remove an attachment from Jira using the SOAP Api, but it seems that this is not possible natively, and I would prefer not having to implement a new plugin for Jira, as suggested in the accepted answer to this question, or recompiling the existing plugin to support this as mentioned here.

对上述问题的答案似乎完全符合我的要求,但是,,,我无法去工作.我得到的响应是一个错误,指出:

This answer to the abovementioned question seems to do exactly what I want, but alas, I can't get i to work. The response i get is an error stating that:

缺少XSRF安全令牌

JIRA由于缺少表单令牌而无法完成此操作.

您可能已经清除了浏览器cookie,这可能会导致当前表单令牌过期.重新发行了新的表格令牌.

当我使用Asp.Net MVC C#时,我使用了答案中的代码,原样,仅调整了服务器网址,并使用了不同的凭据(一个Jira用户)以及使用以下命令作为请求参数传递的用户名/密码:

As I am using Asp.Net MVC C#, I have used the code from the answer, as is, with only the server url adjusted, as well as with different credentials (a Jira user) and the username/password passed through as request parameters using:

os_username=jirausername&os_password=xxxxxxx

我当前使用的代码如下:

The code I am currently using is as follows:

public void RemoveAttachment(string issueid, string attachmentid)
        {
            using (System.Net.WebClient client = new System.Net.WebClient())
            {
                //Compute jira server base url from WS url
                string baseUrl = _service.Url.Substring(0, _service.Url.IndexOf("/rpc/"));

                //Compute complete attachment url
                string attachmenturl = baseUrl + "/secure/DeleteAttachment.jspa?id=" +
                                       issueid + "&deleteAttachmentId=" + attachmentid;

                client.Credentials = new System.Net.NetworkCredential("jirausername", "xxxxxxx");
                string response = client.DownloadString(attachmenturl);
            }
    }

推荐答案

我最终使用了一种方法,该方法首先请求删除确认表单,然后从表单中提取所需的令牌,最后在其中发布与表单内容等效的内容.为了删除附件.下面的代码.

I ended up using a method that first requests the deletion confirmation form, then extracts a required token from the form, and finally posts something equivalent to the form content in order to delete the attachment. Code below.

public void RemoveAttachment(string issueid, string attachmentid)
{
    //Compute jira server base url from WS url
    string baseUrl = _service.Url.Substring(0, _service.Url.IndexOf("/rpc/"));

    //Compute complete attachment deletion confirm url
    string confirmurl = baseUrl + "/secure/DeleteAttachment!default.jspa?id=" +
                 issueid + "&deleteAttachmentId=" + attachmentid + "&os_username=jirauser&os_password=xxxxxx";

    //Create a cookie container to maintain the xsrf security token cookie.
    CookieContainer jiracontainer = new CookieContainer();

    //Create a get request for the page containing the delete confirmation.
    HttpWebRequest confirmrequest = (HttpWebRequest)WebRequest.Create(confirmurl);
    confirmrequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
    confirmrequest.CookieContainer = jiracontainer;

    //Get the response and the responsestream.
    WebResponse confirmdeleteresponse = confirmrequest.GetResponse();
    Stream ReceiveStream = confirmdeleteresponse.GetResponseStream();

    // Open the stream using a StreamReader for easy access.
    StreamReader confirmreader = new StreamReader(ReceiveStream);
    // Read the content.
    string confirmresponse = confirmreader.ReadToEnd();

    //Create a regex to extract the atl/xsrf token from a hidden field. (Might be nicer to read it from a cookie, which should also be possible).
    Regex atl_token_matcher = new Regex("<input[^>]*id=\"atl_token\"[^>]*value=\"(?<token>\\S+)\"[^>]*>", RegexOptions.Singleline);
    Match token_match = atl_token_matcher.Match(confirmresponse);

    if (token_match.Success)
    {
        //If we found the token get the value.
        string token = token_match.Groups["token"].Value;

        //Compute attachment delete url.
        string deleteurl = baseUrl + "/secure/DeleteAttachment.jspa";

        //Construct form data.
        string postdata = "atl_token=" + HttpContext.Current.Server.UrlEncode(token) + "&id=" + issueid + "&deleteAttachmentId=" + attachmentid + "&Delete=Delete&os_username=jirauser&os_password=xxxxxx";

        //Create a post request for the deletion page.
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(deleteurl);
        request.KeepAlive = false;
        request.CookieContainer = jiracontainer; // Remember to set the cookiecontainer.
        request.ProtocolVersion = HttpVersion.Version10;
        request.Method = "POST";

        //Turn our request string into a byte stream
        byte[] postBytes = Encoding.ASCII.GetBytes(postdata);

        //Make sure you specify the proper type.
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = postBytes.Length;
        Stream requestStream = request.GetRequestStream();

        //Send the post.
        requestStream.Write(postBytes, 0, postBytes.Length);
        requestStream.Close();

        //Get the response.
        WebResponse deleteresponse = request.GetResponse();

        // Open the responsestream using a StreamReader for easy access.
        StreamReader deleteresponsereader = new StreamReader(deleteresponse.GetResponseStream());

        // Read the content.
        string deleteresponsecontent = deleteresponsereader.ReadToEnd();

        // do whatever validation/reporting with the response...
    }
    else
    {
        //We couldn't find the atl_token. Throw an error or something...
    }

}

同样的事情也适用于删除评论.将'attachment'替换为'comment',将'deleteAttachmentId'替换为'commentId',

Same thing works for removing comments. Replace 'attachment' with 'comment' and 'deleteAttachmentId' with 'commentId' and you should be good to go.

这篇关于如何使用Http从Jira 4.4删除附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆