mirror of
https://github.com/OneUptime/oneuptime.git
synced 2026-01-16 23:00:51 +00:00
- Implement CodeRepositoryService for database interactions. - Enhance GitHub utility functions for app authentication and repository management. - Introduce new permissions for Code Repository actions (create, delete, edit, read). - Create Code Repository pages and routes in the dashboard. - Add side menu and breadcrumbs for Code Repository navigation. - Implement settings and delete functionality for Code Repositories. - Update Helm chart to include GitHub App configuration options. - Modify example environment configuration to include GitHub App credentials.
330 lines
11 KiB
TypeScript
330 lines
11 KiB
TypeScript
import Express, {
|
|
ExpressRequest,
|
|
ExpressResponse,
|
|
ExpressRouter,
|
|
} from "../Utils/Express";
|
|
import Response from "../Utils/Response";
|
|
import BadDataException from "../../Types/Exception/BadDataException";
|
|
import logger from "../Utils/Logger";
|
|
import { JSONObject } from "../../Types/JSON";
|
|
import {
|
|
DashboardClientUrl,
|
|
GitHubAppClientId,
|
|
} from "../EnvironmentConfig";
|
|
import ObjectID from "../../Types/ObjectID";
|
|
import GitHubUtil, {
|
|
GitHubRepository,
|
|
} from "../Utils/CodeRepository/GitHub/GitHub";
|
|
import CodeRepositoryService from "../Services/CodeRepositoryService";
|
|
import CodeRepository from "../../Models/DatabaseModels/CodeRepository";
|
|
import CodeRepositoryType from "../../Types/CodeRepository/CodeRepositoryType";
|
|
import URL from "../../Types/API/URL";
|
|
import UserMiddleware from "../Middleware/UserAuthorization";
|
|
import BaseModel from "../../Models/DatabaseModels/DatabaseBaseModel/DatabaseBaseModel";
|
|
|
|
export default class GitHubAPI {
|
|
public getRouter(): ExpressRouter {
|
|
const router: ExpressRouter = Express.getRouter();
|
|
|
|
// GitHub App installation callback
|
|
// This is called after a user installs the GitHub App
|
|
router.get(
|
|
"/github/auth/:projectId/:userId/callback",
|
|
async (req: ExpressRequest, res: ExpressResponse) => {
|
|
try {
|
|
const projectId: string | undefined =
|
|
req.params["projectId"]?.toString();
|
|
const userId: string | undefined = req.params["userId"]?.toString();
|
|
|
|
if (!projectId) {
|
|
return Response.sendErrorResponse(
|
|
req,
|
|
res,
|
|
new BadDataException("Project ID is required"),
|
|
);
|
|
}
|
|
|
|
if (!userId) {
|
|
return Response.sendErrorResponse(
|
|
req,
|
|
res,
|
|
new BadDataException("User ID is required"),
|
|
);
|
|
}
|
|
|
|
// GitHub sends installation_id in query params after app installation
|
|
const installationId: string | undefined =
|
|
req.query["installation_id"]?.toString();
|
|
|
|
if (!installationId) {
|
|
return Response.sendErrorResponse(
|
|
req,
|
|
res,
|
|
new BadDataException(
|
|
"Installation ID is required. Please install the GitHub App first.",
|
|
),
|
|
);
|
|
}
|
|
|
|
// Store the installation ID - we'll create repositories when user selects them
|
|
// For now, redirect back to dashboard with installation ID
|
|
const redirectUrl: string = `${DashboardClientUrl.toString()}/dashboard/${projectId}/code-repository?installation_id=${installationId}`;
|
|
|
|
return Response.redirect(req, res, URL.fromString(redirectUrl));
|
|
} catch (error) {
|
|
logger.error("GitHub Auth Callback Error:");
|
|
logger.error(error);
|
|
return Response.sendErrorResponse(
|
|
req,
|
|
res,
|
|
error instanceof Error
|
|
? new BadDataException(error.message)
|
|
: new BadDataException("An error occurred"),
|
|
);
|
|
}
|
|
},
|
|
);
|
|
|
|
// Initiate GitHub App installation
|
|
router.get(
|
|
"/github/auth/:projectId/:userId/install",
|
|
async (req: ExpressRequest, res: ExpressResponse) => {
|
|
try {
|
|
if (!GitHubAppClientId) {
|
|
return Response.sendErrorResponse(
|
|
req,
|
|
res,
|
|
new BadDataException(
|
|
"GitHub App is not configured. Please set GITHUB_APP_CLIENT_ID.",
|
|
),
|
|
);
|
|
}
|
|
|
|
const projectId: string | undefined =
|
|
req.params["projectId"]?.toString();
|
|
const userId: string | undefined = req.params["userId"]?.toString();
|
|
|
|
if (!projectId || !userId) {
|
|
return Response.sendErrorResponse(
|
|
req,
|
|
res,
|
|
new BadDataException("Project ID and User ID are required"),
|
|
);
|
|
}
|
|
|
|
// Redirect to GitHub App installation page
|
|
// The state parameter helps us track the installation
|
|
const state: string = Buffer.from(
|
|
JSON.stringify({ projectId, userId }),
|
|
).toString("base64");
|
|
|
|
const installUrl: string = `https://github.com/apps/${GitHubAppClientId}/installations/new?state=${state}`;
|
|
|
|
return Response.redirect(req, res, URL.fromString(installUrl));
|
|
} catch (error) {
|
|
logger.error("GitHub Install Redirect Error:");
|
|
logger.error(error);
|
|
return Response.sendErrorResponse(
|
|
req,
|
|
res,
|
|
error instanceof Error
|
|
? new BadDataException(error.message)
|
|
: new BadDataException("An error occurred"),
|
|
);
|
|
}
|
|
},
|
|
);
|
|
|
|
// List repositories for an installation
|
|
router.get(
|
|
"/github/repositories/:projectId/:installationId",
|
|
UserMiddleware.getUserMiddleware,
|
|
async (req: ExpressRequest, res: ExpressResponse) => {
|
|
try {
|
|
const installationId: string | undefined =
|
|
req.params["installationId"]?.toString();
|
|
|
|
if (!installationId) {
|
|
return Response.sendErrorResponse(
|
|
req,
|
|
res,
|
|
new BadDataException("Installation ID is required"),
|
|
);
|
|
}
|
|
|
|
const repositories: Array<GitHubRepository> =
|
|
await GitHubUtil.listRepositoriesForInstallation(installationId);
|
|
|
|
return Response.sendJsonObjectResponse(req, res, {
|
|
repositories: repositories as unknown,
|
|
} as JSONObject);
|
|
} catch (error) {
|
|
logger.error("GitHub List Repositories Error:");
|
|
logger.error(error);
|
|
return Response.sendErrorResponse(
|
|
req,
|
|
res,
|
|
error instanceof Error
|
|
? new BadDataException(error.message)
|
|
: new BadDataException("An error occurred"),
|
|
);
|
|
}
|
|
},
|
|
);
|
|
|
|
// Connect a repository to a project
|
|
router.post(
|
|
"/github/repository/connect",
|
|
UserMiddleware.getUserMiddleware,
|
|
async (req: ExpressRequest, res: ExpressResponse) => {
|
|
try {
|
|
const body: JSONObject = req.body;
|
|
|
|
const projectId: string | undefined = body["projectId"]?.toString();
|
|
const installationId: string | undefined =
|
|
body["installationId"]?.toString();
|
|
const repositoryName: string | undefined =
|
|
body["repositoryName"]?.toString();
|
|
const organizationName: string | undefined =
|
|
body["organizationName"]?.toString();
|
|
const name: string | undefined = body["name"]?.toString();
|
|
const defaultBranch: string | undefined =
|
|
body["defaultBranch"]?.toString();
|
|
const repositoryUrl: string | undefined =
|
|
body["repositoryUrl"]?.toString();
|
|
|
|
if (!projectId) {
|
|
return Response.sendErrorResponse(
|
|
req,
|
|
res,
|
|
new BadDataException("Project ID is required"),
|
|
);
|
|
}
|
|
|
|
if (!installationId) {
|
|
return Response.sendErrorResponse(
|
|
req,
|
|
res,
|
|
new BadDataException("Installation ID is required"),
|
|
);
|
|
}
|
|
|
|
if (!repositoryName) {
|
|
return Response.sendErrorResponse(
|
|
req,
|
|
res,
|
|
new BadDataException("Repository name is required"),
|
|
);
|
|
}
|
|
|
|
if (!organizationName) {
|
|
return Response.sendErrorResponse(
|
|
req,
|
|
res,
|
|
new BadDataException("Organization name is required"),
|
|
);
|
|
}
|
|
|
|
// Create the code repository record
|
|
const codeRepository: CodeRepository = new CodeRepository();
|
|
codeRepository.projectId = new ObjectID(projectId);
|
|
codeRepository.name = name || `${organizationName}/${repositoryName}`;
|
|
codeRepository.repositoryHostedAt = CodeRepositoryType.GitHub;
|
|
codeRepository.organizationName = organizationName;
|
|
codeRepository.repositoryName = repositoryName;
|
|
codeRepository.mainBranchName = defaultBranch || "main";
|
|
codeRepository.gitHubAppInstallationId = installationId;
|
|
|
|
if (repositoryUrl) {
|
|
codeRepository.repositoryUrl = URL.fromString(repositoryUrl);
|
|
}
|
|
|
|
const createdRepository: CodeRepository =
|
|
await CodeRepositoryService.create({
|
|
data: codeRepository,
|
|
props: {
|
|
isRoot: true,
|
|
},
|
|
});
|
|
|
|
return Response.sendJsonObjectResponse(req, res, {
|
|
repository: BaseModel.toJSON(createdRepository, CodeRepository),
|
|
} as JSONObject);
|
|
} catch (error) {
|
|
logger.error("GitHub Connect Repository Error:");
|
|
logger.error(error);
|
|
return Response.sendErrorResponse(
|
|
req,
|
|
res,
|
|
error instanceof Error
|
|
? new BadDataException(error.message)
|
|
: new BadDataException("An error occurred"),
|
|
);
|
|
}
|
|
},
|
|
);
|
|
|
|
// GitHub webhook handler
|
|
router.post(
|
|
"/github/webhook",
|
|
async (req: ExpressRequest, res: ExpressResponse) => {
|
|
try {
|
|
const signature: string | undefined = req.headers[
|
|
"x-hub-signature-256"
|
|
] as string | undefined;
|
|
const event: string | undefined = req.headers["x-github-event"] as
|
|
| string
|
|
| undefined;
|
|
|
|
if (!signature) {
|
|
return Response.sendErrorResponse(
|
|
req,
|
|
res,
|
|
new BadDataException("Missing webhook signature"),
|
|
);
|
|
}
|
|
|
|
// Get raw body for signature verification
|
|
const rawBody: string = JSON.stringify(req.body);
|
|
|
|
// Verify webhook signature
|
|
const isValid: boolean = GitHubUtil.verifyWebhookSignature(
|
|
rawBody,
|
|
signature,
|
|
);
|
|
|
|
if (!isValid) {
|
|
return Response.sendErrorResponse(
|
|
req,
|
|
res,
|
|
new BadDataException("Invalid webhook signature"),
|
|
);
|
|
}
|
|
|
|
logger.debug(`Received GitHub webhook event: ${event}`);
|
|
|
|
// Handle different webhook events here
|
|
// For now, just acknowledge receipt
|
|
// Future: Handle push, pull_request, check_run events
|
|
|
|
return Response.sendJsonObjectResponse(req, res, {
|
|
success: true,
|
|
message: "Webhook received",
|
|
} as JSONObject);
|
|
} catch (error) {
|
|
logger.error("GitHub Webhook Error:");
|
|
logger.error(error);
|
|
return Response.sendErrorResponse(
|
|
req,
|
|
res,
|
|
error instanceof Error
|
|
? new BadDataException(error.message)
|
|
: new BadDataException("An error occurred"),
|
|
);
|
|
}
|
|
},
|
|
);
|
|
|
|
return router;
|
|
}
|
|
}
|