Skip to content

Commit a5eb181

Browse files
author
Diego Manzanarez
committed
Refactored code
1 parent b42aab6 commit a5eb181

4 files changed

Lines changed: 75 additions & 93 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
const { CloudTasksClient } = require('@google-cloud/tasks');
2+
const yenv = require('yenv')
3+
const { v4: uuidv4 } = require('uuid');
4+
5+
const parsedDocument = yenv('.github/workflows/gql-perf-arch-github_workflow.yml')
6+
const client = new CloudTasksClient();
7+
8+
const constructPayload = () => {
9+
const jobId = String(uuidv4());
10+
const commitHash = parsedDocument.COMMIT_HASH;
11+
const classes = parsedDocument.CLASSES;
12+
const pullRequestNumber = parsedDocument.PULL_REQUEST_NUMBER;
13+
14+
const payloadStructure = {
15+
"jobId": jobId,
16+
"commitHash": commitHash,
17+
"classes": classes,
18+
"pullRequest": pullRequestNumber,
19+
}
20+
return payloadStructure;
21+
}
22+
23+
const formatPayload = (payloadStructure) => {
24+
const parsedPayload = JSON.stringify(JSON.stringify(payloadStructure));
25+
const payload = `{"argument": ${parsedPayload}}`;
26+
console.log(`Payload: ${payload}`);
27+
return payload;
28+
}
29+
30+
const constructTask = (serviceAccountEmail, payload, url) => {
31+
const task = {
32+
httpRequest: {
33+
httpMethod: 'POST',
34+
url,
35+
oauthToken: {
36+
serviceAccountEmail,
37+
},
38+
body: Buffer.from(payload).toString('base64'),
39+
},
40+
};
41+
return task;
42+
}
43+
44+
const createRequestBody = (payload) => {
45+
const project = parsedDocument.PROJECT_ID;
46+
const queue = parsedDocument.QUEUE_ID;
47+
const location = parsedDocument.LOCATION;
48+
const url = parsedDocument.WORKFLOW_URL;
49+
const serviceAccountEmail = parsedDocument.SERVICE_ACCOUNT_EMAIL;
50+
const requestBody = {
51+
"fullyQualifiedQueueName": client.queuePath(project, location, queue),
52+
"task": constructTask(serviceAccountEmail, payload, url)
53+
}
54+
return requestBody;
55+
}
56+
57+
async function createHttpTaskWithToken() {
58+
const payloadStructure = constructPayload();
59+
const payload = formatPayload(payloadStructure);
60+
const requestBody = createRequestBody(payload);
61+
const request = { parent: requestBody.fullyQualifiedQueueName, task: requestBody.task };
62+
const [response] = await client.createTask(request);
63+
const name = response.name;
64+
console.log(`Created task ${name}`);
65+
}
66+
createHttpTaskWithToken();

.github/workflows/gql-perf-arch-github_workflow.yml

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: gql-perf-arch-github_workflow
22
on:
33
push:
44
branches:
5-
- master
5+
- main
66
workflow_dispatch:
77
inputs:
88
COMMIT_HASH_INPUT:
@@ -25,40 +25,20 @@ env:
2525
COMMIT_HASH: ${{ (github.sha) }}
2626
CLASSES: ${{ (github.event.inputs.CLASSES_TO_EXECUTE_INPUT) }}
2727
PULL_REQUEST_NUMBER: ${{ (github.event.inputs.PULL_REQUEST_NUMBER_INPUT) }}
28+
2829
jobs:
29-
push_action:
30+
execute_workflow:
3031
runs-on: ubuntu-latest
31-
if: ${{ github.event_name == 'push' }}
3232
steps:
3333
- uses: actions/checkout@v3
3434
- uses: actions/setup-node@v3
3535
with:
3636
node-version: '14'
3737
- run: npm install
38-
- run: npm i yenv
39-
- run: npm i uuid
40-
41-
- id: 'auth'
42-
name: 'Authenticate to Google Cloud'
43-
uses: google-github-actions/auth@v0.4.0
44-
with:
45-
credentials_json: ${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }}
4638

47-
- name: Execute JS script
48-
run: node google_cloud_http_target_task.js
49-
50-
workflow_dispatch_action:
51-
runs-on: ubuntu-latest
52-
if: ${{ github.event_name == 'workflow_dispatch' }}
53-
steps:
54-
- uses: actions/checkout@v3
55-
- uses: actions/setup-node@v3
56-
with:
57-
node-version: '14'
58-
- run: npm install
59-
- run: npm i yenv
60-
- run: npm install uuid
61-
- run: echo "COMMIT_HASH=${{ (github.event.inputs.COMMIT_HASH_INPUT) }} " >> $GITHUB_ENV
39+
- name: Update COMMIT_HASH
40+
if: ${{ github.event_name == 'workflow_dispatch' }}
41+
run: echo "COMMIT_HASH=${{ (github.event.inputs.COMMIT_HASH_INPUT) }} " >> $GITHUB_ENV
6242

6343
- id: 'auth'
6444
name: 'Authenticate to Google Cloud'
@@ -67,4 +47,4 @@ jobs:
6747
credentials_json: ${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }}
6848

6949
- name: Execute JS script
70-
run: node google_cloud_http_target_task.js
50+
run: node .github/workflows/google_cloud_http_target_task.js

google_cloud_http_target_task.js

Lines changed: 0 additions & 63 deletions
This file was deleted.

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@
1313
"dependencies": {
1414
"@google-cloud/tasks": "^3.0.0",
1515
"body-parser": "^1.18.3",
16-
"express": "^4.16.3"
16+
"express": "^4.16.3",
17+
"yenv": "^3.0.1"
1718
},
1819
"devDependencies": {
19-
"chai": "^4.2.0",
20-
"mocha": "^8.0.0",
2120
"uuid": "^8.0.0"
2221
}
2322
}

0 commit comments

Comments
 (0)