use of io.dockstore.webservice.helpers.GitHubSourceCodeRepo in project dockstore by dockstore.
the class AbstractWorkflowResource method createServicesAndVersionsFromDockstoreYml.
/**
* Create or retrieve services based on Dockstore.yml, add or update tag version
* ONLY WORKS FOR v1.1
* @param repository Repository path (ex. dockstore/dockstore-ui2)
* @param gitReference Git reference from GitHub (ex. refs/tags/1.0)
* @param installationId installation id needed to set up GitHub Apps
* @param user User that triggered action
* @param dockstoreYml
* @return List of new and updated services
*/
private List<Workflow> createServicesAndVersionsFromDockstoreYml(Service12 service, String repository, String gitReference, String installationId, User user, final SourceFile dockstoreYml) {
GitHubSourceCodeRepo gitHubSourceCodeRepo = (GitHubSourceCodeRepo) SourceCodeRepoFactory.createGitHubAppRepo(gitHubAppSetup(installationId));
final List<Workflow> updatedServices = new ArrayList<>();
if (service != null) {
if (!DockstoreYamlHelper.filterGitReference(Path.of(gitReference), service.getFilters())) {
return updatedServices;
}
final DescriptorLanguageSubclass subclass = service.getSubclass();
final Boolean publish = service.getPublish();
final var defaultVersion = service.getLatestTagAsDefault();
final List<YamlAuthor> yamlAuthors = service.getAuthors();
Workflow workflow = createOrGetWorkflow(Service.class, repository, user, "", subclass.getShortName(), gitHubSourceCodeRepo);
addDockstoreYmlVersionToWorkflow(repository, gitReference, dockstoreYml, gitHubSourceCodeRepo, workflow, defaultVersion, yamlAuthors);
if (publish != null && workflow.getIsPublished() != publish) {
LambdaEvent lambdaEvent = createBasicEvent(repository, gitReference, user.getUsername(), LambdaEvent.LambdaEventType.PUBLISH);
try {
workflow = publishWorkflow(workflow, publish);
} catch (CustomWebApplicationException ex) {
LOG.warn("Could not set publish state from YML.", ex);
lambdaEvent.setSuccess(false);
lambdaEvent.setMessage(ex.getMessage());
}
lambdaEventDAO.create(lambdaEvent);
}
updatedServices.add(workflow);
}
return updatedServices;
}
use of io.dockstore.webservice.helpers.GitHubSourceCodeRepo in project dockstore by dockstore.
the class AbstractWorkflowResource method syncEntities.
/**
* Syncs entities based on GitHub app installation, optionally limiting to orgs in the GitHub organization <code>organization</code>.
*
* 1. Finds all repos that have the Dockstore GitHub app installed
* 2. For existing entities, ensures that <code>user</code> is one of the entity's users
*
* @param user
* @param gitHubToken
*/
private void syncEntities(User user, Token gitHubToken) {
GitHubSourceCodeRepo gitHubSourceCodeRepo = (GitHubSourceCodeRepo) SourceCodeRepoFactory.createSourceCodeRepo(gitHubToken);
// Get all GitHub repositories for the user
final Map<String, String> workflowGitUrl2Name = gitHubSourceCodeRepo.getWorkflowGitUrl2RepositoryId();
// Filter by organization if necessary
final Collection<String> repositories = workflowGitUrl2Name.values();
// Add user to any services they should have access to that already exist on Dockstore
final List<Workflow> existingWorkflows = findDockstoreWorkflowsForGitHubRepos(repositories);
existingWorkflows.stream().filter(workflow -> !workflow.getUsers().contains(user)).forEach(workflow -> workflow.getUsers().add(user));
// No longer adds stub services, though code could be useful
// final Set<String> existingWorkflowPaths = existingWorkflows.stream()
// .map(workflow -> workflow.getWorkflowPath()).collect(Collectors.toSet());
//
// GitHubHelper.checkJWT(gitHubAppId, gitHubPrivateKeyFile);
//
// GitHubHelper.reposToCreateEntitiesFor(repositories, organization, existingWorkflowPaths).stream()
// .forEach(repositoryName -> {
// final T entity = initializeEntity(repositoryName, gitHubSourceCodeRepo);
// entity.addUser(user);
// final long entityId = workflowDAO.create(entity);
// final Workflow createdEntity = workflowDAO.findById(entityId);
// final Workflow updatedEntity = gitHubSourceCodeRepo.getWorkflow(repositoryName, Optional.of(createdEntity));
// updateDBWorkflowWithSourceControlWorkflow(createdEntity, updatedEntity, user);
// });
}
use of io.dockstore.webservice.helpers.GitHubSourceCodeRepo in project dockstore by dockstore.
the class UserResource method updateUserMetadataToGetIds.
// TODO: Do equivalent of below, but for Google users.
@GET
@Timed
@UnitOfWork
@RolesAllowed("admin")
@Path("/updateUserMetadataToGetIds")
@Deprecated
@Operation(operationId = "updateUserMetadataToGetIds", description = "Attempt to update the metadata of all GitHub and Google users and then returns a list of Dockstore users who could not be updated.", security = @SecurityRequirement(name = OPENAPI_JWT_SECURITY_DEFINITION_NAME))
@ApiResponse(responseCode = HttpStatus.SC_OK + "", description = "Get list Dockstore users we were unable to get GitHub/Google IDs for.", content = @Content(mediaType = "application/json", array = @ArraySchema(schema = @Schema(implementation = User.class))))
@ApiOperation(value = "See OpenApi for details", hidden = true)
public List<User> updateUserMetadataToGetIds(@ApiParam(hidden = true) @Parameter(hidden = true, name = "user") @Auth User user) {
List<Token> googleTokens = tokenDAO.findAllGoogleTokens();
List<User> gitHubUsersNotUpdatedWithToken = new ArrayList<>();
List<User> gitHubUsersNotUpdatedByAvatarUrl = new ArrayList<>();
List<User> usersCouldNotBeUpdated = new ArrayList<>();
// Try to update Google metadata using user's token. This is the only option for Google.
LOG.info("Beginning update for " + googleTokens.size() + " Google users.");
for (Token t : googleTokens) {
User currentUser = userDAO.findById(t.getUserId());
if (currentUser != null) {
updateGoogleAccessToken(currentUser.getId());
try {
currentUser.updateUserMetadata(tokenDAO, TokenType.GOOGLE_COM, true);
LOG.info("Updated Google id for " + currentUser.getUsername());
} catch (Exception ex) {
LOG.info("Could not retrieve Google ID for user: " + currentUser.getUsername(), ex);
usersCouldNotBeUpdated.add(currentUser);
}
}
}
// Try to update GitHub metadata using user's token and getMyself().
List<User> gitHubUsers = userDAO.findAllGitHubUsers();
LOG.info("Beginning update for " + gitHubUsers.size() + " GitHub users.");
for (User u : gitHubUsers) {
List<Token> tokens = tokenDAO.findGithubByUserId(u.getId());
if (!tokens.isEmpty()) {
try {
GitHubSourceCodeRepo gitHubSourceCodeRepo = (GitHubSourceCodeRepo) SourceCodeRepoFactory.createSourceCodeRepo(tokens.get(0));
gitHubSourceCodeRepo.syncUserMetadataFromGitHub(u, Optional.of(tokenDAO));
LOG.info("Updated GitHub id for " + u.getUsername());
} catch (Exception ex) {
LOG.info("Could not retrieve GitHub ID using token for user: " + u.getUsername());
gitHubUsersNotUpdatedWithToken.add(u);
}
} else {
LOG.info("No GitHub token for user: " + u.getUsername());
gitHubUsersNotUpdatedWithToken.add(u);
}
}
// For users we could not get their GitHub id using their token, get the GitHub id from the avatarurl we have stored.
LOG.info("Beginning update for " + gitHubUsersNotUpdatedWithToken.size() + " GitHub users who could not be updated with token.");
for (User u : gitHubUsersNotUpdatedWithToken) {
if (!u.isBanned()) {
User.Profile userProfile = u.getUserProfiles().get(TokenType.GITHUB_COM.toString());
String avatarUrl = userProfile.avatarURL;
if (avatarUrl != null) {
Matcher m = GITHUB_ID_PATTERN.matcher(avatarUrl);
if (m.matches()) {
String id = m.group(1);
userProfile.onlineProfileId = id;
List<Token> userTokens = tokenDAO.findGithubByUserId(u.getId());
if (!userTokens.isEmpty()) {
userTokens.get(0).setOnlineProfileId(id);
}
LOG.info("Updated Github id for " + u.getUsername() + " with GitHub id " + id);
}
} else {
gitHubUsersNotUpdatedByAvatarUrl.add(u);
LOG.info(u.getUsername() + "could not be updated.");
}
}
}
// Get the GitHub token of the admin making this call to avoid rate limiting
LOG.info("Beginning update for " + gitHubUsersNotUpdatedByAvatarUrl.size() + " GitHub users who could not be updated with token or by avatarurl stored in db.");
Token t = tokenDAO.findGithubByUserId(user.getId()).get(0);
GitHubSourceCodeRepo gitHubSourceCodeRepo = (GitHubSourceCodeRepo) SourceCodeRepoFactory.createSourceCodeRepo(t);
for (User u : gitHubUsersNotUpdatedByAvatarUrl) {
try {
gitHubSourceCodeRepo.syncUserMetadataFromGitHubByUsername(u, tokenDAO);
LOG.info("Updated GitHub id for " + u.getUsername());
} catch (Exception ex) {
usersCouldNotBeUpdated.add(u);
LOG.info(ex.getMessage(), ex);
}
}
return usersCouldNotBeUpdated;
}
use of io.dockstore.webservice.helpers.GitHubSourceCodeRepo in project dockstore by dockstore.
the class LambdaEventResource method getLambdaEventsByOrganization.
@GET
@Timed
@UnitOfWork(readOnly = true)
@Path("/{organization}")
@Operation(operationId = "getLambdaEventsByOrganization", description = "Get all of the Lambda Events for the given GitHub organization.", security = @SecurityRequirement(name = ResourceConstants.OPENAPI_JWT_SECURITY_DEFINITION_NAME))
@ApiOperation(value = "See OpenApi for details")
public List<LambdaEvent> getLambdaEventsByOrganization(@ApiParam(hidden = true) @Parameter(hidden = true, name = "user") @Auth User user, @ApiParam(value = "organization", required = true) @PathParam("organization") String organization, @ApiParam(value = PAGINATION_OFFSET_TEXT) @QueryParam("offset") @DefaultValue("0") String offset, @ApiParam(value = PAGINATION_LIMIT_TEXT, allowableValues = "range[1,100]", defaultValue = PAGINATION_LIMIT) @DefaultValue(PAGINATION_LIMIT) @QueryParam("limit") Integer limit) {
User authUser = userDAO.findById(user.getId());
List<Token> githubToken = tokenDAO.findGithubByUserId(authUser.getId());
if (githubToken.size() == 0) {
throw new CustomWebApplicationException("You do not have GitHub connected to your account.", HttpStatus.SC_BAD_REQUEST);
}
GitHubSourceCodeRepo sourceCodeRepoInterface = (GitHubSourceCodeRepo) SourceCodeRepoFactory.createSourceCodeRepo(githubToken.get(0));
Set<String> organizations = sourceCodeRepoInterface.getMyOrganizations();
if (!organizations.contains(organization)) {
throw new CustomWebApplicationException("You do not have access to the GitHub organization '" + organization + "'", HttpStatus.SC_UNAUTHORIZED);
}
return lambdaEventDAO.findByOrganization(organization, offset, limit);
}
use of io.dockstore.webservice.helpers.GitHubSourceCodeRepo in project dockstore by dockstore.
the class TokenResource method handleGitHubUser.
private Token handleGitHubUser(User authUser, String code, boolean registerUser) {
String accessToken = GitHubHelper.getGitHubAccessToken(code, this.githubClientID, this.githubClientSecret);
String githubLogin;
Token dockstoreToken = null;
Token githubToken = null;
long gitHubId;
try {
GitHub github = new GitHubBuilder().withOAuthToken(accessToken).build();
githubLogin = github.getMyself().getLogin();
gitHubId = github.getMyself().getId();
} catch (IOException ex) {
throw new CustomWebApplicationException("Token ignored due to IOException", HttpStatus.SC_CONFLICT);
}
User user = userDAO.findByGitHubUserId(String.valueOf(gitHubId));
long userID;
if (registerUser) {
// check that there was no previous user, but by default use the github login
String username = githubLogin;
int count = 1;
while (userDAO.findByUsername(username) != null || DeletedUserHelper.nonReusableUsernameFound(username, deletedUsernameDAO)) {
username = githubLogin + count++;
}
if (user == null && authUser == null) {
User newUser = new User();
newUser.setUsername(username);
userID = userDAO.create(newUser);
userDAO.findById(userID);
} else {
throw new CustomWebApplicationException("User already exists, cannot register new user", HttpStatus.SC_FORBIDDEN);
}
} else {
if (authUser != null) {
userID = authUser.getId();
} else if (user != null) {
userID = user.getId();
} else {
throw new CustomWebApplicationException("Login failed, you may need to register an account", HttpStatus.SC_UNAUTHORIZED);
}
List<Token> tokens = tokenDAO.findDockstoreByUserId(userID);
if (!tokens.isEmpty()) {
dockstoreToken = tokens.get(0);
}
tokens = tokenDAO.findGithubByUserId(userID);
if (!tokens.isEmpty()) {
githubToken = tokens.get(0);
}
}
// check that user has accepted the latest version of the TOS and privacy policy. If not, update since acceptance for both is passively done by logging in/registering
user = userDAO.findById(userID);
acceptTOSAndPrivacyPolicy(user);
if (dockstoreToken == null) {
LOG.info("Could not find user's dockstore token. Making new one...");
dockstoreToken = createDockstoreToken(userID, user.getUsername());
}
if (githubToken == null) {
LOG.info("Could not find user's github token. Making new one...");
// CREATE GITHUB TOKEN
githubToken = new Token();
githubToken.setTokenSource(TokenType.GITHUB_COM);
githubToken.setContent(accessToken);
githubToken.setUserId(userID);
githubToken.setUsername(githubLogin);
githubToken.setOnlineProfileId(String.valueOf(gitHubId));
checkIfAccountHasBeenLinked(githubToken, TokenType.GITHUB_COM);
tokenDAO.create(githubToken);
user = userDAO.findById(userID);
GitHubSourceCodeRepo gitHubSourceCodeRepo = (GitHubSourceCodeRepo) SourceCodeRepoFactory.createSourceCodeRepo(githubToken);
gitHubSourceCodeRepo.syncUserMetadataFromGitHub(user, Optional.empty());
}
return dockstoreToken;
}
Aggregations