Skip to content

Commit bca84b4

Browse files
committed
feat(python-cli-parity): create Jira tasks from scan reports
1 parent 459af52 commit bca84b4

13 files changed

Lines changed: 897 additions & 73 deletions

File tree

‎docs/commands/scan/report.md‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,28 @@ socket scan create --report
8787
The bot posts to Slack's `chat.postMessage` API. It checks both the HTTP status
8888
and Slack's JSON `ok` response. Rate limits and API errors are reported without
8989
printing the token or changing the scan decision.
90+
91+
## Jira CI tasks
92+
93+
The scan creation report flow can create one Jira Task per report with findings.
94+
Reruns for the same repository, branch, pull request, and commit update the
95+
existing task. If CI cannot provide a commit SHA, reruns use the stable
96+
repository, branch, and pull request identity instead. Store the API token in
97+
your CI secret store and enable Jira only for trusted jobs:
98+
99+
```sh
100+
export SOCKET_JIRA_ENABLED=true
101+
# Set SOCKET_JIRA_CONFIG_JSON from your CI secret store.
102+
socket ci
103+
```
104+
105+
Store a JSON value like this in `SOCKET_JIRA_CONFIG_JSON`:
106+
107+
```json
108+
{"url":"https://example.atlassian.net","email":"bot@example.com","api_token":"<JIRA_API_TOKEN>","project":"SEC"}
109+
```
110+
111+
`SOCKET_JIRA_CONFIG_JSON` requires an HTTPS Jira base URL, the Atlassian account
112+
email, an API token, and a Jira project key. Notifications include the Socket
113+
report link, package, alert, source, and manifest details. Jira lookup or
114+
delivery failures are reported as warnings and do not change the scan result.

‎src/commands/ci/handle-ci.mts‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
import { logger } from '../../utils/logger.mts'
2626
import { getPackageFilesForScan } from '../../utils/path-resolve.mts'
2727
import { serializeResultJson } from '../../utils/serialize-result-json.mts'
28+
import { spawn } from '../../utils/spawn.mts'
2829
import { fetchSupportedScanFileNames } from '../scan/fetch-supported-scan-file-names.mts'
2930
import { handleCreateNewScan } from '../scan/handle-create-new-scan.mts'
3031
import {
@@ -247,12 +248,30 @@ async function runCiScan({
247248
)
248249
debugDir('inspect', { orgSlug, cwd, branchName, repoName })
249250

251+
// Branch builds also carry a revision, even without a PR context.
252+
let commitHash =
253+
pythonJsonContext.commitSha ||
254+
process.env['GITHUB_SHA'] ||
255+
process.env['CI_COMMIT_SHA'] ||
256+
process.env['BUILD_SOURCEVERSION'] ||
257+
''
258+
if (!commitHash) {
259+
try {
260+
const revision = await spawn('git', ['rev-parse', '--verify', 'HEAD'], {
261+
cwd,
262+
})
263+
commitHash = revision.stdout.trim()
264+
} catch {
265+
debugFn('notice', 'No Git revision is available for this scan')
266+
}
267+
}
268+
250269
const [owner, repo] = constants.ENV.GITHUB_REPOSITORY.split('/')
251270
await handleCreateNewScan({
252271
autoManifest,
253272
branchName,
254273
commitMessage: '',
255-
commitHash: '',
274+
commitHash,
256275
committers: '',
257276
cwd,
258277
defaultBranch: false,

‎src/commands/ci/handle-ci.test.mts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ it('uses GH_TOKEN to publish the finished CI report and update comments on rerun
137137
vi.stubEnv('GH_TOKEN', 'synthetic')
138138
vi.stubEnv('GITHUB_BASE_REF', 'main')
139139
vi.stubEnv('GITHUB_BASE_SHA', 'base-sha')
140+
vi.stubEnv('GITHUB_SHA', 'd'.repeat(40))
140141
vi.stubEnv('GITHUB_SERVER_URL', 'https://github.com')
141142
vi.stubEnv('GITHUB_REF', 'refs/pull/42/merge')
142143
vi.stubEnv('GITHUB_REPOSITORY', 'example/project')
@@ -184,11 +185,9 @@ it('uses GH_TOKEN to publish the finished CI report and update comments on rerun
184185
vi.doMock('../../utils/path-resolve.mts', () => ({
185186
getPackageFilesForScan: async () => ['package.json'],
186187
}))
188+
const upload = vi.fn(async () => ({ ok: true, data: { id: 'test-scan' } }))
187189
vi.doMock('../scan/fetch-create-org-full-scan.mts', () => ({
188-
fetchCreateOrgFullScan: async () => ({
189-
ok: true,
190-
data: { id: 'test-scan' },
191-
}),
190+
fetchCreateOrgFullScan: upload,
192191
}))
193192
const { handleCi } = await import('./handle-ci.mts')
194193
await handleCi(false)
@@ -201,6 +200,12 @@ it('uses GH_TOKEN to publish the finished CI report and update comments on rerun
201200
await handleCi(false)
202201
vi.stubEnv('GITHUB_REF', 'refs/heads/main')
203202
await handleCi(false)
203+
expect(upload).toHaveBeenLastCalledWith(
204+
expect.anything(),
205+
expect.anything(),
206+
expect.objectContaining({ commitHash: 'd'.repeat(40), pullRequest: 0 }),
207+
expect.anything(),
208+
)
204209
expect(fetchComments).toHaveBeenCalledTimes(3)
205210
expect(writes).toEqual(['POST', 'POST', 'PATCH', 'PATCH', 'PATCH'])
206211
expect(comments).toHaveLength(2)

‎src/commands/scan/handle-create-new-scan.mts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,8 @@ async function createNewScan(
629629
notificationContext: {
630630
repository: repoName,
631631
branch: branchName,
632+
pullRequest,
633+
commitSha: commitHash || undefined,
632634
reachabilityEnabled,
633635
reachabilityFacts: notificationReachabilityFacts,
634636
},
@@ -646,6 +648,8 @@ async function createNewScan(
646648
notificationContext: {
647649
repository: repoName,
648650
branch: branchName,
651+
pullRequest,
652+
commitSha: commitHash || undefined,
649653
reachabilityEnabled,
650654
reachabilityFacts: notificationReachabilityFacts,
651655
},
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { isJiraEnabled, sendJiraTaskNotification } from './jira.mts'
2+
import { sendSlackBotNotifications } from './slack-bot.mts'
3+
import {
4+
getSlackNotificationConfig,
5+
isSlackNotificationConfigured,
6+
sendSlackWebhookNotifications,
7+
} from './slack-webhook.mts'
8+
import { logger } from '../../../utils/logger.mts'
9+
10+
import type { NotificationReport } from './report.mts'
11+
12+
type NotificationProvider = {
13+
name: string
14+
isEnabled: () => boolean
15+
send: (report: NotificationReport) => Promise<void>
16+
}
17+
18+
const notificationProviders: NotificationProvider[] = [
19+
{
20+
name: 'Jira',
21+
isEnabled: isJiraEnabled,
22+
send: sendJiraTaskNotification,
23+
},
24+
{
25+
name: 'Slack',
26+
isEnabled: isSlackNotificationConfigured,
27+
send: async report => {
28+
const config = getSlackNotificationConfig() ?? null
29+
await sendSlackWebhookNotifications(report, config)
30+
await sendSlackBotNotifications(report, config)
31+
},
32+
},
33+
]
34+
35+
/** Deliver reports to each independently configured notification provider. */
36+
export async function dispatchScanNotifications(
37+
report: NotificationReport,
38+
): Promise<void> {
39+
for (const provider of notificationProviders) {
40+
if (!provider.isEnabled()) {
41+
continue
42+
}
43+
try {
44+
// eslint-disable-next-line no-await-in-loop -- Providers are dispatched independently in order.
45+
await provider.send(report)
46+
} catch {
47+
logger.warn(
48+
`${provider.name} notification delivery failed; scan policy is unchanged.`,
49+
)
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)