Thanks to visit codestin.com
Credit goes to github.com

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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.marker.ci.*;

import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URI;
Expand Down Expand Up @@ -160,10 +161,17 @@ public static GitProvenance fromProjectDirectory(Path projectDir) {
throw new UncheckedIOException(e);
}
} else {
try {
return environment.buildGitProvenance();
} catch (IncompleteGitConfigException e) {
File gitDir = new RepositoryBuilder().findGitDir(projectDir.toFile()).getGitDir();
if (gitDir != null && gitDir.exists()) {
//it has been cloned with --depth > 0
return fromGitConfig(projectDir);
} else {
//there is not .git config
try {
return environment.buildGitProvenance();
} catch (IncompleteGitConfigException e) {
return fromGitConfig(projectDir);
}
}
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.InvalidRefNameException;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.RepositoryCache;
import org.eclipse.jgit.transport.TagOpt;
Expand Down Expand Up @@ -276,6 +277,24 @@ void supportsGitHubActions(@TempDir Path projectDir) {
assertThat(prov.getChange()).isEqualTo("287364287357");
}

@Test
void ignoresBuildEnvironmentIfThereIsGitConfig(@TempDir Path projectDir) throws GitAPIException {
Map<String, String> envVars = new HashMap<>();
envVars.put("GITHUB_API_URL", "https://api.github.com");
envVars.put("GITHUB_REPOSITORY", "octocat/Hello-World");
envVars.put("GITHUB_REF", "refs/heads/foo");
envVars.put("GITHUB_SHA", "287364287357");
envVars.put("GITHUB_HEAD_REF", "");
try (Git g = Git.init().setDirectory(projectDir.toFile()).setInitialBranch("main").call()) {
GitProvenance prov = GitProvenance.fromProjectDirectory(projectDir,
GithubActionsBuildEnvironment.build(var -> envVars.get(var)));
assertThat(prov != null);
assertThat(prov.getOrigin()).isNotEqualTo("https://github.com/octocat/Hello-World.git");
assertThat(prov.getBranch()).isEqualTo("main");
assertThat(prov.getChange()).isNotEqualTo("287364287357");
}
}

@Test
void supportsCustomBuildEnvironment(@TempDir Path projectDir) {
Map<String, String> envVars = new HashMap<>();
Expand Down