Skip to content

Commit

Permalink
GH_OWNER is not optional and got from token if set (#449)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonesbusy authored Dec 12, 2024
1 parent 637caf6 commit 5fbe390
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.inject.Guice;
import io.jenkins.tools.pluginmodernizer.cli.options.GlobalOptions;
import io.jenkins.tools.pluginmodernizer.core.GuiceModule;
import io.jenkins.tools.pluginmodernizer.core.config.Config;
import io.jenkins.tools.pluginmodernizer.core.impl.PluginModernizer;
import io.jenkins.tools.pluginmodernizer.core.model.ModernizerException;
import org.slf4j.Logger;
Expand All @@ -28,11 +29,14 @@ public class ValidateCommand implements ICommand {

@Override
public Integer call() throws Exception {
PluginModernizer modernizer = Guice.createInjector(
new GuiceModule(options.getBuilderForOptions().build()))
.getInstance(PluginModernizer.class);
Config config = options.getBuilderForOptions().build();
PluginModernizer modernizer =
Guice.createInjector(new GuiceModule(config)).getInstance(PluginModernizer.class);
try {
modernizer.validate();
LOG.info("GitHub owner: {}", modernizer.getGithubOwner());
LOG.info("Maven home: {}", config.getMavenHome());
LOG.info("Cache path: {}", config.getCachePath());
} catch (ModernizerException e) {
LOG.error("Validation error");
LOG.error(e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void validate() {
|| config.getGithubAppTargetInstallationId() == null)) {
throw new ModernizerException("Please set GH_TOKEN, GITHUB_TOKEN or configure GitHub app authentication.");
}
if (config.getGithubOwner() == null) {
if (getGithubOwner() == null) {
throw new ModernizerException(
"GitHub owner (username/organization) is not set. Please set GH_OWNER or GITHUB_OWNER environment variable. Or use --github-owner if running from CLI");
}
Expand All @@ -85,10 +85,20 @@ public void validate() {
}
}

public boolean isConnected() {
return github != null;
}

/**
* Connect to GitHub using the GitHub auth token
*/
public void connect() {
if (Settings.GITHUB_TOKEN == null
&& (config.getGithubAppId() == null
|| config.getGithubAppSourceInstallationId() == null
|| config.getGithubAppTargetInstallationId() == null)) {
throw new ModernizerException("Please set GH_TOKEN, GITHUB_TOKEN or configure GitHub app authentication.");
}
if (github != null) {
throw new ModernizerException("GitHub client is already connected.");
}
Expand Down Expand Up @@ -187,7 +197,7 @@ public GHRepository getRepositoryFork(Plugin plugin) {
throw new PluginProcessingException("Cannot get fork repository in dry-run mode", plugin);
}
try {
return github.getRepository(config.getGithubOwner() + "/" + plugin.getRepositoryName());
return github.getRepository(getGithubOwner() + "/" + plugin.getRepositoryName());
} catch (IOException e) {
throw new PluginProcessingException("Failed to get repository", e, plugin);
}
Expand Down Expand Up @@ -320,7 +330,7 @@ private GHRepository forkRepository(GHRepository originalRepo) throws IOExceptio
*/
private GHOrganization getOrganization() throws IOException {
try {
return github.getOrganization(config.getGithubOwner());
return github.getOrganization(getGithubOwner());
} catch (GHFileNotFoundException e) {
LOG.debug("Owner is not an organization: {}", config.getGithubOwner());
return null;
Expand Down Expand Up @@ -611,6 +621,10 @@ public void commitChanges(Plugin plugin) {
* @return The current user
*/
public GHUser getCurrentUser() {
if (!isConnected()) {
LOG.debug("Not able to get current user. GitHub client is not connected");
return null;
}
try {
// Get myself
if (config.getGithubAppId() == null) {
Expand Down Expand Up @@ -750,7 +764,7 @@ public void openPullRequest(Plugin plugin) {
try {
GHPullRequest pr = repository.createPullRequest(
prTitle,
config.getGithubOwner() + ":" + BRANCH_NAME,
getGithubOwner() + ":" + BRANCH_NAME,
repository.getDefaultBranch(),
prBody,
false,
Expand Down Expand Up @@ -822,6 +836,16 @@ private Optional<GHPullRequest> checkIfPullRequestExists(Plugin plugin) {
}
}

/**
* Determine the GitHub owner from config or using current token
* @return The GitHub owner
*/
public String getGithubOwner() {
return config.getGithubOwner() != null
? config.getGithubOwner()
: getCurrentUser().getLogin();
}

/**
* Ensure the forked reository correspond of the origin parent repository
* @param originalRepo The original repository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,18 @@ public class PluginModernizer {
public void validate() {
mavenInvoker.validateMavenHome();
mavenInvoker.validateMavenVersion();
ghService.connect();
ghService.validate();
}

/**
* Expose the effective GitHub owner from either config or current owner of token
* @return The GitHub owner
*/
public String getGithubOwner() {
return ghService.getGithubOwner();
}

/**
* Entry point to start the plugin modernization process
*/
Expand All @@ -51,7 +60,6 @@ public void start() {
validate();

// Setup
this.ghService.connect();
if (config.isRemoveLocalData()) {
cacheManager.wipe();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,7 @@ public void shouldOpenPullRequest() throws Exception {

doReturn(recipe).when(config).getRecipe();
doReturn("recipe1").when(recipe).getName();
doReturn("test").when(config).getGithubOwner();
doReturn(null).when(config).getGithubAppTargetInstallationId();
doReturn(false).when(config).isDraft();
doReturn(true).when(plugin).hasChangesPushed();
Expand Down Expand Up @@ -672,6 +673,7 @@ public void shouldOpenDraftPullRequest() throws Exception {

doReturn(recipe).when(config).getRecipe();
doReturn("recipe1").when(recipe).getName();
doReturn("test").when(config).getGithubOwner();
doReturn(null).when(config).getGithubAppTargetInstallationId();
doReturn(true).when(config).isDraft();
doReturn(true).when(plugin).hasChangesPushed();
Expand Down

0 comments on commit 5fbe390

Please sign in to comment.