Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 30 additions & 28 deletions assets/scripts/buildStatus.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import GitAgent from './GitAgent.js'
import { getOAuthServiceAPI } from './oauth-services-api/index.js'
import { isItStillCompiling } from './utils.js'

/**
*
Expand Down Expand Up @@ -44,7 +45,10 @@ export default function (scribouilliGitRepo, gitAgent) {
reaction(repoStatus)
}

if (repoStatus === 'in_progress' || repoStatus === 'not_public') {
if (
repoStatus === 'in_progress' ||
repoStatus === 'needs_account_verification'
) {
scheduleCheck()
}
})
Expand All @@ -68,9 +72,6 @@ export default function (scribouilliGitRepo, gitAgent) {
return buildStatusObject
}

/** Delay (in seconds) after which a non-updated website is assumed to have failed to build. */
const ERROR_DELAY = 60

/**
* mimoza includes the hash of the latest built commit in a comment in the HTML
* of each page. We use that to know which version is currently online, and
Expand All @@ -82,30 +83,33 @@ const ERROR_DELAY = 60
*/
async function getBuildStatus(currentRepository, gitAgent) {
const publishedWebsiteURL = await currentRepository.publishedWebsiteURL
const lastCommit = await gitAgent.currentCommit()

let html
try {
const req = await fetch(publishedWebsiteURL, {
cache: 'no-store',
})

const url = new URL(req.url)
if (req.redirected && url.hostname.endsWith('projects.gitlab.io')) {
throw new Error(
'Redirection vers la page de login GitLab: le site est privé',
)
}

html = await req.text()
} catch {
// If the website is a "private" GitLab repo, we will receive a redirection to
// GitLab to login, which will fail because of CORS. In case it doesn't fail,
// the Error we throw above should still catch this redirection.
return 'not_public'
const response = await fetch(publishedWebsiteURL, {
cache: 'no-store',
})

const url = new URL(response.url)

if (response.redirected && url.hostname.endsWith('projects.gitlab.io')) {
// We handle the case where GitLab redirects to the login page
// because the account is not verified.
return 'needs_account_verification'
}

const dom = new DOMParser().parseFromString(html, 'text/html')
if (!response.ok && isItStillCompiling(lastCommit)) {
return 'in_progress'
}

const lastCommit = await gitAgent.currentCommit()
if (!response.ok) {
return 'error'
}

html = await response.text()

const dom = new DOMParser().parseFromString(html, 'text/html')

for (const node of dom.documentElement.childNodes) {
if (node.nodeType === dom.COMMENT_NODE) {
Expand All @@ -125,12 +129,10 @@ async function getBuildStatus(currentRepository, gitAgent) {
if (hash === lastCommit.oid.slice(0, 7)) {
return 'success'
} else {
const currentTime = new Date().getTime() / 1000
const deltaTime = currentTime - lastCommit.committer.timestamp
if (deltaTime > ERROR_DELAY) {
return 'error'
} else {
if (isItStillCompiling(lastCommit)) {
return 'in_progress'
} else {
return 'error'
}
}
}
Expand Down
31 changes: 13 additions & 18 deletions assets/scripts/components/Header.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
})
}

/** @type {boolean} */
let notPublic
$: notPublic = status === 'not_public'
/** @type {boolean}*/
let needsAccountVerification
$: needsAccountVerification = status === 'needs_account_verification'

$: buildStatusClass = buildStatus ? `build-${status}` : undefined

Expand Down Expand Up @@ -67,11 +67,6 @@
let resolutionURL;
$: resolutionURL = makeResolutionDesynchronisationURL(account || '', repoName || '')

/** @type {string | undefined} */
let gitlabSettingsUrl
$: gitlabSettingsUrl = currentRepository?.publicRepositoryURL
? `${currentRepository.publicRepositoryURL}/edit#js-shared-permissions`
: undefined
</script>

<header>
Expand Down Expand Up @@ -156,19 +151,19 @@
</section>
{/if}


{#if notPublic}
{#if needsAccountVerification}
<section class="warning warning-public">
<p class="centered"><span>⚠️</span> <strong>Votre site n'est pas (encore) public.</strong></p>
<p class="centered"><span>⚠️</span> <strong>Attention..</strong></p>

<p>Pour le rendre public, il vous faut :</p>
<p>
Votre site ne peut pas être publié car GitLab exige de vérifier votre identité avec un n° de téléphone (ou un n° de carte bleue).
</p>

<ol>
<li>Aller sur la page "Paramètres / Général / <a href="{gitlabSettingsUrl}">Visibilité, fonctionnalités du projet, autorisations</a>" de Gitlab</li>
<li>Trouver la sous-section "<strong>Pages</strong>"</li>
<li>Choisir, au lieu de "Only project members", "<strong>Everyone with access</strong>" dans la liste déroulante</li>
<li>Enregistrer le changement en appuyant sur <strong>le bouton bleu "Save changes"</strong></li>
</ol>
<p>
<a href="https://gitlab.com/-/identity_verification" class="btn btn__medium">
Vérifier mon identité
</a>
</p>
</section>
{/if}

Expand Down
23 changes: 21 additions & 2 deletions assets/scripts/types/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
*/

/**
* @description On pense que ça correspond au build status de GitHub?
* @typedef {"in_progress" | "success" | "error" | "not_public"} BuildStatus
* @typedef {"in_progress" | "success" | "error" | "needs_account_verification" } BuildStatus
*/

/**
Expand Down Expand Up @@ -67,3 +66,23 @@
* @property {Object} owner
* @property {string} owner.login
*/

/**
* A git commit object.
*
* @typedef {Object} CommitObject
* @property {string} message Commit message
* @property {string} tree SHA-1 object id of corresponding file tree
* @property {string[]} parent an array of zero or more SHA-1 object ids
* @property {Object} author
* @property {string} author.name The author's name
* @property {string} author.email The author's email
* @property {number} author.timestamp UTC Unix timestamp in seconds
* @property {number} author.timezoneOffset Timezone difference from UTC in minutes
* @property {Object} committer
* @property {string} committer.name The committer's name
* @property {string} committer.email The committer's email
* @property {number} committer.timestamp UTC Unix timestamp in seconds
* @property {number} committer.timezoneOffset Timezone difference from UTC in minutes
* @property {string} [gpgsig] PGP signature (if present)
*/
13 changes: 13 additions & 0 deletions assets/scripts/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,16 @@ export const logMessage = (errorMessage, caller = 'unknown', level = 'log') => {
* @returns {Promise<void>}
*/
export const delay = ms => new Promise(resolve => setTimeout(resolve, ms))

/**
*
* @param {CommitObject} lastCommit
* @returns {boolean}
*/
export const isItStillCompiling = lastCommit => {
// Delay (in seconds) after which a non-updated website is assumed to have failed to build.
const ERROR_DELAY = 60 * 1000
const currentTime = new Date().getTime() / 1000
const deltaTime = currentTime - lastCommit.committer.timestamp
return deltaTime <= ERROR_DELAY
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"ts": "tsc && echo 'Tout va bien 🎉'",
"dev": "rollup -c -w",
"build": "rollup -c",
"test": "mocha tests/**/*.test.js",
"test": "mocha --recursive tests",
"prepare": "husky install",
"svelte-check": "svelte-check --tsconfig tsconfig.json"
},
Expand Down
49 changes: 49 additions & 0 deletions tests/utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import './setup.js'
import { isItStillCompiling } from '../assets/scripts/utils.js'

describe('Utils function', () => {
let now, clock

beforeEach(() => {
now = new Date('2024-12-03 10:00')
clock = sinon.useFakeTimers(now.getTime())
})

afterEach(() => {
clock.restore()
})

describe('#isItStillCompiling', () => {
it('returns true when just commited', () => {
const lastCommit = {
committer: {
timestamp: now.getTime() / 1000,
},
}

expect(isItStillCompiling(lastCommit)).to.be.true
})

it('returns true when commited since less than DELAY', () => {
const commitDatetime = new Date('2024-12-03 10:29')
const lastCommit = {
committer: {
timestamp: commitDatetime.getTime() / 1000,
},
}

expect(isItStillCompiling(lastCommit)).to.be.true
})

it('returns false when commited since more than DELAY', () => {
const commitDatetime = new Date('2024-12-02 15:30')
const lastCommit = {
committer: {
timestamp: commitDatetime.getTime() / 1000,
},
}

expect(isItStillCompiling(lastCommit)).to.be.false
})
})
})
Loading