From 6441035845179aedf5425d449e830fc4d572c507 Mon Sep 17 00:00:00 2001
From: quidl <43738052+quidl@users.noreply.github.com>
Date: Fri, 30 Aug 2024 17:43:46 +0700
Subject: [PATCH 1/2] Update

---
 src/core/api/api.js       |  9 +++++++++
 src/core/api/url-param.js |  4 ++++
 src/service/agent.js      | 17 ++++++++++++++++-
 3 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/src/core/api/api.js b/src/core/api/api.js
index 176ea6f5..f7c9669e 100644
--- a/src/core/api/api.js
+++ b/src/core/api/api.js
@@ -79,8 +79,17 @@ module.exports = {
     return httpInternal.patch(urlParam.pingJob(jobId), null, withAuthorization(apiKey));
   },
 
+<<<<<<< Updated upstream
   requestJob(uuid, organizationId) {
     return httpInternal.get(urlParam.requestJob(uuid, organizationId));
+=======
+  getJob(jobId) {
+    return httpInternal.get(urlParam.requestJob(jobId));
+  },
+
+  requestJob(uuid, teamId) {
+    return httpInternal.get(urlParam.requestJob(uuid, teamId));
+>>>>>>> Stashed changes
   },
 
   updateJob(body, apiKey) {
diff --git a/src/core/api/url-param.js b/src/core/api/url-param.js
index d41c03dd..c5a5b6a4 100644
--- a/src/core/api/url-param.js
+++ b/src/core/api/url-param.js
@@ -61,6 +61,10 @@ module.exports = {
     return buildUrl({ params }, PATHS.JOB, 'get-job');
   },
 
+  getJob(jobId) {
+    return buildUrl({}, PATHS.JOB, jobId);
+  },
+
   updateJob() {
     return buildUrl({}, PATHS.JOB, 'update-job');
   },
diff --git a/src/service/agent.js b/src/service/agent.js
index 4095cdb6..669dd46d 100644
--- a/src/service/agent.js
+++ b/src/service/agent.js
@@ -399,7 +399,7 @@ class Agent {
 
       // Read job configuration from file
       const jobBody = fs.readJsonSync('job.json', { encoding: 'utf-8' });
-      const {
+      let {
         id: jobId,
         parameter,
         testProject: { projectId },
@@ -408,6 +408,21 @@ class Agent {
         .find((item) => item.name === jobApiKeyEnv);
       const apiKey = jobApiKey ? jobApiKey.value : this.apikey;
 
+      if (!parameter) {
+        const requestJobResponse = await api.getJob(jobId);
+        if (
+          !requestJobResponse ||
+          !requestJobResponse.body ||
+          !requestJobResponse.body.parameter ||
+          !requestJobResponse.body.testProject
+        ) {
+          // There is no job to execute
+          return;
+        }
+        parameter = requestJobResponse.body.parameter;
+        testProject = requestJobResponse.body.testProject;
+      }
+
       let ksArgs;
       if (config.isOnPremise) {
         ksArgs = utils.overrideCommand(parameter.command, {

From c56d8d2742228a9dcbe365c310e18d21d32ea9cb Mon Sep 17 00:00:00 2001
From: quidl <43738052+quidl@users.noreply.github.com>
Date: Fri, 30 Aug 2024 17:46:26 +0700
Subject: [PATCH 2/2] Update

---
 src/core/api/api.js  | 5 ++---
 src/service/agent.js | 7 ++++---
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/src/core/api/api.js b/src/core/api/api.js
index f7c9669e..69fa1b63 100644
--- a/src/core/api/api.js
+++ b/src/core/api/api.js
@@ -79,17 +79,16 @@ module.exports = {
     return httpInternal.patch(urlParam.pingJob(jobId), null, withAuthorization(apiKey));
   },
 
-<<<<<<< Updated upstream
   requestJob(uuid, organizationId) {
     return httpInternal.get(urlParam.requestJob(uuid, organizationId));
-=======
+  },
+  
   getJob(jobId) {
     return httpInternal.get(urlParam.requestJob(jobId));
   },
 
   requestJob(uuid, teamId) {
     return httpInternal.get(urlParam.requestJob(uuid, teamId));
->>>>>>> Stashed changes
   },
 
   updateJob(body, apiKey) {
diff --git a/src/service/agent.js b/src/service/agent.js
index 669dd46d..651d5125 100644
--- a/src/service/agent.js
+++ b/src/service/agent.js
@@ -404,9 +404,6 @@ class Agent {
         parameter,
         testProject: { projectId },
       } = jobBody;
-      const jobApiKey = parameter.environmentVariables
-        .find((item) => item.name === jobApiKeyEnv);
-      const apiKey = jobApiKey ? jobApiKey.value : this.apikey;
 
       if (!parameter) {
         const requestJobResponse = await api.getJob(jobId);
@@ -423,6 +420,10 @@ class Agent {
         testProject = requestJobResponse.body.testProject;
       }
 
+      const jobApiKey = parameter.environmentVariables
+        .find((item) => item.name === jobApiKeyEnv);
+      const apiKey = jobApiKey ? jobApiKey.value : this.apikey; 
+
       let ksArgs;
       if (config.isOnPremise) {
         ksArgs = utils.overrideCommand(parameter.command, {