fix: Github token
This commit is contained in:
parent
3bd2183655
commit
22200fd8a7
@ -1,4 +1,6 @@
|
||||
<script lang="ts">
|
||||
import { browser } from '$app/env';
|
||||
|
||||
import { goto } from '$app/navigation';
|
||||
|
||||
export let githubToken;
|
||||
@ -33,13 +35,9 @@
|
||||
let token = null;
|
||||
|
||||
async function loadRepositoriesByPage(page = 0) {
|
||||
try {
|
||||
return await get(`${apiUrl}/installation/repositories?per_page=100&page=${page}`, {
|
||||
Authorization: `token ${token}`
|
||||
});
|
||||
} catch ({ error }) {
|
||||
return errorNotification(error);
|
||||
}
|
||||
return await get(`${apiUrl}/installation/repositories?per_page=100&page=${page}`, {
|
||||
Authorization: `token ${$session.ghToken}`
|
||||
});
|
||||
}
|
||||
async function loadRepositories() {
|
||||
token = await getGithubToken({ apiUrl, githubToken, application });
|
||||
@ -90,7 +88,41 @@
|
||||
}
|
||||
|
||||
onMount(async () => {
|
||||
await loadRepositories();
|
||||
try {
|
||||
await loadRepositories();
|
||||
} catch (error) {
|
||||
if (
|
||||
error.error === 'invalid_token' ||
|
||||
error.error_description ===
|
||||
'Token is expired. You can either do re-authorization or token refresh.' ||
|
||||
error.message === '401 Unauthorized'
|
||||
) {
|
||||
if (application.gitSource.gitlabAppId) {
|
||||
let htmlUrl = application.gitSource.htmlUrl;
|
||||
const left = screen.width / 2 - 1020 / 2;
|
||||
const top = screen.height / 2 - 618 / 2;
|
||||
const newWindow = open(
|
||||
`${htmlUrl}/oauth/authorize?client_id=${application.gitSource.gitlabApp.appId}&redirect_uri=${window.location.origin}/webhooks/gitlab&response_type=code&scope=api+email+read_repository&state=${$page.params.id}`,
|
||||
'GitLab',
|
||||
'resizable=1, scrollbars=1, fullscreen=0, height=618, width=1020,top=' +
|
||||
top +
|
||||
', left=' +
|
||||
left +
|
||||
', toolbar=0, menubar=0, status=0'
|
||||
);
|
||||
const timer = setInterval(() => {
|
||||
if (newWindow?.closed) {
|
||||
clearInterval(timer);
|
||||
window.location.reload();
|
||||
}
|
||||
}, 100);
|
||||
}
|
||||
}
|
||||
if (error.message === 'Bad credentials') {
|
||||
browser && window.location.reload();
|
||||
}
|
||||
return errorNotification(error);
|
||||
}
|
||||
});
|
||||
async function handleSubmit() {
|
||||
try {
|
||||
|
@ -1,10 +1,11 @@
|
||||
import { getTeam, getUserDetails } from '$lib/common';
|
||||
import { getUserDetails } from '$lib/common';
|
||||
import { getGithubToken } from '$lib/components/common';
|
||||
import * as db from '$lib/database';
|
||||
import { ErrorHandler } from '$lib/database';
|
||||
import { checkContainer } from '$lib/haproxy';
|
||||
import type { RequestHandler } from '@sveltejs/kit';
|
||||
import jsonwebtoken from 'jsonwebtoken';
|
||||
import { get as getRequest } from '$lib/api';
|
||||
|
||||
export const get: RequestHandler = async (event) => {
|
||||
const { teamId, status, body } = await getUserDetails(event);
|
||||
@ -20,15 +21,33 @@ export const get: RequestHandler = async (event) => {
|
||||
const application = await db.getApplication({ id, teamId });
|
||||
const { gitSource } = application;
|
||||
if (gitSource?.type === 'github' && gitSource?.githubApp) {
|
||||
const payload = {
|
||||
iat: Math.round(new Date().getTime() / 1000),
|
||||
exp: Math.round(new Date().getTime() / 1000 + 60),
|
||||
iss: gitSource.githubApp.appId
|
||||
};
|
||||
githubToken = jsonwebtoken.sign(payload, gitSource.githubApp.privateKey, {
|
||||
algorithm: 'RS256'
|
||||
});
|
||||
ghToken = await getGithubToken({ apiUrl: gitSource.apiUrl, application, githubToken });
|
||||
if (!event.locals.session.data.ghToken) {
|
||||
const payload = {
|
||||
iat: Math.round(new Date().getTime() / 1000),
|
||||
exp: Math.round(new Date().getTime() / 1000 + 600),
|
||||
iss: gitSource.githubApp.appId
|
||||
};
|
||||
githubToken = jsonwebtoken.sign(payload, gitSource.githubApp.privateKey, {
|
||||
algorithm: 'RS256'
|
||||
});
|
||||
ghToken = await getGithubToken({ apiUrl: gitSource.apiUrl, application, githubToken });
|
||||
} else {
|
||||
try {
|
||||
await getRequest(`${gitSource.apiUrl}/installation/repositories`, {
|
||||
Authorization: `token ${event.locals.session.data.ghToken}`
|
||||
});
|
||||
} catch (error) {
|
||||
const payload = {
|
||||
iat: Math.round(new Date().getTime() / 1000),
|
||||
exp: Math.round(new Date().getTime() / 1000 + 600),
|
||||
iss: gitSource.githubApp.appId
|
||||
};
|
||||
githubToken = jsonwebtoken.sign(payload, gitSource.githubApp.privateKey, {
|
||||
algorithm: 'RS256'
|
||||
});
|
||||
ghToken = await getGithubToken({ apiUrl: gitSource.apiUrl, application, githubToken });
|
||||
}
|
||||
}
|
||||
}
|
||||
if (application.destinationDockerId) {
|
||||
isRunning = await checkContainer(application.destinationDocker.engine, id);
|
||||
|
Loading…
x
Reference in New Issue
Block a user