Skip to content

Commit

Permalink
Merge pull request #428 from jonesbusy/feature/more-title
Browse files Browse the repository at this point in the history
Handle push error
  • Loading branch information
jonesbusy authored Dec 10, 2024
2 parents df2bb2d + cdecd60 commit c252253
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
import java.nio.file.Files;
import java.util.List;
import java.util.Optional;
import java.util.stream.StreamSupport;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.ResetCommand;
import org.eclipse.jgit.api.Status;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.RefAlreadyExistsException;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.transport.PushResult;
import org.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.transport.URIish;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
Expand Down Expand Up @@ -677,17 +679,34 @@ public void pushChanges(Plugin plugin) {
return;
}
try (Git git = Git.open(plugin.getLocalRepository().toFile())) {
git.push()
.setForce(true)
.setCredentialsProvider(new UsernamePasswordCredentialsProvider(Settings.GITHUB_TOKEN, ""))
.setRemote("origin")
.setRefSpecs(new RefSpec(BRANCH_NAME + ":" + BRANCH_NAME))
.call();
List<PushResult> results = StreamSupport.stream(
git.push()
.setForce(true)
.setCredentialsProvider(
new UsernamePasswordCredentialsProvider(Settings.GITHUB_TOKEN, ""))
.setRemote("origin")
.setRefSpecs(new RefSpec(BRANCH_NAME + ":" + BRANCH_NAME))
.call()
.spliterator(),
false)
.toList();
results.forEach(result -> {
LOG.debug("Push result: {}", result.getMessages());
// TODO: Always use <user>@users.noreply.github.com instead of granting right to read primary email

Check warning on line 695 in plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/github/GHService.java

View check run for this annotation

ci.jenkins.io / Open Tasks Scanner

TODO

NORMAL: Always use @users.noreply.github.com instead of granting right to read primary email
if (result.getMessages().contains("GH007")) {
plugin.addError("Not allow to push. Your push would publish a private email address.");
plugin.raiseLastError();
} else if (result.getMessages().contains("error")) {
plugin.addError("Unexpected push error: %s".formatted(result.getMessages()));
plugin.raiseLastError();
}
});
plugin.withoutCommits();
plugin.withChangesPushed();
LOG.info("Pushed changes to forked repository for plugin {}", plugin.getName());
} catch (IOException | GitAPIException e) {
plugin.addError("Failed to push changes", e);
plugin.raiseLastError();
}
}

Expand Down Expand Up @@ -750,6 +769,7 @@ public void openPullRequest(Plugin plugin) {
plugin.withPullRequest();
} catch (IOException e) {
plugin.addError("Failed to create pull request", e);
plugin.raiseLastError();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@import io.jenkins.tools.pluginmodernizer.core.model.Plugin
@import org.openrewrite.Recipe
@param Plugin plugin
@param Recipe recipe
Require ${plugin.getMetadata().getJenkinsVersion()} and Java 17
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,25 @@ public void testFriendlyPrTitleUpgradeToLatestJava11CoreVersion() {
// Assert
assertEquals("Require 2.462.3", result);
}

@Test
public void testFriendlyPrTitleUpgradeNextMajorParentVersion() {

// Mocks
Plugin plugin = mock(Plugin.class);
PluginMetadata metadata = mock(PluginMetadata.class);
Recipe recipe = mock(Recipe.class);

doReturn(metadata).when(plugin).getMetadata();
doReturn("2.479.1").when(metadata).getJenkinsVersion();
doReturn("io.jenkins.tools.pluginmodernizer.UpgradeNextMajorParentVersion")
.when(recipe)
.getName();

// Test
String result = TemplateUtils.renderPullRequestTitle(plugin, recipe);

// Assert
assertEquals("Require 2.479.1 and Java 17", result);
}
}

0 comments on commit c252253

Please sign in to comment.