use serde::{Deserialize, Serialize};
use super::{JobSummary, Tone, duration};
pub const JOB_ACTIONS_CHECK: &str = "job actions";
pub const SIBLINGS_MAX: usize = 50;
const NAME_MAX: usize = 80;
pub const CONFIRM_SECS: u64 = 300;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum JobAction {
Cancel,
Rerun,
RerunFailed,
#[serde(other)]
Unsupported,
}
impl JobAction {
pub fn for_job(job: &JobSummary) -> &'static [JobAction] {
if !job.state.is_final() {
&[JobAction::Cancel]
} else if job.outcome().kind == super::OutcomeKind::Failed {
&[JobAction::Rerun, JobAction::RerunFailed]
} else {
&[JobAction::Rerun]
}
}
pub fn slug(self) -> &'static str {
match self {
JobAction::Cancel => "cancel",
JobAction::Rerun => "rerun",
JobAction::RerunFailed => "rerun-failed",
JobAction::Unsupported => "unsupported",
}
}
pub fn confirm_label(self) -> &'static str {
match self {
JobAction::Cancel => "Cancel run",
JobAction::Rerun => "Re-run job",
JobAction::RerunFailed => "Re-run failed jobs",
JobAction::Unsupported => "Confirm",
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum JobActions {
On,
Off,
#[default]
#[serde(other)]
Unsupported,
}
impl JobActions {
pub fn configured(on: bool) -> JobActions {
if on { JobActions::On } else { JobActions::Off }
}
pub fn offered(self) -> bool {
self == JobActions::On
}
pub fn why_not(self) -> Option<&'static str> {
match self {
JobActions::On => None,
JobActions::Off => Some(actions_off()),
JobActions::Unsupported => {
Some("this runnerd cannot cancel or re-run jobs: it is older than this client")
}
}
}
}
pub fn actions_off() -> &'static str {
"runnerd does not cancel or re-run jobs: [github] job_actions is false in its config"
}
pub fn actions_not_configured(repository: &str) -> String {
format!(
"{repository} is not a configured repository; runnerd cancels and re-runs only the jobs of configured ones"
)
}
pub fn actions_in_flight(other: JobAction, run: u64) -> String {
let what = match other {
JobAction::Cancel => "a cancel",
JobAction::Rerun => "a re-run of a job",
JobAction::RerunFailed => "a re-run of the failed jobs",
JobAction::Unsupported => "another action",
};
format!("{what} of run {run} is being sent to GitHub; ask again once it answers")
}
pub fn actions_no_run(job: u64) -> String {
format!("the history has no workflow run for job {job}, so runnerd cannot ask GitHub about it")
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct ActionQuery {
#[serde(default)]
pub force: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub confirm: Option<String>,
#[serde(default)]
pub failed: bool,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ActionTarget {
pub action: JobAction,
pub job: u64,
pub name: String,
pub repository: String,
pub run: u64,
pub attempt: Option<u32>,
}
impl ActionTarget {
fn run_words(&self) -> String {
match self.attempt {
Some(attempt) => format!("run {} (attempt {attempt})", self.run),
None => format!("run {}", self.run),
}
}
fn name(&self) -> String {
let plain = super::jobs::plain(&self.name);
let plain = plain.trim();
if plain.is_empty() {
return format!("job {}", self.job);
}
if plain.chars().count() > NAME_MAX {
let cut: String = plain.chars().take(NAME_MAX - 1).collect();
return format!("{cut}…");
}
plain.to_owned()
}
fn title(&self) -> String {
let (run, repository) = (self.run_words(), &self.repository);
match self.action {
JobAction::Cancel => format!("Cancel {run} of {repository}?"),
JobAction::Rerun => format!("Re-run {} of {repository}?", self.name()),
JobAction::RerunFailed => {
format!("Re-run the failed jobs of {run} of {repository}?")
}
JobAction::Unsupported => format!("Act on {run} of {repository}?"),
}
}
fn scope(&self) -> String {
let run = self.run;
match self.action {
JobAction::Cancel => "GitHub cancels the whole run, not one job: every job of it still queued or running \
stops, here and on any other runner."
.to_owned(),
JobAction::Rerun => format!(
"GitHub re-runs this job and the jobs that depend on it, in a new attempt of run {run}."
),
JobAction::RerunFailed => format!(
"GitHub re-runs the failed jobs of run {run} and the jobs that depend on them, in a new attempt."
),
JobAction::Unsupported => String::new(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ActionPreview {
pub message: String,
pub action: JobAction,
pub job: u64,
pub repository: String,
pub run: u64,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub attempt: Option<u32>,
pub title: String,
pub scope: String,
#[serde(default)]
pub notes: Vec<String>,
#[serde(default)]
pub siblings: Vec<JobSummary>,
pub siblings_note: String,
pub confirm_label: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub confirm: Option<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RunSeen {
Now {
attempt: u32,
completed: bool,
},
Unread {
status: Option<u16>,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Superseded {
Unknown,
Used,
OtherRequest,
ConfigChanged,
AttemptChanged,
Unverified,
}
#[derive(Debug, Clone)]
pub struct PreviewFacts {
pub target: ActionTarget,
pub job_attempt: u32,
pub run: RunSeen,
pub superseded: Option<Superseded>,
pub confirm: Option<String>,
pub paused: bool,
pub siblings: Vec<JobSummary>,
pub more: bool,
}
impl ActionPreview {
pub fn new(facts: PreviewFacts) -> ActionPreview {
let PreviewFacts {
mut target,
job_attempt,
run,
superseded,
confirm,
paused,
siblings,
more,
} = facts;
let mut notes = Vec::new();
if let Some(superseded) = superseded {
notes.push(superseded_note(superseded));
}
let rerun = matches!(target.action, JobAction::Rerun | JobAction::RerunFailed);
match run {
RunSeen::Now { attempt, completed } => {
target.attempt = Some(attempt);
if completed && target.action == JobAction::Cancel {
notes.push(format!(
"Run {} has completed: GitHub has nothing left to cancel.",
target.run
));
}
if !completed && rerun {
notes.push(format!(
"Run {} has not completed; GitHub refuses to re-run a run that has not.",
target.run
));
}
if job_attempt < attempt {
notes.push(match target.action {
JobAction::Cancel => format!(
"This job ran in attempt {job_attempt}; the cancel stops attempt {attempt}, the run's \
current one."
),
_ => format!("This job ran in attempt {job_attempt}; the run is at attempt {attempt} now."),
});
}
}
RunSeen::Unread { status } => {
target.attempt = None;
let answered = status.map_or_else(
|| "it did not answer".to_owned(),
|status| format!("it answered {status}"),
);
notes.push(format!(
"runnerd could not read run {} from GitHub ({answered}), so it cannot say which attempt this \
would act on, and offers no confirmation. Ask again later.",
target.run
));
}
}
if paused && rerun {
notes.push(format!(
"{} is paused: runnerd starts none of the new attempt's jobs until it is resumed; other runners \
may take them.",
target.repository
));
}
let siblings_note = match (siblings.len(), more) {
(0, _) => {
"runnerd knows no other job of this run; GitHub may have some, on other runners."
.to_owned()
}
(1, false) => {
"runnerd knows 1 other job of this run; GitHub may have more, on other runners."
.to_owned()
}
(count, false) => format!(
"runnerd knows {count} other jobs of this run; GitHub may have more, on other runners."
),
(count, true) => format!(
"runnerd knows more than {count} other jobs of this run, the first {count} shown; GitHub may have \
more still."
),
};
let title = target.title();
let scope = target.scope();
ActionPreview {
message: format!("{title} {scope}"),
action: target.action,
job: target.job,
repository: target.repository.clone(),
run: target.run,
attempt: target.attempt,
title,
scope,
notes,
siblings: siblings.into_iter().map(cleaned).collect(),
siblings_note,
confirm_label: target.action.confirm_label().to_owned(),
confirm,
}
}
}
fn superseded_note(superseded: Superseded) -> String {
let minutes = CONFIRM_SECS / 60;
match superseded {
Superseded::Unknown => format!(
"That confirmation is not one this runnerd holds: it lasts {minutes} minutes, and a restart forgets it. \
Nothing was sent; this is a new preview."
),
Superseded::Used => {
"That confirmation was sent already, and is sent once. Nothing more was sent; this is a new preview."
.to_owned()
}
Superseded::OtherRequest => "That confirmation is for another action or job. Nothing was sent; this is a \
new preview."
.to_owned(),
Superseded::ConfigChanged => "runnerd's config changed what it may do since that confirmation. Nothing was \
sent; this is a new preview."
.to_owned(),
Superseded::AttemptChanged => "The run moved to another attempt since that confirmation. Nothing was sent; \
this is a new preview of the attempt it is at now."
.to_owned(),
Superseded::Unverified => "runnerd could not read the run again right before sending, so it sent nothing; \
this is a new preview."
.to_owned(),
}
}
fn cleaned(job: JobSummary) -> JobSummary {
let clean = |text: &String| super::jobs::plain(text);
JobSummary {
name: clean(&job.name),
repository: clean(&job.repository),
workflow: job.workflow.as_ref().map(clean),
branch: job.branch.as_ref().map(clean),
conclusion: job.conclusion.as_ref().map(clean),
url: job.url.as_ref().map(clean),
labels: job.labels.iter().map(clean).collect(),
runner: job.runner.as_ref().map(|runner| super::JobRunner {
name: clean(&runner.name),
..runner.clone()
}),
..job
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum ActionOutcome {
Accepted,
AlreadyFinished,
Refused,
#[serde(other)]
Unknown,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum ActionRefusal {
NoToken,
Unreachable,
Unauthorized,
Forbidden,
NotFound,
Conflict,
Unprocessable,
RateLimited,
Failed,
Withdrawn,
#[serde(other)]
Other,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UnknownCause {
TimedOut,
Broken,
ServerError,
Stopping,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ActionAnswer {
Accepted {
status: u16,
},
AlreadyFinished {
status: u16,
},
Refused {
refusal: ActionRefusal,
status: Option<u16>,
until: Option<u64>,
},
Unknown {
cause: UnknownCause,
status: Option<u16>,
},
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ActionResult {
pub action: JobAction,
pub job: u64,
pub repository: String,
pub run: u64,
pub outcome: ActionOutcome,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub status: Option<u16>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub refusal: Option<ActionRefusal>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub until: Option<u64>,
pub message: String,
}
impl ActionResult {
pub fn new(target: &ActionTarget, answer: ActionAnswer, now: u64) -> ActionResult {
let (outcome, status, refusal, until) = match answer {
ActionAnswer::Accepted { status } => {
(ActionOutcome::Accepted, Some(status), None, None)
}
ActionAnswer::AlreadyFinished { status } => {
(ActionOutcome::AlreadyFinished, Some(status), None, None)
}
ActionAnswer::Refused {
refusal,
status,
until,
} => (ActionOutcome::Refused, status, Some(refusal), until),
ActionAnswer::Unknown { status, .. } => (ActionOutcome::Unknown, status, None, None),
};
let message = match answer {
ActionAnswer::Accepted { .. } => accepted(target),
ActionAnswer::AlreadyFinished { .. } => format!(
"Run {} of {} had completed already: nothing was cancelled.",
target.run, target.repository
),
ActionAnswer::Refused {
refusal,
status,
until,
} => refused(target, refusal, status, until, now),
ActionAnswer::Unknown { cause, status } => unknown(target, cause, status),
};
ActionResult {
action: target.action,
job: target.job,
repository: target.repository.clone(),
run: target.run,
outcome,
status,
refusal,
until,
message,
}
}
pub fn tone(&self) -> Tone {
match self.outcome {
ActionOutcome::Accepted => Tone::Good,
ActionOutcome::AlreadyFinished => Tone::Neutral,
ActionOutcome::Refused => Tone::Bad,
ActionOutcome::Unknown => Tone::Warn,
}
}
pub fn succeeded(&self) -> bool {
matches!(
self.outcome,
ActionOutcome::Accepted | ActionOutcome::AlreadyFinished
)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RerunRefused {
pub run: u64,
pub since: u64,
pub refusal: ActionRefusal,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub status: Option<u16>,
pub message: String,
}
impl RerunRefused {
pub fn new(result: &ActionResult, now: u64) -> RerunRefused {
RerunRefused {
run: result.run,
since: now,
refusal: result.refusal.unwrap_or(ActionRefusal::Other),
status: result.status,
message: result.message.clone(),
}
}
pub fn unread(repository: &str, run: u64, answer: ActionAnswer, now: u64) -> RerunRefused {
let (refusal, status) = match answer {
ActionAnswer::Refused {
refusal, status, ..
} => (refusal, status),
ActionAnswer::Accepted { status } | ActionAnswer::AlreadyFinished { status } => {
(ActionRefusal::Other, Some(status))
}
ActionAnswer::Unknown { status, .. } => (ActionRefusal::Other, status),
};
let code = status.map_or_else(String::new, |status| format!(" ({status})"));
let message = match refusal {
ActionRefusal::NoToken => format!(
"runnerd could read no GitHub token, so it could not read run {run} of {repository}. Nothing was \
sent."
),
ActionRefusal::Unauthorized => format!(
"GitHub refused runnerd's token{code} when it read run {run} of {repository}. Nothing was sent."
),
_ => format!(
"GitHub would not show run {run} of {repository} to runnerd{code}: the token may lack Actions: read \
on it, or the run is gone. Nothing was sent."
),
};
RerunRefused {
run,
since: now,
refusal,
status,
message,
}
}
pub fn title(&self, repository: &str) -> String {
format!(
"runnerd holds the re-runs of {repository}'s runner faults for an hour: {}",
self.message
)
}
pub fn fix(&self) -> &'static str {
match self.refusal {
ActionRefusal::NoToken | ActionRefusal::Unauthorized => {
"Give runnerd a GitHub token it can use; it tries again when the hour is up"
}
_ => {
"Give runnerd's token Actions: write on the repository (the repo scope for a classic token); it tries \
again when the hour is up. Or set rerun_runner_faults = false"
}
}
}
}
fn accepted(target: &ActionTarget) -> String {
let (run, repository) = (target.run_words(), &target.repository);
match target.action {
JobAction::Cancel => format!(
"GitHub is cancelling {run} of {repository}; its jobs end as their runners report it."
),
JobAction::Rerun => format!(
"GitHub re-runs {} and the jobs that depend on it in a new attempt of {run}; they show here as \
they queue.",
target.name()
),
JobAction::RerunFailed => format!(
"GitHub re-runs the failed jobs of {run} and the jobs that depend on them in a new attempt; they \
show here as they queue."
),
JobAction::Unsupported => format!("GitHub accepted the action on {run}."),
}
}
fn asked(target: &ActionTarget) -> String {
match target.action {
JobAction::Cancel => format!("cancel {}", target.run_words()),
JobAction::Rerun => format!("re-run {}", target.name()),
JobAction::RerunFailed => format!("re-run the failed jobs of {}", target.run_words()),
JobAction::Unsupported => format!("act on {}", target.run_words()),
}
}
fn refused(
target: &ActionTarget,
refusal: ActionRefusal,
status: Option<u16>,
until: Option<u64>,
now: u64,
) -> String {
let asked = asked(target);
let repository = &target.repository;
let rerun = matches!(target.action, JobAction::Rerun | JobAction::RerunFailed);
let why = match refusal {
ActionRefusal::NoToken => {
return format!("runnerd could read no GitHub token, so it did not {asked}.");
}
ActionRefusal::Unreachable => {
return format!("runnerd could not reach GitHub, so it did not {asked}.");
}
ActionRefusal::RateLimited => {
let wait = until.map_or_else(String::new, |until| {
format!(
"; try again in {}",
duration(until.saturating_sub(now).max(1))
)
});
return match status {
Some(status) => format!(
"GitHub refused to {asked} ({status}): its rate limit is used up{wait}. Nothing was done."
),
None => {
format!("GitHub's rate limit is used up, so runnerd did not {asked}{wait}.")
}
};
}
ActionRefusal::Withdrawn => {
return format!(
"runnerd did not {asked}: its config changed before the request went out (job actions turned off, \
or the repositories changed). Nothing was sent."
);
}
ActionRefusal::Unauthorized => "it refused the token".to_owned(),
ActionRefusal::Forbidden if rerun => format!(
"the token may lack Actions: write (or the repo scope) on {repository}, or the run has not finished"
),
ActionRefusal::Forbidden => {
format!("the token may lack Actions: write (or the repo scope) on {repository}")
}
ActionRefusal::NotFound => {
"it does not show the run to this token, or the run is gone".to_owned()
}
ActionRefusal::Conflict => "the run's state does not allow it now".to_owned(),
ActionRefusal::Unprocessable => "it cannot do that for this run".to_owned(),
ActionRefusal::Failed | ActionRefusal::Other => "it failed".to_owned(),
};
let status = status.map_or_else(String::new, |status| format!(" ({status})"));
format!("GitHub refused to {asked}{status}: {why}. Nothing was done.")
}
fn unknown(target: &ActionTarget, cause: UnknownCause, status: Option<u16>) -> String {
let why = match cause {
UnknownCause::TimedOut => "GitHub did not answer in time".to_owned(),
UnknownCause::Broken => "the connection broke after the request went out".to_owned(),
UnknownCause::ServerError => match status {
Some(status) => format!(
"GitHub answered {status}, a server error, which does not say whether it acted"
),
None => "GitHub answered with a server error, which does not say whether it acted"
.to_owned(),
},
UnknownCause::Stopping => "runnerd began to stop while GitHub was asked".to_owned(),
};
let asked = asked(target);
format!(
"runnerd cannot tell whether GitHub did {asked}: {why}. Check the run on GitHub before asking again."
)
}
#[cfg(all(test, any(feature = "host", feature = "client")))]
mod tests;