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

Update PR title and body when relaunching a campaign #711

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,19 @@ public void validate() {
}
}

private void updatePullRequestTitleAndBody(GHPullRequest pr, String newTitle, String newBody) throws IOException {
String currentTitle = pr.getTitle();
String currentBody = pr.getBody();

if (!currentTitle.equals(newTitle) || !currentBody.equals(newBody)) {
pr.setTitle(newTitle); // Sets the new title
biru-codeastromer marked this conversation as resolved.
Show resolved Hide resolved
pr.setBody(newBody); // Sets the new body
biru-codeastromer marked this conversation as resolved.
Show resolved Hide resolved
LOG.info("Updated PR title and body for PR: {}", pr.getHtmlUrl());
} else {
LOG.info("No changes detected in PR title or body for PR: {}", pr.getHtmlUrl());
}
}

public boolean isConnected() {
return github != null;
}
Expand Down Expand Up @@ -909,11 +922,10 @@ public void pushChanges(Plugin plugin) {
* @param plugin The plugin to open a pull request for
*/
public void openPullRequest(Plugin plugin) {

// Ensure to refresh client to target installation
refreshToken(config.getGithubAppTargetInstallationId());

// Renders parts and log then even if dry-run
// Renders parts and log them even if dry-run
String prTitle = TemplateUtils.renderPullRequestTitle(plugin, config.getRecipe());
String prBody = TemplateUtils.renderPullRequestBody(plugin, config.getRecipe());
LOG.debug("Pull request title: {}", prTitle);
Expand All @@ -937,15 +949,20 @@ public void openPullRequest(Plugin plugin) {
return;
}

// Check if existing PR exists
GHRepository repository = plugin.getRemoteRepository(this);
Optional<GHPullRequest> existingPR = checkIfPullRequestExists(plugin);
if (existingPR.isPresent()) {
LOG.info("Pull request already exists: {}", existingPR.get().getHtmlUrl());
return;
}

try {
GHRepository repository = plugin.getRemoteRepository(this);

// Check if an existing PR exists
Optional<GHPullRequest> existingPR = checkIfPullRequestExists(plugin);
if (existingPR.isPresent()) {
GHPullRequest pr = existingPR.get();
LOG.info("Pull request already exists: {}", pr.getHtmlUrl());
updatePullRequestTitleAndBody(pr, prTitle, prBody);
} else {
LOG.info("Existing pull request is null. Skipping update.");
biru-codeastromer marked this conversation as resolved.
Show resolved Hide resolved
}

// Create a new pull request
String branchName = TemplateUtils.renderBranchName(plugin, config.getRecipe());
GHPullRequest pr = repository.createPullRequest(
prTitle,
Expand All @@ -954,23 +971,30 @@ public void openPullRequest(Plugin plugin) {
prBody,
false,
config.isDraft());
LOG.info("Pull request created: {}", pr.getHtmlUrl());
plugin.withPullRequest();
try {
String[] tags = plugin.getTags().stream()
.filter(ALLOWED_TAGS::contains)
.sorted()
.toArray(String[]::new);
if (tags.length > 0) {
pr.addLabels(tags);

if (pr != null) {
LOG.info("Pull request created: {}", pr.getHtmlUrl());
plugin.withPullRequest();

// Add labels to the PR if applicable
try {
String[] tags = plugin.getTags().stream()
.filter(ALLOWED_TAGS::contains)
.sorted()
.toArray(String[]::new);
if (tags.length > 0) {
pr.addLabels(tags);
}
} catch (Exception e) {
LOG.debug("Failed to add labels to pull request: {}. Probably missing permission.", e.getMessage());
} finally {
plugin.withoutTags();
}
} catch (Exception e) {
LOG.debug("Failed to add labels to pull request: {}. Probably missing permission.", e.getMessage());
} finally {
plugin.withoutTags();
} else {
LOG.error("Failed to create pull request for plugin {}", plugin.getName());
}
} catch (IOException e) {
plugin.addError("Failed to create pull request", e);
plugin.addError("Failed to handle pull request", e);
plugin.raiseLastError();
}
}
Expand Down
Loading