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

Develop #131

Merged
merged 2 commits into from
Feb 15, 2024
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 @@ -61,6 +61,42 @@ public class ArtemisConfiguration {
@Value("${artemis.ts3.is-local}")
private boolean test3IsLocal;

@Value("${artemis.ts7.url}")
private String test7Url;

@Value("${artemis.ts7.cleanup-enabled}")
private boolean test7Cleanup;

@Value("${artemis.ts7.prometheus-instances.artemis}")
private String[] test7PrometheusInstanceArtemis;

@Value("${artemis.ts7.prometheus-instances.vcs}")
private String[] test7PrometheusInstanceVcs;

@Value("${artemis.ts7.prometheus-instances.ci}")
private String[] test7PrometheusInstanceCi;

@Value("${artemis.ts7.is-local}")
private boolean test7IsLocal;

@Value("${artemis.ts8.url}")
private String test8Url;

@Value("${artemis.ts8.cleanup-enabled}")
private boolean test8Cleanup;

@Value("${artemis.ts8.prometheus-instances.artemis}")
private String[] test8PrometheusInstanceArtemis;

@Value("${artemis.ts8.prometheus-instances.vcs}")
private String[] test8PrometheusInstanceVcs;

@Value("${artemis.ts8.prometheus-instances.ci}")
private String[] test8PrometheusInstanceCi;

@Value("${artemis.ts8.is-local}")
private boolean test8IsLocal;

@Value("${artemis.staging.url}")
private String stagingUrl;

Expand Down Expand Up @@ -120,6 +156,8 @@ public String getUrl(ArtemisServer server) {
case LOCAL -> localUrl;
case TS1 -> test1Url;
case TS3 -> test3Url;
case TS7 -> test7Url;
case TS8 -> test8Url;
case STAGING -> stagingUrl;
case STAGING2 -> staging2Url;
case PRODUCTION -> productionUrl;
Expand All @@ -131,6 +169,8 @@ public boolean getCleanup(ArtemisServer server) {
case LOCAL -> localCleanup;
case TS1 -> test1Cleanup;
case TS3 -> test3Cleanup;
case TS7 -> test7Cleanup;
case TS8 -> test8Cleanup;
case STAGING -> stagingCleanup;
case STAGING2 -> staging2Cleanup;
case PRODUCTION -> productionCleanup;
Expand All @@ -142,6 +182,8 @@ public String[] getPrometheusInstancesArtemis(ArtemisServer server) {
case LOCAL -> localPrometheusInstanceArtemis;
case TS1 -> test1PrometheusInstanceArtemis;
case TS3 -> test3PrometheusInstanceArtemis;
case TS7 -> test7PrometheusInstanceArtemis;
case TS8 -> test8PrometheusInstanceArtemis;
case STAGING -> stagingPrometheusInstanceArtemis;
case STAGING2 -> staging2PrometheusInstanceArtemis;
case PRODUCTION -> productionPrometheusInstanceArtemis;
Expand All @@ -153,6 +195,8 @@ public String[] getPrometheusInstancesVcs(ArtemisServer server) {
case LOCAL -> localPrometheusInstanceVcs;
case TS1 -> test1PrometheusInstanceVcs;
case TS3 -> test3PrometheusInstanceVcs;
case TS7 -> test7PrometheusInstanceVcs;
case TS8 -> test8PrometheusInstanceVcs;
case STAGING -> stagingPrometheusInstanceVcs;
case STAGING2 -> staging2PrometheusInstanceVcs;
case PRODUCTION -> productionPrometheusInstanceVcs;
Expand All @@ -164,6 +208,8 @@ public String[] getPrometheusInstancesCi(ArtemisServer server) {
case LOCAL -> localPrometheusInstanceCi;
case TS1 -> test1PrometheusInstanceCi;
case TS3 -> test3PrometheusInstanceCi;
case TS7 -> test7PrometheusInstanceCi;
case TS8 -> test8PrometheusInstanceCi;
case STAGING -> stagingPrometheusInstanceCi;
case STAGING2 -> staging2PrometheusInstanceCi;
case PRODUCTION -> productionPrometheusInstanceCi;
Expand All @@ -175,6 +221,8 @@ public boolean getIsLocal(ArtemisServer server) {
case LOCAL -> localIsLocal;
case TS1 -> test1IsLocal;
case TS3 -> test3IsLocal;
case TS7 -> test7IsLocal;
case TS8 -> test8IsLocal;
case STAGING -> stagingIsLocal;
case STAGING2 -> staging2IsLocal;
case PRODUCTION -> productionIsLocal;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.opencsv.bean.CsvToBeanBuilder;
import de.tum.cit.ase.domain.ArtemisUser;
import de.tum.cit.ase.repository.ArtemisUserRepository;
import de.tum.cit.ase.service.artemis.interaction.SimulatedArtemisAdmin;
import de.tum.cit.ase.service.artemis.interaction.SimulatedArtemisUser;
import de.tum.cit.ase.service.dto.ArtemisUserForCreationDTO;
import de.tum.cit.ase.service.dto.ArtemisUserPatternDTO;
import de.tum.cit.ase.util.ArtemisServer;
Expand All @@ -24,13 +26,16 @@ public class ArtemisUserService {

private final Logger log = LoggerFactory.getLogger(ArtemisUserService.class);
private final ArtemisUserRepository artemisUserRepository;
private final ArtemisConfiguration artemisConfiguration;

public ArtemisUserService(ArtemisUserRepository artemisUserRepository) {
public ArtemisUserService(ArtemisUserRepository artemisUserRepository, ArtemisConfiguration artemisConfiguration) {
this.artemisUserRepository = artemisUserRepository;
this.artemisConfiguration = artemisConfiguration;
}

/**
* Creates a list of ArtemisUsers from a pattern.
* If createOnArtemis is true, the users will also be created on the Artemis server.
*
* @param server the ArtemisServer to create the users for
* @param pattern the pattern to use for the usernames and passwords
Expand All @@ -50,15 +55,41 @@ public List<ArtemisUser> createArtemisUsersByPattern(ArtemisServer server, Artem
throw new BadRequestAlertException("server must not be null", "artemisUser", "missingServer");
}

SimulatedArtemisAdmin simulatedArtemisAdmin = null;
if (pattern.isCreateOnArtemis()) {
ArtemisUser admin = getAdminUser(server);
if (admin == null) {
throw new BadRequestAlertException("No admin user found for server", "artemisUser", "missingAdmin");
}
simulatedArtemisAdmin =
SimulatedArtemisUser.createArtemisAdminFromCredentials(
artemisConfiguration.getUrl(server),
admin.getUsername(),
admin.getPassword()
);
simulatedArtemisAdmin.login();
}

List<ArtemisUser> createdUsers = new ArrayList<>();
for (int i = pattern.getFrom(); i < pattern.getTo(); i++) {
ArtemisUser artemisUser = new ArtemisUser();
artemisUser.setServer(server);
artemisUser.setServerWideId(i);
artemisUser.setUsername(pattern.getUsernamePattern().replace("{i}", String.valueOf(i)));
artemisUser.setPassword(pattern.getPasswordPattern().replace("{i}", String.valueOf(i)));
var username = pattern.getUsernamePattern().replace("{i}", String.valueOf(i));
var password = pattern.getPasswordPattern().replace("{i}", String.valueOf(i));
artemisUser.setUsername(username);
artemisUser.setPassword(password);
try {
createdUsers.add(saveArtemisUser(artemisUser));
ArtemisUser createdUser = saveArtemisUser(artemisUser);
// Create user on Artemis if necessary
if (pattern.isCreateOnArtemis() && simulatedArtemisAdmin != null) {
var firstName = pattern.getFirstNamePattern().replace("{i}", String.valueOf(i));
var lastName = pattern.getLastNamePattern().replace("{i}", String.valueOf(i));
var email = pattern.getEmailPattern().replace("{i}", String.valueOf(i));
simulatedArtemisAdmin.createUser(username, password, firstName, lastName, email);
}
// The order of operations is important here, as the user might not be created on Artemis if an exception is thrown
createdUsers.add(createdUser);
} catch (BadRequestAlertException e) {
log.debug(e.getMessage() + ". Skipping user.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import de.tum.cit.ase.artemisModel.*;
import de.tum.cit.ase.domain.ArtemisUser;
import de.tum.cit.ase.service.artemis.ArtemisUserService;
import de.tum.cit.ase.service.artemis.util.ArtemisUserDTO;
import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.core.Scheduler;
import io.reactivex.rxjava3.schedulers.Schedulers;
Expand Down Expand Up @@ -482,6 +483,28 @@ public void deleteExam(long courseId, long examId) {
.block();
}

/**
* Create a new student on Artemis.
*
* @param username the username of the student
* @param password the password of the student
* @param firstName the first name of the student
* @param lastName the last name of the student
* @param email the email of the student
*/
public void createUser(String username, String password, String firstName, String lastName, String email) {
if (!authenticated) {
throw new IllegalStateException("User " + username + " is not logged in or does not have the necessary access rights.");
}
var user = new ArtemisUserDTO();
user.setLogin(username);
user.setPassword(password);
user.setFirstName(firstName);
user.setLastName(lastName);
user.setEmail(email);
webClient.post().uri("api/admin/users").bodyValue(user).retrieve().toBodilessEntity().block();
}

/**
* Get the build queue for the given course.
* <p>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package de.tum.cit.ase.service.artemis.util;

import java.util.List;

public class ArtemisUserDTO {

public boolean activated = true;
public List<String> authorities = List.of("ROLE_USER");
public List<String> groups = List.of();
public List<String> guidedTourSettings = List.of();
public String email;
public String firstName;
public String lastName;
public String login;
public String password;
public String visibleRegistrationNumber;

public boolean isActivated() {
return activated;
}

public void setActivated(boolean activated) {
this.activated = activated;
}

public List<String> getAuthorities() {
return authorities;
}

public void setAuthorities(List<String> authorities) {
this.authorities = authorities;
}

public List<String> getGroups() {
return groups;
}

public void setGroups(List<String> groups) {
this.groups = groups;
}

public List<String> getGuidedTourSettings() {
return guidedTourSettings;
}

public void setGuidedTourSettings(List<String> guidedTourSettings) {
this.guidedTourSettings = guidedTourSettings;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getLogin() {
return login;
}

public void setLogin(String login) {
this.login = login;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getVisibleRegistrationNumber() {
return visibleRegistrationNumber;
}

public void setVisibleRegistrationNumber(String visibleRegistrationNumber) {
this.visibleRegistrationNumber = visibleRegistrationNumber;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ public class ArtemisUserPatternDTO implements Serializable {

private String usernamePattern;
private String passwordPattern;

private String firstNamePattern;
private String lastNamePattern;
private String emailPattern;
private int from;
private int to;
private boolean createOnArtemis;

public String getUsernamePattern() {
return usernamePattern;
Expand Down Expand Up @@ -40,4 +45,36 @@ public int getTo() {
public void setTo(int to) {
this.to = to;
}

public String getFirstNamePattern() {
return firstNamePattern;
}

public void setFirstNamePattern(String firstNamePattern) {
this.firstNamePattern = firstNamePattern;
}

public String getLastNamePattern() {
return lastNamePattern;
}

public void setLastNamePattern(String lastNamePattern) {
this.lastNamePattern = lastNamePattern;
}

public String getEmailPattern() {
return emailPattern;
}

public void setEmailPattern(String emailPattern) {
this.emailPattern = emailPattern;
}

public boolean isCreateOnArtemis() {
return createOnArtemis;
}

public void setCreateOnArtemis(boolean createOnArtemis) {
this.createOnArtemis = createOnArtemis;
}
}
2 changes: 2 additions & 0 deletions src/main/java/de/tum/cit/ase/util/ArtemisServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ public enum ArtemisServer {
LOCAL,
TS1,
TS3,
TS7,
TS8,
STAGING,
STAGING2,
PRODUCTION,
Expand Down
16 changes: 16 additions & 0 deletions src/main/resources/config/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,29 @@ artemis:
vcs:
ci:
ts1:
url: https://artemis-test7.artemis.cit.tum.de/
cleanup-enabled: false
is-local: false
prometheus-instances:
artemis: artemis-test1.artemis.cit.tum.de:9100
vcs: bitbucket-prelive-node1.ase.in.tum.de:9100
ci: bamboo-prelive-instance.ase.in.tum.de:9100
ts7:
url: https://artemis-test7.artemis.cit.tum.de/
cleanup-enabled: false
is-local: true
prometheus-instances:
artemis: artemis-test7.artemis.cit.tum.de:9100
vcs:
ci:
ts8:
url: https://artemis-test8.artemis.cit.tum.de/
cleanup-enabled: false
is-local: true
prometheus-instances:
artemis: artemis-test8.artemis.cit.tum.de:9100
vcs:
ci:
staging:
url: https://artemis-staging.artemis.in.tum.de/
cleanup-enabled: false
Expand Down
Loading