Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-${jenkins.baseline}.x</artifactId>
<version>5857.vb_f3dd0731f44</version>
<version>5933.vcf06f7b_5d1a_2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down Expand Up @@ -227,5 +227,4 @@
<url>https://repo.jenkins-ci.org/public/</url>
</pluginRepository>
</pluginRepositories>

</project>
37 changes: 32 additions & 5 deletions src/main/java/jenkins/plugins/git/AbstractGitSCMSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevTag;
import org.eclipse.jgit.revwalk.RevTree;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.transport.RefSpec;
Expand Down Expand Up @@ -425,6 +426,34 @@
}
}

private long getTagTimestamp(RevWalk walk, ObjectId objectId) throws IOException {
try {
// Annotated tag object
RevTag tag = walk.parseTag(objectId);

if (tag.getTaggerIdent() != null
&& tag.getTaggerIdent().getWhen() != null) {
return tag.getTaggerIdent().getWhen().getTime();
}

// No tagger ident (or weird tag) — walk the tag chain to a commit, if any
Object target = tag.getObject();
for (int i = 0; i < 32 && target instanceof RevTag; i++) {
target = ((RevTag) target).getObject();
}

if (target instanceof RevCommit) {
return TimeUnit.SECONDS.toMillis(((RevCommit) target).getCommitTime());
}

throw new IOException("Tag does not ultimately reference a commit: " + objectId.name());

Check warning on line 449 in src/main/java/jenkins/plugins/git/AbstractGitSCMSource.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 432-449 are not covered by tests
} catch (org.eclipse.jgit.errors.IncorrectObjectTypeException e) {
// Lightweight tag (or direct commit id)
RevCommit commit = walk.parseCommit(objectId);
return TimeUnit.SECONDS.toMillis(commit.getCommitTime());
}
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -786,8 +815,7 @@
}
count++;
final String tagName = StringUtils.removeStart(ref.getKey(), Constants.R_TAGS);
RevCommit commit = walk.parseCommit(ref.getValue());
final long lastModified = TimeUnit.SECONDS.toMillis(commit.getCommitTime());
final long lastModified = getTagTimestamp(walk, ref.getValue());
if (request.process(new GitTagSCMHead(tagName, lastModified),
new SCMSourceRequest.IntermediateLambda<ObjectId>() {
@Nullable
Expand All @@ -804,7 +832,7 @@
@Nullable ObjectId revisionInfo)
throws IOException, InterruptedException {
RevCommit commit = walk.parseCommit(revisionInfo);
final long lastModified = TimeUnit.SECONDS.toMillis(commit.getCommitTime());
final long lastModified = getTagTimestamp(walk, revisionInfo);
final RevTree tree = commit.getTree();
return new TreeWalkingSCMProbe(tagName, lastModified, repository, tree);
}
Expand Down Expand Up @@ -1012,8 +1040,7 @@
final Repository repository = client.getRepository();
RevWalk walk = new RevWalk(repository)) {
ObjectId ref = client.revParse(tagRef);
RevCommit commit = walk.parseCommit(ref);
long lastModified = TimeUnit.SECONDS.toMillis(commit.getCommitTime());
long lastModified = getTagTimestamp(walk, ref);
listener.getLogger().printf("Resolved tag %s revision %s%n", revision,
ref.getName());
return new GitTagSCMRevision(new GitTagSCMHead(revision, lastModified),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -169,6 +170,25 @@ void indexingHasBranchAndTagDiscoveryTraitIgnoreTagDiscoveryTrait() throws Excep
assertTrue(tagsFetched);
}

@Test
public void tagTimestampsAreValid() throws Exception {
source.setTraits(Collections.singletonList(new TagDiscoveryTrait()));
Set<SCMHead> heads = source.fetch(LISTENER);

Set<GitTagSCMHead> tags = heads.stream()
.filter(h -> h instanceof GitTagSCMHead)
.map(h -> (GitTagSCMHead) h)
.collect(Collectors.toSet());

assertThat("Should discover both tags", tags.size(), is(2));

long year2000 = 946684800000L; // Jan 1, 2000
for (GitTagSCMHead tag : tags) {
assertThat("Tag " + tag.getName() + " should have valid timestamp",
tag.getTimestamp(), greaterThan(year2000));
}
}

static boolean tagsFetched;

public static class MockGitClientForTags extends TestJGitAPIImpl {
Expand Down
Loading