Skip to content

Commit 259b610

Browse files
authored
Merge pull request #17 from hejieehe/bug_16
bug: 修复Push事件引发的空指针异常 #16
2 parents fadf4ef + 59282ac commit 259b610

File tree

5 files changed

+74
-17
lines changed

5 files changed

+74
-17
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,8 @@
1010
##### 2025-04-03
1111
### 修复
1212
- bug: 修复TGIT MR触发action变量异常 #14 [链接](https://github.com/bkdevops-projects/devops-scm/issues/14)
13+
14+
# 1.0.2
15+
##### 2025-04-24
16+
### 修复
17+
- bug: 修复Push事件引发的空指针异常 #16 [链接](https://github.com/bkdevops-projects/devops-scm/issues/16)

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.1"
3+
const val Version = "1.0.2"
44
}
55

66
object Versions {

devops-scm-api/src/main/java/com/tencent/devops/scm/api/constant/WebhookI18Code.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
public interface WebhookI18Code {
77
// git push事件描述
88
String GIT_PUSH_EVENT_DESC = "bkGitPushEventDesc";
9+
// git push创建分支事件描述
10+
String GIT_PUSH_CREATE_EVENT_DESC = "bkGitPushCreateEventDesc";
911
// git push删除事件描述
1012
String GIT_PUSH_DELETE_EVENT_DESC = "bkGitPushDeleteEventDesc";
1113

devops-scm-api/src/main/java/com/tencent/devops/scm/api/pojo/webhook/git/GitPushHook.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.tencent.devops.scm.api.pojo.webhook.git;
22

3+
import static com.tencent.devops.scm.api.constant.WebhookI18Code.GIT_PUSH_CREATE_EVENT_DESC;
4+
import static com.tencent.devops.scm.api.constant.WebhookI18Code.GIT_PUSH_DELETE_EVENT_DESC;
5+
import static com.tencent.devops.scm.api.constant.WebhookI18Code.GIT_PUSH_EVENT_DESC;
36
import static com.tencent.devops.scm.api.constant.WebhookOutputCode.BK_REPO_GIT_WEBHOOK_BRANCH;
47
import static com.tencent.devops.scm.api.constant.WebhookOutputCode.BK_REPO_GIT_WEBHOOK_COMMIT_ID;
58
import static com.tencent.devops.scm.api.constant.WebhookOutputCode.BK_REPO_GIT_WEBHOOK_PUSH_AFTER_COMMIT;
@@ -29,8 +32,8 @@
2932
import static com.tencent.devops.scm.api.constant.WebhookOutputCode.PIPELINE_WEBHOOK_COMMIT_MESSAGE;
3033
import static com.tencent.devops.scm.api.constant.WebhookOutputCode.PIPELINE_WEBHOOK_EVENT_TYPE;
3134
import static com.tencent.devops.scm.api.constant.WebhookOutputCode.PIPELINE_WEBHOOK_REVISION;
35+
import static com.tencent.devops.scm.api.enums.EventAction.DELETE;
3236

33-
import com.tencent.devops.scm.api.constant.WebhookI18Code;
3437
import com.tencent.devops.scm.api.enums.EventAction;
3538
import com.tencent.devops.scm.api.pojo.Change;
3639
import com.tencent.devops.scm.api.pojo.Commit;
@@ -70,6 +73,7 @@ public class GitPushHook implements Webhook {
7073
private String before;
7174
private String after;
7275
private Commit commit;
76+
private String link;
7377
private User sender;
7478
private List<Commit> commits;
7579
// 变更的文件路径
@@ -94,18 +98,14 @@ public String getUserName() {
9498

9599
@Override
96100
public ScmI18Variable getEventDesc() {
97-
String code = WebhookI18Code.GIT_PUSH_EVENT_DESC;
98-
if (action == EventAction.DELETE) {
99-
code = WebhookI18Code.GIT_PUSH_DELETE_EVENT_DESC;
100-
}
101101
List<String> params = Arrays.asList(
102102
GitUtils.trimRef(ref),
103-
commit.getLink(),
103+
link,
104104
GitUtils.getShortSha(commit.getSha()),
105105
sender.getName()
106106
);
107107
return ScmI18Variable.builder()
108-
.code(code)
108+
.code(getI18Code())
109109
.params(params)
110110
.build();
111111
}
@@ -137,7 +137,7 @@ public Map<String, Object> outputs() {
137137
outputParams.put(PIPELINE_GIT_REPO_URL, repo.getHttpUrl());
138138
outputParams.put(PIPELINE_GIT_REF, "refs/heads/" + ref);
139139
outputParams.put(CI_BRANCH, ref);
140-
if (action == EventAction.DELETE) {
140+
if (action == DELETE) {
141141
outputParams.put(PIPELINE_GIT_EVENT, "delete");
142142
} else {
143143
outputParams.put(PIPELINE_GIT_EVENT, "push");
@@ -169,4 +169,19 @@ public Map<String, Object> outputs() {
169169
public Boolean skipCi() {
170170
return skipCi;
171171
}
172+
173+
private String getI18Code() {
174+
String code;
175+
switch (action) {
176+
case NEW_BRANCH:
177+
code = GIT_PUSH_CREATE_EVENT_DESC;
178+
break;
179+
case DELETE:
180+
code = GIT_PUSH_DELETE_EVENT_DESC;
181+
break;
182+
default:
183+
code = GIT_PUSH_EVENT_DESC;
184+
}
185+
return code;
186+
}
172187
}

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

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,18 +154,34 @@ private GitTagHook parseTagHook(String body) {
154154
);
155155

156156
String refName = GitUtils.trimRef(src.getRef());
157-
String linkUrl = String.format("%s/-/tags/%s", repo.getWebUrl(), refName);
157+
// Tag 最新的commit
158+
Commit commit = null;
159+
String linkUrl;
160+
// 组装tag链接
161+
switch (action) {
162+
case DELETE:
163+
linkUrl = repo.getWebUrl();
164+
commit = Commit.builder()
165+
.sha(src.getBefore())
166+
.message("")
167+
.build();
168+
break;
169+
case CREATE:
170+
linkUrl = String.format("%s/-/tags/%s", repo.getWebUrl(), refName);
171+
commit = CollectionUtils.emptyIfNull(src.getCommits())
172+
.stream()
173+
.findFirst()
174+
.map(TGitObjectConverter::convertCommit)
175+
.orElse(null);
176+
break;
177+
default:
178+
linkUrl = "";
179+
}
158180
Reference ref = Reference.builder()
159181
.name(refName)
160182
.sha(sha)
161183
.linkUrl(linkUrl)
162184
.build();
163-
// Tag 最新的commit
164-
Commit commit = CollectionUtils.emptyIfNull(src.getCommits())
165-
.stream()
166-
.findFirst()
167-
.map(TGitObjectConverter::convertCommit)
168-
.orElse(null);
169185
Map<String, Object> extras = fillTagExtra(src);
170186

171187
return GitTagHook.builder()
@@ -471,6 +487,7 @@ private GitPushHook convertPushHook(TGitPushEvent src) {
471487
.sha(src.getCheckoutSha())
472488
.author(author)
473489
.committer(author)
490+
.message("")
474491
.build();
475492
if (!src.getCommits().isEmpty()) {
476493
TGitEventCommit lastCommit = src.getCommits().get(0);
@@ -514,14 +531,32 @@ private GitPushHook convertPushHook(TGitPushEvent src) {
514531
.stream()
515532
.map(TGitObjectConverter::convertCommit)
516533
.collect(Collectors.toList());
534+
String ref = GitUtils.trimRef(src.getRef());
535+
String link;
536+
// 根据事件动作封装事件详情链接
537+
switch (action) {
538+
case NEW_BRANCH:
539+
link = (new StringBuilder())
540+
.append(repository.getWebUrl())
541+
.append("/tree/")
542+
.append(ref)
543+
.toString();
544+
break;
545+
case DELETE:
546+
link = repository.getWebUrl();
547+
break;
548+
default:
549+
link = commit.getLink();
550+
}
517551
return GitPushHook.builder()
518552
.action(action)
519-
.ref(GitUtils.trimRef(src.getRef()))
553+
.ref(ref)
520554
.eventType(TGitEventType.PUSH.name())
521555
.repo(repository)
522556
.before(src.getBefore())
523557
.after(src.getAfter())
524558
.commit(commit)
559+
.link(link)
525560
.sender(user)
526561
.commits(commits)
527562
.changes(changes)

0 commit comments

Comments
 (0)