Skip to content

Commit

Permalink
style: better expression processing GitHub workflow runs
Browse files Browse the repository at this point in the history
  • Loading branch information
piquark6046 committed Mar 11, 2024
1 parent e51bae4 commit b345896
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions sources/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,36 @@ import * as GitHub from '@octokit/rest'
import {DateTime} from 'luxon'
import type {ProgramOptionsType} from './types.js'

async function ListWorkflowRuns(ProgramOptions: ProgramOptionsType, EventType: string) {
const GitHubInstance = new GitHub.Octokit({auth: ProgramOptions.ghToken})
const [RepoOwner, RepoName] = ProgramOptions.repo.split('/')
const WorkflowRuns = await GitHubInstance.actions.listWorkflowRuns({
event: EventType,
owner: RepoOwner, repo: RepoName,
workflow_id: /(?<=^[A-Za-z0-9-_.]+\/[A-Za-z0-9-_.]+\/\.github\/workflows\/).+\.yml(?=@refs\/)/.exec(ProgramOptions.workflowRef)[0],
}).then(WorkflowRuns => WorkflowRuns.data.workflow_runs)
return WorkflowRuns
}

/**
* @name GetLatestWorkflowTime
* @description Get the latest workflow time.
* @param {ProgramOptionsType} ProgramOptions The program options.
* @returns {Promise<number>} The latest workflow time in milliseconds.
*/
export async function GetLatestWorkflowTime(ProgramOptions: ProgramOptionsType): Promise<number> {
const GitHubInstance = new GitHub.Octokit({auth: ProgramOptions.ghToken})
const [RepoOwner, RepoName] = ProgramOptions.repo.split('/')
var LatestWorkflowRunTime = 0
const WorkflowRuns = await GitHubInstance.actions.listWorkflowRuns({
event: 'push',
owner: RepoOwner, repo: RepoName,
workflow_id: /(?<=^[A-Za-z0-9-_.]+\/[A-Za-z0-9-_.]+\/\.github\/workflows\/).+\.yml(?=@refs\/)/.exec(ProgramOptions.workflowRef)[0],
}).then(WorkflowRuns => WorkflowRuns.data.workflow_runs)
WorkflowRuns.push(...(await GitHubInstance.actions.listWorkflowRuns({
event: 'release',
owner: RepoOwner, repo: RepoName,
workflow_id: /(?<=^[A-Za-z0-9-_.]+\/[A-Za-z0-9-_.]+\/\.github\/workflows\/).+\.yml(?=@refs\/)/.exec(ProgramOptions.workflowRef)[0],
}).then(WorkflowRuns => WorkflowRuns.data.workflow_runs)))
let WorkflowRuns: ReturnType<typeof ListWorkflowRuns> = null
for (const EventType of ['push', 'release']) {
if (WorkflowRuns === null) {
WorkflowRuns = ListWorkflowRuns(ProgramOptions, EventType)
} else {
// eslint-disable-next-line no-await-in-loop
(await WorkflowRuns).push(...await ListWorkflowRuns(ProgramOptions, EventType))
}
}

for (const WorkflowRun of WorkflowRuns) {
for (const WorkflowRun of await WorkflowRuns) {
if (WorkflowRun.status === 'completed' && WorkflowRun.conclusion === 'success'
&& DateTime.fromISO(WorkflowRun.updated_at).toMillis() > LatestWorkflowRunTime) {
LatestWorkflowRunTime = DateTime.fromISO(WorkflowRun.updated_at).toMillis()
Expand Down

0 comments on commit b345896

Please sign in to comment.