Skip to content

Commit

Permalink
feat: added summary
Browse files Browse the repository at this point in the history
  • Loading branch information
gentlementlegen committed Aug 21, 2024
1 parent 6b5cfbf commit 125f313
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
32 changes: 32 additions & 0 deletions src/handlers/summary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import * as core from "@actions/core";
import { Context } from "../types";

export interface ResultInfo {
url: string;
merged: boolean;
}

function generateGitHubSummary(context: Context, urls: ResultInfo[]): string {
let monitored = context.config.repos.monitor.join(" | ") + "\n\n";
const ignored = context.config.repos.ignore.join(" | ") + "\n\n";

monitored +=
ignored +
urls
.map(({ url, merged }) => {
const status = merged ? `<span style="color:green">merged</span>` : `<span style="color:grey">no change</span>`;
return `- [${url}](${url}) - ${status}`;
})
.join("\n");

return monitored;
}

export async function generateSummary(context: Context, results: ResultInfo[]) {
try {
const summary = generateGitHubSummary(context, results);
await core.summary.addRaw(summary).write();
} catch (e) {
context.logger.error("Could not publish the summary", { e });
}
}
15 changes: 12 additions & 3 deletions src/helpers/update-pull-requests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { RestEndpointMethodTypes } from "@octokit/rest";
import ms from "ms";
import { getAllTimelineEvents } from "../handlers/github-events";
import { generateSummary, ResultInfo } from "../handlers/summary";
import { Context } from "../types";
import {
getApprovalCount,
Expand All @@ -27,11 +28,12 @@ function isIssueEvent(event: object): event is IssueEvent {

export async function updatePullRequests(context: Context) {
const { logger } = context;
const results: ResultInfo[] = [];

if (!context.config.repos.monitor.length) {
const owner = context.payload.repository.owner;
if (owner) {
logger.info(`No organizations or repo have been specified, will default to the organization owner: ${owner}.`);
logger.info(`No organizations or repo have been specified, will default to the organization owner: ${owner.login}.`);
context.config.repos.monitor.push(owner.login);
} else {
return logger.error("Could not set a default organization to watch, skipping.");
Expand All @@ -43,11 +45,14 @@ export async function updatePullRequests(context: Context) {
if (!pullRequests?.length) {
return logger.info("Nothing to do.");
}

let isMerged = false;
for (const { html_url } of pullRequests) {
isMerged = false;
try {
const gitHubUrl = parseGitHubUrl(html_url);
const pullRequestDetails = await getPullRequestDetails(context, gitHubUrl);
logger.debug(`Processing pull-request ${html_url}...`);
logger.debug(`Processing pull-request ${html_url} ...`);
if (pullRequestDetails.merged || pullRequestDetails.closed_at) {
logger.info(`The pull request ${html_url} is already merged or closed, nothing to do.`);
continue;
Expand All @@ -72,14 +77,16 @@ export async function updatePullRequests(context: Context) {
if (isNaN(lastActivityDate.getTime())) {
logger.info(`PR ${html_url} does not seem to have any activity, nothing to do.`);
} else if (isPastOffset(lastActivityDate, requirements.mergeTimeout)) {
await attemptMerging(context, { gitHubUrl, htmlUrl: html_url, requirements, lastActivityDate, pullRequestDetails });
isMerged = await attemptMerging(context, { gitHubUrl, htmlUrl: html_url, requirements, lastActivityDate, pullRequestDetails });
} else {
logger.info(`PR ${html_url} has activity up until (${lastActivityDate}), nothing to do.`);
}
} catch (e) {
logger.error(`Could not process pull-request ${html_url} for auto-merge: ${e}`);
}
results.push({ url: html_url, merged: isMerged });
}
await generateSummary(context, results);
}

async function attemptMerging(
Expand All @@ -96,12 +103,14 @@ async function attemptMerging(
if (await isCiGreen(context, data.pullRequestDetails.head.sha, data.gitHubUrl)) {
context.logger.info(`Pull-request ${data.htmlUrl} is past its due date (${data.requirements.mergeTimeout} after ${data.lastActivityDate}), will merge.`);
await mergePullRequest(context, data.gitHubUrl);
return true;
} else {
context.logger.info(`Pull-request ${data.htmlUrl} (sha: ${data.pullRequestDetails.head.sha}) does not pass all CI tests, won't merge.`);
}
} else {
context.logger.info(`Pull-request ${data.htmlUrl} does not have sufficient reviewer approvals to be merged.`);
}
return false;
}

function isPastOffset(lastActivityDate: Date, offset: string): boolean {
Expand Down

0 comments on commit 125f313

Please sign in to comment.