97 lines
2.6 KiB
TypeScript
97 lines
2.6 KiB
TypeScript
// src/platform.ts
|
|
import { Gitea } from '@go-gitea/sdk.js';
|
|
import github from '@actions/github';
|
|
import { PushEvent } from '@octokit/webhooks-types';
|
|
|
|
export const REWRITE_MARKER = '[OPENCOMMIT]';
|
|
|
|
export type NormalizedCommit = {
|
|
id: string;
|
|
message: string;
|
|
author?: { name?: string; email?: string };
|
|
};
|
|
|
|
export function isGitea(): boolean {
|
|
return !!process.env.GITEA_BASE_URL;
|
|
}
|
|
|
|
export async function getPushCommits(): Promise<NormalizedCommit[]> {
|
|
if (isGitea()) {
|
|
const payload = github.context.payload as any;
|
|
const commits = payload.commits as Array<any>;
|
|
|
|
return commits
|
|
.filter((commit) => {
|
|
if (commit.message.startsWith('Merge')) return false;
|
|
if (commit.message.includes(REWRITE_MARKER)) return false;
|
|
return true;
|
|
})
|
|
.map((commit) => ({
|
|
id: commit.id,
|
|
message: commit.message,
|
|
author: commit.author
|
|
? {
|
|
name: commit.author.name,
|
|
email: commit.author.email ?? undefined, // coerce null → undefined
|
|
}
|
|
: undefined,
|
|
}));
|
|
} else {
|
|
const payload = github.context.payload as PushEvent;
|
|
const commits = payload.commits;
|
|
|
|
return commits
|
|
.filter((commit) => {
|
|
if (commit.message.startsWith('Merge')) return false;
|
|
if (commit.message.includes(REWRITE_MARKER)) return false;
|
|
return true;
|
|
})
|
|
.map((commit) => ({
|
|
id: commit.id,
|
|
message: commit.message,
|
|
author: {
|
|
name: commit.author.name,
|
|
email: commit.author.email ?? undefined, // coerce null → undefined
|
|
},
|
|
}));
|
|
}
|
|
}
|
|
|
|
export async function fetchCommitDiff(
|
|
sha: string,
|
|
giteaConfig?: { baseUrl: string; token: string }
|
|
): Promise<{ sha: string; diff: string }> {
|
|
if (isGitea() && giteaConfig) {
|
|
const gitea = new Gitea({
|
|
baseUrl: giteaConfig.baseUrl,
|
|
auth: giteaConfig.token,
|
|
});
|
|
|
|
const { data } = await gitea.rest.git.getCommit({
|
|
owner: process.env.GITEA_REPO_OWNER!,
|
|
repo: process.env.GITEA_REPO_NAME!,
|
|
sha,
|
|
});
|
|
|
|
const filesChanged =
|
|
data.files?.map((f: any) => `diff --git a/${f.filename} b/${f.filename}`).join('\n') ?? '';
|
|
|
|
return { sha, diff: filesChanged };
|
|
} else {
|
|
const octokit = github.getOctokit(process.env.GITHUB_TOKEN!);
|
|
const context = github.context;
|
|
|
|
const { data: diff } = await octokit.request<string>(
|
|
'GET /repos/{owner}/{repo}/commits/{ref}',
|
|
{
|
|
owner: context.repo.owner,
|
|
repo: context.repo.repo,
|
|
ref: sha,
|
|
headers: { Accept: 'application/vnd.github.v3.diff' },
|
|
}
|
|
);
|
|
|
|
return { sha, diff };
|
|
}
|
|
}
|