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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
import {CachedFunction} from 'webext-storage-cache';
import React from 'dom-chef';
import select from 'select-dom';
import {StopIcon, PlayIcon} from '@primer/octicons-react';
import {parseCron} from '@cheap-glitch/mi-cron';
import * as pageDetect from 'github-url-detection';
import features from '../feature-manager.js';
import api from '../github-helpers/api.js';
import {cacheByRepo} from '../github-helpers/index.js';
import observe from '../helpers/selector-observer.js';
import GetWorkflows from './github-actions-indicators.gql';
type Workflow = {
name: string;
isEnabled: boolean;
};
type WorkflowDetails = {
schedule?: string;
manuallyDispatchable: boolean;
};
function addTooltip(element: HTMLElement, tooltip: string): void {
const existingTooltip = element.getAttribute('aria-label');
if (existingTooltip) {
element.setAttribute('aria-label', existingTooltip + '.\n' + tooltip);
} else {
element.classList.add('tooltipped', 'tooltipped-s');
element.setAttribute('aria-label', tooltip);
}
}
// There is no way to get a workflow list in the v4 API #6543
async function getWorkflows(): Promise<Workflow[]> {
const response = await api.v3('actions/workflows');
const workflows = response.workflows as any[];
// The response is not reliable: Some workflow's path is '' and deleted workflow's state is 'active'
return workflows
.map<Workflow>(workflow => ({
name: workflow.path.split('/').pop()!,
isEnabled: workflow.state === 'active',
}));
}
async function getFilesInWorkflowPath(): Promise<Record<string, string>> {
const {repository: {workflowFiles}} = await api.v4(GetWorkflows);
const workflows: any[] = workflowFiles?.entries ?? [];
const result: Record<string, string> = {};
for (const workflow of workflows) {
result[workflow.name] = workflow.object.text;
}
return result;
}
const workflowDetails = new CachedFunction('workflows-details', {
async updater(): Promise<Record<string, Workflow & WorkflowDetails>> {
const [workflows, workflowFiles] = await Promise.all([getWorkflows(), getFilesInWorkflowPath()]);
const details: Record<string, Workflow & WorkflowDetails> = {};
for (const workflow of workflows) {
const workflowYaml = workflowFiles[workflow.name];
if (workflowYaml === undefined) {
// Cannot find workflow yaml; workflow removed.
continue;
}
const cron = /schedule[:\s-]+cron[:\s'"]+([^'"\n]+)/m.exec(workflowYaml);
details[workflow.name] = {
...workflow,
schedule: cron?.[1],
manuallyDispatchable: workflowYaml.includes('workflow_dispatch:'),
};
}
return details;
},
maxAge: {days: 1},
staleWhileRevalidate: {days: 10},
cacheKey: cacheByRepo,
});
async function addIndicators(workflowListItem: HTMLAnchorElement): Promise<void> {
// There might be a disabled indicator already
if (select.exists('.octicon-stop', workflowListItem)) {
return;
}
// Called in `init`, memoized
const workflows = await workflowDetails.get();
const workflowName = workflowListItem.href.split('/').pop()!;
const workflow = workflows[workflowName];
if (!workflow) {
return;
}
const svgTrailer = <div className="ActionListItem-visual--trailing m-auto d-flex gap-2"/>;
workflowListItem.append(svgTrailer);
if (!workflow.isEnabled) {
svgTrailer.append(<StopIcon className="m-auto"/>);
addTooltip(workflowListItem, 'This workflow is not enabled');
}
if (workflow.manuallyDispatchable) {
svgTrailer.append(<PlayIcon className="m-auto"/>);
addTooltip(workflowListItem, 'This workflow can be triggered manually');
}
if (!workflow.schedule) {
return;
}
const nextTime = parseCron.nextDate(workflow.schedule);
if (!nextTime) {
return;
}
const relativeTime = <relative-time datetime={String(nextTime)}/>;
select('.ActionList-item-label', workflowListItem)!.append(
<em>
({relativeTime})
</em>,
);
setTimeout(() => {
// The content of `relative-time` might is not immediately available
addTooltip(workflowListItem, `Next run: ${relativeTime.shadowRoot!.textContent!}`);
}, 500);
}
async function init(signal: AbortSignal): Promise<false | void> {
// Do it as soon as possible, before the page loads
const workflows = await workflowDetails.get();
if (!workflows) {
return false;
}
observe('a.ActionList-content', addIndicators, {signal});
}
void features.add(import.meta.url, {
include: [
pageDetect.isRepositoryActions,
],
init,
});
/*
## Test URLs
Manual:
https://github.com/fregante/browser-extension-template/actions
Manual + scheduled:
https://github.com/fregante/eslint-formatters/actions
Manually disabled:
https://github.com/134130/134130/actions
*/
|