Skip to content

Commit

Permalink
feat(refactor): created plugin version of github api queries
Browse files Browse the repository at this point in the history
  • Loading branch information
j-lanson authored and mchernicoff committed Oct 7, 2024
1 parent 8bd71a2 commit 346304a
Show file tree
Hide file tree
Showing 14 changed files with 968 additions and 10 deletions.
39 changes: 29 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ members = [
"plugins/dummy_sha256_sdk",
"sdk/rust",
"hipcheck-sdk-macros",
"plugins/github_api",
]

# Make sure Hipcheck is run with `cargo run`.
Expand Down
31 changes: 31 additions & 0 deletions plugins/github_api/Cargo.toml
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"] }
49 changes: 49 additions & 0 deletions plugins/github_api/src/code_search.rs
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)
}
32 changes: 32 additions & 0 deletions plugins/github_api/src/data.rs
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)
}
}
60 changes: 60 additions & 0 deletions plugins/github_api/src/gh_query.graphql
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,
email
}
authoredDate,
committer {
name,
email
},
committedDate,
signature {
isValid,
signature,
signer {
name
}
}
}
}
}
number,
reviews {
nodes {
databaseId
}
}
}
}
}
Loading

0 comments on commit 346304a

Please sign in to comment.