Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-68030] Optionally report most recent commit as changelog of a new build #1565

Merged
merged 37 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
3e62161
Add files via upload
rhit-inskeeda Mar 20, 2024
838cc41
code for gui
rhit-inskeeda Mar 20, 2024
d76a507
trait file
rhit-inskeeda Mar 20, 2024
5f681b3
gitscm changes
rhit-inskeeda Mar 20, 2024
756f77c
testing for new trait behavior
rhit-inskeeda Mar 20, 2024
b5ba0e8
Merge pull request #1 from rhit-inskeeda/new-trait
rhit-inskeeda Mar 20, 2024
d6f6cf9
null check time
rhit-gawronja Mar 20, 2024
27d5942
update to proper version number
rhit-inskeeda Mar 22, 2024
ce2fd99
Update FirstBuildChangelogTrait
rhit-inskeeda Mar 22, 2024
d17fafd
Merge branch 'master' into integration
MarkEWaite Apr 2, 2024
370d763
Format new files with spotless:apply
MarkEWaite Apr 2, 2024
c66d139
Remove trailing space characters
MarkEWaite Apr 2, 2024
88640c5
Changed equals to use proper equals method
rhit-gawronja Apr 2, 2024
62aad20
Changed equals to use proper equals method
rhit-gawronja Apr 2, 2024
cab31cc
Changed equals to use proper equals method
rhit-gawronja Apr 2, 2024
baa7384
Merge remote-tracking branch 'origin/integration' into integration
rhit-gawronja Apr 2, 2024
84846d7
changed to compatible equal
rhit-gawronja Apr 2, 2024
6f4fa4d
Fix Mark's poor code review
MarkEWaite Apr 2, 2024
1664429
Use leading lower case letter for the symbol
MarkEWaite Apr 2, 2024
47ab82b
Use sentence case for display name
MarkEWaite Apr 2, 2024
a9eacdf
Set API release to 5.3.0
MarkEWaite Apr 2, 2024
24c444b
Merge branch 'master' into integration
MarkEWaite Apr 8, 2024
143b0d2
removed groovy and checkbox
rhit-inskeeda Apr 16, 2024
ae92de6
Merge pull request #2 from rhit-inskeeda/no-groovy
rhit-inskeeda Apr 16, 2024
5dfe120
Update to documentation and applying spotless
rhit-gawronja Apr 17, 2024
a803d34
Pushing documentation image
rhit-gawronja Apr 17, 2024
45e7fff
Undo spotless changes - not ready for spotless
MarkEWaite May 1, 2024
4499e95
Reduce doc diffs
MarkEWaite May 1, 2024
591d0da
Merge branch 'master' into integration
MarkEWaite May 1, 2024
8df79db
Use Unix file format for help
MarkEWaite May 1, 2024
667a4fa
Jenkins Pipeline symbols start with a lower case letter
MarkEWaite May 1, 2024
11955ee
Remove blank line from doc example
MarkEWaite May 1, 2024
34be383
First build changelog reports most recent commit as changelog
MarkEWaite May 1, 2024
13816f4
Format new example same as prior examples.
MarkEWaite May 4, 2024
81606a6
Clarify first build option help and docs
MarkEWaite May 4, 2024
b449318
Merge branch 'master' into integration
MarkEWaite Jul 29, 2024
61bb758
Remove comment as suggested by reviewer
MarkEWaite Jul 29, 2024
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
17 changes: 17 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,23 @@ Inverse::
This is useful, for example, when you have jobs building your master and various release branches and you want a second job which builds all new feature branches.
For example, branches which do not match these patterns without redundantly building master and the release branches again each time they change.

[#first-build-changelog]
==== First build changelog

image:/images/git-extension-for-first-build.png[First build changelog]

The Jenkins git plugin provides an option to trigger a Pipeline build on the first commit on a branch.
By default, no changelog is generated for the first build because the first build has no predecessor build for comparison.
When the first build changelog option is enabled, the most recent commit will be used as the changelog of the first build.

[source,groovy]
----
checkout scmGit(
branches: [[name: 'master']],
extensions: [ firstBuildChangelog() ],
userRemoteConfigs: [[url: 'https://github.com/jenkinsci/git-plugin.git']])
----

[#merge-extensions]
=== Merge Extensions

Expand Down
Binary file added images/git-extension-for-first-build.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 9 additions & 3 deletions src/main/java/hudson/plugins/git/GitSCM.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import hudson.plugins.git.extensions.impl.BuildSingleRevisionOnly;
import hudson.plugins.git.extensions.impl.ChangelogToBranch;
import hudson.plugins.git.extensions.impl.CloneOption;
import hudson.plugins.git.extensions.impl.FirstBuildChangelog;
import hudson.plugins.git.extensions.impl.PathRestriction;
import hudson.plugins.git.extensions.impl.LocalBranch;
import hudson.plugins.git.extensions.impl.RelativeTargetDirectory;
Expand Down Expand Up @@ -1493,9 +1494,14 @@
}

if (!exclusion) {
// this is the first time we are building this branch, so there's no base line to compare against.
// if we force the changelog, it'll contain all the changes in the repo, which is not what we want.
listener.getLogger().println("First time build. Skipping changelog.");
FirstBuildChangelog firstBuildChangelog = getExtensions().get(FirstBuildChangelog.class);
if (firstBuildChangelog != null && firstBuildChangelog.isMakeChangelog()) {

Check warning on line 1498 in src/main/java/hudson/plugins/git/GitSCM.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 1498 is only partially covered, one branch is missing
changelog.to(out).max(1).execute();
executed = true;
listener.getLogger().println("First time build. Latest changes added to changelog.");
} else {
listener.getLogger().println("First time build. Skipping changelog.");
}
} else {
changelog.to(out).max(MAX_CHANGELOG).execute();
executed = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package hudson.plugins.git.extensions.impl;

import hudson.Extension;
import hudson.plugins.git.extensions.GitSCMExtension;
import hudson.plugins.git.extensions.GitSCMExtensionDescriptor;
import java.util.Objects;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

/**
* First Build generates a changelog.
*
* @author Derek Inskeep
*/
public class FirstBuildChangelog extends GitSCMExtension {
private boolean makeChangelog;

@DataBoundConstructor
public FirstBuildChangelog() {
makeChangelog = true;
}

public boolean isMakeChangelog() {
return makeChangelog;
}

@DataBoundSetter
public void setMakeChangelog(boolean makeChangelog) {
this.makeChangelog = makeChangelog;
}

/**
* {@inheritDoc}
*/
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
FirstBuildChangelog that = (FirstBuildChangelog) o;
return makeChangelog == that.makeChangelog;
MarkEWaite marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* {@inheritDoc}
*/
@Override
public int hashCode() {
return Objects.hash(makeChangelog);
}

/**
* {@inheritDoc}
*/
@Override
public String toString() {
return "FirstBuildChangelog{" + "makeChangelog=" + makeChangelog + '}';

Check warning on line 61 in src/main/java/hudson/plugins/git/extensions/impl/FirstBuildChangelog.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 30-61 are not covered by tests
}

@Extension
@Symbol("firstBuildChangelog")
public static class DescriptorImpl extends GitSCMExtensionDescriptor {

/**
* {@inheritDoc}
*/
@Override
public String getDisplayName() {
return "First build changelog";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package jenkins.plugins.git.traits;

import edu.umd.cs.findbugs.annotations.CheckForNull;
import hudson.Extension;
import hudson.plugins.git.extensions.impl.FirstBuildChangelog;
import jenkins.scm.api.trait.SCMSourceTrait;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;

/**
* Exposes {@link FirstBuildChangelog} as a {@link SCMSourceTrait}.
*
* @since 5.3.0
*/
public class FirstBuildChangelogTrait extends GitSCMExtensionTrait<FirstBuildChangelog> {

/**
* @deprecated Use constructor that accepts extension instead.
*/
@Deprecated
public FirstBuildChangelogTrait() {
this(null);
}

/**
* Stapler constructor.
*
* @param extension the option to force first build to have a non-empty changelog.
*/
@DataBoundConstructor
public FirstBuildChangelogTrait(@CheckForNull FirstBuildChangelog extension) {
super(extension == null ? new FirstBuildChangelog() : extension);
}

Check warning on line 33 in src/main/java/jenkins/plugins/git/traits/FirstBuildChangelogTrait.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 22-33 are not covered by tests

/**
* Our {@link hudson.model.Descriptor}
*/
@Extension
@Symbol("firstBuildChangelog")
public static class DescriptorImpl extends GitSCMExtensionTraitDescriptor {
/**
* {@inheritDoc}
*/
@Override
public String getDisplayName() {
return "First Build Changelog";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div>
First builds will populate the changelog with the latest commit, if any, to allow Pipelines to check and test for file changes.
By default, no changelog is generated for the first build because the first build has no predecessor build for comparison.
When the first build changelog option is enabled, the most recent commit on the branch will be used as the changelog of the first build.
</div>
19 changes: 19 additions & 0 deletions src/test/java/hudson/plugins/git/GitSCMTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,25 @@ public void testCleanBeforeCheckout() throws Exception {
assertThat("Cleaning should happen before fetch", cleaningLogLine, is(lessThan(fetchingLogLine)));
}

@Test
public void testFirstBuiltChangelog() throws Exception {
assumeTrue("Test class max time " + MAX_SECONDS_FOR_THESE_TESTS + " exceeded", isTimeAvailable());
FreeStyleProject p = setupProject("master", false, null, null, "Jane Doe", null);
FirstBuildChangelog fbc = new FirstBuildChangelog();
((GitSCM) p.getScm()).getExtensions().add(fbc);

/* First build should should generate a changelog */
final String commitFile1 = "commitFile1";
commit(commitFile1, johnDoe, janeDoe, "Commit number 1");
final FreeStyleBuild firstBuild = build(p, Result.SUCCESS, commitFile1);
assertThat(firstBuild.getLog(50), hasItem("First time build. Latest changes added to changelog."));
/* Second build should have normal behavior */
final String commitFile2 = "commitFile2";
commit(commitFile2, johnDoe, janeDoe, "Commit number 2");
final FreeStyleBuild secondBuild = build(p, Result.SUCCESS, commitFile2);
assertThat(secondBuild.getLog(50), not(hasItem("First time build. Latest changes added to changelog.")));
}

@Issue("JENKINS-8342")
@Test
public void testExcludedRegionMultiCommit() throws Exception {
Expand Down