(三)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的頭部。(具體還未做過)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM