async pipeline

pull/479/head
Frostebite 2023-01-18 22:04:23 +00:00
parent de55a733c5
commit f10887252c
3 changed files with 161 additions and 73 deletions

110
dist/index.js vendored
View File

@ -6342,36 +6342,49 @@ const cloud_runner_1 = __importDefault(__nccwpck_require__(79144));
const cloud_runner_options_1 = __importDefault(__nccwpck_require__(96552)); const cloud_runner_options_1 = __importDefault(__nccwpck_require__(96552));
const core_1 = __nccwpck_require__(76762); const core_1 = __nccwpck_require__(76762);
class GitHub { class GitHub {
static get octokit() {
return new core_1.Octokit({
auth: process.env.GITHUB_TOKEN,
});
}
static get sha() {
return cloud_runner_1.default.buildParameters.gitSha;
}
static get checkName() {
return `Cloud Runner (${cloud_runner_1.default.buildParameters.buildGuid})`;
}
static get nameReadable() {
return GitHub.checkName;
}
static get checkRunId() {
return cloud_runner_1.default.githubCheckId;
}
static get owner() {
return cloud_runner_options_1.default.githubOwner;
}
static get repo() {
return cloud_runner_options_1.default.githubRepoName;
}
static createGitHubCheck(summary) { static createGitHubCheck(summary) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
if (!cloud_runner_options_1.default.githubChecks) { if (!cloud_runner_options_1.default.githubChecks) {
return ``; return ``;
} }
const sha = cloud_runner_1.default.buildParameters.gitSha;
const name = `Cloud Runner (${cloud_runner_1.default.buildParameters.buildGuid})`;
const nameReadable = name;
const token = process.env.GITHUB_TOKEN;
const owner = cloud_runner_options_1.default.githubOwner;
const repo = cloud_runner_options_1.default.githubRepoName;
GitHub.startedDate = new Date().toISOString(); GitHub.startedDate = new Date().toISOString();
// call github api to create a check cloud_runner_logger_1.default.log(`POST /repos/${GitHub.owner}/${GitHub.repo}/check-runs`);
const octokit = new core_1.Octokit({ const data = {
auth: token, owner: GitHub.owner,
}); repo: GitHub.repo,
cloud_runner_logger_1.default.log(`POST /repos/${owner}/${repo}/check-runs`); name: GitHub.checkName,
const result = yield octokit.request(`POST /repos/${owner}/${repo}/check-runs`, {
owner,
repo,
name,
// eslint-disable-next-line camelcase // eslint-disable-next-line camelcase
head_sha: sha, head_sha: GitHub.sha,
status: 'queued', status: 'queued',
// eslint-disable-next-line camelcase // eslint-disable-next-line camelcase
external_id: cloud_runner_1.default.buildParameters.buildGuid, external_id: cloud_runner_1.default.buildParameters.buildGuid,
// eslint-disable-next-line camelcase // eslint-disable-next-line camelcase
started_at: GitHub.startedDate, started_at: GitHub.startedDate,
output: { output: {
title: nameReadable, title: GitHub.nameReadable,
summary, summary,
text: '', text: '',
images: [ images: [
@ -6382,39 +6395,62 @@ class GitHub {
}, },
], ],
}, },
}); };
if (yield cloud_runner_options_1.default.asyncCloudRunner) {
yield GitHub.runUpdateAsyncChecksWorkflow(data, `update`);
return;
}
const result = yield GitHub.octokit.request(`POST /repos/${GitHub.owner}/${GitHub.repo}/check-runs`, data);
return result.data.id; return result.data.id;
}); });
} }
static runUpdateAsyncChecksWorkflow(data, mode) {
return __awaiter(this, void 0, void 0, function* () {
const workflowsResult = yield GitHub.octokit.request(`GET /repos/${GitHub.owner}/${GitHub.repo}/actions/workflows`, {
owner: GitHub.owner,
repo: GitHub.repo,
});
const workflows = workflowsResult.data.workflows;
let selectedId = ``;
for (let index = 0; index < workflowsResult.data.total_count; index++) {
if (workflows[index].name === `Async Checks API`) {
selectedId = workflows[index].id;
}
}
if (selectedId === ``) {
throw new Error(`no workflow with name "Async Checks API"`);
}
yield GitHub.octokit.request(`POST /repos/{owner}/{repo}/actions/workflows/{workflow_id}/dispatches`, {
owner: GitHub.owner,
repo: GitHub.repo,
// eslint-disable-next-line camelcase
workflow_id: selectedId,
ref: 'topic-branch',
inputs: {
checksObject: JSON.stringify({ data, mode }),
},
});
});
}
static updateGitHubCheck(longDescription, summary, result = `neutral`, status = `in_progress`) { static updateGitHubCheck(longDescription, summary, result = `neutral`, status = `in_progress`) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
if (!cloud_runner_options_1.default.githubChecks) { if (!cloud_runner_options_1.default.githubChecks) {
return; return;
} }
GitHub.longDescriptionContent += `\n${longDescription}`; GitHub.longDescriptionContent += `\n${longDescription}`;
const sha = cloud_runner_1.default.buildParameters.gitSha;
const name = `Cloud Runner (${cloud_runner_1.default.buildParameters.buildGuid})`;
const nameReadable = name;
const token = process.env.GITHUB_TOKEN;
const checkRunId = cloud_runner_1.default.githubCheckId;
const owner = cloud_runner_options_1.default.githubOwner;
const repo = cloud_runner_options_1.default.githubRepoName;
const octokit = new core_1.Octokit({
auth: token,
});
const data = { const data = {
owner, owner: GitHub.owner,
repo, repo: GitHub.owner,
// eslint-disable-next-line camelcase // eslint-disable-next-line camelcase
check_run_id: checkRunId, check_run_id: GitHub.checkRunId,
name, name: GitHub.checkName,
// eslint-disable-next-line camelcase // eslint-disable-next-line camelcase
head_sha: sha, head_sha: GitHub.sha,
// eslint-disable-next-line camelcase // eslint-disable-next-line camelcase
started_at: GitHub.startedDate, started_at: GitHub.startedDate,
status, status,
output: { output: {
title: nameReadable, title: GitHub.nameReadable,
summary, summary,
text: GitHub.longDescriptionContent, text: GitHub.longDescriptionContent,
annotations: [], annotations: [],
@ -6428,7 +6464,11 @@ class GitHub {
data.completed_at = GitHub.endedDate || GitHub.startedDate; data.completed_at = GitHub.endedDate || GitHub.startedDate;
data.conclusion = result; data.conclusion = result;
} }
yield octokit.request(`PATCH /repos/${owner}/${repo}/check-runs/${checkRunId}`, data); if (yield cloud_runner_options_1.default.asyncCloudRunner) {
yield GitHub.runUpdateAsyncChecksWorkflow(data, `update`);
return;
}
yield GitHub.octokit.request(`PATCH /repos/${GitHub.owner}/${GitHub.repo}/check-runs/${GitHub.checkRunId}`, data);
}); });
} }
} }

2
dist/index.js.map vendored

File diff suppressed because one or more lines are too long

View File

@ -7,39 +7,56 @@ class GitHub {
private static longDescriptionContent: string = ``; private static longDescriptionContent: string = ``;
private static startedDate: string; private static startedDate: string;
private static endedDate: string; private static endedDate: string;
private static get octokit() {
return new Octokit({
auth: process.env.GITHUB_TOKEN,
});
}
private static get sha() {
return CloudRunner.buildParameters.gitSha;
}
private static get checkName() {
return `Cloud Runner (${CloudRunner.buildParameters.buildGuid})`;
}
private static get nameReadable() {
return GitHub.checkName;
}
private static get checkRunId() {
return CloudRunner.githubCheckId;
}
private static get owner() {
return CloudRunnerOptions.githubOwner;
}
private static get repo() {
return CloudRunnerOptions.githubRepoName;
}
public static async createGitHubCheck(summary) { public static async createGitHubCheck(summary) {
if (!CloudRunnerOptions.githubChecks) { if (!CloudRunnerOptions.githubChecks) {
return ``; return ``;
} }
const sha = CloudRunner.buildParameters.gitSha;
const name = `Cloud Runner (${CloudRunner.buildParameters.buildGuid})`;
const nameReadable = name;
const token = process.env.GITHUB_TOKEN;
const owner = CloudRunnerOptions.githubOwner;
const repo = CloudRunnerOptions.githubRepoName;
GitHub.startedDate = new Date().toISOString(); GitHub.startedDate = new Date().toISOString();
// call github api to create a check CloudRunnerLogger.log(`POST /repos/${GitHub.owner}/${GitHub.repo}/check-runs`);
const octokit = new Octokit({
auth: token,
});
CloudRunnerLogger.log(`POST /repos/${owner}/${repo}/check-runs`); const data = {
owner: GitHub.owner,
const result = await octokit.request(`POST /repos/${owner}/${repo}/check-runs`, { repo: GitHub.repo,
owner, name: GitHub.checkName,
repo,
name,
// eslint-disable-next-line camelcase // eslint-disable-next-line camelcase
head_sha: sha, head_sha: GitHub.sha,
status: 'queued', status: 'queued',
// eslint-disable-next-line camelcase // eslint-disable-next-line camelcase
external_id: CloudRunner.buildParameters.buildGuid, external_id: CloudRunner.buildParameters.buildGuid,
// eslint-disable-next-line camelcase // eslint-disable-next-line camelcase
started_at: GitHub.startedDate, started_at: GitHub.startedDate,
output: { output: {
title: nameReadable, title: GitHub.nameReadable,
summary, summary,
text: '', text: '',
images: [ images: [
@ -50,41 +67,67 @@ class GitHub {
}, },
], ],
}, },
}); };
if (await CloudRunnerOptions.asyncCloudRunner) {
await GitHub.runUpdateAsyncChecksWorkflow(data, `update`);
return;
}
const result = await GitHub.octokit.request(`POST /repos/${GitHub.owner}/${GitHub.repo}/check-runs`, data);
return result.data.id; return result.data.id;
} }
public static async runUpdateAsyncChecksWorkflow(data, mode) {
const workflowsResult = await GitHub.octokit.request(
`GET /repos/${GitHub.owner}/${GitHub.repo}/actions/workflows`,
{
owner: GitHub.owner,
repo: GitHub.repo,
},
);
const workflows = workflowsResult.data.workflows;
let selectedId = ``;
for (let index = 0; index < workflowsResult.data.total_count; index++) {
if (workflows[index].name === `Async Checks API`) {
selectedId = workflows[index].id;
}
}
if (selectedId === ``) {
throw new Error(`no workflow with name "Async Checks API"`);
}
await GitHub.octokit.request(`POST /repos/{owner}/{repo}/actions/workflows/{workflow_id}/dispatches`, {
owner: GitHub.owner,
repo: GitHub.repo,
// eslint-disable-next-line camelcase
workflow_id: selectedId,
ref: 'topic-branch',
inputs: {
checksObject: JSON.stringify({ data, mode }),
},
});
}
public static async updateGitHubCheck(longDescription, summary, result = `neutral`, status = `in_progress`) { public static async updateGitHubCheck(longDescription, summary, result = `neutral`, status = `in_progress`) {
if (!CloudRunnerOptions.githubChecks) { if (!CloudRunnerOptions.githubChecks) {
return; return;
} }
GitHub.longDescriptionContent += `\n${longDescription}`; GitHub.longDescriptionContent += `\n${longDescription}`;
const sha = CloudRunner.buildParameters.gitSha;
const name = `Cloud Runner (${CloudRunner.buildParameters.buildGuid})`;
const nameReadable = name;
const token = process.env.GITHUB_TOKEN;
const checkRunId = CloudRunner.githubCheckId;
const owner = CloudRunnerOptions.githubOwner;
const repo = CloudRunnerOptions.githubRepoName;
const octokit = new Octokit({
auth: token,
});
const data: any = { const data: any = {
owner, owner: GitHub.owner,
repo, repo: GitHub.owner,
// eslint-disable-next-line camelcase // eslint-disable-next-line camelcase
check_run_id: checkRunId, check_run_id: GitHub.checkRunId,
name, name: GitHub.checkName,
// eslint-disable-next-line camelcase // eslint-disable-next-line camelcase
head_sha: sha, head_sha: GitHub.sha,
// eslint-disable-next-line camelcase // eslint-disable-next-line camelcase
started_at: GitHub.startedDate, started_at: GitHub.startedDate,
status, status,
output: { output: {
title: nameReadable, title: GitHub.nameReadable,
summary, summary,
text: GitHub.longDescriptionContent, text: GitHub.longDescriptionContent,
annotations: [], annotations: [],
@ -100,7 +143,12 @@ class GitHub {
data.conclusion = result; data.conclusion = result;
} }
await octokit.request(`PATCH /repos/${owner}/${repo}/check-runs/${checkRunId}`, data); if (await CloudRunnerOptions.asyncCloudRunner) {
await GitHub.runUpdateAsyncChecksWorkflow(data, `update`);
return;
}
await GitHub.octokit.request(`PATCH /repos/${GitHub.owner}/${GitHub.repo}/check-runs/${GitHub.checkRunId}`, data);
} }
} }