jenkins 凭证credentials java api

自定义的实体类
@Data
@NoArgsConstructor
@ApiModel(description = "CredentialsStoreActionDTO")
@AllArgsConstructor
public class CredentialsStoreActionDTO {

    private String _class;

    private List<JenkinsCredentialsInfoDTO> credentials;

    private String description;

    private String displayName;

    private String fullDisplayName;

    private String fullName;

    private Boolean global;

    private String urlName;
}
@Data
@NoArgsConstructor
@ApiModel(description = "JenkinsCredentialsInfoDTO")
@AllArgsConstructor
public class JenkinsCredentialsInfoDTO {

    private String id;

    private String description;

    private String displayName;

    private Object fingerprint;

    private String fullName;

    private String typeName;
}
// 获取jenkinsHttpClient
public JenkinsHttpClient getClient(String jenkinsUrl, String userName, String token) {
    JenkinsHttpClient jenkinsHttpClient = null;
    try {
        jenkinsHttpClient = new JenkinsHttpClient(new URI(jenkinsUrl), userName, token);
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
    return jenkinsHttpClient;
}
谢谢  https://cloud.tencent.com/developer/article/1536203 根据里面的方法整理出来的。
1: 查询 凭证:jenkins的路径,用户名,密码(用户要有权限)
public List<JenkinsCredentialsInfoDTO> queryCredentialsList(String url, String userName, String password) throws
        Exception {
    JenkinsHttpClient jenkinsHttpClient = getClient(url, userName, password);
  
    // 获取凭证列表 curl -X GET http://www.xxx.xxx/credentials/store/system/domain/_/api/json?depth=1 --user username:password
    String credentials = null;
    if (org.apache.commons.lang3.StringUtils.endsWith(url, "/")) {
        credentials = jenkinsHttpClient.get(url + "credentials/store/system/domain/_/api/json?depth=1");
    } else {
        credentials = jenkinsHttpClient.get(url + "/credentials/store/system/domain/_/api/json?depth=1");
    }
    if (!StringUtils.isEmpty(credentials)) {
        ObjectMapper objectMapper = new ObjectMapper();
        CredentialsStoreActionDTO credentialsStoreActionDTO = objectMapper.readValue(credentials, CredentialsStoreActionDTO.class);
        return CollectionUtils.isEmpty(credentialsStoreActionDTO.getCredentials()) == true ? new ArrayList<>() : credentialsStoreActionDTO.getCredentials();
    }
    return new ArrayList();
}

2:创建凭证: 如果自己定义凭证credentialsId就传入凭证,如果没有。就不输入,jenkins自己创建

public void createCredentials(String credentialsId, String url, String userName, String password) throws
        Exception {
    JenkinsHttpClient jenkinsHttpClient = getClient(url, userName, password);
    List<NameValuePair> data = new ArrayList<>();
    data.add(createCredentialsParam(credentialsId, userName, password));
    HttpResponse httpResponse = jenkinsHttpClient.post_form_with_result("credentials/store/system/domain/_/createCredentials", data, false);
    if (httpResponse.getStatusLine().getStatusCode() == 302) {
        logger.info("创建凭证成功,返回状态[{}]", httpResponse.getStatusLine().getStatusCode());
    } else {
        throw new Exception(“  ”);
    }
}
// 创建凭证参数
private BasicNameValuePair createCredentialsParam(String credentialsId, String userName, String password) {
    if (StringUtils.isEmpty(credentialsId)) {
        return new BasicNameValuePair("json", "{" + "\"\": \"0\"," + "\"credentials\": " +
                "{" + "\"scope\": \"GLOBAL\"," +
                "\"id\": \"\"," +
                "\"username\": \"" + userName + "\"," +
                "\"password\": \"" + password + "\"," +
                "\"description\": \"\"," +
                "\"$class\": \"com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl\"" +
                "}" +
                "}");
    } else {
        return new BasicNameValuePair("json", "{" + "\"\": \"0\"," + "\"credentials\": " +
                "{" + "\"scope\": \"GLOBAL\"," +
                "\"id\": \"" + credentialsId + "\"," +
                "\"username\": \"" + userName + "\"," +
                "\"password\": \"" + password + "\"," +
                "\"description\": \"\"," +
                "\"$class\": \"com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl\"" +
                "}" +
                "}");
    }
}

3:删除凭证

public void deleteCredentials(String credentialsId, String url, String userName, String password) throws
        Exception {
    JenkinsHttpClient jenkinsHttpClient = getClient(url, userName, password);
    String cUrl = url + "credentials/store/system/domain/_/credential/" + credentialsId + "/doDelete";
    jenkinsHttpClient.post(cUrl);
}
4:更新凭证
public void updateCredentials(String newId, String oldId, String url, String userName, String password) throws
        Exception {
    String jeniknsUrl = url + "credentials/store/system/domain/_/credential/" + oldId + "/updateSubmit";
    String json = null;
    if (StringUtils.isEmpty(newId)) {
        json = "{\"stapler-class\": \"com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl\", " +
                "\"scope\": \"GLOBAL\", \"username\":\"" + userName + "\", \"password\":\"" + password + "\"," +
                "\"id\":\"\", \"description\": \"\"}";
    } else {
        json = "{\"stapler-class\": \"com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl\", " +
                "\"scope\": \"GLOBAL\", \"username\":\"" + userName + "\", \"password\":\"" + password + "\"," +
                "\"id\":\"" + newId + "\", \"description\": \"\"}";
    }
    List<NameValuePair> data = new ArrayList<>();
    data.add(new BasicNameValuePair("json", json));
    JenkinsHttpClient jenkinsHttpClient = getClient(url, userName, password);
    HttpResponse httpResponse = jenkinsHttpClient.post_form_with_result(jeniknsUrl, data, false);
    if (httpResponse.getStatusLine().getStatusCode() == 302) {
        logger.info("更新凭证成功,返回状态[{}]", httpResponse.getStatusLine().getStatusCode());
    }
}
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值