-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(refactor): created plugin version of github api queries
- Loading branch information
1 parent
8bd71a2
commit 346304a
Showing
14 changed files
with
968 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
[package] | ||
name = "github_api" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[dependencies] | ||
anyhow = "1.0.89" | ||
clap = { version = "4.5.19", features = ["derive"] } | ||
graphql_client = "0.14.0" | ||
hipcheck-sdk = { version = "0.1.0", path = "../../sdk/rust", features = ["macros"] } | ||
log = "0.4.22" | ||
# Exactly matching the version of rustls used by ureq | ||
# Get rid of default features since we don't use the AWS backed crypto | ||
# provider (we use ring) and it breaks stuff on windows. | ||
rustls = { version = "0.23.10", default-features = false, features = [ | ||
"logging", | ||
"std", | ||
"tls12", | ||
"ring", | ||
] } | ||
rustls-native-certs = "0.8.0" | ||
schemars = { version = "0.8.21", features = ["url"] } | ||
serde = "1.0.210" | ||
serde_json = "1.0.128" | ||
tokio = { version = "1.40.0", features = ["rt"] } | ||
ureq = { version = "2.10.1", default-features = false, features = [ | ||
"json", | ||
"tls", | ||
] } | ||
url = { version = "2.5.2", features = ["serde"] } |
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,49 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use crate::util::authenticated_agent::AuthenticatedAgent; | ||
use anyhow::{anyhow, Result}; | ||
use serde_json::Value; | ||
|
||
const GH_API_V4_SEARCH: &str = "https://api.github.com/search/code"; | ||
|
||
/// Make a request to the GitHub Code Search API. | ||
pub fn search_code_request( | ||
agent: &AuthenticatedAgent<'_>, | ||
repo: impl AsRef<String>, | ||
) -> Result<bool> { | ||
// Example query will look like this: | ||
// https://api.github.com/search/code?q=github.com%20assimp%20assimp+in:file+filename:project.yaml+repo:google/oss-fuzz | ||
// | ||
// Logic based on these docs: | ||
// https://docs.github.com/en/search-github/searching-on-github/searching-code#considerations-for-code-search | ||
// | ||
// Breaking repo out in to more easily searchable string since full | ||
// GitHub repo urls were not working for a few fuzzed urls. | ||
|
||
let repo_query = repo | ||
.as_ref() | ||
.replace("https://", "") | ||
.replace("http://", "") | ||
.replace('/', "%20"); | ||
|
||
let sub_query = format!( | ||
"{}+in:file+filename:project.yaml+repo:google/oss-fuzz", | ||
repo_query | ||
); | ||
|
||
let query = format!("{}?q={}", GH_API_V4_SEARCH.to_owned(), sub_query); | ||
|
||
// Make the get request. | ||
let json = get_request(agent, query).map_err(|_| anyhow!("unable to query fuzzing info"))?; | ||
|
||
match &json["total_count"].to_string().parse::<u64>() { | ||
Ok(count) => Ok(count > &0), | ||
_ => Err(anyhow!("unable to get fuzzing status")), | ||
} | ||
} | ||
|
||
/// Get call using agent | ||
fn get_request(agent: &AuthenticatedAgent<'_>, query: String) -> Result<Value> { | ||
let response = agent.get(&query).call()?.into_json()?; | ||
Ok(response) | ||
} |
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,32 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use crate::{ | ||
code_search::search_code_request, graphql::get_all_reviews, types::GitHubPullRequest, | ||
util::authenticated_agent::AuthenticatedAgent, | ||
}; | ||
use anyhow::{Context, Result}; | ||
use std::rc::Rc; | ||
|
||
pub struct GitHub<'a> { | ||
owner: &'a str, | ||
repo: &'a str, | ||
agent: AuthenticatedAgent<'a>, | ||
} | ||
|
||
impl<'a> GitHub<'a> { | ||
pub fn new(owner: &'a str, repo: &'a str, token: &'a str) -> Result<GitHub<'a>> { | ||
Ok(GitHub { | ||
owner, | ||
repo, | ||
agent: AuthenticatedAgent::new(token), | ||
}) | ||
} | ||
|
||
pub fn fuzz_check(&self, repo_uri: Rc<String>) -> Result<bool> { | ||
search_code_request(&self.agent, repo_uri).context("unable to search fuzzing information; please check the HC_GITHUB_TOKEN system environment variable") | ||
} | ||
|
||
pub fn get_reviews_for_pr(&self) -> Result<Vec<GitHubPullRequest>> { | ||
get_all_reviews(&self.agent, self.owner, self.repo) | ||
} | ||
} |
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,60 @@ | ||
|
||
query Reviews($owner:String!, $repo:String!, $cursor:String) { | ||
repository(owner: $owner, name: $repo) { | ||
pullRequests(first: 100, after: $cursor, states: MERGED) { | ||
pageInfo { | ||
hasNextPage, | ||
endCursor | ||
}, | ||
nodes { | ||
number, | ||
reviews(first: 100, states: APPROVED) { | ||
nodes { | ||
databaseId | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
query Review($owner:String!, $repo:String!, $number:Int!, $cursor:String) { | ||
repository(owner: $owner, name: $repo) { | ||
pullRequest(number: $number) { | ||
commits(first: 100, after: $cursor) { | ||
pageInfo { | ||
hasNextPage, | ||
endCursor | ||
} | ||
nodes { | ||
commit { | ||
oid, | ||
author { | ||
name, | ||
} | ||
authoredDate, | ||
committer { | ||
name, | ||
}, | ||
committedDate, | ||
signature { | ||
isValid, | ||
signature, | ||
signer { | ||
name | ||
} | ||
} | ||
} | ||
} | ||
} | ||
number, | ||
reviews { | ||
nodes { | ||
databaseId | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.