(
accessToken: string,
projectId: string | number,
sha: string,
state: GitLabCommitStatusState,
options: {
targetUrl?: string;
description?: string;
} = {},
instanceUrl: string = DEFAULT_GITLAB_URL
)
| 2691 | |
| 2692 | /** |
| 2693 | * GitLab allows max 1 year expiration. |
| 2694 | */ |
| 2695 | export function calculateProjectAccessTokenExpiry(daysFromNow: number = 365): string { |
| 2696 | const maxDays = 365; |
| 2697 | const days = Math.min(daysFromNow, maxDays); |
| 2698 | |
| 2699 | const expiryDate = new Date(); |
| 2700 | expiryDate.setDate(expiryDate.getDate() + days); |
| 2701 | |
| 2702 | return expiryDate.toISOString().split('T')[0]; |
| 2703 | } |
| 2704 | |
| 2705 | export function isProjectAccessTokenExpiringSoon( |
| 2706 | expiresAt: string, |
| 2707 | withinDays: number = 7 |
| 2708 | ): boolean { |
| 2709 | const expiryDate = new Date(expiresAt); |
| 2710 | const now = new Date(); |
| 2711 | const daysUntilExpiry = Math.floor( |
| 2712 | (expiryDate.getTime() - now.getTime()) / (1000 * 60 * 60 * 24) |
| 2713 | ); |
| 2714 | |
| 2715 | return daysUntilExpiry <= withinDays; |
| 2716 | } |
| 2717 | |
| 2718 | export type GitLabProjectAccessTokenValidation = |
| 2719 | | { status: 'valid' } |
| 2720 | | { status: 'invalid'; reason: 'unauthorized' | 'forbidden'; httpStatus: number } |
| 2721 | | { |
| 2722 | status: 'unverified'; |
| 2723 | reason: 'instance-url' | 'transport' | 'http-error'; |
| 2724 | httpStatus?: number; |
| 2725 | }; |
| 2726 | |
| 2727 | /** |
| 2728 | * Checks whether a stored token is still valid on GitLab |
| 2729 | * (e.g., it might have been manually revoked by the user). |
| 2730 | * |
| 2731 | * Reports the authenticated probe outcome for the caller's credential policy. |
| 2732 | */ |
| 2733 | export async function validateProjectAccessToken( |
| 2734 | token: string, |
| 2735 | instanceUrl: string = DEFAULT_GITLAB_URL |
| 2736 | ): Promise<GitLabProjectAccessTokenValidation> { |
| 2737 | let userUrl: string; |
| 2738 | try { |
| 2739 | userUrl = buildGitLabUrl(instanceUrl, '/api/v4/user'); |
| 2740 | } catch (error) { |
| 2741 | logExceptInTest('[validateProjectAccessToken] Invalid GitLab instance URL', { |
| 2742 | error: error instanceof Error ? error.message : String(error), |
| 2743 | }); |
| 2744 | return { status: 'unverified', reason: 'instance-url' }; |
| 2745 | } |
no test coverage detected