Search in sources :

Example 36 with RepoResult

use of com.searchcode.app.model.RepoResult in project searchcode-server by boyter.

the class Repo method getRepoByName.

@Override
public synchronized RepoResult getRepoByName(String repositoryName) {
    if (repositoryName == null) {
        return null;
    }
    RepoResult result = null;
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    try {
        connection = this.dbConfig.getConnection();
        preparedStatement = connection.prepareStatement("select rowid,name,scm,url,username,password,source,branch,data from repo where name=?;");
        preparedStatement.setString(1, repositoryName);
        resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            int rowId = resultSet.getInt("rowid");
            String repoName = resultSet.getString("name");
            String repoScm = resultSet.getString("scm");
            String repoUrl = resultSet.getString("url");
            String repoUsername = resultSet.getString("username");
            String repoPassword = resultSet.getString("password");
            String repoSource = resultSet.getString("source");
            String repoBranch = resultSet.getString("branch");
            String repoData = resultSet.getString("data");
            result = new RepoResult(rowId, repoName, repoScm, repoUrl, repoUsername, repoPassword, repoSource, repoBranch, repoData);
        }
    } catch (SQLException ex) {
        Singleton.getLogger().severe(" caught a " + ex.getClass() + "\n with message: " + ex.getMessage());
    } finally {
        Singleton.getHelpers().closeQuietly(resultSet);
        Singleton.getHelpers().closeQuietly(preparedStatement);
    }
    return result;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) RepoResult(com.searchcode.app.model.RepoResult)

Example 37 with RepoResult

use of com.searchcode.app.model.RepoResult in project searchcode-server by boyter.

the class Repo method getAllRepo.

@Override
public synchronized List<RepoResult> getAllRepo() {
    List<RepoResult> repoResults = new ArrayList<>();
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    try {
        connection = this.dbConfig.getConnection();
        preparedStatement = connection.prepareStatement("select rowid,name,scm,url,username,password,source,branch,data from repo order by rowid desc;");
        resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            int rowId = resultSet.getInt("rowid");
            String repoName = resultSet.getString("name");
            String repoScm = resultSet.getString("scm");
            String repoUrl = resultSet.getString("url");
            String repoUsername = resultSet.getString("username");
            String repoPassword = resultSet.getString("password");
            String repoSource = resultSet.getString("source");
            String repoBranch = resultSet.getString("branch");
            String repoData = resultSet.getString("data");
            repoResults.add(new RepoResult(rowId, repoName, repoScm, repoUrl, repoUsername, repoPassword, repoSource, repoBranch, repoData));
        }
    } catch (SQLException ex) {
        Singleton.getLogger().severe(" caught a " + ex.getClass() + "\n with message: " + ex.getMessage());
    } finally {
        Singleton.getHelpers().closeQuietly(resultSet);
        Singleton.getHelpers().closeQuietly(preparedStatement);
    }
    return repoResults;
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) RepoResult(com.searchcode.app.model.RepoResult)

Example 38 with RepoResult

use of com.searchcode.app.model.RepoResult in project searchcode-server by boyter.

the class Repo method saveRepo.

// TODO add retry logic here as this can fail and as such should just trigger again
@Override
public synchronized boolean saveRepo(RepoResult repoResult) {
    RepoResult existing = this.getRepoByName(repoResult.getName());
    boolean isNew = false;
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    // Update with new details
    try {
        connection = this.dbConfig.getConnection();
        if (existing != null) {
            preparedStatement = connection.prepareStatement("UPDATE \"repo\" SET \"name\" = ?, \"scm\" = ?, \"url\" = ?, \"username\" = ?, \"password\" = ?, \"source\" = ?, \"branch\" = ?, \"data\" = ? WHERE  \"name\" = ?");
            preparedStatement.setString(9, repoResult.getName());
        } else {
            isNew = true;
            preparedStatement = connection.prepareStatement("INSERT INTO repo(\"name\",\"scm\",\"url\", \"username\", \"password\",\"source\",\"branch\",\"data\") VALUES (?,?,?,?,?,?,?,?)");
        }
        preparedStatement.setString(1, repoResult.getName());
        preparedStatement.setString(2, repoResult.getScm());
        preparedStatement.setString(3, repoResult.getUrl());
        preparedStatement.setString(4, repoResult.getUsername());
        preparedStatement.setString(5, repoResult.getPassword());
        preparedStatement.setString(6, repoResult.getSource());
        preparedStatement.setString(7, repoResult.getBranch());
        preparedStatement.setString(8, repoResult.getDataAsJson());
        preparedStatement.execute();
    } catch (SQLException ex) {
        Singleton.getLogger().severe(" caught a " + ex.getClass() + "\n with message: " + ex.getMessage());
    } finally {
        Singleton.getHelpers().closeQuietly(preparedStatement);
    }
    return isNew;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) RepoResult(com.searchcode.app.model.RepoResult)

Example 39 with RepoResult

use of com.searchcode.app.model.RepoResult in project searchcode-server by boyter.

the class ApiRouteService method repoDelete.

public ApiResponse repoDelete(Request request, Response response) {
    if (!apiEnabled) {
        return new ApiResponse(false, "API not enabled");
    }
    String publicKey = request.queryParams("pub");
    String signedKey = request.queryParams("sig");
    String reponames = request.queryParams("reponame");
    String hmacTypeString = request.queryParams("hmac");
    hmacTypeString = hmacTypeString == null ? Values.EMPTYSTRING : hmacTypeString;
    if (reponames == null || reponames.trim().equals(Values.EMPTYSTRING)) {
        return new ApiResponse(false, "reponame is a required parameter");
    }
    if (apiAuth) {
        if (publicKey == null || publicKey.trim().equals(Values.EMPTYSTRING)) {
            return new ApiResponse(false, "pub is a required parameter");
        }
        if (signedKey == null || signedKey.trim().equals(Values.EMPTYSTRING)) {
            return new ApiResponse(false, "sig is a required parameter");
        }
        String toValidate = String.format("pub=%s&reponame=%s", URLEncoder.encode(publicKey), URLEncoder.encode(reponames));
        ApiService.HmacType hmacType = hmacTypeString.toLowerCase().equals("sha512") ? ApiService.HmacType.SHA512 : ApiService.HmacType.SHA1;
        boolean validRequest = apiService.validateRequest(publicKey, signedKey, toValidate, hmacType);
        if (!validRequest) {
            Singleton.getLogger().apiLog("Invalid signed repoDelete API call using publicKey=" + publicKey);
            return new ApiResponse(false, "invalid signed url");
        }
    }
    RepoResult rr = this.repo.getRepoByName(reponames);
    if (rr == null) {
        return new ApiResponse(false, "repository already deleted");
    }
    this.dataService.addToPersistentDelete(rr.getName());
    Singleton.getLogger().apiLog("Valid signed repoDelete API call using publicKey=" + publicKey);
    return new ApiResponse(true, "repository queued for deletion");
}
Also used : ApiResponse(com.searchcode.app.dto.api.ApiResponse) RepoResultApiResponse(com.searchcode.app.dto.api.RepoResultApiResponse) RepoResult(com.searchcode.app.model.RepoResult)

Example 40 with RepoResult

use of com.searchcode.app.model.RepoResult in project searchcode-server by boyter.

the class CodeRouteService method getCode.

public Map<String, Object> getCode(Request request, Response response) {
    Map<String, Object> map = new HashMap<>();
    Repo repo = Singleton.getRepo();
    Data data = Singleton.getData();
    SearchcodeLib scl = Singleton.getSearchcodeLib(data);
    OWASPClassifier owaspClassifier = new OWASPClassifier();
    Cocomo2 coco = new Cocomo2();
    String codeId = request.params(":codeid");
    CodeResult codeResult = this.codeSearcher.getByCodeId(codeId);
    if (codeResult == null) {
        response.redirect("/404/");
        halt();
    }
    List<String> codeLines = codeResult.code;
    StringBuilder code = new StringBuilder();
    StringBuilder lineNos = new StringBuilder();
    String padStr = "";
    for (int total = codeLines.size() / 10; total > 0; total = total / 10) {
        padStr += " ";
    }
    for (int i = 1, d = 10, len = codeLines.size(); i <= len; i++) {
        if (i / d > 0) {
            d *= 10;
            // Del last char
            padStr = padStr.substring(0, padStr.length() - 1);
        }
        code.append("<span id=\"").append(i).append("\"></span>").append(StringEscapeUtils.escapeHtml4(codeLines.get(i - 1))).append("\n");
        lineNos.append(padStr).append("<a href=\"#").append(i).append("\">").append(i).append("</a>").append("\n");
    }
    List<OWASPMatchingResult> owaspResults = new ArrayList<OWASPMatchingResult>();
    if (CommonRouteService.owaspAdvisoriesEnabled()) {
        if (!codeResult.languageName.equals("Text") && !codeResult.languageName.equals("Unknown")) {
            owaspResults = owaspClassifier.classifyCode(codeLines, codeResult.languageName);
        }
    }
    int limit = Integer.parseInt(Properties.getProperties().getProperty(Values.HIGHLIGHT_LINE_LIMIT, Values.DEFAULT_HIGHLIGHT_LINE_LIMIT));
    boolean highlight = Singleton.getHelpers().tryParseInt(codeResult.codeLines, "0") <= limit;
    RepoResult repoResult = repo.getRepoByName(codeResult.repoName);
    if (repoResult != null) {
        map.put("source", repoResult.getSource());
    }
    map.put("fileName", codeResult.fileName);
    // TODO fix this properly code path includes the repo name and should be removed
    String codePath = codeResult.codePath;
    if (codeResult.codePath.contains("/")) {
        codePath = codeResult.codePath.substring(codeResult.codePath.indexOf('/'), codeResult.codePath.length());
    }
    if (!codePath.startsWith("/")) {
        codePath = "/" + codePath;
    }
    map.put("codePath", codePath);
    map.put("codeLength", codeResult.codeLines);
    map.put("linenos", lineNos.toString());
    map.put("languageName", codeResult.languageName);
    map.put("md5Hash", codeResult.md5hash);
    map.put("repoName", codeResult.repoName);
    map.put("highlight", highlight);
    map.put("repoLocation", codeResult.getRepoLocation());
    map.put("codeValue", code.toString());
    map.put("highligher", CommonRouteService.getSyntaxHighlighter());
    map.put("codeOwner", codeResult.getCodeOwner());
    map.put("owaspResults", owaspResults);
    double estimatedEffort = coco.estimateEffort(scl.countFilteredLines(codeResult.getCode()));
    int estimatedCost = (int) coco.estimateCost(estimatedEffort, CommonRouteService.getAverageSalary());
    if (estimatedCost != 0 && !scl.languageCostIgnore(codeResult.getLanguageName())) {
        map.put("estimatedCost", estimatedCost);
    }
    map.put("logoImage", CommonRouteService.getLogo());
    map.put("isCommunity", App.ISCOMMUNITY);
    map.put(Values.EMBED, Singleton.getData().getDataByName(Values.EMBED, Values.EMPTYSTRING));
    return map;
}
Also used : Data(com.searchcode.app.dao.Data) Repo(com.searchcode.app.dao.Repo) RepoResult(com.searchcode.app.model.RepoResult)

Aggregations

RepoResult (com.searchcode.app.model.RepoResult)59 Repo (com.searchcode.app.dao.Repo)13 ApiResponse (com.searchcode.app.dto.api.ApiResponse)9 RepoResultApiResponse (com.searchcode.app.dto.api.RepoResultApiResponse)9 Request (spark.Request)9 ValidatorResult (com.searchcode.app.model.ValidatorResult)8 ApiRouteService (com.searchcode.app.service.route.ApiRouteService)8 UniqueRepoQueue (com.searchcode.app.util.UniqueRepoQueue)8 Connection (java.sql.Connection)5 PreparedStatement (java.sql.PreparedStatement)5 SQLException (java.sql.SQLException)5 ResultSet (java.sql.ResultSet)4 ArrayList (java.util.ArrayList)3 RepositoryChanged (com.searchcode.app.dto.RepositoryChanged)2 RunningIndexJob (com.searchcode.app.dto.RunningIndexJob)2 File (java.io.File)2 Gson (com.google.gson.Gson)1 Data (com.searchcode.app.dao.Data)1 IndexBaseRepoJob (com.searchcode.app.jobs.repository.IndexBaseRepoJob)1 IndexFileRepoJob (com.searchcode.app.jobs.repository.IndexFileRepoJob)1