(三)Jira scriptrunner插件

  公司想将项目管理系统合并到jira中,这折腾的不要不要的,硬要实现项目管理系统的需求。好家伙没办法为了这份工作咱只能研究呗。

项目流程审批还需要调第三方系统去审批,jira自身就有审批流程偏不要走其他系统审批。

开始使用webhook去调用接口。接口是自己写的一个很简单的实例。

将一个值写入jira的评论中。接口如下:

public class ValuesController : ApiController
    {
        private List<string> list = new List<string> { "Item1", "Item2", "Item3", "Item4", "Item5" };// GET api/values/5
        public string GetItem(int id)
        {
            string a= list.Find(i => i.ToString().Contains(id.ToString()));
            String url = "http://xxx:8000/rest/auth/1/session";
            string postuser = "{"username":"" + "xxx" + "","password":"" + "xxx" + ""}";
            string seesionsjson = HttpPostTojira("Post", url, postuser, "");
            return  HttpPostTojira("Post", "http://xxx:8000/rest/api/2/issue/PRO2-7/comment", "{"body":"" + a + ""}", seesionsjson);
           
        }
        public string HttpPostTojira(string type, string url, string body, string header)
        {
            try
            {
                Encoding encoding = Encoding.UTF8;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.KeepAlive = false;
                request.ProtocolVersion = HttpVersion.Version10;
                request.Method = type;
                request.Accept = "text/html, application/xhtml+xml, */*";
                request.ContentType = "application/json";
                if (header != "")
                {
                    JObject obj = (JObject)JsonConvert.DeserializeObject(header);
                    string ss = obj["session"]["name"].ToString() + "=" + obj["session"]["value"].ToString();
                    request.Headers.Add("Cookie", ss);

                }
                byte[] buffer = encoding.GetBytes(body);
                request.ContentLength = buffer.Length;

                request.GetRequestStream().Write(buffer, 0, buffer.Length);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    return reader.ReadToEnd();
                }

            }
            catch (Exception ex)
            {
                return "0";

            }
        }

        }

jira工作流中后处理功能添加引用一个webhook,在系统配置网络钩子设置webhook,但万万没想到一直无法将值写入评论,接口没问题单独访问可以直接写入值。

这个办法宣告失败。

尝试第二种方式:scriptrunner插件

1.安装插件

2.点击工作流-》编辑-》后处理功能-》添加script post-function(此安装插件后显示)-》Custom script post-function

3.写脚本,简单调用接口,成功调用并写入评论。

import org.apache.http.HttpResponse
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.CloseableHttpClient
import org.apache.http.impl.client.DefaultHttpClient
import org.apache.http.util.EntityUtils
import org.jsoup.Jsoup
import org.jsoup.nodes.Document


CloseableHttpClient httpClient = new DefaultHttpClient();

String url = "http://xxx:8888/api/values/getitem?id=2";
HttpGet method1 = new HttpGet(url);
method1.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)" +
        " Chrome/56.0.2924.87 Safari/537.36");
String retVal = "";
// 转换参数并设置编码格式
HttpResponse result1;
result1 = httpClient.execute(method1);
retVal = EntityUtils.toString(result1.getEntity(), "utf-8");
//获取列表页Document对象
Document doc = Jsoup.parse(retVal);
print doc;

改功能实现,很开心。

scriptrunner其他功能:

1.运行台,可以run脚本是否正确。

 2.Script Fragments,增加按钮在issue的头部。(具体还未做过)

原文地址:https://www.cnblogs.com/yokiblogs/p/11821737.html