首页 > 其他分享 >JIRA API 集成经验总结

JIRA API 集成经验总结

时间:2022-11-29 17:26:40浏览次数:72  
标签:JIRA cts API requestMessage new issue 经验总结

JIRA集成总结

由于工作需要, 系统需要集成JIRA API去自动创建ticket, 期间几乎没有遇到太多坑, 可能和JIRA API document规范有关吧, 不得不说,JIRA API document 是我见过的比较规范的文档之一.  通过阅读JIRA API Document, 我们可以发现 集成JIRA API需要OAUTH 或者Http Basic Authentication认证, 我这边是用的http Basic Authentication 方式, 下面是请求相关的参数配置

Attribute Name Description
Base URI The base jira uri. For example, https://yourjirahost.com
Header.Authorization This is a base64 format string. Its plaintext format is username:password. The value format in the header is Basic {base64}. For example, Basic MTIzNDU2Nzg=
Header.Accept application/json
Header.ContentType application/json

Main APIs

URL Method Description Request Parameter
/rest/api/2/issue POST Single Create issue 查看代码
{
    "fields": {
        "project": {
            "key": "MyProject"
        },
        "summary": "[Test] Please ignore this issue",
        "description": "[Test] Please ignore this issue. CC:[~san.zhang]",
        "issuetype": {
            "name": "Story"
        },
        "assignee": {
            "name": "san.zhang"
        },
        "customized_number":"这里是自定义参数"
    }
}
/rest/api/2/issue/issueId PUT Single Update issue { "fields": { "assignee":{"name":"charlie"} } }
rest/api/2/issue/myissueid/comment POST Add comment { "update" : { "components" : [{"add" : {"name" : "Engine"}}] } }
  POST Edit comment {
    "update": {
        "comment": [
            {
                "add": {
                    "body": "It is time to finish this task"
                }
            }
        ]
    }
}

下面是实现源代码

主要代码

//Add or edit
var fields = new MyClass()
{
    fields = new fields()
    {
        project = new project()
        {
            key = "myproject"
        },
        summary = "[Test] Please ignore this issue",
        description = "[Test] Please ignore this issue. CC:[~san.zhang]",
        assignee = new assignees() { name = "san.zhang" },
        issuetype = new issuetype()
        {
            name = "Story"
        },
        customfield_自定义号码 = "这是自定义字段"
    }
};
string JsonString = JsonConvert.SerializeObject(fields);
var requestMessage = new HttpRequestMessage
{
    RequestUri = new Uri(create_issue),
    Method = HttpMethod.Post, //Put for edit; Post for Create; Get for search
};
string encodeStr = $"{UserName}:{Password}";
var bytes = Encoding.UTF8.GetBytes(encodeStr);
string base64 = Convert.ToBase64String(bytes);
requestMessage.Headers.Add("Authorization", "Basic " + base64);
requestMessage.Headers.Add("Accept", "application/json");
requestMessage.Headers.Add("ContentType", "application/json");
requestMessage.Content = new StringContent(JsonString, Encoding.UTF8,
    "application/json");
try
{
    using (var cts = new CancellationTokenSource())
    {
        cts.CancelAfter(TimeSpan.FromMinutes(2));

        using (var responseA = await SendAsync(requestMessage, cts, 60000, false))
        {
            var res = await responseA.Content.ReadAsStringAsync(); //When updated successfully, response null here
        }
    }
}
catch (Exception ex)
{

}

 

http client公共方法

        /// <summary>
        /// Sends the asynchronous.
        /// </summary>
        /// <param name="requestMessage">The request message.</param>
        /// <param name="cts">The CTS.</param>
        /// <param name="timeoutMilliseconds">The timeout seconds.</param>
        /// <param name="isUseDefaultCredentials">To identify whether apply to the default credential</param>
        /// <returns></returns>
        /// <exception cref="System.TimeoutException"></exception>
        public static async Task<HttpResponseMessage> SendAsync(HttpRequestMessage requestMessage, CancellationTokenSource cts, double timeoutMilliseconds = 60000, bool isUseDefaultCredentials = false)
        {
            try
            {
                if (isUseDefaultCredentials)
                {
                    using (HttpClient client = new HttpClient(new HttpClientHandler { UseDefaultCredentials = true }))
                    {
                        client.Timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
                        return await client.SendAsync(requestMessage, cts.Token);
                    }
                }
                else
                {
                    using (HttpClient client = new HttpClient())
                    {
                        client.Timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
                        return await client.SendAsync(requestMessage, cts.Token);
                    }
                }
            }
            catch (OperationCanceledException)
                when (!cts.IsCancellationRequested)
            {
                throw new TimeoutException();
            }
        }

 

标签:JIRA,cts,API,requestMessage,new,issue,经验总结
From: https://www.cnblogs.com/AlphaThink-AT003/p/16931902.html

相关文章

  • Spring Boot中使用Swagger3.0.0版本构建RESTful APIs
    文章目录​​一、项目描述​​​​二、简介​​​​三、Swagger2.X和Swagger3.0.0的对比​​​​1)SpringFox2.x发布​​​​2)SpringFox3.0.0发布​​​​3)swagger3.0......
  • 幂等——Java基础api设计
    幂等什么是幂等幂等性:****多次调用方法或者接口不会改变业务状态,可以保证重复调用的结果和单次调用的结果一致。使用幂等的场景1.前端重复提交在用户注册、用户创建商......
  • 拓端tecdat|【视频】Rapidminer辅导关联规则和文本挖掘模型对评论数据进行分析
    关联规则是if/then语句,可帮助发现看似无关的数据之间的关系。关联规则的一个示例是“如果客户购买鸡蛋,那么他有80%的可能性也购买牛奶”。关联规则包含......
  • WebAPI如何提供下载文件功能
    提问WebAPI如何提供下载文件功能回答//开启目录浏览服务builder.Services.AddDirectoryBrowser();//指定文件目录app.UseFileServer(newFileServerOptions{Fi......
  • cudnn Backend API注意事项
    一、在包含多个节点的图中,不支持in-placenode。(如果图只包含一个节点,支持in-placenode)Notethatgraphswithmorethanoneoperationnodedonotsupportin-placeo......
  • OCC BRepPrimAPI_MakeBox实现步骤记录
    OCC构建Box实现过程解析:TopoDS_Shape=BRepPrimAPI_MakeBox(5,5,5); //BRepPrimAPI_MakeBox简单介绍BRepPrimAPI_MakeBox:publicBRepBuilderAPI_MakeShape{p......
  • 软件吞噬世界,Api快速入门到放弃
    正如汽车行业必须达到一定的规模,才能让企业只生产一个部件。软件产业现在已经足够大了,尤其当你接受所谓的“软件吞噬世界”此类的说法时更是如此。因此,和汽车工业不再生产自......
  • .NET6之MiniAPI(二十一):限流
    限流,可以网络的基础设施进行配置实现,也可以在网关的地方进行限流,但服务本身的限流也不可或缺,因为当多副本时,一个副本故障,流量对于其他副本来说会提高,如果超过其承受请求量......
  • .NET6之MiniAPI(二十一):限流
    限流,可以网络的基础设施进行配置实现,也可以在网关的地方进行限流,但服务本身的限流也不可或缺,因为当多副本时,一个副本故障,流量对于其他副本来说会提高,如果超过其承受请求量......
  • .NET6之MiniAPI(十九):NLog
    在本系例文章的第八篇中,我们聊过官方的日志实现,即《.NET6之MiniAPI(八):日志》。但官方的日志功能更多是提供了一个实现基础,对于要求一个灵活,强大,方便的日志体系,官方的还是......