Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug job id return in gitlab host connector #1604

Merged
merged 14 commits into from
Jun 19, 2024
3 changes: 2 additions & 1 deletion src/ts/plugins/server/GitlabConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface GitlabOptions {
repoPrefix: string
scope: string
domain: string
timeOut: number
}

export interface GitlabToken {
Expand Down Expand Up @@ -201,7 +202,7 @@ export default class GitlabConnector implements StorageConnector {
if(!this.options.clientId) throw new Error('Missing Gitlab client ID')
if(!this.options.clientSecret) throw new Error('Missing Gitlab client secret')
if(!this.options.domain) throw new Error('Missing Gitlab domain')

if(!this.options.timeOut) this.options.timeOut = 15000 /* default value */
}

// **
Expand Down
37 changes: 25 additions & 12 deletions src/ts/plugins/server/GitlabHostingConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ import GitlabConnector, { GitlabOptions, GitlabSession} 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'
import { stat } from 'fs'
import { setTimeout } from 'timers/promises'

const waitTimeOut = 5000 /* for wait loop in job pages getting */

export default class GitlabHostingConnector extends GitlabConnector implements HostingConnector {

Expand Down Expand Up @@ -80,8 +81,8 @@ export default class GitlabHostingConnector extends GitlabConnector implements H
// Update the job status
if(status === JobStatus.SUCCESS) {
/* Squash and tag the commits */
const succes = await this.createTag(session, websiteId, job, { startJob, jobSuccess, jobError })
if(!succes) {
const successTag = await this.createTag(session, websiteId, job, { startJob, jobSuccess, jobError })
if(!successTag) {
// jobError will have been called in createTag
return
}
Expand All @@ -100,7 +101,7 @@ export default class GitlabHostingConnector extends GitlabConnector implements H
job.logs[0].push(`Page URL: ${pageUrl}`)
job.message = 'Getting the deployment logs URL...'
job.logs[0].push(job.message)
const gitlabJobLogsUrl = await this.getGitlabJobLogsUrl(session, websiteId, adminUrl)
const gitlabJobLogsUrl = await this.getGitlabJobLogsUrl(session, websiteId, job, { startJob, jobSuccess, jobError }, adminUrl, successTag)
job.logs[0].push(`Deployment logs URL: ${gitlabJobLogsUrl}`)
const message = `
<p><a href="${gitlabUrl}" target="_blank">Your website is now live here</a>.</p>
Expand Down Expand Up @@ -146,12 +147,23 @@ export default class GitlabHostingConnector extends GitlabConnector implements H
return `${projectUrl}/pages`
}

async getGitlabJobLogsUrl(session: GitlabSession, websiteId: WebsiteId, projectUrl: string): Promise<string> {
const jobs = await this.callApi(session, `api/v4/projects/${websiteId}/jobs`, 'GET')
return `${projectUrl}/-/jobs/${jobs[0].id}`
// waiting for the job corresponding to the current tag
async getGitlabJobLogsUrl(session: GitlabSession, websiteId: WebsiteId, job: PublicationJobData, { startJob, jobSuccess, jobError }: JobManager, projectUrl: string, tag): Promise<string> {
const t0 = Date.now()
do {
const jobs = await this.callApi(session, `api/v4/projects/${websiteId}/jobs`, 'GET')
if (jobs[0].ref === tag) {return `${projectUrl}/-/jobs/${jobs[0].id}`}
await setTimeout(waitTimeOut)
} while ((Date.now() - t0) < this.options.timeOut)

// failed in timelaps allowed (avoiding infinite loop)
jobError(job.jobId, 'Failed to get job id')
job.message = 'Unable to get job id'
job.logs[0].push(job.message)
return `${projectUrl}/-/jobs/`
}

async createTag(session: GitlabSession, websiteId: WebsiteId, job: JobData, { startJob, jobSuccess, jobError }: JobManager): Promise<boolean> {
async createTag(session: GitlabSession, websiteId: WebsiteId, job: JobData, { startJob, jobSuccess, jobError }: JobManager): Promise<string | null> {
const projectId = websiteId // Assuming websiteId corresponds to GitLab project ID

// Fetch the latest tag and determine the new tag
Expand All @@ -166,7 +178,7 @@ export default class GitlabHostingConnector extends GitlabConnector implements H
} catch (error) {
console.error('Error during fetching latest tag:', error.message)
jobError(job.jobId, `Failed to fetch latest tag: ${error.message}`)
return false
return null
}

// Create a new tag
Expand All @@ -180,9 +192,10 @@ export default class GitlabHostingConnector extends GitlabConnector implements H
} catch (error) {
console.error('Error during creating new tag:', error.message)
jobError(job.jobId, `Failed to create new tag: ${error.message}`)
return false
return null
}
return true
// return new tag
return newTag
}

}
Loading