Skip to content

Commit 1479a7e

Browse files
harshjain12rutvijmehta-harnessdevkimittal
authored
fix: [CI-7838]: Add support for API URL in Gitlab connector (#47016)
* feat: [CI-7838]: Add support for API URL in Gitlab connector (#46978) * feat: [CI-7838]: Add support for API URL in Gitlab connector * feat: [CI-7838]: Add support for API URL in Gitlab connector * feat: [CI-7838]: Add UTs and address comments * ut fix --------- Co-authored-by: Dev Mittal <[email protected]> * fix: [CI-7838]: Add support for API URL in Gitlab connector --------- Co-authored-by: Rutvij Mehta <[email protected]> Co-authored-by: Dev Mittal <[email protected]>
1 parent 39137f8 commit 1479a7e

File tree

10 files changed

+166
-11
lines changed

10 files changed

+166
-11
lines changed

440-connector-nextgen/src/main/java/io/harness/connector/entities/embedded/gitlabconnector/GitlabTokenApiAccess.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@
1616
@TypeAlias("io.harness.connector.entities.embedded.gitlabconnector.GitlabTokenApiAccess")
1717
public class GitlabTokenApiAccess implements GitlabApiAccess {
1818
String tokenRef;
19+
String apiUrl;
1920
}

440-connector-nextgen/src/main/java/io/harness/connector/mappers/gitlabconnector/GitlabDTOToEntity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ private GitlabApiAccess getApiAcessByType(GitlabApiAccessSpecDTO spec, GitlabApi
134134
final GitlabTokenSpecDTO tokenSpec = (GitlabTokenSpecDTO) spec;
135135
return GitlabTokenApiAccess.builder()
136136
.tokenRef(SecretRefHelper.getSecretConfigString(tokenSpec.getTokenRef()))
137+
.apiUrl(tokenSpec.getApiUrl())
137138
.build();
138139
case OAUTH:
139140
final GitlabOauthDTO gitlabOauthDTO = (GitlabOauthDTO) spec;

440-connector-nextgen/src/main/java/io/harness/connector/mappers/gitlabconnector/GitlabEntityToDTO.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ private GitlabApiAccessDTO buildApiAccess(GitlabConnector connector) {
139139
final GitlabTokenSpecDTO gitlabTokenSpecDTO =
140140
GitlabTokenSpecDTO.builder()
141141
.tokenRef(SecretRefHelper.createSecretRef(gitlabTokenApiAccess.getTokenRef()))
142+
.apiUrl(gitlabTokenApiAccess.getApiUrl())
142143
.build();
143144
return GitlabApiAccessDTO.builder().type(GitlabApiAccessType.TOKEN).spec(gitlabTokenSpecDTO).build();
144145
case OAUTH:

440-connector-nextgen/src/test/resources/schema/Connectors/all.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4411,6 +4411,9 @@
44114411
"tokenRef"
44124412
],
44134413
"properties": {
4414+
"apiUrl": {
4415+
"type": "string"
4416+
},
44144417
"tokenRef": {
44154418
"type": "string"
44164419
}

952-scm-java-client/src/main/java/io/harness/impl/scm/ScmGitProviderMapper.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import io.harness.delegate.beans.connector.scm.github.GithubOauthDTO;
2929
import io.harness.delegate.beans.connector.scm.github.GithubTokenSpecDTO;
3030
import io.harness.delegate.beans.connector.scm.gitlab.GitlabApiAccessDTO;
31+
import io.harness.delegate.beans.connector.scm.gitlab.GitlabApiAccessSpecDTO;
3132
import io.harness.delegate.beans.connector.scm.gitlab.GitlabConnectorDTO;
3233
import io.harness.delegate.beans.connector.scm.gitlab.GitlabOauthDTO;
3334
import io.harness.delegate.beans.connector.scm.gitlab.GitlabTokenSpecDTO;
@@ -162,7 +163,7 @@ private Provider mapToGitLabProvider(GitlabConnectorDTO gitlabConnector, boolean
162163
return Provider.newBuilder()
163164
.setGitlab(createGitLabProvider(gitlabConnector))
164165
.setDebug(debug)
165-
.setEndpoint(GitClientHelper.getGitlabApiURL(gitlabConnector.getUrl()))
166+
.setEndpoint(GitClientHelper.getGitlabApiURL(gitlabConnector.getUrl(), getGitlabApiUrl(gitlabConnector)))
166167
.setSkipVerify(skipVerify)
167168
.setAdditionalCertsPath(getAdditionalCertsPath())
168169
.build();
@@ -257,4 +258,17 @@ private String getOauthToken(GithubConnectorDTO githubConnector) {
257258
}
258259
return String.valueOf(githubOauthDTO.getTokenRef().getDecryptedValue());
259260
}
261+
262+
private String getGitlabApiUrl(GitlabConnectorDTO gitlabConnector) {
263+
if (gitlabConnector.getApiAccess() == null || gitlabConnector.getApiAccess().getSpec() == null) {
264+
// not expected
265+
return null;
266+
}
267+
GitlabApiAccessSpecDTO spec = gitlabConnector.getApiAccess().getSpec();
268+
if (spec instanceof GitlabTokenSpecDTO) {
269+
GitlabTokenSpecDTO tokenSpec = (GitlabTokenSpecDTO) spec;
270+
return tokenSpec.getApiUrl();
271+
}
272+
return null;
273+
}
260274
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* Copyright 2023 Harness Inc. All rights reserved.
3+
* Use of this source code is governed by the PolyForm Free Trial 1.0.0 license
4+
* that can be found in the licenses directory at the root of this repository, also available at
5+
* https://polyformproject.org/wp-content/uploads/2020/05/PolyForm-Free-Trial-1.0.0.txt.
6+
*/
7+
8+
package io.harness.impl.scm;
9+
10+
import static io.harness.delegate.beans.connector.scm.GitAuthType.HTTP;
11+
import static io.harness.rule.OwnerRule.RUTVIJ_MEHTA;
12+
13+
import static org.assertj.core.api.Assertions.assertThat;
14+
15+
import io.harness.category.element.UnitTests;
16+
import io.harness.delegate.beans.connector.scm.GitConnectionType;
17+
import io.harness.delegate.beans.connector.scm.gitlab.GitlabApiAccessDTO;
18+
import io.harness.delegate.beans.connector.scm.gitlab.GitlabApiAccessType;
19+
import io.harness.delegate.beans.connector.scm.gitlab.GitlabAuthenticationDTO;
20+
import io.harness.delegate.beans.connector.scm.gitlab.GitlabConnectorDTO;
21+
import io.harness.delegate.beans.connector.scm.gitlab.GitlabHttpCredentialsDTO;
22+
import io.harness.delegate.beans.connector.scm.gitlab.GitlabTokenSpecDTO;
23+
import io.harness.delegate.beans.connector.scm.gitlab.GitlabUsernameTokenDTO;
24+
import io.harness.encryption.SecretRefData;
25+
import io.harness.encryption.SecretRefHelper;
26+
import io.harness.product.ci.scm.proto.Provider;
27+
import io.harness.rule.Owner;
28+
29+
import org.junit.Before;
30+
import org.junit.Test;
31+
import org.junit.experimental.categories.Category;
32+
import org.mockito.InjectMocks;
33+
import org.mockito.MockitoAnnotations;
34+
35+
public class ScmGitProviderMapperTest {
36+
@InjectMocks ScmGitProviderMapper scmGitProviderMapper;
37+
38+
@Before
39+
public void setUp() {
40+
MockitoAnnotations.initMocks(this);
41+
}
42+
43+
@Test
44+
@Owner(developers = RUTVIJ_MEHTA)
45+
@Category(UnitTests.class)
46+
public void testMapToGitlabProviderWithApiUrl() {
47+
final String url = "https://harness.io/gitlab/rutvij.mehta1/spring-cloud-alibaba.git\n";
48+
SecretRefData tokenRef = SecretRefHelper.createSecretRef("tokenRef");
49+
tokenRef.setDecryptedValue("tokenRef".toCharArray());
50+
51+
GitlabHttpCredentialsDTO gitlabHttpCredentialsDTO =
52+
GitlabHttpCredentialsDTO.builder().httpCredentialsSpec(GitlabUsernameTokenDTO.builder().build()).build();
53+
GitlabAuthenticationDTO gitlabAuthenticationDTO =
54+
GitlabAuthenticationDTO.builder().authType(HTTP).credentials(gitlabHttpCredentialsDTO).build();
55+
GitlabApiAccessDTO gitlabApiAccessDTO =
56+
GitlabApiAccessDTO.builder()
57+
.type(GitlabApiAccessType.TOKEN)
58+
.spec(GitlabTokenSpecDTO.builder().tokenRef(tokenRef).apiUrl("https://harness.io/gitlab/").build())
59+
.build();
60+
61+
final GitlabConnectorDTO gitlabConnectorDTO = GitlabConnectorDTO.builder()
62+
.connectionType(GitConnectionType.REPO)
63+
.url(url)
64+
.authentication(gitlabAuthenticationDTO)
65+
.apiAccess(gitlabApiAccessDTO)
66+
.build();
67+
68+
final Provider provider = scmGitProviderMapper.mapToSCMGitProvider(gitlabConnectorDTO);
69+
assertThat(provider).isNotNull();
70+
assertThat(provider.getEndpoint()).isEqualTo("https://harness.io/gitlab/");
71+
}
72+
73+
public void testMapToGitlabProviderWithApiUrlNoSlash() {
74+
final String url = "https://harness.io/gitlab/rutvij.mehta1/spring-cloud-alibaba.git\n";
75+
SecretRefData tokenRef = SecretRefHelper.createSecretRef("tokenRef");
76+
tokenRef.setDecryptedValue("tokenRef".toCharArray());
77+
78+
GitlabHttpCredentialsDTO gitlabHttpCredentialsDTO =
79+
GitlabHttpCredentialsDTO.builder().httpCredentialsSpec(GitlabUsernameTokenDTO.builder().build()).build();
80+
GitlabAuthenticationDTO gitlabAuthenticationDTO =
81+
GitlabAuthenticationDTO.builder().authType(HTTP).credentials(gitlabHttpCredentialsDTO).build();
82+
GitlabApiAccessDTO gitlabApiAccessDTO =
83+
GitlabApiAccessDTO.builder()
84+
.type(GitlabApiAccessType.TOKEN)
85+
.spec(GitlabTokenSpecDTO.builder().tokenRef(tokenRef).apiUrl("https://harness.io/gitlab").build())
86+
.build();
87+
88+
final GitlabConnectorDTO gitlabConnectorDTO = GitlabConnectorDTO.builder()
89+
.connectionType(GitConnectionType.REPO)
90+
.url(url)
91+
.authentication(gitlabAuthenticationDTO)
92+
.apiAccess(gitlabApiAccessDTO)
93+
.build();
94+
95+
final Provider provider = scmGitProviderMapper.mapToSCMGitProvider(gitlabConnectorDTO);
96+
assertThat(provider).isNotNull();
97+
assertThat(provider.getEndpoint()).isEqualTo("https://harness.io/gitlab/");
98+
}
99+
100+
@Test
101+
@Owner(developers = RUTVIJ_MEHTA)
102+
@Category(UnitTests.class)
103+
public void testMapToGitlabProviderWithoutApiUrl() {
104+
final String url = "https://harness.io/gitlab/rutvij.mehta1/spring-cloud-alibaba.git\n";
105+
SecretRefData tokenRef = SecretRefHelper.createSecretRef("tokenRef");
106+
tokenRef.setDecryptedValue("tokenRef".toCharArray());
107+
108+
GitlabHttpCredentialsDTO gitlabHttpCredentialsDTO =
109+
GitlabHttpCredentialsDTO.builder().httpCredentialsSpec(GitlabUsernameTokenDTO.builder().build()).build();
110+
GitlabAuthenticationDTO gitlabAuthenticationDTO =
111+
GitlabAuthenticationDTO.builder().authType(HTTP).credentials(gitlabHttpCredentialsDTO).build();
112+
GitlabApiAccessDTO gitlabApiAccessDTO = GitlabApiAccessDTO.builder()
113+
.type(GitlabApiAccessType.TOKEN)
114+
.spec(GitlabTokenSpecDTO.builder().tokenRef(tokenRef).build())
115+
.build();
116+
117+
final GitlabConnectorDTO gitlabConnectorDTO = GitlabConnectorDTO.builder()
118+
.connectionType(GitConnectionType.REPO)
119+
.url(url)
120+
.authentication(gitlabAuthenticationDTO)
121+
.apiAccess(gitlabApiAccessDTO)
122+
.build();
123+
124+
final Provider provider = scmGitProviderMapper.mapToSCMGitProvider(gitlabConnectorDTO);
125+
assertThat(provider).isNotNull();
126+
assertThat(provider.getEndpoint()).isEqualTo("https://harness.io/");
127+
}
128+
}

954-connector-beans/src/main/java/io/harness/delegate/beans/connector/scm/gitlab/GitlabTokenSpecDTO.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@
2929
description = "This contains details of the information such as references of token needed for Gitlab API access")
3030
public class GitlabTokenSpecDTO implements GitlabApiAccessSpecDTO {
3131
@ApiModelProperty(dataType = "string") @NotNull @SecretReference SecretRefData tokenRef;
32+
@ApiModelProperty(dataType = "string") String apiUrl;
3233
}

960-api-services/src/main/java/io/harness/git/GitClientHelper.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,10 @@ public static String getHttpProtocolPrefix(String url) {
235235
return "https://";
236236
}
237237

238-
public static String getGitlabApiURL(String url) {
238+
public static String getGitlabApiURL(String url, String apiUrl) {
239+
if (!StringUtils.isBlank(apiUrl)) {
240+
return StringUtils.stripEnd(apiUrl, "/") + "/";
241+
}
239242
if (GitClientHelper.isGitlabSAAS(url)) {
240243
return "https://gitlab.com/";
241244
} else {

960-api-services/src/test/java/io/harness/git/GitClientHelperTest.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -472,22 +472,25 @@ public void testGetGithubApiURL() {
472472
@Owner(developers = DEV_MITTAL)
473473
@Category(UnitTests.class)
474474
public void testGetGitlabApiURL() {
475-
assertThat(GitClientHelper.getGitlabApiURL("https://gitlab.com/devki.mittal/test.git"))
475+
assertThat(GitClientHelper.getGitlabApiURL("https://gitlab.com/devki.mittal/test.git", ""))
476476
.isEqualTo("https://gitlab.com/");
477-
assertThat(GitClientHelper.getGitlabApiURL("https://www.gitlab.com/devki.mittal/test.git"))
477+
assertThat(GitClientHelper.getGitlabApiURL("https://www.gitlab.com/devki.mittal/test.git", ""))
478478
.isEqualTo("https://gitlab.com/");
479-
assertThat(GitClientHelper.getGitlabApiURL("https://gitlab.com/devki.mittal/test"))
479+
assertThat(GitClientHelper.getGitlabApiURL("https://gitlab.com/devki.mittal/test", ""))
480480
.isEqualTo("https://gitlab.com/");
481-
assertThat(GitClientHelper.getGitlabApiURL("https://paypal.gitlab.com/devki.mittal/test.git"))
481+
assertThat(GitClientHelper.getGitlabApiURL("https://paypal.gitlab.com/devki.mittal/test.git", ""))
482482
.isEqualTo("https://paypal.gitlab.com/");
483-
assertThat(GitClientHelper.getGitlabApiURL("https://gitlab.paypal.com/devki.mittal/test.git"))
483+
assertThat(GitClientHelper.getGitlabApiURL("https://gitlab.paypal.com/devki.mittal/test.git", ""))
484484
.isEqualTo("https://gitlab.paypal.com/");
485-
assertThat(GitClientHelper.getGitlabApiURL("[email protected]:devki.mittal/test.git"))
485+
assertThat(GitClientHelper.getGitlabApiURL("[email protected]:devki.mittal/test.git", ""))
486486
.isEqualTo("https://gitlab.com/");
487-
assertThat(GitClientHelper.getGitlabApiURL("[email protected]:devki.mittal/test.git"))
487+
assertThat(GitClientHelper.getGitlabApiURL("[email protected]:devki.mittal/test.git", ""))
488488
.isEqualTo("https://gitlab.com/");
489-
assertThat(GitClientHelper.getGitlabApiURL("http://10.67.0.1/devkimittal/harness-core"))
489+
assertThat(GitClientHelper.getGitlabApiURL("http://10.67.0.1/devkimittal/harness-core", ""))
490490
.isEqualTo("http://10.67.0.1/");
491+
assertThat(GitClientHelper.getGitlabApiURL(
492+
"https://harness.io/gitlab/devki.mittal/test.git", "https://harness.io/gitlab/"))
493+
.isEqualTo("https://harness.io/gitlab/");
491494
}
492495

493496
@Test

build.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
build.majorVersion=1
22
build.minorVersion=0
3-
build.number=78927
3+
build.number=78928
44
build.patch=000
55
delegate.version=23.03.12
66
delegate.patch=000

0 commit comments

Comments
 (0)