Skip to content

Commit 9e67462

Browse files
authored
Merge pull request #20 from hejieehe/perf_11694
perf: 优化代码源webhook解析流程 #11694
2 parents 259b610 + be315b8 commit 9e67462

File tree

8 files changed

+130
-153
lines changed

8 files changed

+130
-153
lines changed

buildSrc/src/main/kotlin/Versions.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
object Release {
22
const val Group = "com.tencent.bk.devops.scm"
3-
const val Version = "1.0.2"
3+
const val Version = "1.0.3"
44
}
55

66
object Versions {

devops-scm-api/src/main/java/com/tencent/devops/scm/api/exception/NotFoundScmApiException.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package com.tencent.devops.scm.api.exception;
22

3+
import lombok.Getter;
4+
35
/**
46
* 资源不存在(HTTP 404)
57
* 用于处理404异常
68
*/
9+
@Getter
710
public class NotFoundScmApiException extends ScmApiException {
8-
911
public NotFoundScmApiException(String message) {
1012
super(message);
13+
this.statusCode = 404;
1114
}
1215

1316
public NotFoundScmApiException(String message, Throwable cause) {

devops-scm-api/src/main/java/com/tencent/devops/scm/api/exception/ScmApiException.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
package com.tencent.devops.scm.api.exception;
22

3+
import lombok.Getter;
4+
35
/**
46
* scm api异常基类
57
*/
8+
@Getter
69
public class ScmApiException extends RuntimeException {
10+
Integer statusCode;
11+
12+
public ScmApiException(String message, int code) {
13+
super(message);
14+
this.statusCode = code;
15+
}
716

817
public ScmApiException(String message) {
918
super(message);

devops-scm-api/src/main/java/com/tencent/devops/scm/api/exception/UnAuthorizedScmApiException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
* 4. 权限范围不足
1010
*/
1111
public class UnAuthorizedScmApiException extends ScmApiException {
12-
1312
public UnAuthorizedScmApiException(String message) {
1413
super(message);
14+
this.statusCode = 401;
1515
}
1616

1717
public UnAuthorizedScmApiException(String message, Throwable cause) {

devops-scm-provider/devops-scm-provider-git/devops-scm-provider-tgit/src/main/java/com/tencent/devops/scm/provider/git/tgit/TGitApiTemplate.java

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.tencent.devops.scm.api.exception.NotFoundScmApiException;
44
import com.tencent.devops.scm.api.exception.ScmApiException;
5+
import com.tencent.devops.scm.api.exception.UnAuthorizedScmApiException;
56
import com.tencent.devops.scm.api.pojo.auth.IScmAuth;
67
import com.tencent.devops.scm.api.pojo.repository.ScmProviderRepository;
78
import com.tencent.devops.scm.api.pojo.repository.git.GitScmProviderRepository;
@@ -25,11 +26,7 @@ public static <R> R execute(ScmProviderRepository repository, TGitApiFactory api
2526
TGitApi tGitApi = apiFactory.fromAuthProvider(TGitAuthProviderFactory.create(repo.getAuth()));
2627
return apiFunction.apply(repo, tGitApi);
2728
} catch (Throwable t) {
28-
if (t instanceof TGitApiException) {
29-
throw translateException((TGitApiException) t);
30-
} else {
31-
throw new ScmApiException(t);
32-
}
29+
throw handleThrowable(t);
3330
}
3431
}
3532

@@ -38,11 +35,7 @@ public static <R> R execute(IScmAuth auth, TGitApiFactory apiFactory, Function<T
3835
TGitApi tGitApi = apiFactory.fromAuthProvider(TGitAuthProviderFactory.create(auth));
3936
return apiFunction.apply(tGitApi);
4037
} catch (Throwable t) {
41-
if (t instanceof TGitApiException) {
42-
throw translateException((TGitApiException) t);
43-
} else {
44-
throw new ScmApiException(t);
45-
}
38+
throw handleThrowable(t);
4639
}
4740
}
4841

@@ -53,21 +46,26 @@ public static void execute(ScmProviderRepository repository, TGitApiFactory apiF
5346
TGitApi tGitApi = apiFactory.fromAuthProvider(TGitAuthProviderFactory.create(repo.getAuth()));
5447
apiConsumer.accept(repo, tGitApi);
5548
} catch (Throwable t) {
56-
if (t instanceof TGitApiException) {
57-
throw translateException((TGitApiException) t);
58-
} else {
59-
throw new ScmApiException(t);
60-
}
49+
throw handleThrowable(t);
6150
}
6251
}
6352

6453
private static ScmApiException translateException(TGitApiException e) {
6554
switch (e.getStatusCode()) {
6655
case 404:
6756
return new NotFoundScmApiException(e.getMessage());
57+
case 401, 403:
58+
return new UnAuthorizedScmApiException(e.getMessage());
6859
default:
69-
return new ScmApiException(e);
60+
return new ScmApiException(e.getMessage(), e.getStatusCode());
61+
}
62+
}
7063

64+
private static ScmApiException handleThrowable(Throwable t) {
65+
if (t instanceof TGitApiException) {
66+
return translateException((TGitApiException) t);
67+
} else {
68+
return new ScmApiException(t);
7169
}
7270
}
7371
}

0 commit comments

Comments
 (0)