Skip to content

Commit b963f2b

Browse files
committed
Add GitHub compare helper on issue
1 parent 14fefb6 commit b963f2b

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/github.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,18 @@ pub struct FileDiff {
320320
pub diff: String,
321321
}
322322

323+
/// The return from GitHub compare API
324+
#[derive(Debug, serde::Deserialize)]
325+
pub struct GithubCompare {
326+
/// The base commit of the PR
327+
pub base_commit: GithubCommit,
328+
/// The merge base commit
329+
///
330+
/// See <https://git-scm.com/docs/git-merge-base> for more details
331+
pub merge_base_commit: GithubCommit,
332+
// FIXME: Also retrieve and use the files list (see our diff function)
333+
}
334+
323335
impl PullRequestDetails {
324336
pub fn new() -> PullRequestDetails {
325337
PullRequestDetails {
@@ -994,6 +1006,23 @@ impl Issue {
9941006
Ok(Some(diff))
9951007
}
9961008

1009+
/// Returns the comparison of this event.
1010+
///
1011+
/// Returns `None` if the issue is not a PR.
1012+
pub async fn compare(&self, client: &GithubClient) -> anyhow::Result<Option<GithubCompare>> {
1013+
let (before, after) = if let (Some(base), Some(head)) = (&self.base, &self.head) {
1014+
(&base.sha, &head.sha)
1015+
} else {
1016+
return Ok(None);
1017+
};
1018+
1019+
let req = client.get(&format!(
1020+
"{}/compare/{before}...{after}",
1021+
self.repository().url(client)
1022+
));
1023+
Ok(Some(client.json(req).await?))
1024+
}
1025+
9971026
/// Returns the commits from this pull request (no commits are returned if this `Issue` is not
9981027
/// a pull request).
9991028
pub async fn commits(&self, client: &GithubClient) -> anyhow::Result<Vec<GithubCommit>> {

0 commit comments

Comments
 (0)