Skip to content

Commit cc98dc4

Browse files
authored
[JENKINS-63102] Added two new Project Naming Strategies for "Contextual Project Path" and "Simple Project Path" (#181)
Co-authored-by: Robert Ross <robert.ross@nurocor.com>
1 parent 0228310 commit cc98dc4

File tree

7 files changed

+75
-33
lines changed

7 files changed

+75
-33
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ you require the following plugins:
1616
* `io.jenkins.plugins.gitlabserverconfig` - Manages server configuration and web hooks management. Ideally should reside inside another plugin with name `GitLab Plugin`. In future, this package will be moved into a new plugin.
1717

1818
* `io.jenkins.plugins.gitlabbranchsource` - Adds GitLab Branch Source for Multi-branch Pipeline Jobs (including
19-
Merge Requests) and Folder organisation.
19+
Merge Requests) and Folder organization.
2020

2121
## Getting Started
2222

@@ -364,7 +364,7 @@ See handling secrets [section](https://github.com/jenkinsci/configuration-as-cod
364364
GitLab Branch Source Plugin allows you to create 2 type of jobs:
365365
366366
* `Multibranch Pipeline Jobs` - For single project.
367-
* `Folder Organisation` - For multiple projects inside a owner (user/group/subgroup).
367+
* `Folder Organization` - For multiple projects inside a owner (user/group/subgroup).
368368
369369
### Multibranch Pipeline Jobs
370370
@@ -476,7 +476,7 @@ You may want to disable this option because trusted members do not include membe
476476
477477
* `Disable GitLab project avatar` - Disable avatars of GitLab project(s). It is not possible to fetch avatars when API has no token authentication or project is private. So you may use this option as a workaround. We will fix this issue in a later release.
478478
479-
* `Project Naming Strategy` - Choose whether you want `project name` or the `project path (with namespace)` as job names of each project. Users generally prefer the first option but due to legacy reasons we have `project path (with namespace)` as default naming scheme. Note if a job is already created and the naming strategy is changed it will cause projects and build logs to be destroyed.
479+
* `Project Naming Strategy` - Choose whether you want `project name`, the `full project path (with namespace)`, `contextual project path (partial namespace`, or `simple project path (no namespace)` as job names of each project. Due to legacy reasons, we have `project path (with namespace)` as default naming scheme. Note if a job is already created and the naming strategy is changed it will cause projects and build logs to be destroyed.
480480
481481
* `Filter by name (with regex)` - Filter the type of items you want to discover in your project based on the regular expression specified. For example, to discover only `master` branch, `develop` branch and all Merge Requests add `(master|develop|MR-.*)`.
482482
@@ -747,7 +747,7 @@ GITLAB_CHANGES_UPDATED_AT_CURR
747747
748748
To create a Job DSL seed job see this [tutorial](https://github.com/jenkinsci/job-dsl-plugin/wiki/Tutorial---Using-the-Jenkins-Job-DSL).
749749
750-
Here is a sample seed job script for folder organisation job:
750+
Here is a sample seed job script for folder organization job:
751751
752752
```groovy
753753
organizationFolder('GitLab Organization Folder') {

src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMNavigator.java

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import io.jenkins.plugins.gitlabserverconfig.servers.GitLabServer;
2626
import io.jenkins.plugins.gitlabserverconfig.servers.GitLabServers;
2727
import java.io.IOException;
28+
import java.net.URI;
29+
import java.net.URISyntaxException;
2830
import java.util.ArrayList;
2931
import java.util.Collections;
3032
import java.util.HashSet;
@@ -93,7 +95,7 @@ public class GitLabSCMNavigator extends SCMNavigator {
9395
private String credentialsId;
9496

9597
/**
96-
* The behavioural traits to apply.
98+
* The behavioral traits to apply.
9799
*/
98100
private List<SCMTrait<? extends SCMTrait<?>>> traits;
99101

@@ -159,23 +161,23 @@ public String getProjectOwner() {
159161
}
160162

161163
/**
162-
* Gets the behavioural traits that are applied to this navigator and any {@link
164+
* Gets the behavioral traits that are applied to this navigator and any {@link
163165
* GitLabSCMSource} instances it discovers.
164166
*
165-
* @return the behavioural traits.
167+
* @return the behavioral traits.
166168
*/
167169
@NonNull
168170
public List<SCMTrait<? extends SCMTrait<?>>> getTraits() {
169171
return Collections.unmodifiableList(traits);
170172
}
171173

172174
/**
173-
* Sets the behavioural traits that are applied to this navigator and any {@link
175+
* Sets the behavioral traits that are applied to this navigator and any {@link
174176
* GitLabSCMSource} instances it discovers. The new traits will take affect on the next
175177
* navigation through any of the {@link #visitSources(SCMSourceObserver)} overloads or {@link
176178
* #visitSource(String, SCMSourceObserver)}.
177179
*
178-
* @param traits the new behavioural traits.
180+
* @param traits the new behavioral traits.
179181
*/
180182
@DataBoundSetter
181183
public void setTraits(@CheckForNull SCMTrait[] traits) {
@@ -202,12 +204,12 @@ private GitLabOwner getGitlabOwner(GitLabApi gitLabApi) {
202204
}
203205

204206
/**
205-
* Sets the behavioural traits that are applied to this navigator and any {@link
207+
* Sets the behavioral traits that are applied to this navigator and any {@link
206208
* GitLabSCMSource} instances it discovers. The new traits will take affect on the next
207209
* navigation through any of the {@link #visitSources(SCMSourceObserver)} overloads or {@link
208210
* #visitSource(String, SCMSourceObserver)}.
209211
*
210-
* @param traits the new behavioural traits.
212+
* @param traits the new behavioral traits.
211213
*/
212214
@Override
213215
public void setTraits(@CheckForNull List<SCMTrait<? extends SCMTrait<?>>> traits) {
@@ -310,25 +312,37 @@ public void visitSources(@NonNull final SCMSourceObserver observer)
310312
}
311313
}
312314
observer.getListener().getLogger().format("%n%d projects were processed%n", count);
313-
} catch (GitLabApiException e) {
315+
} catch (GitLabApiException | URISyntaxException e) {
314316
LOGGER.log(Level.WARNING, "Exception caught:" + e, e);
315317
throw new IOException("Failed to visit SCM source", e);
316318
}
317319
}
318320

319321
@NonNull
320-
private String getProjectName(int projectNamingStrategy, Project project) {
322+
private String getProjectName(int projectNamingStrategy, Project project) throws URISyntaxException {
323+
String fullPath = project.getPathWithNamespace();
321324
String projectName;
322325
switch (projectNamingStrategy) {
323326
default:
324-
// for legacy reasons default naming strategy is set to full path
327+
// for legacy reasons default naming strategy is set to Full Project path
325328
case 1:
326-
projectName = project.getPathWithNamespace();
329+
projectName = fullPath;
327330
break;
328331
case 2:
332+
// Project name
329333
projectName = project.getNameWithNamespace()
330334
.replace(String.format("%s / ", getGitlabOwner().getFullName()), "");
331335
break;
336+
case 3:
337+
// Contextual project path
338+
URI ownerPathUri = new URI(projectOwner);
339+
URI fullPathUri = new URI(fullPath);
340+
projectName = ownerPathUri.relativize(fullPathUri).toString();
341+
break;
342+
case 4:
343+
// Simple project path
344+
projectName = fullPath.substring(fullPath.lastIndexOf('/') + 1);
345+
break;
332346
}
333347
return projectName;
334348
}

src/main/java/io/jenkins/plugins/gitlabbranchsource/ProjectNamingStrategyTrait.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void setStrategyId(int strategyId) {
3838
protected void decorateContext(SCMNavigatorContext<?, ?> context) {
3939
if (context instanceof GitLabSCMNavigatorContext) {
4040
GitLabSCMNavigatorContext ctx = (GitLabSCMNavigatorContext) context;
41-
ctx.withProjectNamingStrategy(strategyId);
41+
ctx.withProjectNamingStrategy(getStrategyId());
4242
}
4343
}
4444

@@ -75,6 +75,8 @@ public ListBoxModel doFillStrategyIdItems() {
7575
ListBoxModel result = new ListBoxModel();
7676
result.add(Messages.ProjectNamingStrategyTrait_fullProjectPath(), "1");
7777
result.add(Messages.ProjectNamingStrategyTrait_projectName(), "2");
78+
result.add(Messages.ProjectNamingStrategyTrait_contextualProjectPath(), "3");
79+
result.add(Messages.ProjectNamingStrategyTrait_simpleProjectPath(), "4");
7880
return result;
7981
}
8082
}

src/main/java/io/jenkins/plugins/gitlabbranchsource/helpers/GitLabPipelineStatusNotifier.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -266,26 +266,23 @@ private static void sendNotifications(Run<?, ?> build, TaskListener listener) {
266266
status.setTargetUrl(url);
267267

268268
if (Result.SUCCESS.equals(result)) {
269-
status.setDescription(build.getParent().getFullName() + ": This commit looks good");
269+
status.setDescription(build.toString() + ": This commit looks good.");
270270
status.setStatus("SUCCESS");
271271
state = Constants.CommitBuildState.SUCCESS;
272272
} else if (Result.UNSTABLE.equals(result)) {
273-
status.setDescription(
274-
build.getParent().getFullName() + ": This commit has test failures");
273+
status.setDescription(build.toString() + ": This commit is unstable with partial failure.");
275274
status.setStatus("FAILED");
276275
state = Constants.CommitBuildState.FAILED;
277276
} else if (Result.FAILURE.equals(result)) {
278-
status.setDescription(
279-
build.getParent().getFullName() + ": There was a failure building this commit");
277+
status.setDescription(build.toString() + ": There was a failure building this commit.");
280278
status.setStatus("FAILED");
281279
state = Constants.CommitBuildState.FAILED;
282280
} else if (result != null) { // ABORTED, NOT_BUILT.
283-
status.setDescription(build.getParent().getFullName()
284-
+ ": Something is wrong with the build of this commit");
281+
status.setDescription(build.toString() + ": Something is wrong with the build of this commit.");
285282
status.setStatus("CANCELED");
286283
state = Constants.CommitBuildState.CANCELED;
287284
} else {
288-
status.setDescription(build.getParent().getFullName() + ": Build started...");
285+
status.setDescription(build.toString() + ": Build started...");
289286
status.setStatus("RUNNING");
290287
state = Constants.CommitBuildState.RUNNING;
291288
}

src/main/resources/index.jelly

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?jelly escape-by-default='true'?>
22
<div>
3-
Provides branch source and folder organisation functionality for GitLab Repositories in Jenkins
3+
Provides branch source and folder organization functionality for GitLab Repositories in Jenkins
44
</div>

src/main/resources/io/jenkins/plugins/gitlabbranchsource/Messages.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ ForkMergeRequestDiscoveryTrait.permissionsDisplayName=Trusted Members
1818
GitLabTagSCMHead.Pronoun=Tag
1919
ProjectNamingStrategyTrait.displayName=Project Naming Strategy
2020
ProjectNamingStrategyTrait.fullProjectPath=Full Project Path
21+
ProjectNamingStrategyTrait.contextualProjectPath=Contextual Project Path
22+
ProjectNamingStrategyTrait.simpleProjectPath=Simple Project Path
2123
ProjectNamingStrategyTrait.projectName=Project Name
2224
SubGroupProjectDiscoveryTrait.displayName=Discover subgroup projects
2325
TagDiscoveryTrait.authorityDisplayName=Trust origin tags

src/main/resources/io/jenkins/plugins/gitlabbranchsource/ProjectNamingStrategyTrait/help-strategyId.html

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,44 @@
33
<dl>
44
<dt>Full Project Path</dt>
55
<dd>
6-
This option will use the full project path for the group and project. Example:
7-
my-group/my-project
8-
my-group/other-project
9-
my-group/my-subgroup/third-project
6+
This option will use the full project path for the group and project.
7+
Examples:
8+
<ul>
9+
<li><code><mark>my-group/</mark>my-project</code></li>
10+
<li><code><mark>my-group/</mark>other-project</code></li>
11+
<li><code><mark>my-group/my-subgroup/</mark>third-project</code></li>
12+
</ul>
1013
</dd>
1114
<dt>Project Name</dt>
1215
<dd>
13-
This option will use project names from GitLab project name. Example:
14-
My Project
15-
Other project
16-
My Subgroup / Third project
16+
This option will use project <b>names</b> from GitLab project name.
17+
This is the "friendly" name of the project and could contain capitalization, spaces or other special characters.
18+
Examples:
19+
<ul>
20+
<li>My Project</li>
21+
<li>Other project</li>
22+
<li>My Subgroup / Third project</li>
23+
</ul>
24+
</dd>
25+
<dt>Contextual Project Path</dt>
26+
<dd>
27+
This option will use the project path with nested groups, but not the top-level <code>owner</code> segment specified on the Jenkins pipeline.
28+
Examples where <code>owner</code> on pipeline has been set to "my-group":
29+
<ul>
30+
<li><code>my-project</code></li>
31+
<li><code>other-project</code></li>
32+
<li><code><mark>my-subgroup/</mark>third-project</code></li>
33+
</ul>
34+
</dd>
35+
<dt>Simple Project Path</dt>
36+
<dd>
37+
This option will use the project path with <i>no</i> parent group information added.
38+
Examples:
39+
<ul>
40+
<li><code>my-project</code></li>
41+
<li><code>other-project</code></li>
42+
<li><code>third-project</code></li>
43+
</ul>
1744
</dd>
1845
</dl>
1946
</div>

0 commit comments

Comments
 (0)