Search in sources :

Example 1 with GitHubSourceCodeRepo

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;
}
Also used : ArrayList(java.util.ArrayList) YamlWorkflow(io.dockstore.common.yaml.YamlWorkflow) BioWorkflow(io.dockstore.webservice.core.BioWorkflow) Workflow(io.dockstore.webservice.core.Workflow) CustomWebApplicationException(io.dockstore.webservice.CustomWebApplicationException) YamlAuthor(io.dockstore.common.yaml.YamlAuthor) LambdaEvent(io.dockstore.webservice.core.LambdaEvent) GitHubSourceCodeRepo(io.dockstore.webservice.helpers.GitHubSourceCodeRepo) DescriptorLanguageSubclass(io.dockstore.common.DescriptorLanguageSubclass)

Example 2 with GitHubSourceCodeRepo

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);
// });
}
Also used : WorkflowVersion(io.dockstore.webservice.core.WorkflowVersion) WorkflowDAO(io.dockstore.webservice.jdbi.WorkflowDAO) LoggerFactory(org.slf4j.LoggerFactory) CustomWebApplicationException(io.dockstore.webservice.CustomWebApplicationException) HttpStatus(org.apache.http.HttpStatus) DockstoreWebserviceConfiguration(io.dockstore.webservice.DockstoreWebserviceConfiguration) SourceFile(io.dockstore.webservice.core.SourceFile) STUB(io.dockstore.webservice.core.WorkflowMode.STUB) Transaction(org.hibernate.Transaction) SourceCodeRepoFactory(io.dockstore.webservice.helpers.SourceCodeRepoFactory) YamlWorkflow(io.dockstore.common.yaml.YamlWorkflow) Duration(java.time.Duration) Map(java.util.Map) TokenType(io.dockstore.webservice.core.TokenType) CacheConfigManager(io.dockstore.webservice.helpers.CacheConfigManager) User(io.dockstore.webservice.core.User) Path(java.nio.file.Path) WorkflowVersionDAO(io.dockstore.webservice.jdbi.WorkflowVersionDAO) SKIP_COMMIT_ID(io.dockstore.webservice.Constants.SKIP_COMMIT_ID) Service(io.dockstore.webservice.core.Service) GitHubHelper(io.dockstore.webservice.helpers.GitHubHelper) LAMBDA_FAILURE(io.dockstore.webservice.Constants.LAMBDA_FAILURE) Collection(java.util.Collection) YamlAuthor(io.dockstore.common.yaml.YamlAuthor) SessionFactory(org.hibernate.SessionFactory) FULL(io.dockstore.webservice.core.WorkflowMode.FULL) Set(java.util.Set) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) DOCKSTORE_YML_PATH(io.dockstore.webservice.Constants.DOCKSTORE_YML_PATH) Objects(java.util.Objects) List(java.util.List) BioWorkflow(io.dockstore.webservice.core.BioWorkflow) DOCKSTORE_YML(io.dockstore.webservice.core.WorkflowMode.DOCKSTORE_YML) Optional(java.util.Optional) GHRateLimit(org.kohsuke.github.GHRateLimit) LambdaEvent(io.dockstore.webservice.core.LambdaEvent) Validation(io.dockstore.webservice.core.Validation) WorkflowMode(io.dockstore.webservice.core.WorkflowMode) HashMap(java.util.HashMap) PublicStateManager(io.dockstore.webservice.helpers.PublicStateManager) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Author(io.dockstore.webservice.core.Author) OrcidAuthor(io.dockstore.webservice.core.OrcidAuthor) HttpClient(org.apache.http.client.HttpClient) FileFormatDAO(io.dockstore.webservice.jdbi.FileFormatDAO) Checksum(io.dockstore.webservice.core.Checksum) EventDAO(io.dockstore.webservice.jdbi.EventDAO) Api(io.swagger.annotations.Api) Token(io.dockstore.webservice.core.Token) Workflow(io.dockstore.webservice.core.Workflow) UserDAO(io.dockstore.webservice.jdbi.UserDAO) FileDAO(io.dockstore.webservice.jdbi.FileDAO) DockstoreYaml12(io.dockstore.common.yaml.DockstoreYaml12) DockstoreYamlHelper(io.dockstore.common.yaml.DockstoreYamlHelper) GitHelper(io.dockstore.webservice.helpers.GitHelper) FileFormatHelper(io.dockstore.webservice.helpers.FileFormatHelper) Logger(org.slf4j.Logger) DescriptorLanguageSubclass(io.dockstore.common.DescriptorLanguageSubclass) IOException(java.io.IOException) SourceCodeRepoInterface(io.dockstore.webservice.helpers.SourceCodeRepoInterface) StateManagerMode(io.dockstore.webservice.helpers.StateManagerMode) Version(io.dockstore.webservice.core.Version) LambdaEventDAO(io.dockstore.webservice.jdbi.LambdaEventDAO) GitHubSourceCodeRepo(io.dockstore.webservice.helpers.GitHubSourceCodeRepo) TokenDAO(io.dockstore.webservice.jdbi.TokenDAO) Comparator(java.util.Comparator) Service12(io.dockstore.common.yaml.Service12) YamlWorkflow(io.dockstore.common.yaml.YamlWorkflow) BioWorkflow(io.dockstore.webservice.core.BioWorkflow) Workflow(io.dockstore.webservice.core.Workflow) GitHubSourceCodeRepo(io.dockstore.webservice.helpers.GitHubSourceCodeRepo)

Example 3 with GitHubSourceCodeRepo

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;
}
Also used : User(io.dockstore.webservice.core.User) OrganizationUser(io.dockstore.webservice.core.OrganizationUser) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) Token(io.dockstore.webservice.core.Token) CustomWebApplicationException(io.dockstore.webservice.CustomWebApplicationException) GitHubSourceCodeRepo(io.dockstore.webservice.helpers.GitHubSourceCodeRepo) Path(javax.ws.rs.Path) UnitOfWork(io.dropwizard.hibernate.UnitOfWork) RolesAllowed(javax.annotation.security.RolesAllowed) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) Operation(io.swagger.v3.oas.annotations.Operation) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponse(io.swagger.v3.oas.annotations.responses.ApiResponse)

Example 4 with GitHubSourceCodeRepo

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);
}
Also used : User(io.dockstore.webservice.core.User) Token(io.dockstore.webservice.core.Token) CustomWebApplicationException(io.dockstore.webservice.CustomWebApplicationException) GitHubSourceCodeRepo(io.dockstore.webservice.helpers.GitHubSourceCodeRepo) Path(javax.ws.rs.Path) UnitOfWork(io.dropwizard.hibernate.UnitOfWork) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiOperation(io.swagger.annotations.ApiOperation) Operation(io.swagger.v3.oas.annotations.Operation)

Example 5 with GitHubSourceCodeRepo

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;
}
Also used : User(io.dockstore.webservice.core.User) GitHub(org.kohsuke.github.GitHub) Token(io.dockstore.webservice.core.Token) BearerToken(com.google.api.client.auth.oauth2.BearerToken) GitHubBuilder(org.kohsuke.github.GitHubBuilder) CustomWebApplicationException(io.dockstore.webservice.CustomWebApplicationException) IOException(java.io.IOException) GitHubSourceCodeRepo(io.dockstore.webservice.helpers.GitHubSourceCodeRepo)

Aggregations

GitHubSourceCodeRepo (io.dockstore.webservice.helpers.GitHubSourceCodeRepo)8 CustomWebApplicationException (io.dockstore.webservice.CustomWebApplicationException)7 User (io.dockstore.webservice.core.User)5 LambdaEvent (io.dockstore.webservice.core.LambdaEvent)4 Token (io.dockstore.webservice.core.Token)4 ArrayList (java.util.ArrayList)4 YamlAuthor (io.dockstore.common.yaml.YamlAuthor)3 YamlWorkflow (io.dockstore.common.yaml.YamlWorkflow)3 BioWorkflow (io.dockstore.webservice.core.BioWorkflow)3 Workflow (io.dockstore.webservice.core.Workflow)3 Timed (com.codahale.metrics.annotation.Timed)2 DescriptorLanguageSubclass (io.dockstore.common.DescriptorLanguageSubclass)2 DockstoreYaml12 (io.dockstore.common.yaml.DockstoreYaml12)2 SourceFile (io.dockstore.webservice.core.SourceFile)2 UnitOfWork (io.dropwizard.hibernate.UnitOfWork)2 ApiOperation (io.swagger.annotations.ApiOperation)2 Operation (io.swagger.v3.oas.annotations.Operation)2 IOException (java.io.IOException)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2