-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #818 from nicoulaj/issue-751-search-code
#751: add support for code search API
- Loading branch information
Showing
16 changed files
with
496 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
github4s/shared/src/main/scala/github4s/algebras/Search.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright 2016-2022 47 Degrees Open Source <https://www.47deg.com> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package github4s.algebras | ||
|
||
import github4s.GHResponse | ||
import github4s.domain._ | ||
|
||
trait Search[F[_]] { | ||
|
||
/** | ||
* Search code | ||
* | ||
* @param query query string | ||
* @param searchParams search parameters | ||
* @param textMatches enable text matches | ||
* @param pagination Limit and Offset for pagination | ||
* @param headers optional user headers to include in the request | ||
* @return GHResponse[SearchCodeResult] the search results | ||
*/ | ||
def searchCode( | ||
query: String, | ||
searchParams: List[SearchCodeParam], | ||
textMatches: Boolean = false, | ||
pagination: Option[Pagination] = None, | ||
headers: Map[String, String] = Map() | ||
): F[GHResponse[SearchCodeResult]] | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
github4s/shared/src/main/scala/github4s/domain/Search.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package github4s.domain | ||
|
||
final case class SearchResultTextMatch( | ||
object_url: String, | ||
object_type: Option[String], | ||
property: String, | ||
fragment: String, | ||
matches: List[SearchResultTextMatchLocation] | ||
) | ||
|
||
final case class SearchResultTextMatchLocation( | ||
text: String, | ||
indices: List[Int] | ||
) | ||
|
||
sealed abstract class ComparisonOperator(val value: String) | ||
case object LesserThan extends ComparisonOperator("<=") | ||
case object StrictlyLesserThan extends ComparisonOperator("<") | ||
case object GreaterThan extends ComparisonOperator(">=") | ||
case object StrictlyGreaterThan extends ComparisonOperator(">") |
88 changes: 88 additions & 0 deletions
88
github4s/shared/src/main/scala/github4s/domain/SearchCode.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package github4s.domain | ||
|
||
final case class SearchCodeResult( | ||
total_count: Int, | ||
incomplete_results: Boolean, | ||
items: List[SearchCodeResultItem] | ||
) | ||
|
||
final case class SearchCodeResultItem( | ||
name: String, | ||
path: String, | ||
sha: String, | ||
url: String, | ||
git_url: String, | ||
html_url: String, | ||
repository: RepositoryMinimal, | ||
score: Double, | ||
file_size: Option[Long], | ||
language: Option[String], | ||
last_modified_at: Option[String], | ||
line_numbers: Option[List[String]], | ||
text_matches: Option[List[SearchResultTextMatch]] | ||
) | ||
|
||
sealed trait SearchCodeParam { | ||
protected def paramName: String | ||
protected def paramValue: String | ||
def value: String = s"$paramName:$paramValue" | ||
} | ||
|
||
object SearchCodeParam { | ||
|
||
final case class In(values: Set[In.Value]) extends SearchCodeParam { | ||
override def paramName: String = "in" | ||
override def paramValue: String = values.map(_.value).mkString(",") | ||
} | ||
object In { | ||
sealed trait Value { | ||
def value: String | ||
} | ||
case object File extends Value { | ||
override def value: String = "file" | ||
} | ||
case object Path extends Value { | ||
override def value: String = "path" | ||
} | ||
} | ||
|
||
final case class User(name: String) extends SearchCodeParam { | ||
override def paramName: String = "user" | ||
override def paramValue: String = name | ||
} | ||
|
||
final case class Organization(name: String) extends SearchCodeParam { | ||
override def paramName: String = "org" | ||
override def paramValue: String = name | ||
} | ||
|
||
final case class Repository(owner: String, repo: String) extends SearchCodeParam { | ||
override def paramName: String = "repo" | ||
override def paramValue: String = s"$owner/$repo" | ||
} | ||
|
||
final case class Path(path: String) extends SearchCodeParam { | ||
override def paramName: String = "path" | ||
override def paramValue: String = path | ||
} | ||
|
||
final case class Language(language: String) extends SearchCodeParam { | ||
override def paramName: String = "language" | ||
override def paramValue: String = language | ||
} | ||
|
||
final case class Size(op: Option[ComparisonOperator] = None, size: Long) extends SearchCodeParam { | ||
override def paramName: String = "size" | ||
override def paramValue: String = s"${op.getOrElse("")}$size" | ||
} | ||
|
||
final case class Filename(filename: String) extends SearchCodeParam { | ||
override def paramName: String = "filename" | ||
override def paramValue: String = filename | ||
} | ||
|
||
final case class Extension(extension: String) extends SearchCodeParam { | ||
override def paramName: String = "extension" | ||
override def paramValue: String = extension | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
github4s/shared/src/main/scala/github4s/interpreters/SearchInterpreter.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright 2016-2022 47 Degrees Open Source <https://www.47deg.com> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package github4s.interpreters | ||
|
||
import github4s.Decoders._ | ||
import github4s.GHResponse | ||
import github4s.algebras.Search | ||
import github4s.domain._ | ||
import github4s.http.HttpClient | ||
|
||
class SearchInterpreter[F[_]](implicit client: HttpClient[F]) extends Search[F] { | ||
|
||
private val textMatchesHeader = "Accept" -> "application/vnd.github.text-match+json" | ||
|
||
override def searchCode( | ||
query: String, | ||
searchParams: List[SearchCodeParam] = Nil, | ||
textMatches: Boolean = false, | ||
pagination: Option[Pagination] = None, | ||
headers: Map[String, String] = Map.empty | ||
): F[GHResponse[SearchCodeResult]] = | ||
client.get[SearchCodeResult]( | ||
method = s"search/code", | ||
if (textMatches) headers + textMatchesHeader else headers, | ||
params = Map("q" -> s"$query+${searchParams.map(_.value).mkString("+")}"), | ||
pagination | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.