Skip to content

Commit e0f31f2

Browse files
authored
Add mr contributor and metadata (#37)
* add mr contributor and metadata * remove populating all owned projects for empty projectowner * hashmaps -> concurrenthashmaps
1 parent c8bb86f commit e0f31f2

File tree

1 file changed

+37
-9
lines changed

1 file changed

+37
-9
lines changed

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

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.util.Map;
3636
import java.util.Objects;
3737
import java.util.Set;
38+
import java.util.concurrent.ConcurrentHashMap;
3839
import java.util.stream.Collectors;
3940
import jenkins.model.Jenkins;
4041
import jenkins.plugins.git.AbstractGitSCMSource;
@@ -54,6 +55,7 @@
5455
import jenkins.scm.api.SCMSourceDescriptor;
5556
import jenkins.scm.api.SCMSourceEvent;
5657
import jenkins.scm.api.SCMSourceOwner;
58+
import jenkins.scm.api.metadata.ContributorMetadataAction;
5759
import jenkins.scm.api.metadata.ObjectMetadataAction;
5860
import jenkins.scm.api.metadata.PrimaryInstanceMetadataAction;
5961
import jenkins.scm.api.mixin.ChangeRequestCheckoutStrategy;
@@ -111,6 +113,17 @@ public class GitLabSCMSource extends AbstractGitSCMSource {
111113
private transient Project gitlabProject;
112114
private int projectId = -1;
113115

116+
/**
117+
* The cache of {@link ObjectMetadataAction} instances for each open MR.
118+
*/
119+
@NonNull
120+
private transient /*effectively final*/ Map<Integer,ObjectMetadataAction> mergeRequestMetadataCache = new ConcurrentHashMap<>();
121+
/**
122+
* The cache of {@link ObjectMetadataAction} instances for each open MR.
123+
*/
124+
@NonNull
125+
private transient /*effectively final*/ Map<Integer,ContributorMetadataAction> mergeRequestContributorCache = new ConcurrentHashMap<>();
126+
114127
@DataBoundConstructor
115128
public GitLabSCMSource(String serverName, String projectOwner, String projectPath) {
116129
this.serverName = serverName;
@@ -354,6 +367,18 @@ public SCMSourceCriteria.Probe create(@NonNull BranchSCMHead head,
354367
final MergeRequest m =
355368
gitLabApi.getMergeRequestApi()
356369
.getMergeRequest(gitlabProject, mr.getIid());
370+
mergeRequestContributorCache.put(mr.getIid(),
371+
new ContributorMetadataAction(
372+
mr.getAuthor().getUsername(),
373+
mr.getAuthor().getName(),
374+
mr.getAuthor().getEmail()
375+
));
376+
mergeRequestMetadataCache.put(mr.getIid(),
377+
new ObjectMetadataAction(
378+
mr.getTitle(),
379+
mr.getDescription(),
380+
mr.getWebUrl()
381+
));
357382
count++;
358383
listener.getLogger().format("%nChecking merge request %s%n",
359384
HyperlinkNote.encodeTo(
@@ -560,15 +585,21 @@ protected List<Action> retrieveActions(@NonNull SCMHead head, SCMHeadEvent event
560585
result.add(new PrimaryInstanceMetadataAction());
561586
}
562587
} else if (head instanceof MergeRequestSCMHead) {
588+
int iid = Integer.parseInt(((MergeRequestSCMHead) head).getId());
563589
String mergeUrl = mergeRequestUriTemplate(serverName)
564590
.set("project", splitPath(projectPath))
565-
.set("iid", ((MergeRequestSCMHead) head).getId())
591+
.set("iid", iid)
566592
.expand();
567-
result.add(new ObjectMetadataAction(
568-
null,
569-
null,
570-
mergeUrl
571-
));
593+
ObjectMetadataAction metadataAction = mergeRequestMetadataCache.get(iid);
594+
if (metadataAction == null) {
595+
// best effort
596+
metadataAction = new ObjectMetadataAction(null, null, mergeUrl);
597+
}
598+
result.add(metadataAction);
599+
ContributorMetadataAction contributor = mergeRequestContributorCache.get(iid);
600+
if (contributor != null) {
601+
result.add(contributor);
602+
}
572603
result.add(GitLabLink.toMergeRequest(mergeUrl));
573604
} else if (head instanceof GitLabTagSCMHead) {
574605
String tagUrl = tagUriTemplate(serverName)
@@ -806,9 +837,6 @@ public ListBoxModel doFillProjectPathItems(@AncestorInPath SCMSourceOwner contex
806837
}
807838

808839
if (projectOwner.equals("")) {
809-
// for(Project p : gitLabApi.getProjectApi().getOwnedProjects()) {
810-
// result.add(p.getPathWithNamespace());
811-
// }
812840
return new StandardListBoxModel().includeEmptyValue();
813841
}
814842
try {

0 commit comments

Comments
 (0)