gpt4 book ai didi

jenkins - 从 Jenkins 脚本控制台访问 Slack 插件

转载 作者:行者123 更新时间:2023-12-02 21:26:34 30 4
gpt4 key购买 nike

我想知道是否可以从 Jenkins 脚本控制台模拟管道插件操作。例如,Slack 插件用于使用以下命令通过管道作业发送通知:

slackSend (color: colorCode, message: summary)

我想尝试一下并查看对象和属性。我非常确定 Jenkins 脚本控制台中的 groovy 是可能的。

最佳答案

我发现它通常需要查看源代码。在本例中,源位于 GitHub 上:jenkinsci/slack-plugin .

以下是我要做的步骤:

通过查看代码,我注意到的主要部分是 StandardSlackService使用提供给该步骤的参数创建,并且可能来自全局 Jenkins 配置。

如果您想从脚本控制台执行此代码,则可以对其进行一些调整,因为很难以与管道路径相同的方式执行它。

下面是我认为的相关代码供引用:

SlackService slackService = getSlackService(baseUrl, team, token, tokenCredentialId, botUser, channel);
boolean publishSuccess;
if(step.attachments != null){
JsonSlurper jsonSlurper = new JsonSlurper();
JSON json = null;
try {
json = jsonSlurper.parseText(step.attachments);
} catch (JSONException e) {
listener.error(Messages.NotificationFailedWithException(e));
return null;
}
if(!(json instanceof JSONArray)){
listener.error(Messages.NotificationFailedWithException(new IllegalArgumentException("Attachments must be JSONArray")));
return null;
}
JSONArray jsonArray = (JSONArray) json;
for (int i = 0; i < jsonArray.size(); i++) {
Object object = jsonArray.get(i);
if(object instanceof JSONObject){
JSONObject jsonNode = ((JSONObject) object);
if (!jsonNode.has("fallback")) {
jsonNode.put("fallback", step.message);
}
}
}
publishSuccess = slackService.publish(jsonArray, color);
}else{
publishSuccess = slackService.publish(step.message, color);
}
if (!publishSuccess && step.failOnError) {
throw new AbortException(Messages.NotificationFailed());
} else if (!publishSuccess) {
listener.error(Messages.NotificationFailed());
}
// rest of method...

// ...

//streamline unit testing
SlackService getSlackService(String baseUrl, String team, String token, String tokenCredentialId, boolean botUser, String channel) {
return new StandardSlackService(baseUrl, team, token, tokenCredentialId, botUser, channel);
}

关于jenkins - 从 Jenkins 脚本控制台访问 Slack 插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47970976/

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