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-42849] add BranchProperty to assign ownership to branch jobs #63

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

jordancoll
Copy link

No description provided.

Copy link
Member

@oleg-nenashev oleg-nenashev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First wave of comments. Ideally it needs a bunch of tests proving the change won't cause regressions in multi-branch projects by default


@DataBoundConstructor
public BranchNameOwnershipStrategy(@RegEx String pattern, @Nonnull String ownerExpression) {
this.pattern = Pattern.compile(pattern);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This constructor is a part of public API, but it will throw runtime exception in the case of wrong regexp. It needs to be documented

@Override
@Nonnull
public String getDisplayName() {
return "By branch name";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally it should be a localizable string coming from resources

return "By branch name";
}

public FormValidation doCheckPattern(@QueryParameter String value) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Such API methods should be @Restricted(NoExternalUse.class)

return FormValidation.ok();
}

public FormValidation doCheckOwnerExpression(@QueryParameter String value) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

* @param branch The branch
* @return The prospective owner's user ID or full name. {@code null} if the owner cannot be determined.
*/
@Nullable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be @CheckForNull, Nullable does not enforce static checks

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class FromScmOwnershipStrategy extends BranchOwnershipStrategy {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FromScmBranchOwnershipStrategy, just to avoid possible confusion

@Override
@Nonnull
public String getDisplayName() {
return "From scm";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs to be localizable

import com.synopsys.arc.jenkins.plugins.ownership.OwnershipDescription;
import com.synopsys.arc.jenkins.plugins.ownership.jobs.JobOwnerHelper;
import hudson.Extension;
import hudson.model.*;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid bulk imports in the main code part.

Branch branch = multiBranchProject.getProjectFactory().getBranch(project);

String prospectiveOwner = strategy.determineOwner(branch);
String owner = prospectiveOwner != null ? prospectiveOwner : fallbackOwner;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you do not try job/folder ownership?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inherit the owner from multibranch job?
maybe we could have the fallback owner an optional setting. if unset, we fallback to inheritance of ownership according to global config.

@jordancoll
Copy link
Author

Ideally it needs a bunch of tests proving the change won't cause regressions in multi-branch projects by default

The BranchProperty only affects the branches it is applied to, so I don't think there is much risk of changing existing behavior. That said, I do want to add tests.

Is there a way to reuse branch-api's test harness here? Or do I need to make my own?

@oleg-nenashev
Copy link
Member

Sorry, missed the update

Copy link
Member

@oleg-nenashev oleg-nenashev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks better, some minor comments.

I think there should be the default SameAsParentBranchOwnershipStrategy which just falls back to the original logic.

Do you also plan to create tests for the new functionality?

@DataBoundConstructor
public BranchNameOwnershipStrategy(@RegEx String pattern, @Nonnull String ownerExpression) throws PatternSyntaxException {
this.pattern = Pattern.compile(pattern);
this.ownerExpression = ownerExpression;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix empty and trim here as well? Then you can make this field @CheckForNull

<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>branch-api</artifactId>
<version>2.0.8</version>
<type>jar</type>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to remove explicit jar requirement. It may become hpi at some point

@Override
@Nullable
public String determineOwner(Branch branch) {
Matcher matcher = pattern.matcher(branch.getName());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No PatternSyntaxException handling. It would be better to catch it here just in case (e.g. manual config edits on the disk)

*/
public class BranchNameOwnershipStrategy extends BranchOwnershipStrategy {

private Pattern pattern;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure it will be persisted correctly and then recovered? I have some doubts about it, config roundtrip tests would be useful

@DataBoundConstructor
public OwnershipBranchProperty(@Nonnull String fallbackOwner, @Nonnull BranchOwnershipStrategy strategy) {
this.strategy = strategy;
this.fallbackOwner = fallbackOwner;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixEmptyAndTrim()? Null would be better

try {
JobOwnerHelper.setOwnership(project, ownershipDescription);
} catch (IOException ioe) {
// TODO: handle somehow
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handle what?

@ssbarnea
Copy link

@jordancoll please fix it or close the PR.

@jordancoll jordancoll closed this Jun 18, 2018
@oleg-nenashev
Copy link
Member

I would prefer to keep it open so that anybody could pick it up and finalize.
@ssbarnea I kindly ask to avoid making such requests in repositories you do not maintain. Asking for updates is fine, asking to "fix or close" is not fine

@oleg-nenashev oleg-nenashev reopened this Jun 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants