From 5ae9063598c981bf4a84c36b03ec908afb7bb49c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Minelli?= <michael@minelli.me> Date: Fri, 23 Feb 2024 18:27:54 +0100 Subject: [PATCH] Move to GitBeaker --- helpers/Dojo/SharedAssignmentHelper.ts | 12 +-- managers/SharedGitlabManager.ts | 37 +++++---- types/Gitlab/GitlabAccessLevel.ts | 11 --- types/Gitlab/GitlabCommit.ts | 17 ---- types/Gitlab/GitlabFile.ts | 16 ---- types/Gitlab/GitlabGroup.ts | 10 --- types/Gitlab/GitlabMember.ts | 12 --- types/Gitlab/GitlabMilestone.ts | 19 ----- types/Gitlab/GitlabNamespace.ts | 13 --- types/Gitlab/GitlabPipeline.ts | 31 -------- types/Gitlab/GitlabProfile.ts | 40 ---------- types/Gitlab/GitlabRelease.ts | 29 ------- types/Gitlab/GitlabRepository.ts | 106 ------------------------- types/Gitlab/GitlabRoute.ts | 25 ------ types/Gitlab/GitlabTreeFile.ts | 13 --- types/Gitlab/GitlabUser.ts | 11 --- types/Gitlab/GitlabVisibility.ts | 6 +- 17 files changed, 28 insertions(+), 380 deletions(-) delete mode 100644 types/Gitlab/GitlabAccessLevel.ts delete mode 100644 types/Gitlab/GitlabCommit.ts delete mode 100644 types/Gitlab/GitlabFile.ts delete mode 100644 types/Gitlab/GitlabGroup.ts delete mode 100644 types/Gitlab/GitlabMember.ts delete mode 100644 types/Gitlab/GitlabMilestone.ts delete mode 100644 types/Gitlab/GitlabNamespace.ts delete mode 100644 types/Gitlab/GitlabPipeline.ts delete mode 100644 types/Gitlab/GitlabProfile.ts delete mode 100644 types/Gitlab/GitlabRelease.ts delete mode 100644 types/Gitlab/GitlabRepository.ts delete mode 100644 types/Gitlab/GitlabRoute.ts delete mode 100644 types/Gitlab/GitlabTreeFile.ts delete mode 100644 types/Gitlab/GitlabUser.ts diff --git a/helpers/Dojo/SharedAssignmentHelper.ts b/helpers/Dojo/SharedAssignmentHelper.ts index f7547cc..5b11a38 100644 --- a/helpers/Dojo/SharedAssignmentHelper.ts +++ b/helpers/Dojo/SharedAssignmentHelper.ts @@ -1,9 +1,9 @@ import AssignmentFile from '../../types/Dojo/AssignmentFile'; -import GitlabPipelineStatus from '../../types/Gitlab/GitlabPipelineStatus'; import DojoStatusCode from '../../types/Dojo/DojoStatusCode'; -import GitlabPipeline from '../../types/Gitlab/GitlabPipeline'; -import SharedGitlabManager from '../../managers/SharedGitlabManager'; import Json5FileValidator from '../Json5FileValidator'; +import * as Gitlab from '@gitbeaker/rest'; +import GitlabPipelineStatus from '../../types/Gitlab/GitlabPipelineStatus'; +import GlobalHelper from '../../../helpers/GlobalHelper'; class SharedAssignmentHelper { @@ -19,11 +19,11 @@ class SharedAssignmentHelper { } } - async isPublishable(repositoryId: number): Promise<{ isPublishable: boolean, lastPipeline: GitlabPipeline | null, status?: { code: DojoStatusCode, message: string } }> { - const pipelines = await SharedGitlabManager.getRepositoryPipelines(repositoryId, 'main'); + async isPublishable(repositoryId: number): Promise<{ isPublishable: boolean, lastPipeline: Gitlab.PipelineSchema | null, status?: { code: DojoStatusCode, message: string } }> { + const pipelines = await GlobalHelper.sharedGitlabManager.getRepositoryPipelines(repositoryId, 'main'); if ( pipelines.length > 0 ) { const lastPipeline = pipelines[0]; - if ( lastPipeline.status !== GitlabPipelineStatus.SUCCESS ) { + if ( lastPipeline.status != GitlabPipelineStatus.SUCCESS.valueOf() ) { return { isPublishable: false, lastPipeline : lastPipeline, diff --git a/managers/SharedGitlabManager.ts b/managers/SharedGitlabManager.ts index 68ff699..6984792 100644 --- a/managers/SharedGitlabManager.ts +++ b/managers/SharedGitlabManager.ts @@ -1,13 +1,22 @@ -import axios from 'axios'; -import GitlabPipeline from '../types/Gitlab/GitlabPipeline'; -import GitlabRoute from '../types/Gitlab/GitlabRoute'; -import SharedConfig from '../config/SharedConfig'; -import GitlabToken from '../types/Gitlab/GitlabToken'; +import axios from 'axios'; +import SharedConfig from '../config/SharedConfig'; +import * as GitlabCore from '@gitbeaker/core'; +import { Gitlab, PipelineSchema } from '@gitbeaker/rest'; +import GitlabToken from '../types/Gitlab/GitlabToken'; -class GitlabManager { - private getApiUrl(route: GitlabRoute): string { - return `${ SharedConfig.gitlab.apiURL }${ route }`; +class SharedGitlabManager { + private api!: GitlabCore.Gitlab<false>; + + setToken(token: string) { + this.api = new Gitlab({ + host : SharedConfig.gitlab.URL, + token: token + }); + } + + constructor(token: string) { + this.setToken(token); } async getTokens(codeOrRefresh: string, isRefresh: boolean = false, clientSecret: string = ''): Promise<GitlabToken> { @@ -23,16 +32,12 @@ class GitlabManager { return response.data; } - async getRepositoryPipelines(repoId: number, branch: string = 'main'): Promise<Array<GitlabPipeline>> { - const response = await axios.get<Array<GitlabPipeline>>(this.getApiUrl(GitlabRoute.REPOSITORY_PIPELINES).replace('{{id}}', String(repoId)), { - params: { - ref: branch - } + async getRepositoryPipelines(repoId: number, branch: string = 'main'): Promise<Array<PipelineSchema>> { + return await this.api.Pipelines.all(repoId, { + ref: branch }); - - return response.data; } } -export default new GitlabManager(); +export default SharedGitlabManager; diff --git a/types/Gitlab/GitlabAccessLevel.ts b/types/Gitlab/GitlabAccessLevel.ts deleted file mode 100644 index be06ffd..0000000 --- a/types/Gitlab/GitlabAccessLevel.ts +++ /dev/null @@ -1,11 +0,0 @@ -enum GitlabAccessLevel { - GUEST = 10, - REPORTER = 20, - DEVELOPER = 30, - MAINTAINER = 40, - OWNER = 50, - ADMIN = 60 -} - - -export default GitlabAccessLevel; diff --git a/types/Gitlab/GitlabCommit.ts b/types/Gitlab/GitlabCommit.ts deleted file mode 100644 index 5f94c2e..0000000 --- a/types/Gitlab/GitlabCommit.ts +++ /dev/null @@ -1,17 +0,0 @@ -interface GitlabCommit { - id: string; - short_id: string; - created_at: string; - parent_ids: Array<string>; - title: string; - message: string; - author_name: string; - author_email: string; - authored_date: string; - committer_name: string; - committer_email: string; - committed_date: string; -} - - -export default GitlabCommit; \ No newline at end of file diff --git a/types/Gitlab/GitlabFile.ts b/types/Gitlab/GitlabFile.ts deleted file mode 100644 index 05205d4..0000000 --- a/types/Gitlab/GitlabFile.ts +++ /dev/null @@ -1,16 +0,0 @@ -interface GitlabFile { - file_name: string, - file_path: string, - size: number, - encoding: string, - content_sha256: string, - ref: string, - blob_id: string, - commit_id: string, - last_commit_id: string, - execute_filemode: boolean, - content: string, -} - - -export default GitlabFile; \ No newline at end of file diff --git a/types/Gitlab/GitlabGroup.ts b/types/Gitlab/GitlabGroup.ts deleted file mode 100644 index 812f883..0000000 --- a/types/Gitlab/GitlabGroup.ts +++ /dev/null @@ -1,10 +0,0 @@ -interface GitlabGroup { - group_id: number, - group_name: string, - group_full_path: string, - group_access_level: number, - expires_at: string -} - - -export default GitlabGroup; \ No newline at end of file diff --git a/types/Gitlab/GitlabMember.ts b/types/Gitlab/GitlabMember.ts deleted file mode 100644 index 6c7c716..0000000 --- a/types/Gitlab/GitlabMember.ts +++ /dev/null @@ -1,12 +0,0 @@ -import GitlabUser from './GitlabUser'; - - -interface GitlabMember extends GitlabUser { - access_level: number, - created_at: string, - created_by: GitlabUser, - expires_at: string | null -} - - -export default GitlabMember; \ No newline at end of file diff --git a/types/Gitlab/GitlabMilestone.ts b/types/Gitlab/GitlabMilestone.ts deleted file mode 100644 index a7285cd..0000000 --- a/types/Gitlab/GitlabMilestone.ts +++ /dev/null @@ -1,19 +0,0 @@ -interface GitlabMilestone { - id: number; - iid: number; - project_id: number; - title: string; - description: string; - state: string; - created_at: string; - updated_at: string; - due_date: string; - start_date: string; - web_url: string; - issue_stats: { - total: number; closed: number; - }; -} - - -export default GitlabMilestone; \ No newline at end of file diff --git a/types/Gitlab/GitlabNamespace.ts b/types/Gitlab/GitlabNamespace.ts deleted file mode 100644 index 4d892af..0000000 --- a/types/Gitlab/GitlabNamespace.ts +++ /dev/null @@ -1,13 +0,0 @@ -interface GitlabNamespace { - id: number, - name: string, - path: string, - kind: string, - full_path: string, - parent_id: number, - avatar_url: string, - web_url: string -} - - -export default GitlabNamespace; \ No newline at end of file diff --git a/types/Gitlab/GitlabPipeline.ts b/types/Gitlab/GitlabPipeline.ts deleted file mode 100644 index 1ee75b6..0000000 --- a/types/Gitlab/GitlabPipeline.ts +++ /dev/null @@ -1,31 +0,0 @@ -import GitlabPipelineStatus from './GitlabPipelineStatus'; -import GitlabPipelineSource from './GitlabPipelineSource'; -import GitlabUser from './GitlabUser'; - - -interface GitlabPipeline { - id: number, - iid: number, - project_id: number, - status: GitlabPipelineStatus, - source: GitlabPipelineSource, - ref: string, - sha: string, - before_sha: string, - tag: boolean, - name: string, - yaml_errors: string | null, - user: GitlabUser, - web_url: string, - created_at: string, - updated_at: string, - started_at: string | null, - finished_at: string | null, - committed_at: string | null, - duration: number | null, - queued_duration: number | null, - coverage: string | null, -} - - -export default GitlabPipeline; \ No newline at end of file diff --git a/types/Gitlab/GitlabProfile.ts b/types/Gitlab/GitlabProfile.ts deleted file mode 100644 index aa7506d..0000000 --- a/types/Gitlab/GitlabProfile.ts +++ /dev/null @@ -1,40 +0,0 @@ -import GitlabUser from './GitlabUser'; - - -interface GitlabProfile extends GitlabUser { - created_at: string, - bio: string, - location: string, - public_email: string, - skype: string, - linkedin: string, - twitter: string, - discord: string, - website_url: string, - organization: string, - job_title: string, - pronouns: string, - bot: boolean, - work_information: string, - local_time: string, - last_sign_in_at: string, - confirmed_at: string, - last_activity_on: string, - email: string, - theme_id: number, - color_scheme_id: number, - projects_limit: number, - current_sign_in_at: string, - identities: Array<{ - provider: string, extern_uid: string - }>, - can_create_group: boolean, - can_create_project: boolean, - two_factor_enabled: boolean, - external: boolean, - private_profile: boolean, - commit_email: string -} - - -export default GitlabProfile; \ No newline at end of file diff --git a/types/Gitlab/GitlabRelease.ts b/types/Gitlab/GitlabRelease.ts deleted file mode 100644 index a7c68d7..0000000 --- a/types/Gitlab/GitlabRelease.ts +++ /dev/null @@ -1,29 +0,0 @@ -import GitlabUser from './GitlabUser'; -import GitlabCommit from './GitlabCommit'; -import GitlabMilestone from './GitlabMilestone'; - - -interface GitlabRelease { - tag_name: string; - description: string; - created_at: string; - released_at: string; - author: GitlabUser; - commit: GitlabCommit; - milestones: Array<GitlabMilestone>; - commit_path: string; - tag_path: string; - assets: { - count: number; sources: Array<{ - format: string; url: string; - }>; links: Array<{ - id: number; name: string; url: string; link_type: string; - }>; evidence_file_path: string; - }; - evidences: Array<{ - sha: string; filepath: string; collected_at: string; - }>; -} - - -export default GitlabRelease; \ No newline at end of file diff --git a/types/Gitlab/GitlabRepository.ts b/types/Gitlab/GitlabRepository.ts deleted file mode 100644 index 0aa2602..0000000 --- a/types/Gitlab/GitlabRepository.ts +++ /dev/null @@ -1,106 +0,0 @@ -import GitlabGroup from './GitlabGroup'; -import GitlabNamespace from './GitlabNamespace'; - - -interface GitlabRepository { - id: number, - description: string, - name: string, - name_with_namespace: string, - path: string, - path_with_namespace: string, - created_at: string, - default_branch: string, - tag_list: Array<string>, - topics: Array<string>, - ssh_url_to_repo: string, - http_url_to_repo: string, - web_url: string, - readme_url: string, - forks_count: number, - avatar_url: string, - star_count: number, - last_activity_at: string, - namespace: GitlabNamespace, - _links: { - self: string, issues: string, merge_requests: string, repo_branches: string, labels: string, events: string, members: string, cluster_agents: string - }, - packages_enabled: boolean, - empty_repo: boolean, - archived: boolean, - visibility: string, - resolve_outdated_diff_discussions: boolean, - container_expiration_policy: { - cadence: string, enabled: boolean, keep_n: number, older_than: string, name_regex: string, name_regex_keep: string, next_run_at: string - }, - issues_enabled: boolean, - merge_requests_enabled: boolean, - wiki_enabled: boolean, - jobs_enabled: boolean, - snippets_enabled: boolean, - container_registry_enabled: boolean, - service_desk_enabled: boolean, - service_desk_address: string, - can_create_merge_request_in: boolean, - issues_access_level: string, - repository_access_level: string, - merge_requests_access_level: string, - forking_access_level: string, - wiki_access_level: string, - builds_access_level: string, - snippets_access_level: string, - pages_access_level: string, - operations_access_level: string, - analytics_access_level: string, - container_registry_access_level: string, - security_and_compliance_access_level: string, - releases_access_level: string, - environments_access_level: string, - feature_flags_access_level: string, - infrastructure_access_level: string, - monitor_access_level: string, - emails_disabled: boolean, - shared_runners_enabled: boolean, - lfs_enabled: boolean, - creator_id: number, - import_url: string, - import_type: string, - import_status: string, - import_error: string, - open_issues_count: number, - runners_token: string, - ci_default_git_depth: number, - ci_forward_deployment_enabled: boolean, - ci_job_token_scope_enabled: boolean, - ci_separated_caches: boolean, - ci_opt_in_jwt: boolean, - ci_allow_fork_pipelines_to_run_in_parent_project: boolean, - public_jobs: boolean, - build_git_strategy: string, - build_timeout: number, - auto_cancel_pending_pipelines: string, - ci_config_path: string, - shared_with_groups: Array<GitlabGroup>, - only_allow_merge_if_pipeline_succeeds: boolean, - allow_merge_on_skipped_pipeline: boolean, - restrict_user_defined_variables: boolean, - request_access_enabled: boolean, - only_allow_merge_if_all_discussions_are_resolved: boolean, - remove_source_branch_after_merge: boolean, - printing_merge_request_link_enabled: boolean, - merge_method: string, - squash_option: string, - enforce_auth_checks_on_uploads: boolean, - suggestion_commit_message: string, - merge_commit_template: string, - squash_commit_template: string, - issue_branch_template: string, - auto_devops_enabled: boolean, - auto_devops_deploy_strategy: string, - autoclose_referenced_issues: boolean, - keep_latest_artifact: boolean, - runner_token_expiration_interval: number, -} - - -export default GitlabRepository; \ No newline at end of file diff --git a/types/Gitlab/GitlabRoute.ts b/types/Gitlab/GitlabRoute.ts deleted file mode 100644 index 15f0661..0000000 --- a/types/Gitlab/GitlabRoute.ts +++ /dev/null @@ -1,25 +0,0 @@ -const projectIdRoute = '/projects/{{id}}'; - - -enum GitlabRoute { - NOTIFICATION_SETTINGS = '/notification_settings', - PROFILE_GET = '/user', - USERS_GET = '/users', - REPOSITORY_GET = '/projects/{{id}}', - REPOSITORY_CREATE = '/projects', // eslint-disable-next-line @typescript-eslint/no-duplicate-enum-values - REPOSITORY_DELETE = projectIdRoute, // eslint-disable-next-line @typescript-eslint/no-duplicate-enum-values - REPOSITORY_EDIT = projectIdRoute, - REPOSITORY_FORK = '/projects/{{id}}/fork', - REPOSITORY_MEMBER_ADD = '/projects/{{id}}/members', - REPOSITORY_MEMBERS_GET = '/projects/{{id}}/members/all', - REPOSITORY_RELEASES_GET = '/projects/{{id}}/releases', - REPOSITORY_BADGES_ADD = '/projects/{{id}}/badges', - REPOSITORY_VARIABLES_ADD = '/projects/{{id}}/variables', - REPOSITORY_BRANCHES_PROTECT = '/projects/{{id}}/protected_branches', - REPOSITORY_TREE = '/projects/{{id}}/repository/tree', - REPOSITORY_FILE = '/projects/{{id}}/repository/files/{{filePath}}', - REPOSITORY_PIPELINES = '/projects/{{id}}/pipelines', -} - - -export default GitlabRoute; \ No newline at end of file diff --git a/types/Gitlab/GitlabTreeFile.ts b/types/Gitlab/GitlabTreeFile.ts deleted file mode 100644 index b2cf67e..0000000 --- a/types/Gitlab/GitlabTreeFile.ts +++ /dev/null @@ -1,13 +0,0 @@ -import GitlabTreeFileType from './GitlabTreeFileType'; - - -interface GitlabTreeFile { - id: number, - name: string, - type: GitlabTreeFileType, - path: string, - mode: string -} - - -export default GitlabTreeFile; \ No newline at end of file diff --git a/types/Gitlab/GitlabUser.ts b/types/Gitlab/GitlabUser.ts deleted file mode 100644 index bbb7592..0000000 --- a/types/Gitlab/GitlabUser.ts +++ /dev/null @@ -1,11 +0,0 @@ -interface GitlabUser { - id: number, - username: string, - name: string, - state: string, - avatar_url: string, - web_url: string, -} - - -export default GitlabUser; \ No newline at end of file diff --git a/types/Gitlab/GitlabVisibility.ts b/types/Gitlab/GitlabVisibility.ts index 842ff16..1eaf636 100644 --- a/types/Gitlab/GitlabVisibility.ts +++ b/types/Gitlab/GitlabVisibility.ts @@ -1,8 +1,4 @@ -enum GitlabVisibility { - PUBLIC = 'public', - INTERNAL = 'internal', - PRIVATE = 'private' -} +type GitlabVisibility = 'public' | 'internal' | 'private'; export default GitlabVisibility; -- GitLab