Skip to content

Commit

Permalink
Fix GitHub integration logs (#76)
Browse files Browse the repository at this point in the history
fix: GitHub integration logs
  • Loading branch information
chrisdlangton authored Nov 25, 2024
1 parent 725af44 commit 2b14d3a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 140 deletions.
2 changes: 1 addition & 1 deletion src/layouts/components/DefaultLayoutWithVerticalNav.vue
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ function clearAlerts() {
prepend-icon="mdi-github"
title="Connect GitHub"
link
href="https://github.com/apps/vulnetix/installations/new"
href="https://github.com/apps/vulnetix/installations/select_target"
target="_blank"
></VListItem>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Projects.vue
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ const controller = reactive(new Controller())
<template v-slot:append>
<v-btn-group divided>
<v-btn
href="https://github.com/apps/vulnetix/installations/new"
href="https://github.com/apps/vulnetix/installations/select_target"
:color="global.name.value === 'dark' ? '#fff' : '#272727'"
target="_blank"
prepend-icon="mdi-github"
Expand Down
156 changes: 18 additions & 138 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,21 @@ export class GitHub {
}
const tokenExpiry = (new Date(response.headers.get('GitHub-Authentication-Token-Expiration'))).getTime()
const content = JSON.parse(respText)
if (this.memberEmail && this.orgId) {
const createLog = await this.prisma.IntegrationUsageLog.create({
data: {
memberEmail: this.memberEmail,
orgId: this.orgId,
source: 'github',
request: JSON.stringify({ method: "GET", url }).trim(),
response: JSON.stringify({ body: convertIsoDatesToTimestamps(content), tokenExpiry: tokenExpiry }).trim(),
statusCode: response.status,
createdAt: new Date().getTime(),
}
})
// console.log(`GitHub.fetchJSON()`, createLog)
}

return { ok: response.ok, status: response.status, statusText: response.statusText, tokenExpiry, error: { message: content?.message }, content, url, raw: respText }
} catch (e) {
const [, lineno, colno] = e.stack.match(/(\d+):(\d+)/)
Expand Down Expand Up @@ -837,18 +852,6 @@ export class GitHub {
const url = `${this.baseUrl}/repos/${full_name}/code-scanning/analyses?per_page=${perPage}&page=${page}`
// console.log(`github.getRepoSarif(${full_name}) ${url}`)
const data = await this.fetchJSON(url)
const createLog0 = await this.prisma.IntegrationUsageLog.create({
data: {
memberEmail: this.memberEmail,
orgId: this.orgId,
source: 'github',
request: JSON.stringify({ method: "GET", url }).trim(),
response: JSON.stringify({ body: convertIsoDatesToTimestamps(data.content), tokenExpiry: data.tokenExpiry }).trim(),
statusCode: data?.status || 500,
createdAt: new Date().getTime(),
}
})
// console.log(`GitHub.getRepoSarif()`, createLog0)
if (!data?.ok) {
return data
}
Expand Down Expand Up @@ -896,75 +899,27 @@ export class GitHub {
const url = `${this.baseUrl}/repos/${full_name}/dependency-graph/sbom`
// console.log(`github.getRepoSpdx(${full_name}) ${url}`)
const data = await this.fetchJSON(url)
const createLog = await this.prisma.IntegrationUsageLog.create({
data: {
memberEmail: this.memberEmail,
orgId: this.orgId,
source: 'github',
request: JSON.stringify({ method: "GET", url }).trim(),
response: JSON.stringify({ body: convertIsoDatesToTimestamps(data.content), tokenExpiry: data.tokenExpiry }).trim(),
statusCode: data?.status || 500,
createdAt: new Date().getTime(),
}
})
// console.log(`GitHub.getRepoSpdx()`, createLog)
return data
}
async getUserEmails() {
// https://docs.github.com/en/rest/users/emails?apiVersion=2022-11-28#list-email-addresses-for-the-authenticated-user
const url = `${this.baseUrl}/user/emails`
// console.log(`github.getUserEmails() ${url}`)
const data = await this.fetchJSON(url)
const createLog = await this.prisma.IntegrationUsageLog.create({
data: {
memberEmail: this.memberEmail,
orgId: this.orgId,
source: 'github',
request: JSON.stringify({ method: "GET", url }).trim(),
response: JSON.stringify({ body: convertIsoDatesToTimestamps(data.content), tokenExpiry: data.tokenExpiry }).trim(),
statusCode: data?.status || 500,
createdAt: new Date().getTime(),
}
})
// console.log(`GitHub.getUserEmails()`, createLog)
return data
}
async getUser() {
// https://docs.github.com/en/rest/users/users?apiVersion=2022-11-28#get-the-authenticated-user
const url = `${this.baseUrl}/user`
// console.log(`github.getUser() ${url}`)
const data = await this.fetchJSON(url)
const createLog = await this.prisma.IntegrationUsageLog.create({
data: {
memberEmail: this.memberEmail,
orgId: this.orgId,
source: 'github',
request: JSON.stringify({ method: "GET", url }).trim(),
response: JSON.stringify({ body: convertIsoDatesToTimestamps(data.content), tokenExpiry: data.tokenExpiry }).trim(),
statusCode: data?.status || 500,
createdAt: new Date().getTime(),
}
})
// console.log(`GitHub.getUser()`, createLog)
return data
}
async getInstallations() {
// https://docs.github.com/en/rest/apps/installations?apiVersion=2022-11-28#list-app-installations-accessible-to-the-user-access-token
const url = `${this.baseUrl}/user/installations`
// console.log(`github.getInstallations() ${url}`)
const data = await this.fetchJSON(url)
const createLog = await this.prisma.IntegrationUsageLog.create({
data: {
memberEmail: this.memberEmail,
orgId: this.orgId,
source: 'github',
request: JSON.stringify({ method: "GET", url }).trim(),
response: JSON.stringify({ body: convertIsoDatesToTimestamps(data.content), tokenExpiry: data.tokenExpiry }).trim(),
statusCode: data?.status || 500,
createdAt: new Date().getTime(),
}
})
// console.log(`GitHub.getInstallations()`, createLog)
return data
}
async revokeToken() {
Expand All @@ -976,18 +931,7 @@ export class GitHub {
console.error(`req headers=${JSON.stringify(this.headers, null, 2)}`)
console.error(`GitHub error! status: ${response.status} ${response.statusText}`)
}
const createLog = await this.prisma.IntegrationUsageLog.create({
data: {
memberEmail: this.memberEmail,
orgId: this.orgId,
source: 'github',
request: JSON.stringify({ method, url }).trim(),
response: JSON.stringify({ body: convertIsoDatesToTimestamps(response.content), tokenExpiry: response.tokenExpiry }).trim(),
statusCode: response?.status || 500,
createdAt: new Date().getTime(),
}
})
// console.log(`GitHub.revokeToken()`, createLog)

return { ok: response.ok, status: response.status, statusText: response.statusText, url }
} catch (e) {
const [, lineno, colno] = e.stack.match(/(\d+):(\d+)/)
Expand All @@ -1006,18 +950,6 @@ export class GitHub {
const url = `${this.baseUrl}/user/repos?per_page=${perPage}&page=${page}`
// console.log(`github.getRepos() ${url}`)
const data = await this.fetchJSON(url)
const createLog = await this.prisma.IntegrationUsageLog.create({
data: {
memberEmail: this.memberEmail,
orgId: this.orgId,
source: 'github',
request: JSON.stringify({ method: "GET", url }).trim(),
response: JSON.stringify({ body: convertIsoDatesToTimestamps(data.content), tokenExpiry: data.tokenExpiry }).trim(),
statusCode: data?.status || 500,
createdAt: new Date().getTime(),
}
})
// console.log(`GitHub.getRepos()`, createLog)
if (!data?.ok) {
return data
}
Expand All @@ -1037,18 +969,6 @@ export class GitHub {
const url = `${this.baseUrl}/repos/${repo.full_name}/branches/${branch}`
// console.log(`github.getBranch() ${url}`)
const data = await this.fetchJSON(url)
const createLog = await this.prisma.IntegrationUsageLog.create({
data: {
memberEmail: this.memberEmail,
orgId: this.orgId,
source: 'github',
request: JSON.stringify({ method: "GET", url }).trim(),
response: JSON.stringify({ body: convertIsoDatesToTimestamps(data.content), tokenExpiry: data.tokenExpiry }).trim(),
statusCode: data?.status || 500,
createdAt: new Date().getTime(),
}
})
// console.log(`GitHub.getBranch()`, createLog)
return data
}
async getBranches(full_name) {
Expand All @@ -1061,22 +981,9 @@ export class GitHub {
while (true) {
url = `${this.baseUrl}/repos/${full_name}/branches?per_page=${perPage}&page=${page}`
const data = await this.fetchJSON(url)
const createLog = await this.prisma.IntegrationUsageLog.create({
data: {
memberEmail: this.memberEmail,
orgId: this.orgId,
source: 'github',
request: JSON.stringify({ method: "GET", url }).trim(),
response: JSON.stringify({ body: convertIsoDatesToTimestamps(data.content), tokenExpiry: data.tokenExpiry }).trim(),
statusCode: data?.status || 500,
createdAt: new Date().getTime(),
}
})
console.log(`GitHub.getBranches()`, createLog)
if (data?.content?.status === '401') {
break
}
console.log(data.content)
branches.push(...data.content)

if (data.content.length < perPage) {
Expand All @@ -1093,18 +1000,6 @@ export class GitHub {
const url = `${this.baseUrl}/repos/${full_name}/commits/${commit_sha}`
// console.log(`github.getCommit() ${url}`)
const data = await this.fetchJSON(url)
const createLog = await this.prisma.IntegrationUsageLog.create({
data: {
memberEmail: this.memberEmail,
orgId: this.orgId,
source: 'github',
request: JSON.stringify({ method: "GET", url }).trim(),
response: JSON.stringify({ body: convertIsoDatesToTimestamps(data.content), tokenExpiry: data.tokenExpiry }).trim(),
statusCode: data?.status || 500,
createdAt: new Date().getTime(),
}
})
console.log(`GitHub.getCommit()`, createLog)
return data
}
async getCommits(full_name, branch_name) {
Expand All @@ -1116,25 +1011,10 @@ export class GitHub {
while (true) {
url = `${this.baseUrl}/repos/${full_name}/commits?sha=${branch_name}&per_page=${perPage}&page=${page}`
const data = await this.fetchJSON(url)
const createLog = await this.prisma.IntegrationUsageLog.create({
data: {
memberEmail: this.memberEmail,
orgId: this.orgId,
source: 'github',
request: JSON.stringify({ method: "GET", url }).trim(),
response: JSON.stringify({ body: convertIsoDatesToTimestamps(data.content), tokenExpiry: data.tokenExpiry }).trim(),
statusCode: data?.status || 500,
createdAt: new Date().getTime(),
}
})
console.log(`GitHub.getCommits()`, createLog)

commits.push(...currentCommits.content)

if (currentCommits.length < perPage) {
commits.push(...data.content)
if (data.length < perPage) {
break
}

page++
}

Expand Down

0 comments on commit 2b14d3a

Please sign in to comment.