#![cfg_attr(
not(any(
feature = "github",
feature = "gitlab",
feature = "jira",
feature = "bitbucket",
feature = "azure-devops",
feature = "trello",
feature = "gitea",
feature = "linear",
feature = "youtrack",
feature = "redmine",
feature = "gerrit"
)),
allow(dead_code, unused_variables)
)]
use std::sync::Arc;
#[cfg(any(feature = "github", feature = "gitlab"))]
use serde::de::DeserializeOwned;
#[cfg(feature = "bitbucket")]
use crate::bitbucket::BitbucketClient;
use crate::conversation::{
Comment, ConversationCaps, ItemKey, PageCursor, PostingIdentity, TimelineOpts, TimelinePage,
};
#[cfg(feature = "github")]
use crate::github::GitHubClient;
#[cfg(feature = "gitlab")]
use crate::gitlab::GitLabClient;
use crate::http::HttpTransport;
#[cfg(any(feature = "github", feature = "gitlab"))]
use crate::http::{HttpRequest, HttpResponse};
use crate::integration::Integration;
use crate::issue_flow::{CreatedBranch, IssueLinks, NewIssueBranch};
use crate::metadata::{HostedLabel, HostedMilestone, IssueEdit, ItemMeta, MetaKind, PrEdit};
use crate::model::{
HostedUser, IssueCreateOptions, IssueFilter, IssueSummary, PrFilter, PullRequestSummary,
TokenCheck,
};
use crate::project::{HostedProject, HostedSshKey};
use crate::review::{
PullRequestDetail, ReviewCapabilities, ReviewComment, ReviewFile, ReviewOutcome,
ReviewSubmission, ReviewThread,
};
use crate::spec::{spec_for, CheckoutSource};
use crate::{Error, Result};
#[cfg(any(
feature = "github",
feature = "gitlab",
feature = "jira",
feature = "bitbucket",
feature = "azure-devops",
feature = "trello",
feature = "gitea",
feature = "linear",
feature = "youtrack",
feature = "redmine",
feature = "gerrit",
feature = "local-issues"
))]
use gitpoulpi_config::ApiFamily;
#[cfg(any(feature = "github", feature = "gitlab", feature = "azure-devops"))]
pub(crate) const PAGE_SIZE: u32 = 100;
#[cfg(any(
feature = "github",
feature = "gitlab",
feature = "jira",
feature = "bitbucket",
feature = "azure-devops",
feature = "gitea",
feature = "linear",
feature = "youtrack",
feature = "redmine",
feature = "gerrit"
))]
pub(crate) const MAX_PAGES: u32 = 50;
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct NewPullRequest {
pub project: String,
pub source_branch: String,
pub target_branch: String,
pub title: String,
pub body: String,
pub draft: bool,
pub source_project: Option<String>,
pub assignees: Vec<String>,
pub reviewers: Vec<String>,
pub labels: Vec<String>,
pub milestone: Option<u64>,
}
impl NewPullRequest {
pub fn fork_source(&self) -> Option<&str> {
self.source_project
.as_deref()
.filter(|s| !s.is_empty() && !s.eq_ignore_ascii_case(&self.project))
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize)]
pub struct CreatedPullRequest {
pub number: u64,
pub web_url: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub warning: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct NewIssue {
pub project: String,
pub title: String,
pub body: String,
pub labels: Vec<String>,
pub issue_type: Option<String>,
pub list_id: Option<String>,
pub assignees: Vec<String>,
pub milestone: Option<u64>,
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize)]
pub struct CreatedIssue {
pub number: u64,
pub web_url: String,
pub reference: String,
}
#[async_trait::async_trait]
pub trait HostingClient: Send + Sync {
fn integration(&self) -> &Integration;
fn unsupported(&self, what: &str) -> Error {
Error::Unsupported(what.to_string())
}
async fn list_projects(&self) -> Result<Vec<HostedProject>>;
async fn list_ssh_keys(&self) -> Result<Vec<HostedSshKey>>;
async fn upload_ssh_key(&self, title: &str, public_key: &str) -> Result<HostedSshKey>;
async fn create_pull_request(&self, req: &NewPullRequest) -> Result<CreatedPullRequest>;
async fn current_user(&self) -> Result<HostedUser> {
Err(self.unsupported("current_user"))
}
async fn protected_branches(
&self,
_project: &str,
) -> Result<crate::protection::ProtectedBranches> {
Err(self.unsupported("protected_branches"))
}
async fn check_token(&self) -> Result<TokenCheck> {
Err(self.unsupported("check_token"))
}
async fn list_pull_requests(
&self,
_project: &str,
_filter: &PrFilter,
) -> Result<Vec<PullRequestSummary>> {
Err(self.unsupported("list_pull_requests"))
}
async fn pull_request_for_branch(
&self,
_project: &str,
_branch: &str,
) -> Result<Option<PullRequestSummary>> {
Err(self.unsupported("pull_request_for_branch"))
}
async fn list_issues(
&self,
_project: &str,
_filter: &IssueFilter,
) -> Result<Vec<IssueSummary>> {
Err(self.unsupported("list_issues"))
}
async fn create_issue(&self, _req: &NewIssue) -> Result<CreatedIssue> {
Err(self.unsupported("create_issue"))
}
async fn issue_create_options(&self, _project: &str) -> Result<IssueCreateOptions> {
Ok(IssueCreateOptions::default())
}
async fn delete_ssh_key(&self, _id: &str) -> Result<()> {
Err(self.unsupported("delete_ssh_key"))
}
fn review_capabilities(&self) -> ReviewCapabilities {
ReviewCapabilities::default()
}
async fn pull_request_detail(&self, _project: &str, _number: u64) -> Result<PullRequestDetail> {
Err(self.unsupported("pull_request_detail"))
}
async fn pull_request_detail_at(
&self,
project: &str,
number: u64,
_patch_set: Option<u32>,
) -> Result<PullRequestDetail> {
self.pull_request_detail(project, number).await
}
async fn pull_request_files_at(
&self,
project: &str,
number: u64,
_patch_set: Option<u32>,
) -> Result<Vec<ReviewFile>> {
self.pull_request_files(project, number).await
}
async fn pull_request_files_against(
&self,
project: &str,
number: u64,
patch_set: Option<u32>,
_base_patch_set: Option<u32>,
) -> Result<Vec<ReviewFile>> {
self.pull_request_files_at(project, number, patch_set).await
}
async fn submit_change(&self, _project: &str, _number: u64) -> Result<()> {
Err(self.unsupported("submit_change"))
}
async fn abandon_change(
&self,
_project: &str,
_number: u64,
_message: Option<&str>,
) -> Result<()> {
Err(self.unsupported("abandon_change"))
}
async fn pull_request_threads_at(
&self,
project: &str,
number: u64,
_patch_set: Option<u32>,
) -> Result<Vec<ReviewThread>> {
self.pull_request_threads(project, number).await
}
async fn pull_request_files(&self, _project: &str, _number: u64) -> Result<Vec<ReviewFile>> {
Err(self.unsupported("pull_request_files"))
}
async fn pull_request_threads(
&self,
_project: &str,
_number: u64,
) -> Result<Vec<ReviewThread>> {
Err(self.unsupported("pull_request_threads"))
}
async fn pull_request_conversation(
&self,
_project: &str,
_number: u64,
) -> Result<Vec<ReviewComment>> {
Err(self.unsupported("pull_request_conversation"))
}
async fn pull_request_reviews(
&self,
_project: &str,
_number: u64,
) -> Result<Vec<crate::review::SubmittedReview>> {
Err(self.unsupported("pull_request_reviews"))
}
async fn add_comment(
&self,
_project: &str,
_number: u64,
_body: &str,
) -> Result<ReviewComment> {
Err(self.unsupported("add_comment"))
}
async fn reply_to_thread(
&self,
_project: &str,
_number: u64,
_thread_id: &str,
_body: &str,
) -> Result<ReviewComment> {
Err(self.unsupported("reply_to_thread"))
}
async fn resolve_thread(
&self,
_project: &str,
_number: u64,
_thread_id: &str,
_resolved: bool,
) -> Result<()> {
Err(self.unsupported("resolve_thread"))
}
async fn submit_review(
&self,
_project: &str,
_number: u64,
_review: &ReviewSubmission,
) -> Result<ReviewOutcome> {
Err(self.unsupported("submit_review"))
}
fn conversation_caps(&self, _kind: crate::conversation::ItemKind) -> ConversationCaps {
ConversationCaps::NONE
}
async fn posting_identity(&self, _project: &str) -> Result<PostingIdentity> {
Err(self.unsupported("posting_identity"))
}
async fn item_timeline(
&self,
_project: &str,
_key: &ItemKey,
_older: Option<&PageCursor>,
_opts: &TimelineOpts,
) -> Result<TimelinePage> {
Err(self.unsupported("item_timeline"))
}
async fn post_item_comment(
&self,
_project: &str,
_key: &ItemKey,
_body: &str,
_internal: bool,
) -> Result<Comment> {
Err(self.unsupported("post_item_comment"))
}
async fn reply_in_discussion(
&self,
_project: &str,
_key: &ItemKey,
_discussion_id: &str,
_body: &str,
) -> Result<Comment> {
Err(self.unsupported("reply_in_discussion"))
}
async fn edit_item_comment(
&self,
_project: &str,
_key: &ItemKey,
_comment_id: &str,
_body: &str,
) -> Result<Comment> {
Err(self.unsupported("edit_item_comment"))
}
async fn delete_item_comment(
&self,
_project: &str,
_key: &ItemKey,
_comment_id: &str,
) -> Result<()> {
Err(self.unsupported("delete_item_comment"))
}
async fn create_issue_branch(&self, _req: &NewIssueBranch) -> Result<CreatedBranch> {
Err(self.unsupported("create_issue_branch"))
}
async fn issue_links(&self, _project: &str, _number: u64) -> Result<IssueLinks> {
Err(self.unsupported("issue_links"))
}
async fn branch_names(&self, _project: &str, _search: &str) -> Result<Vec<String>> {
Err(self.unsupported("branch_names"))
}
async fn project_members(&self, _project: &str, _query: &str) -> Result<Vec<HostedUser>> {
Err(self.unsupported("project_members"))
}
async fn project_labels(&self, _project: &str) -> Result<Vec<HostedLabel>> {
Err(self.unsupported("project_labels"))
}
async fn project_milestones(&self, _project: &str) -> Result<Vec<HostedMilestone>> {
Err(self.unsupported("project_milestones"))
}
async fn item_meta(&self, _project: &str, _number: u64, _kind: MetaKind) -> Result<ItemMeta> {
Err(self.unsupported("item_meta"))
}
async fn update_issue(&self, _project: &str, _number: u64, _edit: &IssueEdit) -> Result<()> {
Err(self.unsupported("update_issue"))
}
async fn issue_statuses(&self, _project: &str) -> Result<Vec<String>> {
Ok(Vec::new())
}
async fn set_issue_status(&self, _project: &str, _number: u64, _status: &str) -> Result<()> {
Err(self.unsupported("set_issue_status"))
}
async fn set_issue_state(&self, _project: &str, _number: u64, _closed: bool) -> Result<()> {
Err(self.unsupported("set_issue_state"))
}
fn board_source(&self) -> crate::board::BoardSource {
crate::board::BoardSource::OpenClosed
}
async fn move_issue(
&self,
project: &str,
issue: &IssueSummary,
spec: &crate::board::BoardSpec,
to: &crate::board::BoardColumn,
) -> Result<()> {
let plan = crate::board::plan_move(spec, issue, to)?;
if let Some(_list) = &plan.list {
return Err(self.unsupported("move_issue"));
}
if let Some(status) = &plan.status {
self.set_issue_status(project, issue.number, status).await?;
}
if let Some(labels) = &plan.labels {
let edit = IssueEdit {
labels: Some(labels.clone()),
..IssueEdit::default()
};
self.update_issue(project, issue.number, &edit).await?;
}
if let Some(closed) = plan.closed {
self.set_issue_state(project, issue.number, closed).await?;
}
Ok(())
}
async fn update_pull_request(
&self,
_project: &str,
_number: u64,
_edit: &PrEdit,
) -> Result<()> {
Err(self.unsupported("update_pull_request"))
}
async fn pull_request_diff(
&self,
_project: &str,
_number: u64,
_patch_set: Option<u32>,
_compare: Option<u32>,
) -> Result<Vec<crate::review::ApiFileDiff>> {
Err(self.unsupported("pull_request_diff"))
}
async fn pull_request_commits(
&self,
_project: &str,
_number: u64,
) -> Result<Vec<crate::review::ApiCommit>> {
Err(self.unsupported("pull_request_commits"))
}
async fn commit_diff(
&self,
_project: &str,
_sha: &str,
) -> Result<Vec<crate::review::ApiFileDiff>> {
Err(self.unsupported("commit_diff"))
}
async fn file_content(
&self,
_project: &str,
_path: &str,
_rev: Option<&str>,
) -> Result<Option<Vec<u8>>> {
Err(self.unsupported("file_content"))
}
async fn account_items(
&self,
_query: &crate::account::AccountQuery,
) -> Result<crate::account::AccountItems> {
Err(self.unsupported("account_items"))
}
}
pub fn client_for(
integration: Integration,
token: String,
transport: Arc<dyn HttpTransport>,
gate: &crate::trust::TrustGate,
) -> Result<Box<dyn HostingClient>> {
let transport = if integration.kind.capabilities().local_folder {
transport
} else {
let admitted = gate.admit(&integration)?;
let guarded = Arc::new(crate::trust::GuardedTransport::new(
transport,
admitted.subject,
admitted.record.origin.clone(),
admitted.origins,
admitted.record.tls.clone(),
)) as Arc<dyn HttpTransport>;
#[cfg(feature = "github")]
let guarded = if integration.kind.family() == ApiFamily::Github {
Arc::new(crate::scoped_transport::ScopedTransport::new(
guarded,
integration.id.clone(),
)) as Arc<dyn HttpTransport>
} else {
guarded
};
guarded
};
Ok(crate::assignment_guard::AssignmentGuard::wrap(
build_client(integration, token, transport),
))
}
fn build_client(
integration: Integration,
token: String,
transport: Arc<dyn HttpTransport>,
) -> Box<dyn HostingClient> {
match integration.kind.family() {
#[cfg(feature = "github")]
ApiFamily::Github => Box::new(GitHubClient::new(integration, token, transport)),
#[cfg(feature = "gitlab")]
ApiFamily::Gitlab => Box::new(GitLabClient::new(integration, token, transport)),
#[cfg(feature = "jira")]
ApiFamily::Jira => Box::new(crate::jira::JiraClient::new(integration, token, transport)),
#[cfg(feature = "bitbucket")]
ApiFamily::Bitbucket if integration.kind == crate::IntegrationKind::Bitbucket => {
Box::new(BitbucketClient::new(integration, token, transport))
}
#[cfg(feature = "bitbucket")]
ApiFamily::Bitbucket => Box::new(crate::bitbucket::dc::BitbucketDcClient::new(
integration,
token,
transport,
)),
#[cfg(feature = "azure-devops")]
ApiFamily::AzureDevops => Box::new(crate::azure_devops::AzureDevopsClient::new(
integration,
token,
transport,
)),
#[cfg(feature = "trello")]
ApiFamily::Trello => Box::new(crate::trello::TrelloClient::new(
integration,
token,
transport,
)),
#[cfg(feature = "gitea")]
ApiFamily::Gitea => Box::new(crate::gitea::GiteaClient::new(
integration,
token,
transport,
)),
#[cfg(feature = "linear")]
ApiFamily::Linear => Box::new(crate::linear::LinearClient::new(
integration,
token,
transport,
)),
#[cfg(feature = "youtrack")]
ApiFamily::YouTrack => Box::new(crate::youtrack::YouTrackClient::new(
integration,
token,
transport,
)),
#[cfg(feature = "redmine")]
ApiFamily::Redmine => Box::new(crate::redmine::RedmineClient::new(
integration,
token,
transport,
)),
#[cfg(feature = "gerrit")]
ApiFamily::Gerrit => Box::new(crate::gerrit::GerritClient::new(
integration,
token,
transport,
)),
#[cfg(feature = "local-issues")]
ApiFamily::LocalMarkdown => {
Box::new(crate::local_markdown::LocalMarkdownClient::new(integration))
}
#[allow(unreachable_patterns)]
_ => Box::new(UnsupportedClient { integration }),
}
}
pub struct UnsupportedClient {
integration: Integration,
}
#[async_trait::async_trait]
impl HostingClient for UnsupportedClient {
fn integration(&self) -> &Integration {
&self.integration
}
fn unsupported(&self, _what: &str) -> Error {
Error::NotImplemented(self.integration.kind.label().to_string())
}
async fn list_projects(&self) -> Result<Vec<HostedProject>> {
Err(self.unsupported("list_projects"))
}
async fn list_ssh_keys(&self) -> Result<Vec<HostedSshKey>> {
Err(self.unsupported("list_ssh_keys"))
}
async fn upload_ssh_key(&self, _title: &str, _public_key: &str) -> Result<HostedSshKey> {
Err(self.unsupported("upload_ssh_key"))
}
async fn create_pull_request(&self, _req: &NewPullRequest) -> Result<CreatedPullRequest> {
Err(self.unsupported("create_pull_request"))
}
}
#[cfg(any(feature = "github", feature = "gitlab"))]
pub(crate) async fn fetch_pages<T: DeserializeOwned>(
transport: &dyn HttpTransport,
request: impl Fn(String) -> HttpRequest,
base_url: &str,
what: &str,
) -> Result<Vec<T>> {
fetch_pages_with(transport, request, base_url, what, |r| r.json()).await
}
#[cfg(any(feature = "github", feature = "gitlab"))]
pub(crate) async fn fetch_pages_with<T>(
transport: &dyn HttpTransport,
request: impl Fn(String) -> HttpRequest,
base_url: &str,
what: &str,
rows: impl Fn(&HttpResponse) -> Result<Vec<T>>,
) -> Result<Vec<T>> {
let mut out = Vec::new();
for page in 1..=MAX_PAGES {
let url = format!("{base_url}&per_page={PAGE_SIZE}&page={page}");
let response = transport.send(request(url)).await?;
response.error_for_status(what)?;
let page_rows = rows(&response)?;
let count = page_rows.len();
out.extend(page_rows);
if count < PAGE_SIZE as usize {
break;
}
}
Ok(out)
}
#[cfg_attr(
not(any(
feature = "github",
feature = "gitlab",
feature = "jira",
feature = "bitbucket",
feature = "azure-devops",
feature = "trello",
feature = "gitea",
feature = "youtrack",
feature = "redmine",
feature = "gerrit",
feature = "local-issues"
)),
allow(dead_code)
)]
pub(crate) fn encode_component(text: &str) -> String {
let mut out = String::with_capacity(text.len());
for b in text.bytes() {
if b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_' | b'.' | b'~') {
out.push(b as char);
} else {
out.push_str(&format!("%{b:02X}"));
}
}
out
}
pub use crate::spec::web_base;
pub fn pull_request_web_url(
integration: &Integration,
project: &str,
number: u64,
) -> Option<String> {
let base = web_base(integration)?;
(spec_for(integration.kind.family()).pr_web_url)(&base, project, number)
}
pub fn pull_request_head_ref(
kind: crate::IntegrationKind,
number: u64,
) -> Option<(String, String)> {
let probe = PullRequestSummary {
number,
iid_label: String::new(),
title: String::new(),
state: crate::model::PullRequestState::Open,
source_branch: String::new(),
target_branch: String::new(),
source_project: None,
author: String::new(),
web_url: String::new(),
updated_at: String::new(),
review_requested: false,
assignees: vec![],
reviewers: vec![],
checks: None,
has_conflicts: None,
};
match (spec_for(kind.family()).checkout)(&probe) {
CheckoutSource::HeadRef { remote_ref, local } => Some((remote_ref, local)),
CheckoutSource::Branch { .. } => None,
}
}
pub fn pull_request_edit_url(kind: Option<crate::IntegrationKind>, web_url: &str) -> String {
match kind {
Some(kind) => (spec_for(kind.family()).pr_edit_url)(web_url),
None => web_url.to_string(),
}
}
pub fn issue_web_url(integration: &Integration, project: &str, number: u64) -> Option<String> {
let base = web_base(integration)?;
(spec_for(integration.kind.family()).issue_web_url)(&base, project, &number.to_string())
}
pub fn token_create_url(integration: &Integration) -> Option<String> {
let base = web_base(integration)?;
integration.token_link_url(&base)
}
#[cfg(all(test, feature = "github", feature = "gitlab"))]
mod tests {
use super::*;
use crate::http::RecordedTransport;
use crate::integration::IntegrationKind;
use std::sync::Arc;
fn integration(kind: IntegrationKind, url: &str) -> Integration {
Integration::new("h", kind, url)
}
fn block_on<F: std::future::Future>(f: F) -> F::Output {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
.block_on(f)
}
fn header<'a>(request: &'a HttpRequest, name: &str) -> Option<&'a str> {
request
.headers
.iter()
.find(|(k, _)| k.eq_ignore_ascii_case(name))
.map(|(_, v)| v.as_str())
}
const GITHUB_PAGE: &str = r#"[
{"full_name":"me/one","name":"one","description":"first",
"default_branch":"main","ssh_url":"git@github.com:me/one.git",
"clone_url":"https://github.com/me/one.git",
"html_url":"https://github.com/me/one","private":true}
]"#;
const GITHUB_ORGS_PAGE: &str = r#"[
{"full_name":"me/secret","name":"secret","private":true,
"clone_url":"https://github.com/me/secret.git"},
{"full_name":"acme/site","name":"site","private":false,
"clone_url":"https://github.com/acme/site.git"},
{"full_name":"acme/internal","name":"internal","private":true,
"clone_url":"https://github.com/acme/internal.git"}
]"#;
const GITLAB_GROUPS_PAGE: &str = r#"[
{"id":1,"name":"mine","path_with_namespace":"kai/mine","visibility":"private"},
{"id":2,"name":"open","path_with_namespace":"corp/tools/open","visibility":"public"},
{"id":3,"name":"shared","path_with_namespace":"corp/tools/shared","visibility":"internal"}
]"#;
const GITLAB_PAGE: &str = r#"[
{"id":42,"name":"one","path_with_namespace":"group/sub/one",
"description":"first","default_branch":"main",
"ssh_url_to_repo":"git@gitlab.example.com:group/sub/one.git",
"http_url_to_repo":"https://gitlab.example.com/group/sub/one.git",
"web_url":"https://gitlab.example.com/group/sub/one",
"visibility":"private"}
]"#;
#[test]
fn the_default_issue_create_options_are_empty_and_new_issue_has_no_type() {
let transport = std::sync::Arc::new(RecordedTransport::new());
let gh = Integration::new("gh", IntegrationKind::Github, "https://github.com");
let client = client_for(
gh,
"tok".into(),
transport,
&crate::trust::TrustGate::for_tests(),
)
.unwrap();
let options = block_on(client.issue_create_options("me/thing")).unwrap();
assert_eq!(options, IssueCreateOptions::default());
let new = NewIssue::default();
assert!(new.issue_type.is_none() && new.list_id.is_none());
}
#[test]
fn github_repositories_normalise_into_hosted_projects() {
let transport = Arc::new(RecordedTransport::new().on("/user/repos", 200, GITHUB_PAGE));
let client = client_for(
integration(IntegrationKind::Github, "https://github.com"),
"tok".into(),
transport.clone(),
&crate::trust::TrustGate::for_tests(),
)
.unwrap();
let projects = block_on(client.list_projects()).unwrap();
assert_eq!(projects.len(), 1);
assert_eq!(projects[0].full_name, "me/one");
assert_eq!(projects[0].id, "me/one");
assert_eq!(projects[0].integration_id, "h");
assert!(projects[0].private);
assert_eq!(
projects[0].clone_url(true),
Some("git@github.com:me/one.git")
);
let request = &transport.requests()[0];
assert!(request.url.starts_with("https://api.github.com/user/repos"));
assert_eq!(header(request, "Authorization"), Some("Bearer tok"));
assert!(
request.url.contains("affiliation="),
"the token's own repositories, not a public user listing: {}",
request.url
);
}
#[test]
fn gitlab_projects_normalise_into_hosted_projects() {
let transport = Arc::new(RecordedTransport::new().on("/projects?", 200, GITLAB_PAGE));
let client = client_for(
integration(IntegrationKind::Gitlab, "https://gitlab.example.com"),
"tok".into(),
transport.clone(),
&crate::trust::TrustGate::for_tests(),
)
.unwrap();
let projects = block_on(client.list_projects()).unwrap();
assert_eq!(projects[0].full_name, "group/sub/one");
assert_eq!(projects[0].id, "42");
assert!(projects[0].private);
let request = &transport.requests()[0];
assert!(
request
.url
.starts_with("https://gitlab.example.com/api/v4/projects"),
"a self-managed instance's api lives under its own url: {}",
request.url
);
assert_eq!(header(request, "PRIVATE-TOKEN"), Some("tok"));
assert!(
request.url.contains("membership=true"),
"without membership=true this lists every public project on the instance: {}",
request.url
);
}
#[test]
fn github_listing_carries_owner_and_private_flag_for_user_and_org_repos() {
let transport = Arc::new(RecordedTransport::new().on("/user/repos", 200, GITHUB_ORGS_PAGE));
let client = client_for(
integration(IntegrationKind::Github, "https://github.com"),
"tok".into(),
transport.clone(),
&crate::trust::TrustGate::for_tests(),
)
.unwrap();
let projects = block_on(client.list_projects()).unwrap();
let seen: Vec<(&str, bool)> = projects.iter().map(|p| (p.owner(), p.private)).collect();
assert_eq!(seen, vec![("me", true), ("acme", false), ("acme", true)]);
assert!(
transport.requests()[0]
.url
.contains("affiliation=owner,collaborator,organization_member"),
"organisation repositories are part of the listing"
);
}
#[test]
fn gitlab_listing_carries_the_group_path_and_visibility() {
let transport =
Arc::new(RecordedTransport::new().on("/projects?", 200, GITLAB_GROUPS_PAGE));
let client = client_for(
integration(IntegrationKind::Gitlab, "https://gitlab.example.com"),
"tok".into(),
transport,
&crate::trust::TrustGate::for_tests(),
)
.unwrap();
let projects = block_on(client.list_projects()).unwrap();
let seen: Vec<(&str, bool)> = projects.iter().map(|p| (p.owner(), p.private)).collect();
assert_eq!(
seen,
vec![("kai", true), ("corp/tools", false), ("corp/tools", true)]
);
}
#[test]
fn a_full_page_is_followed_by_another_request() {
let full: String = format!(
"[{}]",
(0..PAGE_SIZE)
.map(|n| format!(
r#"{{"id":{n},"name":"p{n}","path_with_namespace":"g/p{n}","visibility":"public"}}"#
))
.collect::<Vec<_>>()
.join(",")
);
let transport = Arc::new(RecordedTransport::new().on("&page=1", 200, &full).on(
"&page=2",
200,
GITLAB_PAGE,
));
let client = client_for(
integration(IntegrationKind::Gitlab, "https://gitlab.example.com"),
"tok".into(),
transport.clone(),
&crate::trust::TrustGate::for_tests(),
)
.unwrap();
let projects = block_on(client.list_projects()).unwrap();
assert_eq!(projects.len(), PAGE_SIZE as usize + 1);
assert_eq!(transport.requests().len(), 2, "must stop on a short page");
}
#[test]
fn a_rejected_token_is_an_unauthorized_error_not_an_empty_list() {
let transport = Arc::new(RecordedTransport::new().on(
"/user/repos",
401,
r#"{"message":"Bad credentials"}"#,
));
let client = client_for(
integration(IntegrationKind::Github, "https://github.com"),
"stale".into(),
transport,
&crate::trust::TrustGate::for_tests(),
)
.unwrap();
let err = block_on(client.list_projects()).unwrap_err();
assert!(matches!(err, crate::Error::Unauthorized { .. }));
assert!(err.to_string().contains("Bad credentials"));
}
#[test]
fn uploading_a_key_sends_the_hosts_own_field_names() {
for (kind, url, fragment) in [
(
IntegrationKind::Github,
"https://github.com",
"api.github.com",
),
(
IntegrationKind::Gitlab,
"https://gitlab.example.com",
"gitlab.example.com/api/v4",
),
] {
let transport = Arc::new(RecordedTransport::new().on(
"/user/keys",
201,
r#"{"id":7,"title":"laptop","key":"ssh-ed25519 AAAA laptop"}"#,
));
let client = client_for(
integration(kind, url),
"tok".into(),
transport.clone(),
&crate::trust::TrustGate::for_tests(),
)
.unwrap();
let key =
block_on(client.upload_ssh_key("laptop", "ssh-ed25519 AAAA laptop\n")).unwrap();
assert_eq!(key.id, "7");
assert_eq!(key.title, "laptop");
let request = &transport.requests()[0];
assert!(request.url.contains(fragment), "{}", request.url);
let body: serde_json::Value =
serde_json::from_str(request.body.as_deref().unwrap()).unwrap();
assert_eq!(body["title"], "laptop");
assert_eq!(
body["key"], "ssh-ed25519 AAAA laptop",
"the trailing newline must be trimmed - both hosts reject it"
);
}
}
fn new_pr(project: &str) -> NewPullRequest {
NewPullRequest {
project: project.into(),
source_branch: "feature".into(),
target_branch: "main".into(),
title: "Add feature".into(),
body: "does the thing".into(),
draft: false,
source_project: None,
..Default::default()
}
}
#[test]
fn github_pull_request_sends_head_base_and_returns_html_url() {
let transport = Arc::new(RecordedTransport::new().on(
"/pulls",
201,
r#"{"number":42,"html_url":"https://github.com/me/thing/pull/42"}"#,
));
let client = client_for(
integration(IntegrationKind::Github, "https://github.com"),
"tok".into(),
transport.clone(),
&crate::trust::TrustGate::for_tests(),
)
.unwrap();
let created = block_on(client.create_pull_request(&new_pr("me/thing"))).unwrap();
assert_eq!(created.number, 42);
assert_eq!(created.web_url, "https://github.com/me/thing/pull/42");
let request = &transport.requests()[0];
assert!(
request.url.ends_with("/repos/me/thing/pulls"),
"{}",
request.url
);
let body: serde_json::Value =
serde_json::from_str(request.body.as_deref().unwrap()).unwrap();
assert_eq!(body["head"], "feature");
assert_eq!(body["base"], "main");
assert_eq!(body["title"], "Add feature");
assert_eq!(body["draft"], false);
}
#[test]
fn gitlab_merge_request_url_encodes_the_project_path() {
let transport = Arc::new(RecordedTransport::new().on(
"/merge_requests",
201,
r#"{"iid":7,"web_url":"https://gitlab.example.com/group/sub/proj/-/merge_requests/7"}"#,
));
let client = client_for(
integration(IntegrationKind::Gitlab, "https://gitlab.example.com"),
"tok".into(),
transport.clone(),
&crate::trust::TrustGate::for_tests(),
)
.unwrap();
let mut req = new_pr("group/sub/proj");
req.draft = true;
let created = block_on(client.create_pull_request(&req)).unwrap();
assert_eq!(created.number, 7);
assert_eq!(
created.web_url,
"https://gitlab.example.com/group/sub/proj/-/merge_requests/7"
);
let request = &transport.requests()[0];
assert!(
request
.url
.contains("/projects/group%2Fsub%2Fproj/merge_requests"),
"{}",
request.url
);
let body: serde_json::Value =
serde_json::from_str(request.body.as_deref().unwrap()).unwrap();
assert_eq!(body["source_branch"], "feature");
assert_eq!(body["target_branch"], "main");
assert_eq!(
body["title"], "Draft: Add feature",
"GitLab marks drafts by title prefix"
);
}
#[test]
fn a_fork_pull_request_names_the_fork_owner_on_github() {
let transport = Arc::new(RecordedTransport::new().on(
"/pulls",
201,
r#"{"number":3,"html_url":"https://github.com/up/thing/pull/3"}"#,
));
let client = client_for(
integration(IntegrationKind::Github, "https://github.com"),
"tok".into(),
transport.clone(),
&crate::trust::TrustGate::for_tests(),
)
.unwrap();
let mut req = new_pr("up/thing");
req.source_project = Some("me/thing".into());
block_on(client.create_pull_request(&req)).unwrap();
let request = &transport.requests()[0];
assert!(
request.url.ends_with("/repos/up/thing/pulls"),
"{}",
request.url
);
let body: serde_json::Value =
serde_json::from_str(request.body.as_deref().unwrap()).unwrap();
assert_eq!(body["head"], "me:feature");
let mut same = new_pr("up/thing");
same.source_project = Some("UP/thing".into());
assert_eq!(same.fork_source(), None);
}
#[test]
fn a_fork_merge_request_is_posted_from_the_source_project_with_the_target_id() {
let transport = Arc::new(
RecordedTransport::new()
.on(
"/merge_requests",
201,
r#"{"iid":9,"web_url":"https://g.example.com/up/thing/-/merge_requests/9"}"#,
)
.on("/projects/up%2Fthing", 200, r#"{"id":77}"#),
);
let client = client_for(
integration(IntegrationKind::Gitlab, "https://g.example.com"),
"tok".into(),
transport.clone(),
&crate::trust::TrustGate::for_tests(),
)
.unwrap();
let mut req = new_pr("up/thing");
req.source_project = Some("me/thing".into());
let created = block_on(client.create_pull_request(&req)).unwrap();
assert_eq!(created.number, 9);
let requests = transport.requests();
let post = requests.last().unwrap();
assert!(
post.url.contains("/projects/me%2Fthing/merge_requests"),
"{}",
post.url
);
let body: serde_json::Value = serde_json::from_str(post.body.as_deref().unwrap()).unwrap();
assert_eq!(body["target_project_id"], 77);
}
#[test]
fn an_existing_pull_request_surfaces_the_hosts_message() {
let transport = Arc::new(RecordedTransport::new().on(
"/pulls",
422,
r#"{"message":"Validation Failed","errors":[{"message":"A pull request already exists for me:feature."}]}"#,
));
let client = client_for(
integration(IntegrationKind::Github, "https://github.com"),
"tok".into(),
transport,
&crate::trust::TrustGate::for_tests(),
)
.unwrap();
let err = block_on(client.create_pull_request(&new_pr("me/thing"))).unwrap_err();
assert!(matches!(err, crate::Error::Api { status: 422, .. }));
assert!(err.to_string().contains("Validation Failed"), "{err}");
}
#[test]
fn create_pull_request_401_and_403_map_to_the_existing_errors() {
for (status, want_unauthorized) in [(401, true), (403, false)] {
let transport =
Arc::new(RecordedTransport::new().on("/pulls", status, r#"{"message":"nope"}"#));
let client = client_for(
integration(IntegrationKind::Github, "https://github.com"),
"tok".into(),
transport,
&crate::trust::TrustGate::for_tests(),
)
.unwrap();
let err = block_on(client.create_pull_request(&new_pr("me/thing"))).unwrap_err();
if want_unauthorized {
assert!(matches!(err, crate::Error::Unauthorized { .. }));
} else {
assert!(matches!(err, crate::Error::Forbidden { .. }));
}
}
}
#[test]
fn existing_keys_can_be_listed_so_a_duplicate_upload_is_avoidable() {
let transport = Arc::new(RecordedTransport::new().on(
"/user/keys",
200,
r#"[{"id":7,"title":"laptop","key":"ssh-ed25519 AAAA laptop"}]"#,
));
let client = client_for(
integration(IntegrationKind::Github, "https://github.com"),
"tok".into(),
transport,
&crate::trust::TrustGate::for_tests(),
)
.unwrap();
let keys = block_on(client.list_ssh_keys()).unwrap();
assert_eq!(keys[0].title, "laptop");
}
#[test]
fn new_operations_default_to_unsupported() {
struct Old(Integration);
#[async_trait::async_trait]
impl HostingClient for Old {
fn integration(&self) -> &Integration {
&self.0
}
async fn list_projects(&self) -> Result<Vec<HostedProject>> {
Ok(vec![])
}
async fn list_ssh_keys(&self) -> Result<Vec<HostedSshKey>> {
Ok(vec![])
}
async fn upload_ssh_key(&self, _: &str, _: &str) -> Result<HostedSshKey> {
unreachable!()
}
async fn create_pull_request(&self, _: &NewPullRequest) -> Result<CreatedPullRequest> {
unreachable!()
}
}
let old = Old(integration(IntegrationKind::Github, "https://github.com"));
assert!(matches!(
block_on(old.current_user()).unwrap_err(),
Error::Unsupported(_)
));
assert!(matches!(
block_on(old.delete_ssh_key("1")).unwrap_err(),
Error::Unsupported(_)
));
assert!(matches!(
block_on(old.pull_request_for_branch("a/b", "x")).unwrap_err(),
Error::Unsupported(_)
));
}
#[test]
fn pull_request_heads_live_in_the_hosts_special_refs() {
assert_eq!(
pull_request_head_ref(IntegrationKind::Github, 7),
Some(("refs/pull/7/head".into(), "pr/7".into()))
);
assert_eq!(
pull_request_head_ref(IntegrationKind::GitlabSelfManaged, 7),
Some(("refs/merge-requests/7/head".into(), "mr/7".into()))
);
}
#[test]
fn web_url_helpers_per_family() {
let gh = integration(IntegrationKind::Github, "https://github.com/");
assert_eq!(
pull_request_web_url(&gh, "me/thing", 12).as_deref(),
Some("https://github.com/me/thing/pull/12")
);
assert_eq!(
issue_web_url(&gh, "me/thing", 3).as_deref(),
Some("https://github.com/me/thing/issues/3")
);
let ghe = integration(IntegrationKind::GithubEnterprise, "https://ghe.example.com");
assert_eq!(
pull_request_web_url(&ghe, "o/r", 1).as_deref(),
Some("https://ghe.example.com/o/r/pull/1")
);
let gl = integration(
IntegrationKind::GitlabSelfManaged,
"https://gitlab.example.com",
);
assert_eq!(
pull_request_web_url(&gl, "g/sub/p", 53).as_deref(),
Some("https://gitlab.example.com/g/sub/p/-/merge_requests/53")
);
assert_eq!(
issue_web_url(&gl, "g/sub/p", 12).as_deref(),
Some("https://gitlab.example.com/g/sub/p/-/issues/12")
);
let jira = integration(IntegrationKind::JiraCloud, "https://x.atlassian.net");
assert_eq!(pull_request_web_url(&jira, "p", 1), None);
}
#[test]
fn the_token_create_url_needs_a_host_and_a_token_flow() {
assert_eq!(
token_create_url(&integration(IntegrationKind::Github, "")).as_deref(),
Some("https://github.com/settings/tokens/new?description=GitPoulpi&scopes=repo,read:user,admin:public_key")
);
assert_eq!(
token_create_url(&integration(
IntegrationKind::GitlabSelfManaged,
"https://gitlab.example.com/"
))
.as_deref(),
Some("https://gitlab.example.com/-/user_settings/personal_access_tokens?name=GitPoulpi&scopes=api,read_user")
);
assert_eq!(
token_create_url(&integration(IntegrationKind::GitlabSelfManaged, "")),
None
);
assert_eq!(
token_create_url(&integration(IntegrationKind::Trello, "")),
None
);
}
}