refactor: Update branch name in Copilot/Index.ts and add GITHUB_USERNAME to .env.example

This commit updates the branch name in Copilot/Index.ts from 'test-branch-3' to 'test-branch-4'. Additionally, it adds the GITHUB_USERNAME variable to the .env.example file. These changes improve the accuracy and functionality of the code.
This commit is contained in:
Simon Larsen
2024-06-13 13:10:14 +01:00
parent a73b050a4d
commit a406287215
7 changed files with 118 additions and 36 deletions

View File

@@ -188,25 +188,6 @@ export default class CodeRepositoryUtil {
logger.debug(stdout);
}
public static async pushChanges(data: {
repoPath: string;
branchName: string;
remoteName?: string | undefined;
}): Promise<void> {
const remoteName: string = data.remoteName || 'origin';
const command: string = `cd ${data.repoPath} && git push ${remoteName} ${data.branchName}`;
logger.debug("Executing command: " + command);
const stdout = await Execute.executeCommand(
command
);
logger.debug(stdout);
}
public static async getGitCommitHashForFile(data: {
repoPath: string;
filePath: string;

View File

@@ -7,6 +7,8 @@ import PullRequestState from 'Common/Types/CodeRepository/PullRequestState';
import OneUptimeDate from 'Common/Types/Date';
import { JSONArray, JSONObject } from 'Common/Types/JSON';
import API from 'Common/Utils/API';
import Execute from '../../Execute';
import logger from '../../Logger';
export default class GitHubUtil extends HostedCodeRepository {
@@ -152,6 +154,44 @@ export default class GitHubUtil extends HostedCodeRepository {
}
public override async addRemote(data: { remoteName: string; organizationName: string; repositoryName: string; }): Promise<void> {
const url: URL = URL.fromString(
`https://github.com/${data.organizationName}/${data.repositoryName}.git`
);
const command: string = `git remote add ${data.remoteName} ${url.toString()}`;
logger.debug("Executing command: " + command);
const result: string = await Execute.executeCommand(command);
logger.debug(result);
}
public override async pushChanges(data: {
branchName: string;
organizationName: string;
repoName: string;
}){
const branchName: string = data.branchName;
const username: string = this.username;
const password: string = this.authToken;
logger.debug("Pushing changes to remote repository with username: " + username);
const command: string = `git push -u https://${username}:${password}@github.com/${data.organizationName}/${data.repositoryName}.git ${branchName}`;
logger.debug("Executing command: " + command);
const result: string = await Execute.executeCommand(command);
logger.debug(result);
}
public override async createPullRequest(data: {
baseBranchName: string;
headBranchName: string;

View File

@@ -5,15 +5,25 @@ import NotImplementedException from 'Common/Types/Exception/NotImplementedExcept
import ServiceRepository from 'Model/Models/ServiceRepository';
export default class HostedCodeRepository {
public constructor(data: { authToken: string }) {
public constructor(data: {
authToken: string,
username: string,
}) {
if (!data.authToken) {
throw new BadDataException('authToken is required');
}
if (!data.username) {
throw new BadDataException('username is required');
}
this.username = data.username;
this.authToken = data.authToken;
}
public authToken: string = '';
public username: string = '';
public async getNumberOfPullRequestsExistForService(data: {
serviceRepository: ServiceRepository;
@@ -89,4 +99,20 @@ export default class HostedCodeRepository {
}): Promise<PullRequest> {
throw new NotImplementedException();
}
public async pushChanges(_data: {
branchName: string;
organizationName: string;
repoName: string;
}): Promise<void> {
throw new NotImplementedException();
}
public async addRemote(_data: {
remoteName: string;
organizationName: string;
repositoryName: string;
}): Promise<void> {
throw new NotImplementedException();
}
}

View File

@@ -2,3 +2,4 @@ ONEUPTIME_URL=https://oneuptime.com
ONEUPTIME_REPOSITORY_SECRET_KEY=your-repository-secret-key
ONEUPTIME_LOCAL_REPOSITORY_PATH=/repository
GITHUB_TOKEN=
GITHUB_USERNAME=

View File

@@ -24,3 +24,8 @@ export const GetGitHubToken: GetStringOrNullFunction = (): string | null => {
const token: string | null = process.env['GITHUB_TOKEN'] || null;
return token;
};
export const GetGitHubUsername: GetStringOrNullFunction = (): string | null => {
const username: string | null = process.env['GITHUB_USERNAME'] || null;
return username;
}

View File

@@ -26,7 +26,7 @@ const init: PromiseVoidFunction = async (): Promise<void> => {
}`
);
const branchName = 'test-branch-3';
const branchName = 'test-branch-4';
await CodeRepositoryUtil.createOrCheckoutBranch({

View File

@@ -1,5 +1,6 @@
import {
GetGitHubToken,
GetGitHubUsername,
GetLocalRepositoryPath,
GetOneUptimeURL,
GetRepositorySecretKey,
@@ -28,16 +29,26 @@ export default class CodeRepositoryUtil {
public static codeRepositoryResult: CodeRepositoryResult | null = null;
public static gitHubUtil: GitHubUtil | null = null;
public static getGitHubUtil(): GitHubUtil {
if (!this.gitHubUtil) {
const gitHubToken: string | null = GetGitHubToken();
const gitHubUsername: string | null = GetGitHubUsername();
if (!gitHubUsername) {
throw new BadDataException('GitHub Username is required');
}
if (!gitHubToken) {
throw new BadDataException('GitHub Token is required');
}
this.gitHubUtil = new GitHubUtil({
authToken: gitHubToken,
username: gitHubUsername!,
});
}
@@ -49,7 +60,7 @@ export default class CodeRepositoryUtil {
serviceRepository: ServiceRepository;
}): Promise<void> {
const branchName = 'oneuptime-'+(data.serviceRepository.serviceCatalog?.name?.toLowerCase())+'-'+data.branchName;
const branchName = 'oneuptime-' + (data.serviceRepository.serviceCatalog?.name?.toLowerCase()) + '-' + data.branchName;
await CodeRepositoryServerUtil.createBranch({
repoPath: GetLocalRepositoryPath(),
@@ -62,7 +73,7 @@ export default class CodeRepositoryUtil {
branchName: string;
}): Promise<void> {
const branchName = 'oneuptime-'+(data.serviceRepository.serviceCatalog?.name?.toLowerCase())+'-'+data.branchName;
const branchName = 'oneuptime-' + (data.serviceRepository.serviceCatalog?.name?.toLowerCase()) + '-' + data.branchName;
await CodeRepositoryServerUtil.createOrCheckoutBranch({
repoPath: GetLocalRepositoryPath(),
@@ -108,7 +119,7 @@ export default class CodeRepositoryUtil {
})
}
public static async discardChanges(): Promise<void> {
public static async discardChanges(): Promise<void> {
await CodeRepositoryServerUtil.discardChanges({
repoPath: GetLocalRepositoryPath(),
})
@@ -117,7 +128,7 @@ export default class CodeRepositoryUtil {
public static async checkoutBranch(data: {
branchName: string;
}): Promise<void> {
await CodeRepositoryServerUtil.checkoutBranch({
await CodeRepositoryServerUtil.checkoutBranch({
repoPath: GetLocalRepositoryPath(),
branchName: data.branchName,
})
@@ -157,10 +168,28 @@ export default class CodeRepositoryUtil {
branchName: string;
}): Promise<void> {
await CodeRepositoryServerUtil.pushChanges({
repoPath: GetLocalRepositoryPath(),
branchName: data.branchName,
})
const codeRepository: CodeRepositoryModel = await this.getCodeRepository();
if (!codeRepository.mainBranchName) {
throw new BadDataException('Main Branch Name is required');
}
if(!codeRepository.organizationName){
throw new BadDataException('Organization Name is required');
}
if(!codeRepository.repositoryName){
throw new BadDataException('Repository Name is required');
}
if (codeRepository.repositoryHostedAt === CodeRepositoryType.GitHub) {
return await this.getGitHubUtil().pushChanges({
branchName: data.branchName,
organizationName: codeRepository.organizationName,
repoName: codeRepository.repositoryName,
});
}
}
@@ -172,22 +201,22 @@ export default class CodeRepositoryUtil {
const codeRepository: CodeRepositoryModel = await this.getCodeRepository();
if(!codeRepository.mainBranchName){
if (!codeRepository.mainBranchName) {
throw new BadDataException('Main Branch Name is required');
}
if(!codeRepository.organizationName){
if (!codeRepository.organizationName) {
throw new BadDataException('Organization Name is required');
}
if(!codeRepository.repositoryName){
if (!codeRepository.repositoryName) {
throw new BadDataException('Repository Name is required');
}
if(codeRepository.repositoryHostedAt === CodeRepositoryType.GitHub){
if (codeRepository.repositoryHostedAt === CodeRepositoryType.GitHub) {
return await this.getGitHubUtil().createPullRequest({
headBranchName: data.branchName,
headBranchName: data.branchName,
baseBranchName: codeRepository.mainBranchName,
organizationName: codeRepository.organizationName,
repositoryName: codeRepository.repositoryName,
@@ -195,10 +224,10 @@ export default class CodeRepositoryUtil {
body: data.body,
});
}else{
} else {
throw new BadDataException('Code Repository type not supported');
}
}