Skip to content

Commit

Permalink
Merge pull request #31 from ddlin0719/master
Browse files Browse the repository at this point in the history
同步内部版代码
  • Loading branch information
carlyin0801 authored Jun 25, 2021
2 parents dfd528e + bd656df commit 42d4267
Show file tree
Hide file tree
Showing 51 changed files with 2,700 additions and 341 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.tencent.devops.ci-plugins</groupId>
<artifactId>java-plugin-sdk</artifactId>
<version>1.0.7</version>
<version>1.0.8</version>

<inceptionYear>2018-2118</inceptionYear>
<description>bk-ci pipeline plugins sdk for java</description>
Expand Down
27 changes: 8 additions & 19 deletions src/main/java/com/tencent/bk/devops/atom/AtomContext.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.tencent.bk.devops.atom;

import com.fasterxml.jackson.core.type.TypeReference;
import com.tencent.bk.devops.atom.common.Constants;
import com.tencent.bk.devops.atom.pojo.AtomBaseParam;
import com.tencent.bk.devops.atom.pojo.AtomResult;
import com.tencent.bk.devops.atom.utils.http.SdkUtils;
import com.tencent.bk.devops.atom.utils.json.JsonUtil;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
Expand All @@ -25,9 +25,9 @@ public class AtomContext<T extends AtomBaseParam> {
private final String dataDir;
private final String inputFile;
private final String outputFile;
private final T param;
private T param;

private final AtomResult result;
private AtomResult result;

private static final String ATOM_FILE_ENCODING = "UTF-8";

Expand All @@ -39,22 +39,10 @@ public class AtomContext<T extends AtomBaseParam> {
* @param paramClazz 参数类
* @throws IOException 如果环境问题导致读不到参数类
*/
AtomContext(Class<T> paramClazz) throws IOException {
String value = System.getenv(Constants.DATA_DIR_ENV);
if (value == null || value.trim().length() == 0 || !(new File(value)).isDirectory()) {
value = System.getProperty("user.dir");
}
dataDir = value;
value = System.getenv(Constants.INPUT_FILE_ENV);
if (value == null || value.trim().length() == 0) {
value = "input.json";
}
inputFile = value;
value = System.getenv(Constants.OUTPUT_FILE_ENV);
if (value == null || value.trim().length() == 0) {
value = "output.json";
}
outputFile = value;
public AtomContext(Class<T> paramClazz) throws IOException {
dataDir = SdkUtils.getDataDir();
inputFile = SdkUtils.getInputFile();
outputFile = SdkUtils.getOutputFile();
param = readParam(paramClazz);
result = new AtomResult();
}
Expand Down Expand Up @@ -88,6 +76,7 @@ public String getSensitiveConfParam(String filedName) {
*
* @return 结果对象
*/
@SuppressWarnings({"all"})
public AtomResult getResult() {
return result;
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/tencent/bk/devops/atom/AtomRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.tencent.bk.devops.atom.spi.AtomService;
import com.tencent.bk.devops.atom.spi.ServiceLoader;
import com.tencent.bk.devops.atom.spi.TaskAtom;
import com.tencent.bk.devops.plugin.pojo.ErrorType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -18,7 +19,7 @@
@SuppressWarnings("all")
public class AtomRunner {

private final static Logger logger = LoggerFactory.getLogger(AtomRunner.class);
private static final Logger logger = LoggerFactory.getLogger(AtomRunner.class);

public static void main(String[] args) throws IOException {
TaskAtom atom = ServiceLoader.load(TaskAtom.class);
Expand Down
30 changes: 21 additions & 9 deletions src/main/java/com/tencent/bk/devops/atom/api/BaseApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@


import com.google.common.collect.Maps;
import com.tencent.bk.devops.atom.exception.RemoteServiceException;
import com.tencent.bk.devops.atom.utils.json.JsonUtil;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
Expand All @@ -18,6 +22,7 @@
public class BaseApi {

protected static final MediaType JSON_CONTENT_TYPE = MediaType.parse("application/json; charset=utf-8");
private static final Logger logger = LoggerFactory.getLogger(BaseApi.class);

/**
* request请求,返回json格式响应报文
Expand All @@ -28,18 +33,18 @@ public class BaseApi {
*/
protected String request(Request request, String errorMessage) throws IOException {
OkHttpClient httpClient = okHttpClient.newBuilder().build();
Response response = httpClient.newCall(request).execute();
assert response.body() != null;
String responseContent = response.body().string();
if (!response.isSuccessful()) {
System.err.println("Fail to request(" + request + ") with code " + response.code()
try (Response response = httpClient.newCall(request).execute()) {
String responseContent = response.body() != null ? response.body().string() : null;
if (!response.isSuccessful()) {
logger.error("Fail to request(" + request + ") with code " + response.code()
+ " , message " + response.message() + " and response" + responseContent);
throw new RuntimeException(errorMessage);
logger.info("excep>>>>>>>>>>>>" + response);
throw new RemoteServiceException(errorMessage, response.code(), responseContent);
}
return responseContent;
}
return responseContent;
}


private OkHttpClient okHttpClient = new okhttp3.OkHttpClient.Builder()
.connectTimeout(5L, TimeUnit.SECONDS)
.readTimeout(300 * 5L, TimeUnit.SECONDS) // Set to 15 minutes
Expand Down Expand Up @@ -143,13 +148,20 @@ public Request buildPut(String path, RequestBody requestBody, Map<String, String
*
* @param path 请求路径
* @param headers 请求头
* @return request对象
*/
public Request buildDelete(String path, Map<String, String> headers) {
String url = buildUrl(path);
return new Request.Builder().url(url).headers(Headers.of(getAllHeaders(headers))).delete().build();
}

/**
* delete请求,返回request对象
*
* @param path 请求路径
* @param requestBody 请求体
* @param headers 请求头
* @return request对象
*/
public Request buildDelete(String path, RequestBody requestBody, Map<String, String> headers) {
String url = buildUrl(path);
return new Request.Builder().url(url).headers(Headers.of(getAllHeaders(headers))).delete(requestBody).build();
Expand Down
16 changes: 9 additions & 7 deletions src/main/java/com/tencent/bk/devops/atom/api/Header.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
* 请求头
* @version 1.0
*/
class Header {
public class Header {
public static String AUTH_HEADER_BUILD_ID = "X-SODA-BID";
public static String AUTH_HEADER_PROJECT_ID = "X-SODA-PID";

static String AUTH_HEADER_DEVOPS_BUILD_TYPE = "X-DEVOPS-BUILD-TYPE";
public static String AUTH_HEADER_DEVOPS_BUILD_TYPE = "X-DEVOPS-BUILD-TYPE";

static String AUTH_HEADER_DEVOPS_PROJECT_ID = "X-DEVOPS-PROJECT-ID";
static String AUTH_HEADER_DEVOPS_BUILD_ID = "X-DEVOPS-BUILD-ID";
static String AUTH_HEADER_DEVOPS_VM_SEQ_ID = "X-DEVOPS-VM-SID";
public static String AUTH_HEADER_DEVOPS_PROJECT_ID = "X-DEVOPS-PROJECT-ID";
public static String AUTH_HEADER_DEVOPS_BUILD_ID = "X-DEVOPS-BUILD-ID";
public static String AUTH_HEADER_DEVOPS_VM_SEQ_ID = "X-DEVOPS-VM-SID";

static String AUTH_HEADER_DEVOPS_AGENT_ID = "X-DEVOPS-AGENT-ID";
static String AUTH_HEADER_DEVOPS_AGENT_SECRET_KEY = "X-DEVOPS-AGENT-SECRET-KEY";
public static String AUTH_HEADER_DEVOPS_AGENT_ID = "X-DEVOPS-AGENT-ID";
public static String AUTH_HEADER_DEVOPS_AGENT_SECRET_KEY = "X-DEVOPS-AGENT-SECRET-KEY";
}
54 changes: 27 additions & 27 deletions src/main/java/com/tencent/bk/devops/atom/api/SdkEnv.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.tencent.bk.devops.atom.api;

import com.google.common.collect.Maps;
import com.tencent.bk.devops.atom.common.BuildType;
import com.tencent.bk.devops.atom.common.Constants;
import com.tencent.bk.devops.atom.utils.http.SdkUtils;
import com.tencent.bk.devops.atom.utils.json.JsonUtil;
Expand All @@ -27,7 +26,7 @@ public class SdkEnv {

private static final Logger logger = LoggerFactory.getLogger(SdkEnv.class);

private BuildType buildType;
private String buildType;
private String projectId;
private String agentId;
private String secretKey;
Expand All @@ -41,11 +40,13 @@ public class SdkEnv {

private static final String HTTPS_PROTOCOL = "https://";


public static Map<String, String> getSdkHeader() {
Map<String, String> map = Maps.newHashMap();
map.put(Header.AUTH_HEADER_DEVOPS_BUILD_TYPE, instance.buildType.name());
map.put(Header.AUTH_HEADER_DEVOPS_BUILD_TYPE, instance.buildType);

map.put(Header.AUTH_HEADER_DEVOPS_PROJECT_ID, instance.projectId);
map.put(Header.AUTH_HEADER_PROJECT_ID, instance.projectId);

map.put(Header.AUTH_HEADER_DEVOPS_AGENT_SECRET_KEY, instance.secretKey);

Expand All @@ -54,40 +55,25 @@ public static Map<String, String> getSdkHeader() {
map.put(Header.AUTH_HEADER_DEVOPS_VM_SEQ_ID, instance.vmSeqId);

map.put(Header.AUTH_HEADER_DEVOPS_BUILD_ID, instance.buildId);
map.put(Header.AUTH_HEADER_BUILD_ID, instance.buildId);
return map;
}

public static String projectId() {
return instance.projectId;
}

public static String agentId() {
return instance.agentId;
}

public static String buildId() {
return instance.buildId;
}

public static String vmSeqId() {
return instance.vmSeqId;
}

public static String getVmSeqId() {
return instance.vmSeqId;
}

@SuppressWarnings("all")
public static void init() throws IOException {
String dataDir = System.getenv(Constants.DATA_DIR_ENV);
if (dataDir == null || dataDir.trim().length() == 0 || !(new File(dataDir)).isDirectory()) {
dataDir = System.getProperty("user.dir");
}
String sdkFile = ".sdk.json";
File file = new File(dataDir + "/" + sdkFile);
String json = FileUtils.readFileToString(file, Charset.defaultCharset());
boolean flag = file.delete(); //读取完后删除文件
logger.info("[java-atom-sdk] delete sdkFile result is:{}", flag);
instance = JsonUtil.fromJson(json, SdkEnv.class);
try {
String json = FileUtils.readFileToString(file, Charset.defaultCharset());
instance = JsonUtil.fromJson(json, SdkEnv.class);
} finally {
boolean flag = file.delete(); //读取完后删除文件
logger.info("[java-atom-sdk] delete sdkFile result is:{}", flag);
}
}

public static String genUrl(String path) {
Expand All @@ -102,4 +88,18 @@ public static String genUrl(String path) {
return urlPrefix + "/" + StringUtils.removeStart(newPath, "/");
}
}

public static String getGatewayHost() {
String host = instance.gateway;
if (host.startsWith(HTTP_PROTOCOL)) {
host = host.replace(HTTP_PROTOCOL, "");
} else if (host.startsWith(HTTPS_PROTOCOL)) {
host = host.replace(HTTPS_PROTOCOL, "");
}
return host;
}

public static String getVmSeqId() {
return instance.vmSeqId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.tencent.bk.devops.atom.exception;

public class RemoteServiceException extends RuntimeException {

private final int httpStatus;
private final String responseContent;

public RemoteServiceException(String errorMessage, int httpStatus, String responseContent) {
super(errorMessage);
this.httpStatus = httpStatus;
this.responseContent = responseContent;
}

public int getHttpStatus() {
return httpStatus;
}

public String getResponseContent() {
return responseContent;
}
}
29 changes: 23 additions & 6 deletions src/main/java/com/tencent/bk/devops/atom/pojo/AtomBaseParam.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,31 @@ public class AtomBaseParam {
@JsonProperty("BK_CI_BUILD_START_TIME")
private String pipelineStartTimeMills;


/**
* 流水线执行人
*/
@JsonProperty("BK_CI_START_USER_ID")
private String pipelineStartUserId;

/**
* 流水线触发人
*/
@JsonProperty("BK_CI_START_USER_NAME")
private String pipelineStartUserName;

/**
* 流水线创建人
*/
@JsonProperty("BK_CI_PIPELINE_CREATE_USER")
private String pipelineCreateUserName;

/**
* 流水线修改人
*/
@JsonProperty("BK_CI_PIPELINE_UPDATE_USER")
private String pipelineUpdateUserName;

/**
* 插件敏感信息
*/
Expand All @@ -93,10 +112,8 @@ public class AtomBaseParam {
@JsonProperty("BK_CI_BUILD_TASK_ID")
private String pipelineTaskId;

@JsonProperty("BK_CI_PIPELINE_UPDATE_USER")
private String pipelineUpdateUserName;

public String getPipelineUpdateUserName() {
return this.pipelineUpdateUserName;
}
/**
* 插件后置动作标识
*/
private String postEntryParam = System.getProperty("postEntryParam");
}
Loading

0 comments on commit 42d4267

Please sign in to comment.