Files
languageservices/languageserver/src/value-providers/runs-on.ts
T

44 lines
1.5 KiB
TypeScript
Raw Normal View History

2023-02-24 08:53:51 -08:00
import {log} from "@actions/languageservice/log";
import {Value} from "@actions/languageservice/value-providers/config";
import {DEFAULT_RUNNER_LABELS} from "@actions/languageservice/value-providers/default";
2022-11-29 17:41:48 -05:00
import {Octokit} from "@octokit/rest";
import {TTLCache} from "../utils/cache";
2023-03-15 17:37:35 -04:00
import {errorMessage} from "../utils/error";
2022-11-08 17:00:59 -08:00
2023-01-25 18:24:05 -05:00
// Limitation: getRunnerLabels returns default hosted labels and labels for repository self-hosted runners.
// It doesn't return labels for organization runners visible to the repository.
2022-11-29 17:41:48 -05:00
export async function getRunnerLabels(client: Octokit, cache: TTLCache, owner: string, name: string): Promise<Value[]> {
const repoLabels = await cache.get(`${owner}/${name}/runner-labels`, undefined, () =>
fetchRunnerLabels(client, owner, name)
2022-11-28 14:55:50 -08:00
);
2023-02-08 12:22:27 -05:00
for (const label of DEFAULT_RUNNER_LABELS) {
2022-11-28 18:08:11 -05:00
repoLabels.add(label);
}
2022-11-29 17:41:48 -05:00
return Array.from(repoLabels).map(label => ({label}));
2022-11-28 18:08:11 -05:00
}
2022-11-29 17:41:48 -05:00
async function fetchRunnerLabels(client: Octokit, owner: string, name: string): Promise<Set<string>> {
2022-11-28 18:08:11 -05:00
const labels = new Set<string>();
2022-11-08 17:00:59 -08:00
try {
2023-01-30 11:29:48 -08:00
const itor = client.paginate.iterator(client.actions.listSelfHostedRunnersForRepo, {
owner,
repo: name,
per_page: 100
});
2022-11-08 17:00:59 -08:00
2023-01-30 11:29:48 -08:00
for await (const response of itor) {
2023-01-25 22:08:52 -05:00
for (const runner of response.data) {
for (const label of runner.labels) {
labels.add(label.name);
}
2022-11-08 17:00:59 -08:00
}
2023-01-30 11:29:48 -08:00
}
2022-11-08 17:00:59 -08:00
} catch (e) {
2023-03-15 17:37:35 -04:00
log(`Failure to retrieve runner labels: ${errorMessage(e)}`);
2022-11-08 17:00:59 -08:00
}
2022-11-28 18:08:11 -05:00
return labels;
2022-11-08 17:00:59 -08:00
}