use std::fmt::Debug;
use knope_versioning::{Action, VersionedFile};
use crate::{
app_config,
app_config::{get_github_token, get_or_prompt_for_github_token},
config,
integrations::http::{Client, http_client},
step::{issues, releases},
};
#[derive(Clone, Debug)]
pub(crate) struct State {
pub(crate) jira_config: Option<config::Jira>,
pub(crate) github: GitHub,
pub(crate) gitea: Gitea,
pub(crate) gitea_config: Option<config::Gitea>,
pub(crate) github_config: Option<config::GitHub>,
pub(crate) issue: Issue,
pub(crate) packages: Vec<releases::Package>,
pub(crate) all_versioned_files: Vec<VersionedFile>,
pub(crate) pending_actions: Vec<Action>,
pub(crate) all_git_tags: Vec<String>,
pub(crate) ignore_conventional_commits: bool,
}
impl State {
#[must_use]
pub(crate) fn new(
jira_config: Option<config::Jira>,
github_config: Option<config::GitHub>,
gitea_config: Option<config::Gitea>,
packages: Vec<releases::Package>,
all_versioned_files: Vec<VersionedFile>,
all_git_tags: Vec<String>,
ignore_conventional_commits: bool,
) -> Self {
State {
jira_config,
gitea: Gitea::New,
gitea_config,
github: GitHub::New,
github_config,
issue: Issue::Initial,
packages,
all_versioned_files,
all_git_tags,
pending_actions: Vec::new(),
ignore_conventional_commits,
}
}
}
#[derive(Clone, Copy, Debug)]
pub(crate) enum RunType<T> {
DryRun(T),
Real(T),
}
impl<T> RunType<T> {
#[must_use]
pub(crate) fn of<R>(&self, new_value: R) -> RunType<R> {
match self {
RunType::DryRun(_) => RunType::DryRun(new_value),
RunType::Real(_) => RunType::Real(new_value),
}
}
pub(crate) fn take(self) -> (RunType<()>, T) {
match self {
RunType::DryRun(inner) => (RunType::DryRun(()), inner),
RunType::Real(inner) => (RunType::Real(()), inner),
}
}
pub(crate) fn is_dry_run(&self) -> bool {
matches!(self, RunType::DryRun(_))
}
}
#[derive(Clone, Debug)]
pub(crate) enum Issue {
Initial,
Selected(issues::Issue),
}
#[derive(Clone, Debug)]
pub(crate) enum GitHub {
New,
Authenticated { token: String, client: Client },
Unauthenticated { client: Client },
}
impl GitHub {
pub(crate) fn require_authentication(self) -> Result<(String, Client), app_config::Error> {
Ok(match self {
Self::Authenticated { token, client } => (token, client),
Self::Unauthenticated { .. } | Self::New => {
let token = get_or_prompt_for_github_token()?;
(token, http_client(None)?)
}
})
}
pub(crate) fn maybe_authenticated(self) -> Result<(Option<String>, Client), app_config::Error> {
Ok(match self {
Self::Authenticated { token, client } => (Some(token), client),
Self::Unauthenticated { client } => (None, client),
Self::New => {
let token = get_github_token()?;
let missing_token_env_var = if token.is_none() {
Some("GITHUB_TOKEN")
} else {
None
};
(token, http_client(missing_token_env_var)?)
}
})
}
}
#[derive(Clone, Debug)]
pub(crate) enum Gitea {
New,
Initialized { token: String, client: Client },
}