Skip to content
Merged
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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,15 @@ pullRequestTasks:
# Tasks specific to the improvement issue type:
improvement:
- improvement task1
branches:
# Make the bot add a label with the name of base ref when a pull requests target a non-main branch.
enabled: true
# Prefix is only added when a prefix format is passed. Must contain a single %s that will be replaced with the base ref
titlePrefix: "[%s]"
# Label is only added when a label format is passed. Must contain a single %s that will be replaced with the base ref
label: "branch-%s"
ignore:
# as other config sections, supports ignore rules
```

### Altering the infrastructure
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package org.hibernate.infra.bot;

import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.Random;

import org.hibernate.infra.bot.config.DeploymentConfig;
import org.hibernate.infra.bot.config.RepositoryConfig;

import org.jboss.logging.Logger;

import io.quarkiverse.githubapp.ConfigFile;
import io.quarkiverse.githubapp.event.PullRequest;
import jakarta.inject.Inject;
import org.kohsuke.github.GHEventPayload;
import org.kohsuke.github.GHFileNotFoundException;
import org.kohsuke.github.GHIssueState;
import org.kohsuke.github.GHLabel;
import org.kohsuke.github.GHPullRequest;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GHUser;

public class EditPullRequestAddBranchTagLabel {
private static final Logger LOG = Logger.getLogger( EditPullRequestAddBranchTagLabel.class );

@Inject
DeploymentConfig deploymentConfig;

void pullRequestChanged(
@PullRequest.Opened @PullRequest.Reopened @PullRequest.Edited @PullRequest.Synchronize
GHEventPayload.PullRequest payload,
@ConfigFile("hibernate-github-bot.yml") RepositoryConfig repositoryConfig
) throws IOException {
addTagOrLabel( payload.getRepository(), repositoryConfig, payload.getPullRequest() );
}

private void addTagOrLabel(
GHRepository repository,
RepositoryConfig repositoryConfig,
GHPullRequest pullRequest
) throws IOException {
if ( repositoryConfig == null || repositoryConfig.branches == null
|| !repositoryConfig.branches.getEnabled().orElse( Boolean.FALSE ) ) {
return;
}

if ( !shouldCheck( repository, pullRequest, repositoryConfig.branches.getIgnore() ) ) {
return;
}

String base = pullRequest.getBase().getRef();
if ( "main".equalsIgnoreCase( base ) ) {
return;
}

if ( repositoryConfig.branches.getTitlePrefix().isPresent() ) {
String prefix = String.format( Locale.ROOT, repositoryConfig.branches.getTitlePrefix().get(), base );
String title = pullRequest.getTitle();
if ( !title.startsWith( prefix ) ) {
if ( !deploymentConfig.isDryRun() ) {
pullRequest.setTitle( prefix + " " + title );
}
else {
LOG.info( "Pull request #" + pullRequest.getNumber() + " - Setting title to: " + title );
}
}
}

if ( repositoryConfig.branches.getLabel().isPresent() ) {
String labelName = String.format( Locale.ROOT, repositoryConfig.branches.getLabel().get(), base );
for ( GHLabel ghLabel : pullRequest.getLabels() ) {
if ( ghLabel.getName().equals( labelName ) ) {
return;
}
}

GHLabel label;
try {
label = repository.getLabel( base );
}
catch (GHFileNotFoundException e) {
label = repository.createLabel(
base,
String.format( Locale.ROOT, "%06x", new Random().nextInt( 0xffffff + 1 ) ),
"Label for pull requests targeting [" + base + "] branch."
);
}

if ( !deploymentConfig.isDryRun() ) {
pullRequest.addLabels( label );
}
else {
LOG.info( "Pull request #" + pullRequest.getNumber() + " - Adding label: " + label.getName() );
}
}
}

private boolean shouldCheck(GHRepository repository, GHPullRequest pullRequest, List<RepositoryConfig.IgnoreConfiguration> ignoredPRConfigurations) throws IOException {
GHUser author = pullRequest.getUser();
String title = pullRequest.getTitle();
for ( RepositoryConfig.IgnoreConfiguration ignore : ignoredPRConfigurations ) {
if ( ignore.getUser().equals( author.getLogin() )
&& ignore.getTitlePattern().matcher( title ).matches() ) {
return false;
}
}
return !GHIssueState.CLOSED.equals( pullRequest.getState() )
&& repository.getId() == pullRequest.getBase().getRepository().getId();
}
}
41 changes: 41 additions & 0 deletions src/main/java/org/hibernate/infra/bot/config/RepositoryConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public class RepositoryConfig {

public TaskList pullRequestTasks;

public BranchLabel branches;

public static class JiraConfig {
private Optional<Pattern> issueKeyPattern = Optional.empty();

Expand Down Expand Up @@ -229,4 +231,43 @@ public void setIgnore(List<IgnoreConfiguration> ignore) {
this.ignore = ignore;
}
}

public static class BranchLabel {
private Optional<Boolean> enabled = Optional.empty();
private List<IgnoreConfiguration> ignore = Collections.emptyList();
private Optional<String> label = Optional.empty();
private Optional<String> titlePrefix = Optional.empty();

public Optional<Boolean> getEnabled() {
return enabled;
}

public void setEnabled(Boolean enabled) {
this.enabled = Optional.of( enabled );
}

public List<IgnoreConfiguration> getIgnore() {
return ignore;
}

public void setIgnore(List<IgnoreConfiguration> ignore) {
this.ignore = ignore;
}

public Optional<String> getLabel() {
return label;
}

public void setLabel(String label) {
this.label = Optional.of( label );
}

public Optional<String> getTitlePrefix() {
return titlePrefix;
}

public void setTitlePrefix(String titlePrefix) {
this.titlePrefix = Optional.of( titlePrefix );
}
}
}
Loading