Skip to content

Commit ddc330d

Browse files
authored
add invocation of specific commands (graph) (#1540)
1 parent 20d957c commit ddc330d

File tree

8 files changed

+123
-10
lines changed

8 files changed

+123
-10
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import * as httpClient from '@actions/http-client'
2+
import {ApiClient} from '../src/api-client'
3+
import {ImageService} from '../src/image-service'
4+
import {JobParameters} from '../src/inputs'
5+
import {updaterImageName, PROXY_IMAGE_NAME} from '../src/docker-tags'
6+
import {Updater} from '../src/updater'
7+
8+
import {
9+
integration,
10+
removeDanglingUpdaterContainers,
11+
runFakeDependabotApi
12+
} from './helpers'
13+
14+
const FAKE_SERVER_PORT = 9000
15+
16+
integration('Graph', () => {
17+
let server: any
18+
19+
// Used from this action to get job details and credentials
20+
const dependabotApiUrl = `http://localhost:${FAKE_SERVER_PORT}`
21+
// Used from within the updater container to update the job state and create prs
22+
const internalDockerHost =
23+
process.platform === 'darwin' ? 'host.docker.internal' : '172.17.0.1'
24+
const dependabotApiDockerUrl = `http://${internalDockerHost}:${FAKE_SERVER_PORT}`
25+
const updaterImage = updaterImageName('npm_and_yarn')
26+
27+
// Define jobToken and credentialsToken
28+
const jobToken = 'xxx'
29+
const credentialsToken = 'yyy'
30+
31+
const params = new JobParameters(
32+
1,
33+
jobToken,
34+
credentialsToken,
35+
dependabotApiUrl,
36+
dependabotApiDockerUrl,
37+
updaterImage
38+
)
39+
40+
const client = new httpClient.HttpClient(
41+
'github/dependabot-action integration'
42+
)
43+
const apiClient = new ApiClient(client, params, jobToken, credentialsToken)
44+
45+
beforeAll(async () => {
46+
await ImageService.pull(updaterImageName('npm_and_yarn'))
47+
await ImageService.pull(PROXY_IMAGE_NAME)
48+
49+
const testRetry = true
50+
server = await runFakeDependabotApi(FAKE_SERVER_PORT, testRetry)
51+
})
52+
53+
afterEach(async () => {
54+
server && server() // eslint-disable-line @typescript-eslint/no-unused-expressions
55+
await removeDanglingUpdaterContainers()
56+
})
57+
58+
jest.setTimeout(120000)
59+
60+
it('should run the graph command', async () => {
61+
const details = await apiClient.getJobDetails()
62+
const credentials = await apiClient.getCredentials()
63+
64+
details.command = 'graph'
65+
details.experiments = {
66+
enable_dependency_submission_poc: true
67+
}
68+
69+
const updater = new Updater(
70+
updaterImageName('npm_and_yarn'),
71+
PROXY_IMAGE_NAME,
72+
apiClient,
73+
details,
74+
credentials
75+
)
76+
77+
await updater.runUpdater()
78+
79+
// NOTE: This will not work when running against the actual dependabot-api
80+
// Checks if the graph_updates was persisted in the fake json-server
81+
const res = await client.getJson<any>(`${dependabotApiUrl}/graph_updates/1`)
82+
83+
expect(res.statusCode).toEqual(200)
84+
})
85+
})

__tests__/server/db.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,6 @@
4848
}
4949
}
5050
],
51-
"pull_requests": []
51+
"pull_requests": [],
52+
"graph_updates": []
5253
}

__tests__/server/server.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,18 @@ server.post(
7575
}
7676
)
7777

78+
server.post(
79+
'/update_jobs/:id/create_dependency_submission',
80+
jsonServer.bodyParser,
81+
(req, res) => {
82+
const data = { ...req.body.data, id: req.params.id }
83+
db.graph_updates.push(data)
84+
router.db.write()
85+
86+
res.status(204).send()
87+
}
88+
)
89+
7890
server.post('/update_jobs/:id/record_update_job_error', (_, res) => {
7991
res.status(204).send()
8092
})

dist/main/index.js

Lines changed: 9 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/main/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/api-client.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {TypedResponse} from '@actions/http-client/lib/interfaces'
55

66
// JobDetails are information about the repository and dependencies to be updated
77
export type JobDetails = {
8+
command?: string
89
'allowed-updates': Array<{
910
'dependency-type': string
1011
}>

src/container-service.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export const ContainerService = {
3333
await container.putArchive(tar, {path})
3434
},
3535

36-
async run(container: Container): Promise<boolean> {
36+
async run(container: Container, command?: string): Promise<boolean> {
3737
try {
3838
// Start the container
3939
await container.start()
@@ -56,10 +56,19 @@ export const ContainerService = {
5656
// Then run the dependabot commands as dependabot user
5757
const dependabotCommands = [
5858
'mkdir -p /home/dependabot/dependabot-updater/output',
59-
'$DEPENDABOT_HOME/dependabot-updater/bin/run fetch_files',
60-
'$DEPENDABOT_HOME/dependabot-updater/bin/run update_files'
59+
'$DEPENDABOT_HOME/dependabot-updater/bin/run fetch_files'
6160
]
6261

62+
if (command === 'graph') {
63+
dependabotCommands.push(
64+
'$DEPENDABOT_HOME/dependabot-updater/bin/run update_graph'
65+
)
66+
} else {
67+
dependabotCommands.push(
68+
'$DEPENDABOT_HOME/dependabot-updater/bin/run update_files'
69+
)
70+
}
71+
6372
for (const cmd of dependabotCommands) {
6473
await this.execCommand(
6574
container,

src/updater.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export class Updater {
143143
job: this.details
144144
})
145145

146-
await ContainerService.run(container)
146+
await ContainerService.run(container, this.details.command)
147147
}
148148

149149
private async createContainer(

0 commit comments

Comments
 (0)