Files
gitea/ts/gitea.classes.actionrun.ts

125 lines
4.3 KiB
TypeScript

import type { GiteaClient } from './gitea.classes.giteaclient.js';
import type { IGiteaActionRun } from './gitea.interfaces.js';
import { GiteaActionRunJob } from './gitea.classes.actionrunjob.js';
import {
computeDuration,
resolveGiteaStatus,
extractRefFromPath,
extractWorkflowIdFromPath,
} from './gitea.helpers.js';
export class GiteaActionRun {
// Raw data
public readonly id: number;
public readonly runNumber: number;
public readonly runAttempt: number;
public readonly name: string;
public readonly displayTitle: string;
public readonly status: string;
public readonly conclusion: string;
public readonly headBranch: string;
public readonly headSha: string;
public readonly htmlUrl: string;
public readonly event: string;
public readonly path: string;
public readonly startedAt: string;
public readonly completedAt: string;
public readonly actorLogin: string;
public readonly triggerActorLogin: string;
// Computed
public readonly resolvedStatus: string;
public readonly duration: number;
public readonly ref: string;
public readonly workflowId: string;
/** @internal */
constructor(
private client: GiteaClient,
private ownerRepo: string,
raw: IGiteaActionRun,
) {
this.id = raw.id;
this.runNumber = raw.run_number || 0;
this.runAttempt = raw.run_attempt || 1;
this.name = raw.name || '';
this.displayTitle = raw.display_title || this.name;
this.status = raw.status || '';
this.conclusion = raw.conclusion || '';
this.headBranch = raw.head_branch || '';
this.headSha = raw.head_sha || '';
this.htmlUrl = raw.html_url || '';
this.event = raw.event || '';
this.path = raw.path || '';
this.startedAt = raw.started_at || '';
this.completedAt = raw.completed_at || '';
this.actorLogin = raw.actor?.login || '';
this.triggerActorLogin = raw.trigger_actor?.login || '';
// Computed properties
this.resolvedStatus = resolveGiteaStatus(this.status, this.conclusion);
this.duration = computeDuration(this.startedAt, this.completedAt);
this.ref = this.headBranch || extractRefFromPath(this.path);
this.workflowId = extractWorkflowIdFromPath(this.path);
}
// ---------------------------------------------------------------------------
// Jobs
// ---------------------------------------------------------------------------
async getJobs(): Promise<GiteaActionRunJob[]> {
const jobs = await this.client.requestGetActionRunJobs(this.ownerRepo, this.id);
return jobs.map(j => new GiteaActionRunJob(this.client, this.ownerRepo, j));
}
// ---------------------------------------------------------------------------
// Actions
// ---------------------------------------------------------------------------
/**
* Re-dispatch this workflow on the same ref.
* Gitea 1.25 has no rerun endpoint, so this dispatches the workflow again.
*/
async rerun(inputs?: Record<string, string>): Promise<void> {
const wfId = this.workflowId;
if (!wfId) {
throw new Error(`Cannot rerun: no workflow ID found in path "${this.path}"`);
}
await this.client.requestDispatchWorkflow(this.ownerRepo, wfId, this.ref, inputs);
}
/**
* Delete this action run.
*/
async delete(): Promise<void> {
await this.client.requestDeleteActionRun(this.ownerRepo, this.id);
}
// ---------------------------------------------------------------------------
// Serialization
// ---------------------------------------------------------------------------
toJSON(): IGiteaActionRun {
return {
id: this.id,
name: this.name,
workflow_id: this.workflowId,
status: this.status,
conclusion: this.conclusion,
head_branch: this.headBranch,
head_sha: this.headSha,
html_url: this.htmlUrl,
event: this.event,
path: this.path,
display_title: this.displayTitle,
run_number: this.runNumber,
run_attempt: this.runAttempt,
started_at: this.startedAt,
completed_at: this.completedAt,
actor: { id: 0, login: this.actorLogin, login_name: '', source_id: 0, full_name: '', email: '', avatar_url: '' },
trigger_actor: { id: 0, login: this.triggerActorLogin, login_name: '', source_id: 0, full_name: '', email: '', avatar_url: '' },
repository: { id: 0, name: '', full_name: this.ownerRepo, html_url: '' },
};
}
}