forked from silexlabs/Silex
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP : pour analyse suite modif gitlabConnector par silexlabs#1589
- Loading branch information
Ubuntu
committed
May 20, 2024
1 parent
549bf7b
commit 45c921a
Showing
2 changed files
with
121 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Silex website builder, free/libre no-code tool for makers. | ||
* Copyright (c) 2023 lexoyo and Silex Labs foundation | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
/** | ||
* Gitlab hosting connector | ||
* @fileoverview Gitlab hosting connector for Silex, connect to the user's Gitlab account to host websites with .gitlab-ci.yml file template for plain html by default | ||
* @see https://docs.gitlab.com/ee/api/oauth2.html | ||
* @see https://docs.gitlab.com/ee/user/project/pages/getting_started/pages_ci_cd_template.html | ||
*/ | ||
|
||
import GitlabConnector, { GitlabOptions, GitlabToken } from './GitlabConnector' | ||
import { HostingConnector, ConnectorFile } from '../../server/connectors/connectors' | ||
import { ConnectorType, WebsiteId, JobData, JobStatus, PublicationJobData } from '../../types' | ||
import { JobManager } from '../../server/jobs' | ||
import { join } from 'path' | ||
import { ServerConfig } from '../../server/config' | ||
|
||
//type GitlabSession = ConnectorSession | ||
//type GitlabOption = GitlabOptions | ||
type GitlabSession = Record<string, GitlabToken> | ||
|
||
export default class GitlabHostingConnector extends GitlabConnector implements HostingConnector/*<GitlabSession>*/ { | ||
connectorId = 'gitlab_hosting' | ||
displayName = 'Gitlab hosting' | ||
connectorType = ConnectorType.HOSTING | ||
|
||
constructor( config: ServerConfig, opts: Partial<GitlabOptions>) { | ||
super (config, opts) | ||
/* public directory for standard gitlab pages */ | ||
this.options.assetsFolder= 'public' | ||
} | ||
|
||
async publish(session: GitlabSession, websiteId: WebsiteId, files: ConnectorFile[], {startJob, jobSuccess, jobError}: JobManager): Promise<JobData> { | ||
const job = startJob(`Publishing to ${this.displayName}`) as PublicationJobData | ||
job.logs = [[`Publishing to ${this.displayName}`]] | ||
job.errors = [[]] | ||
/* Configuration file .gitlab-ci.yml contains template for plain html Gitlab pages*/ | ||
const pathYml = '.gitlab-ci.yml' | ||
const contentYml = | ||
`# This file is a template, and might need editing before it works on your project. | ||
# To contribute improvements to CI/CD templates, please follow the Development guide at: | ||
# https://docs.gitlab.com/ee/development/cicd/templates.html | ||
# This specific template is located at: | ||
# https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Pages/HTML.gitlab-ci.yml | ||
# Full project: https://gitlab.com/pages/plain-html | ||
image: busybox | ||
pages: | ||
stage: deploy | ||
environment: production | ||
script: | ||
- echo "The site will be deployed to $CI_PAGES_URL" | ||
artifacts: | ||
paths: | ||
- public | ||
rules: | ||
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH` | ||
console.log(session) | ||
try { | ||
await this.readFile(session, websiteId, pathYml) | ||
} catch (e) { | ||
// If the file .gitlab-ci.yml does not exist, create it, otherwise do nothing (do not overwriting existing one) | ||
if (e.statusCode === 404 || e.httpStatusCode === 404 || e.message.endsWith('A file with this name doesn\'t exist')) { | ||
await this.createFile(session, websiteId, pathYml, contentYml) | ||
} | ||
} | ||
|
||
/* Get Url Gitlab Pages */ | ||
const response = await this.callApi(session, `api/v4/projects/${websiteId}/pages`, 'GET') | ||
const publicationUrl = response.url | ||
|
||
/* publishing all files for website*/ | ||
await this.writeAssets(session, websiteId, files, async ({status, message}) => { | ||
// Update the job status | ||
job.status = status | ||
job.message = message | ||
job.logs[0].push(message) | ||
if(status === JobStatus.SUCCESS) { | ||
jobSuccess(job.jobId, '<a href="' + publicationUrl + '" target="_blank">' + publicationUrl + '<a>') | ||
} else if(status === JobStatus.ERROR) { | ||
job.errors[0].push(message) | ||
jobError(job.jobId, message) | ||
} | ||
}) | ||
return job | ||
} | ||
|
||
async getUrl(session: GitlabSession, websiteId: WebsiteId): Promise<string> { | ||
const id = websiteId | ||
const filePath = join(this.options.domain, id, 'index.html') | ||
const fileUrl = new URL(filePath, 'file://') | ||
return fileUrl.toString() | ||
} | ||
} |