gpt4 book ai didi

java - 通过 Java 编辑 Jenkins 中的构建描述

转载 作者:太空宇宙 更新时间:2023-11-04 12:32:10 25 4
gpt4 key购买 nike

我正在尝试在 Jenkins 中编辑职位描述。以前我是通过 bash 执行 'curl --data-urlencode 'description=ABCDEFGH' --data-urlencode "Submit=Submit""https://Login/APItoken@jenkinsURL/job/JobName/Build/submitDescription "来完成的。现在,我想通过 Java 来做同样的事情。这是我到目前为止所拥有的。与往常一样,我认为“它应该有效,但事实并非如此”:

import java.io.*;
import java.util.*;
import java.net.URL;
import java.net.URLEncoder;
import java.net.HttpURLConnection;


public class JenkinsDesUpdate {

public static void main(String[] args) throws Exception {

URL url = new URL("https://Login:APItoken@jenkinsURL/job/JobName/Build/configSubmit");


Map <String,Object> params = new LinkedHashMap<>();
params.put("description", "THIS IS A NEW DESCRIPTION");
params.put(DisplayName","THIS IS A NEW DISPLAY NAME");
StringBuilder postData = new StringBuilder();

for (Map.Entry <String,Object> param : params.entrySet()) {
if (postData.length() != 0) postData.append('&');
postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
postData.append('=');
postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
}


byte[] postDataBytes = postData.toString().getBytes("UTF-8");

HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
conn.setDoOutput(true);
conn.getOutputStream().write(postDataBytes);

Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));

for (int c; (c = in.read()) >= 0;)
System.out.print((char)c);

}

}

好吧,让我听听你的想法。我得到的是一个网站的 HTML:

function showTranslationDialog() {
if(!translation.launchDialog)
loadScript("address/plugin/translation/dialog.js");
else
translation.launchDialog();
return false;
}</script></body></html>

如果我的 URL 是“.../Build”或

服务器返回HTTP响应代码:403

当我的 URL 为“.../Build/configSubmit”时,如上面的代码所示。

提前致谢!

最佳答案

我建议使用 jenkins 客户端 sdk 进行交互。

Java - https://github.com/jenkinsci/java-client-api

Python - https://pypi.python.org/pypi/jenkinsapi

关于java - 通过 Java 编辑 Jenkins 中的构建描述,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37745513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com