From 02fa46fd19916ec5e6a8a7d247765787271d98e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Picciochi=20Oliveira?= Date: Mon, 28 Jul 2025 15:11:18 +0100 Subject: [PATCH] fix: Always save a timestamp of GitLab project configurations into the config, signalling this plugin has done some work --- .../gitlabbranchsource/GitLabSCMSource.java | 60 ++++++++++++++++--- 1 file changed, 53 insertions(+), 7 deletions(-) diff --git a/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSource.java b/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSource.java index 6fac633f..e7139917 100644 --- a/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSource.java +++ b/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSource.java @@ -117,6 +117,7 @@ public class GitLabSCMSource extends AbstractGitSCMSource { private String httpRemote; private transient Project gitlabProject; private Long projectId; + private Long lastRetrieveTimestamp; /** * The cache of {@link ObjectMetadataAction} instances for each open MR. @@ -166,6 +167,14 @@ public void setSshRemote(String sshRemote) { this.sshRemote = sshRemote; } + public Long getLastRetrieveTimestamp() { + return lastRetrieveTimestamp; + } + + public void setLastRetrieveTimestamp(Long lastRetrieveTimestamp) { + this.lastRetrieveTimestamp = lastRetrieveTimestamp; + } + public String getProjectName() { return projectName; } @@ -271,11 +280,45 @@ public void setTraits(List traits) { this.traits = new ArrayList<>(Util.fixNull(traits)); } + /** + * Saves the current state of this object into a configuration file. + * + * @throws IOException When saving into the file fails. + */ + private void saveOwner() throws IOException { + SCMSourceOwner owner = this.getOwner(); + if (owner != null) { + owner.save(); + } + } + + /** + * When the jobDSL plugin is called, one of the retrieve() methods ends up being called. + * When those methods fail to communicate with GitLab, the config.xml file corresponding to + * the pipeline being analysed is not saved. + * Later, the jobDSL only calls those methods to update the configurations when there are + * changes to the configuration. + *

+ * Because in the above case, the config didn't change, these retrieve() methods are never + * called again. + * We force the saving of a timestamp to make sure _something_ is written in the config at + * all times (and the jobDSL plugin doesn't see it as being unnecessary to resync) + * and future reattempts are allowed. + * + * @throws IOException When saving into the file fails. + */ + private void saveTimestampInOwner() throws IOException { + setLastRetrieveTimestamp(System.currentTimeMillis()); + saveOwner(); + } + @Override - protected SCMRevision retrieve(@NonNull SCMHead head, @NonNull TaskListener listener) - throws IOException, InterruptedException { + protected SCMRevision retrieve(@NonNull SCMHead head, @NonNull TaskListener listener) throws IOException { + saveTimestampInOwner(); + + GitLabApi gitLabApi = apiBuilder(this.getOwner(), serverName); + try { - GitLabApi gitLabApi = apiBuilder(this.getOwner(), serverName); getGitlabProject(gitLabApi); if (head instanceof BranchSCMHead) { listener.getLogger().format("Querying the current revision of branch %s...%n", head.getName()); @@ -336,6 +379,8 @@ protected void retrieve( SCMHeadEvent event, @NonNull TaskListener listener) throws IOException, InterruptedException { + saveTimestampInOwner(); + GitLabApi gitLabApi = apiBuilder(this.getOwner(), serverName); try { getGitlabProject(gitLabApi); @@ -595,10 +640,7 @@ public SCMSourceCriteria.Probe create( LOGGER.log(Level.WARNING, "Exception caught:" + e, e); throw new IOException("Failed to fetch latest heads", e); } finally { - SCMSourceOwner owner = this.getOwner(); - if (owner != null) { - owner.save(); - } + saveOwner(); } } @@ -624,6 +666,8 @@ protected Set retrieveRevisions(@NonNull TaskListener listener) throws I @NonNull @Override protected List retrieveActions(SCMSourceEvent event, @NonNull TaskListener listener) throws IOException { + saveTimestampInOwner(); + List result = new ArrayList<>(); try { getGitlabProject(); @@ -647,6 +691,8 @@ protected List retrieveActions(SCMSourceEvent event, @NonNull TaskListen @Override protected List retrieveActions(@NonNull SCMHead head, SCMHeadEvent event, @NonNull TaskListener listener) throws IOException { + saveTimestampInOwner(); + try { getGitlabProject(); } catch (GitLabApiException e) {