Skip to content

Commit

Permalink
chore: joined comments
Browse files Browse the repository at this point in the history
  • Loading branch information
gentlementlegen committed Sep 23, 2024
1 parent f4fe027 commit 45c9ae4
Showing 1 changed file with 57 additions and 16 deletions.
73 changes: 57 additions & 16 deletions src/github/handlers/push-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function constructErrorBody(
}

export async function handleActionValidationWorkflowCompleted(context: GitHubContext<"repository_dispatch">) {
const { octokit, payload } = context;
const { payload } = context;
const { client_payload } = payload;
let pluginOutput: PluginOutput;
let stateValidation: StateValidation;
Expand Down Expand Up @@ -85,7 +85,6 @@ export async function handleActionValidationWorkflowCompleted(context: GitHubCon
try {
if (errors.length && state.eventPayload.repository.owner) {
const body = [];
body.push(`@${state.eventPayload.sender?.login} Configuration is invalid.\n`);
if (errors.length) {
body.push(
...constructErrorBody(
Expand All @@ -96,18 +95,57 @@ export async function handleActionValidationWorkflowCompleted(context: GitHubCon
)
);
}
await octokit.rest.repos.createCommitComment({
owner: state.eventPayload.repository.owner.login,
repo: state.eventPayload.repository.name,
commit_sha: state.eventPayload.after as string,
body: body.join(""),
});
await createCommitComment(
context,
{
owner: state.eventPayload.repository.owner.login,
repo: state.eventPayload.repository.name,
commitSha: state.eventPayload.after as string,
userLogin: state.eventPayload.sender?.login,
},
body
);
}
} catch (e) {
console.error("handleActionValidationWorkflowCompleted", e);
}
}

async function createCommitComment(
context: GitHubContext,
{ owner, repo, commitSha, userLogin }: { owner: string; repo: string; commitSha: string; userLogin?: string },
body: string[]
) {
const { octokit } = context;

const commit = (
await octokit.rest.repos.listCommentsForCommit({
owner: owner,
repo: repo,
commit_sha: commitSha,
})
).data
.filter((o) => o.user?.type === "Bot")
.pop();
if (commit) {
await octokit.rest.repos.updateCommitComment({
owner: owner,
repo: repo,
commit_sha: commitSha,
comment_id: commit.id,
body: `${commit.body}\n${body.join("")}`,
});
} else {
body.unshift(`@${userLogin} Configuration is invalid.\n`);
await octokit.rest.repos.createCommitComment({
owner: owner,
repo: repo,
commit_sha: commitSha,
body: body.join(""),
});
}
}

async function checkPluginConfigurations(context: GitHubContext<"push">, config: PluginConfiguration, rawData: string | null) {
const { payload, eventHandler } = context;
const errors: (ValueError | YAML.YAMLError)[] = [];
Expand Down Expand Up @@ -176,7 +214,7 @@ async function checkPluginConfigurations(context: GitHubContext<"push">, config:
}

export default async function handlePushEvent(context: GitHubContext<"push">) {
const { octokit, payload } = context;
const { payload } = context;
const { repository, commits, after } = payload;

const didConfigurationFileChange = commits.some((commit) => commit.modified?.includes(CONFIG_FULL_PATH) || commit.added?.includes(CONFIG_FULL_PATH));
Expand All @@ -195,14 +233,17 @@ export default async function handlePushEvent(context: GitHubContext<"push">) {
try {
if (errors.length) {
const body = [];
body.push(`@${payload.sender?.login} Configuration is invalid.\n`);
body.push(...constructErrorBody(errors, rawData, repository, after));
await octokit.rest.repos.createCommitComment({
owner: repository.owner.login,
repo: repository.name,
commit_sha: after,
body: body.join(""),
});
await createCommitComment(
context,
{
owner: repository.owner.login,
repo: repository.name,
commitSha: after,
userLogin: payload.sender?.login,
},
body
);
}
} catch (e) {
console.error("handlePushEventError", e);
Expand Down

0 comments on commit 45c9ae4

Please sign in to comment.