pub mod recognition;
use crate::{Contract, Event, Request};
pub struct Issues;
impl Contract for Issues {
const NAME: &'static str = "issues";
const VERSION: u32 = 1;
}
pub const MAX_RESOLVE: usize = 50;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Changed;
impl Event for Changed {
type Contract = Issues;
const NAME: &'static str = "changed";
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct CurrentScope;
impl Request for CurrentScope {
type Contract = Issues;
const NAME: &'static str = "current-scope";
type Response = Option<IssueScope>;
}
#[derive(Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct IssueScope {
pub profile_id: String,
pub profile_name: String,
pub app_origin: String,
pub organisation_id: String,
pub team_keys: Vec<String>,
pub linear_workspace: Option<String>,
}
impl IssueScope {
pub fn new(
profile_id: &str,
profile_name: &str,
app_origin: &str,
organisation_id: &str,
) -> Self {
Self {
profile_id: profile_id.into(),
profile_name: profile_name.into(),
app_origin: app_origin.into(),
organisation_id: organisation_id.into(),
team_keys: Vec::new(),
linear_workspace: None,
}
}
pub fn with_linear_workspace(mut self, workspace: &str) -> Self {
self.linear_workspace = (!workspace.is_empty()).then(|| workspace.to_owned());
self
}
pub fn with_team_keys(mut self, keys: impl IntoIterator<Item = impl Into<String>>) -> Self {
self.team_keys = keys.into_iter().map(Into::into).collect();
self
}
pub fn navigation_url(&self, url: &str) -> Option<String> {
let mut url = url::Url::parse(url).ok()?;
if url.origin().ascii_serialization() != self.app_origin {
return None;
}
url.query_pairs_mut()
.clear()
.append_pair("orgId", &self.organisation_id)
.append_pair("profile", &self.profile_id);
Some(url.into())
}
}
#[derive(Clone, PartialEq, Eq)]
pub struct Resolve {
profile_id: String,
identifiers: Vec<String>,
}
impl Resolve {
pub fn new(profile_id: &str, identifiers: impl IntoIterator<Item = impl Into<String>>) -> Self {
let mut distinct: Vec<String> = Vec::new();
for identifier in identifiers.into_iter().map(Into::into) {
if distinct.len() == MAX_RESOLVE {
break;
}
if !distinct.contains(&identifier) {
distinct.push(identifier);
}
}
Self {
profile_id: profile_id.into(),
identifiers: distinct,
}
}
pub fn profile_id(&self) -> &str {
&self.profile_id
}
pub fn identifiers(&self) -> &[String] {
&self.identifiers
}
}
impl Request for Resolve {
type Contract = Issues;
const NAME: &'static str = "resolve";
type Response = Vec<Resolution>;
}
#[derive(Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct Resolution {
pub identifier: String,
pub state: ResolutionState,
}
impl Resolution {
pub fn new(identifier: &str, state: ResolutionState) -> Self {
Self {
identifier: identifier.into(),
state,
}
}
}
#[derive(Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum ResolutionState {
Available(IssueSummary),
Unavailable,
Pending,
SignedOut,
Failed,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum StateType {
Triage,
Backlog,
Unstarted,
Started,
Completed,
Canceled,
Duplicate,
}
#[derive(Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct IssueState {
pub name: String,
pub kind: StateType,
}
impl IssueState {
pub fn new(name: &str, kind: StateType) -> Self {
Self {
name: name.into(),
kind,
}
}
}
#[derive(Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct IssueSummary {
pub identifier: String,
pub title: String,
pub url: String,
pub state: IssueState,
pub tracker: bool,
pub archived: bool,
pub assignee: Option<String>,
}
impl IssueSummary {
pub fn new(identifier: &str, title: &str, url: &str, state: IssueState) -> Self {
Self {
identifier: identifier.into(),
title: title.into(),
url: url.into(),
state,
tracker: false,
archived: false,
assignee: None,
}
}
pub fn with_markers(mut self, tracker: bool, archived: bool) -> Self {
self.tracker = tracker;
self.archived = archived;
self
}
pub fn with_assignee(mut self, assignee: Option<&str>) -> Self {
self.assignee = assignee.map(str::to_owned);
self
}
}