Skip to content

Commit bfb9afb

Browse files
committed
wip
1 parent b5fa690 commit bfb9afb

File tree

6 files changed

+343
-89
lines changed

6 files changed

+343
-89
lines changed

src/commands/action/core/classes.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ export class FullScanParams {
221221
commitMessage?: string
222222
commitHash?: string
223223
pullRequest?: number
224-
committer?: string
224+
committers?: string
225225
makeDefaultBranch?: boolean
226226
setAsPendingHead?: boolean
227227

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import simpleGit, { SimpleGit, DefaultLogFields } from 'simple-git'
2+
3+
export interface GitInfo {
4+
path: string
5+
head: string
6+
repoName: string
7+
branch: string
8+
author: string
9+
commitSHA: string
10+
commitMessage: string
11+
committer: string
12+
showFiles: string[]
13+
changedFiles: string[]
14+
}
15+
16+
export async function gitInfo(path: string): Promise<GitInfo> {
17+
const repo = simpleGit(path)
18+
19+
let head: string
20+
let commit: DefaultLogFields | null = null
21+
let repoName: string = ''
22+
let branch: string = ''
23+
let author: string = ''
24+
let commitSHA: string = ''
25+
let commitMessage: string = ''
26+
let committer: string = ''
27+
const showFiles: string[] = []
28+
const changedFiles: string[] = []
29+
30+
// Get the HEAD reference
31+
head = await repo.revparse(['HEAD'])
32+
33+
// Get the latest commit log
34+
const logEntry = await repo.log({ n: 1 })
35+
commit = logEntry.latest
36+
37+
// Extract the repository name from the origin remote URL
38+
const remotes = await repo.getRemotes(true)
39+
const originRemote = remotes.find(remote => remote.name === 'origin')
40+
41+
if (originRemote) {
42+
const url = originRemote.refs.fetch
43+
repoName = url.split('/').pop()?.replace('.git', '') || ''
44+
}
45+
46+
// Get the current branch
47+
try {
48+
const branches = await repo.branchLocal()
49+
branch = decodeURIComponent(branches.current)
50+
} catch (error) {
51+
console.error('Failed to get branch information:', error)
52+
}
53+
54+
// Populate commit details
55+
if (commit) {
56+
author = commit.author_name || ''
57+
commitSHA = commit.hash || ''
58+
commitMessage = commit.message || ''
59+
committer = commit.author_email || ''
60+
}
61+
62+
// List files changed in the latest commit
63+
if (commitSHA) {
64+
const changedFilesOutput = await repo.raw([
65+
'show',
66+
'--name-only',
67+
'--format=%n',
68+
commitSHA
69+
])
70+
71+
changedFilesOutput
72+
.split('\n')
73+
.filter(item => item.trim() !== '')
74+
.forEach(item => {
75+
showFiles.push(item)
76+
changedFiles.push(`${path}/${item}`)
77+
})
78+
}
79+
80+
return {
81+
path,
82+
head,
83+
repoName,
84+
branch,
85+
author,
86+
commitSHA: commitSHA,
87+
commitMessage,
88+
committer,
89+
showFiles,
90+
changedFiles
91+
}
92+
}

src/commands/action/core/github.ts

+30-9
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export class GitHub {
8888
}
8989
}
9090

91-
static checkEventType(): string | null {
91+
checkEventType(): string | null {
9292
switch (env['GITHUB_EVENT_NAME']?.toLowerCase()) {
9393
case 'push':
9494
return env['PR_NUMBER'] ? 'diff' : 'main'
@@ -112,7 +112,7 @@ export class GitHub {
112112
}
113113
}
114114

115-
static async addSocketComments(
115+
async addSocketComments(
116116
securityComment: string,
117117
overviewComment: string,
118118
comments: Record<string, Comment>,
@@ -132,7 +132,7 @@ export class GitHub {
132132
)
133133
} else {
134134
debug('Posting new Dependency Overview comment')
135-
await GitHub.postComment(overviewComment)
135+
await this.postComment(overviewComment)
136136
}
137137
}
138138

@@ -146,19 +146,19 @@ export class GitHub {
146146
)
147147
} else {
148148
debug('Posting new Security Issue comment')
149-
await GitHub.postComment(securityComment)
149+
await this.postComment(securityComment)
150150
}
151151
}
152152
}
153153

154-
static async postComment(body: string): Promise<void> {
154+
async postComment(body: string): Promise<void> {
155155
const repo = env['GITHUB_REPOSITORY']?.split('/')[1]
156156
const path = `repos/${env['GITHUB_REPOSITORY_OWNER']}/${repo}/issues/${env['PR_NUMBER']}/comments`
157157
const payload = JSON.stringify({ body })
158158
await fetch(path, { body: payload, method: 'POST', headers })
159159
}
160160

161-
static async updateComment(body: string, commentId: string): Promise<void> {
161+
async updateComment(body: string, commentId: string): Promise<void> {
162162
const repo = env['GITHUB_REPOSITORY']?.split('/')[1]
163163
const path = `repos/${env['GITHUB_REPOSITORY_OWNER']}/${repo}/issues/comments/${commentId}`
164164
const payload = JSON.stringify({ body })
@@ -174,7 +174,7 @@ export class GitHub {
174174
file.close()
175175
}
176176

177-
static async getCommentsForPr(
177+
async getCommentsForPR(
178178
repo: string,
179179
pr: string
180180
): Promise<Record<string, Comment | Comment[]>> {
@@ -196,14 +196,35 @@ export class GitHub {
196196
return Comments.checkForSocketComments(comments)
197197
}
198198

199-
static async postReaction(commentId: number): Promise<void> {
199+
removeCommentAlerts(comments: Record<string, Comment>): void {
200+
const securityAlert = comments['security']
201+
202+
if (securityAlert) {
203+
const newBody = Comments.processSecurityComment(securityAlert, comments)
204+
this.handleIgnoreReactions(comments)
205+
this.updateComment(newBody, String(securityAlert.id))
206+
}
207+
}
208+
209+
handleIgnoreReactions(comments: Record<string, Comment[]>): void {
210+
if (comments['ignore']) {
211+
for (const comment of comments['ignore']) {
212+
if (comment.body.includes('SocketSecurity ignore')) {
213+
if (!this.commentReactionExists(comment.id)) {
214+
this.postReaction(comment.id)
215+
}
216+
}
217+
}
218+
}
219+
}
220+
async postReaction(commentId: number): Promise<void> {
200221
const repo = env['GITHUB_REPOSITORY']?.split('/')[1]
201222
const path = `repos/${env['GITHUB_REPOSITORY_OWNER']}/${repo}/issues/comments/${commentId}/reactions`
202223
const payload = JSON.stringify({ content: '+1' })
203224
await fetch(path, { body: payload, method: 'POST', headers })
204225
}
205226

206-
static async commentReactionExists(commentId: number): Promise<boolean> {
227+
async commentReactionExists(commentId: number): Promise<boolean> {
207228
const repo = env['GITHUB_REPOSITORY']?.split('/')[1]
208229
const path = `repos/${env['GITHUB_REPOSITORY_OWNER']}/${repo}/issues/comments/${commentId}/reactions`
209230
try {

src/commands/action/core/scm_comments.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ export function processSecurityComment(
172172

173173
export function checkForSocketComments(
174174
comments: Record<string, Comment>
175-
): Record<string, Comment | Comment[]> {
176-
const socketComments: Record<string, Comment | Comment[]> = {}
175+
): Record<string, Comment> {
176+
const socketComments: Record<string, Comment> = {}
177177

178178
for (const [commentId, comment] of Object.entries(comments)) {
179179
if (comment.body?.includes('socket-security-comment-actions')) {

src/commands/action/git.ts

-73
This file was deleted.

0 commit comments

Comments
 (0)