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

Fix link in search results for non-top level jobs #67

Merged
merged 1 commit into from
Jun 30, 2023
Merged
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 @@ -2,12 +2,15 @@

import static org.jenkinsci.plugins.lucene.search.Field.*;

import hudson.model.Item;
import hudson.model.Job;
import hudson.model.Run;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import jenkins.model.Jenkins;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.apache.lucene.analysis.Analyzer;
Expand Down Expand Up @@ -76,6 +79,7 @@ public int compare(String o1, String o2) {
private final Directory index;
private final Analyzer analyzer;
private final IndexWriter dbWriter;
private final Jenkins jenkins;
private volatile ScoreDoc lastDoc;

public LuceneSearchBackend(final File indexPath) throws IOException {
Expand All @@ -84,6 +88,7 @@ public LuceneSearchBackend(final File indexPath) throws IOException {
IndexWriterConfig config = new IndexWriterConfig(analyzer);
dbWriter = new IndexWriter(index, config);
dbWriter.commit();
jenkins = Jenkins.get();
}

public static LuceneSearchBackend create(final Map<String, Object> config) {
Expand Down Expand Up @@ -182,6 +187,7 @@ private Pair<Query, Query, Boolean> parseQuery(String q, IndexSearcher searcher)
fields.contains(CONSOLE.fieldName));
}

@SuppressWarnings("rawtypes")
@Override
public List<FreeTextSearchItemImplementation> getHits(String q, boolean searchNext) {
List<FreeTextSearchItemImplementation> luceneSearchResultImpl = new ArrayList<>();
Expand Down Expand Up @@ -226,7 +232,12 @@ public List<FreeTextSearchItemImplementation> getHits(String q, boolean searchNe
String buildNumber = doc.get(BUILD_NUMBER.fieldName);
String searchName = doc.get(PROJECT_NAME.fieldName) + doc.get(BUILD_DISPLAY_NAME.fieldName);

String url = "/job/" + projectName + "/" + buildNumber + "/";
Item jobItem = jenkins.getItemByFullName(projectName);
if (!(jobItem instanceof Job)) {
throw new IllegalStateException("Unknown project type for project name: " + projectName);
}
Job job = (Job) jobItem;
String url = job.getBuildByNumber(Integer.parseInt(buildNumber)).getUrl();
luceneSearchResultImpl.add(
new FreeTextSearchItemImplementation(
searchName, projectName, bestFragments, url, isShowConsole));
Expand Down