summaryrefslogtreecommitdiff
path: root/source/features/next-scheduled-github-action.tsx
blob: c7f50bce3ee4e42f94cd249fb4d1f4becf330e99 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import React from 'dom-chef';
import cache from 'webext-storage-cache';
import select from 'select-dom';
import {parseCron} from '@cheap-glitch/mi-cron';
import elementReady from 'element-ready';
import * as pageDetect from 'github-url-detection';

import features from '.';
import * as api from '../github-helpers/api';
import {getRepo} from '../github-helpers';

const getScheduledWorkflows = cache.function(async (): Promise<Record<string, string> | false> => {
	const {repository: {object: {entries: workflows}}} = await api.v4(`
		repository() {
			object(expression: "HEAD:.github/workflows") {
				... on Tree {
					entries {
						object {
							... on Blob {
								text
							}
						}
					}
				}
			}
		}
	`);
	if (!workflows) {
		return false;
	}

	const schedules: Record<string, string> = {};
	for (const workflow of workflows) {
		const workflowYaml = workflow.object.text;
		const name = /^name[:\s'"]+(.+)['"]?/m.exec(workflowYaml);
		const cron = /schedule[:\s-]+cron[:\s'"]+(.+)['"]?/m.exec(workflowYaml);

		if (name && cron) {
			schedules[name[1]] = cron[1];
		}
	}

	return schedules;
}, {
	maxAge: {days: 1},
	staleWhileRevalidate: {days: 10},
	cacheKey: () => __filebasename + ':' + getRepo()!.nameWithOwner
});

async function init(): Promise<false | void> {
	const workflows = await getScheduledWorkflows();
	if (!workflows) {
		return false;
	}

	for (const workflowListItem of select.all('[href*="?query"]', await elementReady('.hx_actions-sidebar'))) {
		if (select.exists('.octicon-stop', workflowListItem)) {
			continue;
		}

		const workflowName = workflowListItem.textContent!.trim();
		if (!(workflowName in workflows)) {
			continue;
		}

		const nextTime = parseCron.nextDate(workflows[workflowName]);
		if (nextTime) {
			workflowListItem.append(<em>(next <relative-time datetime={nextTime.toString()}/>)</em>);
		}
	}
}

void features.add(__filebasename, {
	include: [
		pageDetect.isRepositoryActions
	],
	awaitDomReady: false,
	init
});