Skip to content

Commit 7cefbfe

Browse files
committed
keep repo id from git event
1 parent 435411d commit 7cefbfe

File tree

10 files changed

+87
-34
lines changed

10 files changed

+87
-34
lines changed

core/src/main/java/com/flowci/core/common/domain/Variables.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.flowci.core.common.domain;
1818

1919
import com.google.common.collect.ImmutableList;
20+
import com.google.common.collect.ImmutableSet;
2021

2122
import java.util.Collection;
2223

@@ -126,6 +127,23 @@ public abstract static class Git {
126127
*/
127128
public static final String BRANCH = "FLOWCI_GIT_BRANCH";
128129

130+
/**
131+
* For some git sources, they need repo id for write back commit status
132+
*/
133+
public static final String REPO_ID = "FLOWCI_GIT_REPO_ID";
134+
135+
public static final Collection<String> GENERIC_VARS = ImmutableSet.<String>builder()
136+
.add(SOURCE)
137+
.add(COMMIT_ID)
138+
.add(EVENT)
139+
.add(EVENT_ID)
140+
.add(REPO_ID)
141+
.add(BRANCH)
142+
.add(SECRET)
143+
.add(REPO_NAME)
144+
.add(URL)
145+
.build();
146+
129147
/**
130148
* Push / Tag variables
131149
*/
@@ -134,7 +152,7 @@ public abstract static class Git {
134152
public static final String PUSH_MESSAGE = "FLOWCI_GIT_COMMIT_MESSAGE";
135153
public static final String PUSH_COMMIT_TOTAL = "FLOWCI_GIT_COMMIT_TOTAL";
136154
public static final String PUSH_COMMIT_LIST = "FLOWCI_GIT_COMMIT_LIST"; // b64 json
137-
public static final Collection<String> PUSH_TAG_VARS = ImmutableList.<String>builder()
155+
public static final Collection<String> PUSH_TAG_VARS = ImmutableSet.<String>builder()
138156
.add(PUSH_AUTHOR)
139157
.add(PUSH_BRANCH)
140158
.add(PUSH_MESSAGE)
@@ -158,7 +176,7 @@ public abstract static class Git {
158176
public static final String PR_BASE_REPO_NAME = "FLOWCI_GIT_PR_BASE_REPO_NAME";
159177
public static final String PR_BASE_REPO_BRANCH = "FLOWCI_GIT_PR_BASE_REPO_BRANCH";
160178
public static final String PR_BASE_REPO_COMMIT = "FLOWCI_GIT_PR_BASE_REPO_COMMIT";
161-
public static final Collection<String> PR_VARS = ImmutableList.<String>builder()
179+
public static final Collection<String> PR_VARS = ImmutableSet.<String>builder()
162180
.add(PR_TITLE)
163181
.add(PR_MESSAGE)
164182
.add(PR_AUTHOR)
@@ -193,7 +211,7 @@ public abstract static class Git {
193211
public static final String PATCHSET_INSERT_SIZE = "FLOWCI_GIT_PATCHSET_INSERT_SIZE";
194212
public static final String PATCHSET_DELETE_SIZE = "FLOWCI_GIT_PATCHSET_DELETE_SIZE";
195213
public static final String PATCHSET_AUTHOR = "FLOWCI_GIT_PATCHSET_AUTHOR";
196-
public static final Collection<String> PATCHSET_VARS = ImmutableList.<String>builder()
214+
public static final Collection<String> PATCHSET_VARS = ImmutableSet.<String>builder()
197215
.add(PATCHSET_SUBJECT)
198216
.add(PATCHSET_MESSAGE)
199217
.add(PATCHSET_PROJECT)

core/src/main/java/com/flowci/core/git/client/GerritAPIClient.java renamed to core/src/main/java/com/flowci/core/git/client/GerritApiClient.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.fasterxml.jackson.core.JsonProcessingException;
55
import com.fasterxml.jackson.databind.ObjectMapper;
66
import com.flowci.core.git.domain.GitCommitStatus;
7-
import com.flowci.core.git.domain.GitConfig;
87
import com.flowci.core.git.domain.GitConfigWithHost;
98
import com.flowci.core.git.util.CommitHelper;
109
import com.flowci.core.secret.domain.AuthSecret;
@@ -26,13 +25,13 @@
2625
import java.net.http.HttpResponse;
2726

2827
@Log4j2
29-
public class GerritAPIClient implements GitAPIClient {
28+
public class GerritApiClient implements GitApiClient<GitConfigWithHost> {
3029

3130
private final HttpClient httpClient;
3231

3332
private final ObjectMapper objectMapper;
3433

35-
public GerritAPIClient(HttpClient httpClient, ObjectMapper objectMapper) {
34+
public GerritApiClient(HttpClient httpClient, ObjectMapper objectMapper) {
3635
this.httpClient = httpClient;
3736
this.objectMapper = objectMapper;
3837
}
@@ -41,11 +40,7 @@ public GerritAPIClient(HttpClient httpClient, ObjectMapper objectMapper) {
4140
* Write commit status on change only
4241
*/
4342
@Override
44-
public void writeCommitStatus(GitCommitStatus commit, GitConfig config) {
45-
if (!(config instanceof GitConfigWithHost)) {
46-
throw new ArgumentException("GitConfigWithHost is required for Gerrit");
47-
}
48-
43+
public void writeCommitStatus(GitCommitStatus commit, GitConfigWithHost config) {
4944
var secret = config.getSecretObj();
5045
if (!(secret instanceof AuthSecret)) {
5146
throw new ArgumentException("AuthSecret is required for Gerrit");
@@ -57,9 +52,7 @@ public void writeCommitStatus(GitCommitStatus commit, GitConfig config) {
5752
}
5853

5954
try {
60-
var gitConfig = (GitConfigWithHost) config;
61-
62-
var url = UriComponentsBuilder.fromHttpUrl(gitConfig.getHost())
55+
var url = UriComponentsBuilder.fromHttpUrl(config.getHost())
6356
.path("/a/changes/")
6457
.path(changeId.get())
6558
.path("/revisions/")

core/src/main/java/com/flowci/core/git/client/GitAPIClient.java renamed to core/src/main/java/com/flowci/core/git/client/GitApiClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import com.flowci.core.git.domain.GitCommitStatus;
44
import com.flowci.core.git.domain.GitConfig;
55

6-
public interface GitAPIClient {
6+
public interface GitApiClient<C extends GitConfig> {
77

8-
void writeCommitStatus(GitCommitStatus commit, GitConfig config);
8+
void writeCommitStatus(GitCommitStatus commit, C config);
99
}

core/src/main/java/com/flowci/core/git/client/GithubAPIClient.java renamed to core/src/main/java/com/flowci/core/git/client/GitHubApiClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import java.util.Objects;
2424

2525
@Log4j2
26-
public class GithubAPIClient implements GitAPIClient {
26+
public class GitHubApiClient implements GitApiClient<GitConfig> {
2727

2828
private final static UriTemplate HttpTemplate = new UriTemplate("https://github.com/{owner}/{repo}.git");
2929

@@ -37,7 +37,7 @@ public class GithubAPIClient implements GitAPIClient {
3737

3838
private final ObjectMapper objectMapper;
3939

40-
public GithubAPIClient(HttpClient httpClient, ObjectMapper objectMapper) {
40+
public GitHubApiClient(HttpClient httpClient, ObjectMapper objectMapper) {
4141
this.httpClient = httpClient;
4242
this.objectMapper = objectMapper;
4343
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.flowci.core.git.client;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.flowci.core.git.domain.GitCommitStatus;
5+
import com.flowci.core.git.domain.GitConfigWithHost;
6+
import com.flowci.core.secret.domain.TokenSecret;
7+
import com.flowci.exception.ArgumentException;
8+
import lombok.extern.log4j.Log4j2;
9+
10+
import java.net.http.HttpClient;
11+
12+
@Log4j2
13+
public class GitLabV4ApiClient implements GitApiClient<GitConfigWithHost> {
14+
15+
private final HttpClient httpClient;
16+
17+
private final ObjectMapper objectMapper;
18+
19+
public GitLabV4ApiClient(HttpClient httpClient, ObjectMapper objectMapper) {
20+
this.httpClient = httpClient;
21+
this.objectMapper = objectMapper;
22+
}
23+
24+
@Override
25+
public void writeCommitStatus(GitCommitStatus commit, GitConfigWithHost config) {
26+
var secret = config.getSecretObj();
27+
if (!(secret instanceof TokenSecret)) {
28+
throw new ArgumentException("TokenSecret is required for Gerrit");
29+
}
30+
31+
32+
}
33+
}

core/src/main/java/com/flowci/core/git/converter/GitLabConverter.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,13 +204,15 @@ public GitTrigger toTrigger() {
204204
GitPrTrigger.Source head = new GitPrTrigger.Source();
205205
head.setCommit(attributes.lastCommit.id);
206206
head.setRef(attributes.sourceBranch);
207+
head.setRepoId(attributes.source.id);
207208
head.setRepoName(attributes.source.name);
208209
head.setRepoUrl(attributes.source.webUrl);
209210
trigger.setHead(head);
210211

211212
GitPrTrigger.Source base = new GitPrTrigger.Source();
212213
base.setCommit(StringHelper.EMPTY);
213214
base.setRef(attributes.targetBranch);
215+
base.setRepoId(attributes.target.id);
214216
base.setRepoName(attributes.target.name);
215217
base.setRepoUrl(attributes.target.webUrl);
216218
trigger.setBase(base);

core/src/main/java/com/flowci/core/git/domain/GitPrTrigger.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public StringVars toVariableMap() {
7373
map.put(PR_BASE_REPO_COMMIT, base.commit);
7474

7575
map.put(BRANCH, merged ? base.ref : head.ref);
76+
map.put(REPO_ID, merged ? base.repoId : head.repoId);
7677
return map;
7778
}
7879

@@ -99,6 +100,8 @@ public static class Source {
99100

100101
private String repoName;
101102

103+
private String repoId;
104+
102105
private String repoUrl;
103106
}
104107
}

core/src/main/java/com/flowci/core/git/service/GitConfigServiceImpl.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import com.fasterxml.jackson.databind.ObjectMapper;
44
import com.flowci.core.common.domain.GitSource;
55
import com.flowci.core.common.manager.SpringEventManager;
6-
import com.flowci.core.git.client.GerritAPIClient;
7-
import com.flowci.core.git.client.GitAPIClient;
8-
import com.flowci.core.git.client.GithubAPIClient;
6+
import com.flowci.core.git.client.GerritApiClient;
7+
import com.flowci.core.git.client.GitApiClient;
8+
import com.flowci.core.git.client.GitHubApiClient;
99
import com.flowci.core.git.dao.GitConfigDao;
1010
import com.flowci.core.git.domain.GitCommitStatus;
1111
import com.flowci.core.git.domain.GitConfig;
@@ -135,7 +135,7 @@ Secret fetch(String name, Class<?> expected) {
135135

136136
private class GitHubOperationHandler extends OperationHandler {
137137

138-
private final GitAPIClient client = new GithubAPIClient(httpClient, objectMapper);
138+
private final GitApiClient<GitConfig> client = new GitHubApiClient(httpClient, objectMapper);
139139

140140
@Override
141141
public GitConfig save(GitConfig config) {
@@ -161,15 +161,11 @@ public void writeCommit(GitCommitStatus commit, GitConfig config) {
161161

162162
private class GerritOperationHandler extends OperationHandler {
163163

164-
private final GitAPIClient client = new GerritAPIClient(httpClient, objectMapper);
164+
private final GitApiClient<GitConfigWithHost> client = new GerritApiClient(httpClient, objectMapper);
165165

166166
@Override
167167
public GitConfig save(GitConfig config) {
168-
if (!(config instanceof GitConfigWithHost)) {
169-
throw new ArgumentException("GitConfigWithHost is required");
170-
}
171-
172-
var c = (GitConfigWithHost) config;
168+
var c = castConfig(config);
173169
Secret secret = fetch(config.getSecret(), AuthSecret.class);
174170

175171
var optional = gitConfigDao.findBySource(GitSource.GERRIT);
@@ -187,7 +183,15 @@ public GitConfig save(GitConfig config) {
187183
public void writeCommit(GitCommitStatus commit, GitConfig config) {
188184
Secret secret = fetch(config.getSecret(), AuthSecret.class);
189185
config.setSecretObj(secret);
190-
client.writeCommitStatus(commit, config);
186+
client.writeCommitStatus(commit, castConfig(config));
187+
}
188+
189+
private GitConfigWithHost castConfig(GitConfig config) {
190+
if (!(config instanceof GitConfigWithHost)) {
191+
throw new ArgumentException("GitConfigWithHost is required");
192+
}
193+
194+
return (GitConfigWithHost) config;
191195
}
192196
}
193197
}

core/src/main/java/com/flowci/core/job/service/JobServiceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ public Job rerun(Flow flow, Job job) {
289289
continue;
290290
}
291291

292-
if (Objects.equals(key, COMMIT_ID) || Objects.equals(key, SOURCE) || Objects.equals(key, EVENT)) {
292+
if (GENERIC_VARS.contains(key)) {
293293
continue;
294294
}
295295

core/src/test/java/com/flowci/core/test/git/GitHubClientTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.flowci.core.test.git;
22

3-
import com.flowci.core.git.client.GithubAPIClient;
3+
import com.flowci.core.git.client.GitHubApiClient;
44
import com.flowci.core.git.domain.GitCommit;
55
import com.flowci.core.git.domain.GitRepo;
66
import org.junit.Test;
@@ -13,17 +13,17 @@ public void should_extract_owner_repo_from_commit_url() {
1313
GitCommit commit = new GitCommit();
1414
commit.setUrl("https://github.com/gy2006/ci-test.git");
1515

16-
GitRepo repo = GithubAPIClient.getRepo(commit);
16+
GitRepo repo = GitHubApiClient.getRepo(commit);
1717
Assert.assertEquals("gy2006", repo.getOwner());
1818
Assert.assertEquals("ci-test", repo.getName());
1919

2020
commit.setUrl("[email protected]:gy2006/ci-test.git");
21-
repo = GithubAPIClient.getRepo(commit);
21+
repo = GitHubApiClient.getRepo(commit);
2222
Assert.assertEquals("gy2006", repo.getOwner());
2323
Assert.assertEquals("ci-test", repo.getName());
2424

2525
commit.setUrl("ssh://[email protected]:gy2006/ci-test.git");
26-
repo = GithubAPIClient.getRepo(commit);
26+
repo = GitHubApiClient.getRepo(commit);
2727
Assert.assertEquals("gy2006", repo.getOwner());
2828
Assert.assertEquals("ci-test", repo.getName());
2929
}

0 commit comments

Comments
 (0)