All files / global/gitlab driver.mjs

78.57% Statements 33/42
92% Branches 23/25
84.61% Functions 11/13
78.04% Lines 32/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146                                                                                                  83x 1x         76x 4x 2x   2x   4x     76x                               76x 1x 1x             1x     76x 3x 3x 3x     76x 1x     76x 1x     76x 1x     76x 1x       76x 2x             76x 166x   82x               83x 82x           1x        
/*
  Copyright (C) 2023 Sber
 
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
 
          http://www.apache.org/licenses/LICENSE-2.0
 
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/
 
/*
  Copyright (C) 2021 owner Roman Piontik R.Piontik@mail.ru
 
  Copyright (C) 2022 Sber
 
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
 
  http://www.apache.org/licenses/LICENSE-2.0
 
  In any derivative products, you must retain the information of
  owner of the original code and provide clear attribution to the project
 
  https://dochub.info
 
  The use of this product or its derivatives for any purpose cannot be a secret.
 
 
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
 
  Maintainers:
      R.Piontik <r.piontik@mail.ru>
 
  Contributors:
  	  R.Piontik <r.piontik@mail.ru> - 2023
*/
 
function assertGitlabAdapterUrl(config) {
	if (!config.gitlabAdapterUrl) {
		throw Error('Не задана переменная VUE_APP_DOCHUB_GITLAB_ADAPTER_URL, она обязательна при VUE_APP_DOCHUB_GITLAB_MODE=adapter');
	}
}
 
export default function(config) {
	this.axiosInterceptor = async(params) => {
		if (config.gitlab_server && ((new URL(params.url)).host === (new URL(config.gitlab_server)).host)) {
			if (!params.headers) params.headers = {};
			// eslint-disable-next-line no-undef
			params.headers['PRIVATE-TOKEN'] = `${config.personalToken || Vuex?.state?.access_token}`;
		}
		return params;
	};
	// Вспомогательные функции
	this.parseHashParams = (hash) => {
		let hashParams = {};
		let a = /\+/g,  // Regex for replacing addition symbol with a space
			r = /([^&;=]+)=?([^&;]*)/g,
			d = function(s) {
				return decodeURIComponent(s.replace(a, ' '));
			},
			e = r.exec(hash);
 
		if (e) {
			hashParams[d(e[1])] = d(e[2]);
		}
 
		return hashParams;
	};
 
	this.parseURI = (uri) => {
		let url = new URL(uri);
		let result = {
			branch: url.searchParams.get('ref'),
			project: 1 * url.pathname.split('/')[4],
			file: decodeURIComponent(
				url.pathname.split('/repository/files/')[1].replace('/raw', '')
			)
		};
		return result;
	};
 
	this.isGitLabURI = (uri) => {
		let gitlab_server = new URL(config.gitlab_server);
		let uri_server = new URL(uri);
		return gitlab_server.hostname === uri_server.hostname;
	};
 
	this.projectLanguagesURI = (ProjectID) => {
		return new URL(`api/v4/projects/${ProjectID}/languages`, config.gitlab_server);
	};
 
	this.projectsListURI = (page) => {
		return new URL(`api/v4/projects?simple=1&page=${page}`, config.gitlab_server);
	};
 
	this.projectSingleURI = (id) => {
		return new URL(`api/v4/projects/${id}?simple=1`, config.gitlab_server);
	};
 
	this.branchesListURI = (project, page) =>{
		return new URL(`api/v4/projects/${project}/repository/branches?pagination=keyset&per_page=100&order_by=name&page=${page}`,
			config.gitlab_server);
	};
 
	this.commitsListURI = (project, branch, page, file, page_limit) => {
		return new URL(
			`api/v4/projects/${project}/repository/commits?pagination=keyset`
			+ '&per_page=' + (page_limit ? page_limit : 100) + `&order_by=name&page=${page}&ref_name=${branch}`
			+ (file ? '&path=' + encodeURIComponent(file) : '')
			, config.gitlab_server);
	};
 
	this.makeFileURI = (projectID, source, branch, view_type) => {
		switch (config.gitlabMode || 'api') {
			case 'api':
				return new URL(
					`api/v4/projects/${projectID}/repository/files/`
					+ encodeURIComponent(source)
					+ (view_type ? `/${view_type}` : '')
					+ (branch ? `?ref=${branch}` : '')
					, config.gitlab_server
				);
			case 'adapter':
				assertGitlabAdapterUrl(config);
				return new URL('/api/v1/vcs/file/' +
					encodeURIComponent(source).split('%2F').join('/') +
					`?repo=${encodeURIComponent(projectID)}` +
					`&branch=${encodeURIComponent(branch)}`,
					config.gitlabAdapterUrl);
			default:
				throw Error('Некорректно указано значение VUE_APP_DOCHUB_GITLAB_MODE');
		}
	};
}