Skip to content

Commit

Permalink
Integrated code lifecycle: Fix an issue with git cloning over SSH
Browse files Browse the repository at this point in the history
  • Loading branch information
krusche committed Sep 29, 2024
1 parent 3c4fc96 commit 4e5e8e1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ public void setLocalVCBaseUrl(URL localVCBaseUrl) {

// Cache the retrieved repositories for quicker access.
// The resolveRepository method is called multiple times per request.
// Key: repositoryPath --> Value: Repository
private final Map<String, Repository> repositories = new HashMap<>();

public LocalVCServletService(AuthenticationManager authenticationManager, UserRepository userRepository, ProgrammingExerciseRepository programmingExerciseRepository,
Expand Down Expand Up @@ -441,18 +442,23 @@ public void authorizeUser(String repositoryTypeOrUserName, User user, Programmin
catch (AccessForbiddenException e) {
throw new LocalVCForbiddenException(e);
}
// TODO: retrieving the git commit hash should be done ASYNC together with storing the log in the database to avoid long waiting times during permission check
String commitHash = null;
try {
if (repositoryActionType == RepositoryActionType.READ) {
commitHash = getLatestCommitHash(repositories.get(localVCRepositoryUri.getRelativeRepositoryPath().toString()));
String relativeRepositoryPath = localVCRepositoryUri.getRelativeRepositoryPath().toString();
try (Repository repository = resolveRepository(relativeRepositoryPath)) {
commitHash = getLatestCommitHash(repository);
}
}
// Write a access log entry to the database
String finalCommitHash = commitHash;
vcsAccessLogService.ifPresent(service -> service.storeAccessLog(user, participation, repositoryActionType, authenticationMechanism, finalCommitHash, ipAddress));
}
catch (GitAPIException e) {
log.warn("Failed to obtain commit hash for repository {}. Error: {}", localVCRepositoryUri.getRelativeRepositoryPath().toString(), e.getMessage());
// NOTE: we intentionally catch all issues here to avoid that the user is blocked from accessing the repository
catch (Exception e) {
log.warn("Failed to obtain commit hash or store access log for repository {}. Error: {}", localVCRepositoryUri.getRelativeRepositoryPath().toString(), e.getMessage());
}
// Write a access log entry to the database
String finalCommitHash = commitHash;
vcsAccessLogService.ifPresent(service -> service.storeAccessLog(user, participation, repositoryActionType, authenticationMechanism, finalCommitHash, ipAddress));
}

/**
Expand Down Expand Up @@ -520,9 +526,16 @@ public void processNewPush(String commitHash, Repository repository) {
// Process push to any repository other than the test repository.
processNewPushToRepository(participation, commit);

// For push the correct commitHash is only available here, therefore the preliminary null value is overwritten
String finalCommitHash = commitHash;
vcsAccessLogService.ifPresent(service -> service.updateCommitHash(participation, finalCommitHash));
try {
// For push the correct commitHash is only available here, therefore the preliminary null value is overwritten
String finalCommitHash = commitHash;
vcsAccessLogService.ifPresent(service -> service.updateCommitHash(participation, finalCommitHash));
}
// NOTE: we intentionally catch all issues here to avoid that the user is blocked from accessing the repository
catch (Exception e) {
log.warn("Failed to obtain commit hash or store access log for repository {}. Error: {}", localVCRepositoryUri.getRelativeRepositoryPath().toString(),
e.getMessage());
}
}
catch (GitAPIException | IOException e) {
// This catch clause does not catch exceptions that happen during runBuildJob() as that method is called asynchronously.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class VcsAccessLogService {
* @param commitHash The latest commit hash
* @param ipAddress The ip address of the user accessing the repository
*/
// TODO: this should be ASYNC to avoid long waiting times during permission check
public void storeAccessLog(User user, ProgrammingExerciseParticipation participation, RepositoryActionType actionType, AuthenticationMechanism authenticationMechanism,
String commitHash, String ipAddress) {
log.debug("Storing access operation for user {}", user);
Expand All @@ -59,6 +60,7 @@ public void storeAccessLog(User user, ProgrammingExerciseParticipation participa
* @param participation The participation to which the repository belongs to
* @param commitHash The newest commit hash which should get set for the access log entry
*/
// TODO: this should be ASYNC to avoid long waiting times during permission check
public void updateCommitHash(ProgrammingExerciseParticipation participation, String commitHash) {
vcsAccessLogRepository.findNewestByParticipationIdWhereCommitHashIsNull(participation.getId()).ifPresent(entry -> {
entry.setCommitHash(commitHash);
Expand Down

0 comments on commit 4e5e8e1

Please sign in to comment.