-
Notifications
You must be signed in to change notification settings - Fork 77
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
Create testsuite for GithubClient #1698
Open
ehuss
wants to merge
3
commits into
rust-lang:master
Choose a base branch
from
ehuss:gh-test-client
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
use crate::test_record; | ||
use anyhow::{anyhow, Context}; | ||
use async_trait::async_trait; | ||
use bytes::Bytes; | ||
|
@@ -30,15 +31,18 @@ impl GithubClient { | |
.build() | ||
.with_context(|| format!("building reqwest {}", req_dbg))?; | ||
|
||
let test_capture_info = test_record::capture_request(&req); | ||
let mut resp = self.client.execute(req.try_clone().unwrap()).await?; | ||
if let Some(sleep) = Self::needs_retry(&resp).await { | ||
resp = self.retry(req, sleep, MAX_ATTEMPTS).await?; | ||
} | ||
let status = resp.status(); | ||
let maybe_err = resp.error_for_status_ref().err(); | ||
let body = resp | ||
.bytes() | ||
.await | ||
.with_context(|| format!("failed to read response body {req_dbg}"))?; | ||
test_record::record_request(test_capture_info, status, &body); | ||
if let Some(e) = maybe_err { | ||
return Err(anyhow::Error::new(e)) | ||
.with_context(|| format!("response: {}", String::from_utf8_lossy(&body))); | ||
|
@@ -248,7 +252,7 @@ pub struct Issue { | |
pub number: u64, | ||
#[serde(deserialize_with = "opt_string")] | ||
pub body: String, | ||
created_at: chrono::DateTime<Utc>, | ||
pub created_at: chrono::DateTime<Utc>, | ||
pub updated_at: chrono::DateTime<Utc>, | ||
/// The SHA for a merge commit. | ||
/// | ||
|
@@ -264,6 +268,7 @@ pub struct Issue { | |
pub html_url: String, | ||
pub user: User, | ||
pub labels: Vec<Label>, | ||
pub milestone: Option<Milestone>, | ||
pub assignees: Vec<User>, | ||
/// Indicator if this is a pull request. | ||
/// | ||
|
@@ -330,6 +335,7 @@ impl ZulipGitHubReference { | |
|
||
#[derive(Debug, serde::Deserialize)] | ||
pub struct Comment { | ||
pub id: i64, | ||
#[serde(deserialize_with = "opt_string")] | ||
pub body: String, | ||
pub html_url: String, | ||
|
@@ -476,12 +482,22 @@ impl Issue { | |
self.state == IssueState::Open | ||
} | ||
|
||
pub async fn get_comment(&self, client: &GithubClient, id: usize) -> anyhow::Result<Comment> { | ||
pub async fn get_comment(&self, client: &GithubClient, id: u64) -> anyhow::Result<Comment> { | ||
let comment_url = format!("{}/issues/comments/{}", self.repository().url(client), id); | ||
let comment = client.json(client.get(&comment_url)).await?; | ||
Ok(comment) | ||
} | ||
|
||
pub async fn get_comments(&self, client: &GithubClient) -> anyhow::Result<Vec<Comment>> { | ||
let comment_url = format!( | ||
"{}/issues/{}/comments", | ||
self.repository().url(client), | ||
self.number | ||
); | ||
let comments = client.json(client.get(&comment_url)).await?; | ||
Ok(comments) | ||
} | ||
|
||
pub async fn edit_body(&self, client: &GithubClient, body: &str) -> anyhow::Result<()> { | ||
let edit_url = format!("{}/issues/{}", self.repository().url(client), self.number); | ||
#[derive(serde::Serialize)] | ||
|
@@ -855,8 +871,8 @@ struct MilestoneCreateBody<'a> { | |
|
||
#[derive(Debug, serde::Deserialize)] | ||
pub struct Milestone { | ||
number: u64, | ||
title: String, | ||
pub number: u64, | ||
pub title: String, | ||
} | ||
|
||
#[derive(Debug, serde::Deserialize)] | ||
|
@@ -1066,6 +1082,15 @@ impl Repository { | |
|| filters.iter().any(|&(key, _)| key == "no") | ||
|| is_pr && !include_labels.is_empty(); | ||
|
||
let set_is_pr = |issues: &mut [Issue]| { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could use a comment! |
||
if !is_pr { | ||
return; | ||
} | ||
for issue in issues { | ||
issue.pull_request = Some(PullRequestDetails {}); | ||
} | ||
}; | ||
|
||
// If there are more than `per_page` of issues, we need to paginate | ||
let mut issues = vec![]; | ||
loop { | ||
|
@@ -1085,10 +1110,11 @@ impl Repository { | |
|
||
let result = client.get(&url); | ||
if use_search_api { | ||
let result = client | ||
let mut result = client | ||
.json::<IssueSearchResult>(result) | ||
.await | ||
.with_context(|| format!("failed to list issues from {}", url))?; | ||
set_is_pr(&mut result.items); | ||
issues.extend(result.items); | ||
if issues.len() < result.total_count { | ||
ordering.page += 1; | ||
|
@@ -1099,7 +1125,8 @@ impl Repository { | |
issues = client | ||
.json(result) | ||
.await | ||
.with_context(|| format!("failed to list issues from {}", url))? | ||
.with_context(|| format!("failed to list issues from {}", url))?; | ||
set_is_pr(&mut issues); | ||
} | ||
|
||
break; | ||
|
@@ -1562,6 +1589,16 @@ impl Repository { | |
})?; | ||
Ok(()) | ||
} | ||
|
||
pub async fn get_pr(&self, client: &GithubClient, number: u64) -> anyhow::Result<Issue> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this used anywhere? |
||
let url = format!("{}/pulls/{number}", self.url(client)); | ||
let mut pr: Issue = client | ||
.json(client.get(&url)) | ||
.await | ||
.with_context(|| format!("{} failed to get pr {number}", self.full_name))?; | ||
pr.pull_request = Some(PullRequestDetails {}); | ||
Ok(pr) | ||
} | ||
} | ||
|
||
pub struct Query<'a> { | ||
|
@@ -1863,12 +1900,14 @@ impl GithubClient { | |
let req = req | ||
.build() | ||
.with_context(|| format!("failed to build request {:?}", req_dbg))?; | ||
let test_capture_info = test_record::capture_request(&req); | ||
jackh726 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let resp = self.client.execute(req).await.context(req_dbg.clone())?; | ||
let status = resp.status(); | ||
let body = resp | ||
.bytes() | ||
.await | ||
.with_context(|| format!("failed to read response body {req_dbg}"))?; | ||
test_record::record_request(test_capture_info, status, &body); | ||
match status { | ||
StatusCode::OK => Ok(Some(body)), | ||
StatusCode::NOT_FOUND => Ok(None), | ||
|
@@ -2021,6 +2060,7 @@ pub struct GitTreeEntry { | |
pub sha: String, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct RecentCommit { | ||
pub title: String, | ||
pub pr_num: Option<i32>, | ||
|
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ehuss by reading this README snippet I don't easily understand at a glance how the testsuite is meant to be used and maintained. Is the documentation actually meant to be in
tests/github_client/mod.rs
as rustdoc's comments?A few questions ahead. Sorry for the perhaps vague comments 🙂, I'm trying to squint and understand these changes and their mainteinance cost for future other contributors:
What is needed to test a new handler under
./src/handlers
(which is something I'm currently doing)? I assume little to nothing if the new handler is just using the same Github API already tested, right?(echoing the comment from Mark) What is needed to do to update tests? Say, we change a little something in how we interpret Github responses in some structs under
./src/github.rs
?is there a way to regenerate these JSON files automatically?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not quite sure I understand the question. The documentation in README.md is intended as a high-level overview of testing of triagebot, and includes a link to
tests/github_client/mod.rs
to read more details of that specific test suite. If and when more suites are added, they could be linked here, too.Right, handler testing isn't part of this PR. Testing of handlers is implemented in #1678, but I'm finding them to be even harder to wield than these tests, so I broke out just the github side of things to try to make some progress.
As mentioned above. The general process is to point the test at one of your personal repos, and re-record the live data.
Also mentioned above. I can take a look at making it easier to regenerate them.