Merge pull request #1987 from andreas-marschke/fix-integration-json-parsing

fix(integrations): Use payload instead of re-reading closed Body
This commit is contained in:
Denis Gukov 2024-05-21 18:31:42 +02:00 committed by GitHub
commit 3e0f5a44f0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 21 additions and 21 deletions

View File

@ -161,7 +161,7 @@ func ReceiveIntegration(w http.ResponseWriter, r *http.Request) {
continue continue
} }
RunIntegration(integration, project, r) RunIntegration(integration, project, r, payload)
} }
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
@ -227,7 +227,7 @@ func MatchCompare(value interface{}, method db.IntegrationMatchMethodType, expec
} }
} }
func RunIntegration(integration db.Integration, project db.Project, r *http.Request) { func RunIntegration(integration db.Integration, project db.Project, r *http.Request, payload []byte) {
log.Info(fmt.Sprintf("Running integration %d", integration.ID)) log.Info(fmt.Sprintf("Running integration %d", integration.ID))
@ -241,9 +241,8 @@ func RunIntegration(integration db.Integration, project db.Project, r *http.Requ
extractValues = append(extractValues, extractValuesForExtractor...) extractValues = append(extractValues, extractValuesForExtractor...)
var extractedResults = Extract(extractValues, r) var extractedResults = Extract(extractValues, r, payload)
// XXX: LOG AN EVENT HERE
environmentJSONBytes, err := json.Marshal(extractedResults) environmentJSONBytes, err := json.Marshal(extractedResults)
if err != nil { if err != nil {
log.Error(err) log.Error(err)
@ -252,9 +251,10 @@ func RunIntegration(integration db.Integration, project db.Project, r *http.Requ
var environmentJSONString = string(environmentJSONBytes) var environmentJSONString = string(environmentJSONBytes)
var taskDefinition = db.Task{ var taskDefinition = db.Task{
TemplateID: integration.TemplateID, TemplateID: integration.TemplateID,
ProjectID: integration.ProjectID, ProjectID: integration.ProjectID,
Environment: environmentJSONString, Environment: environmentJSONString,
IntegrationID: &integration.ID,
} }
_, err = helpers.TaskPool(r).AddTask(taskDefinition, nil, integration.ProjectID) _, err = helpers.TaskPool(r).AddTask(taskDefinition, nil, integration.ProjectID)
@ -264,7 +264,7 @@ func RunIntegration(integration db.Integration, project db.Project, r *http.Requ
} }
} }
func Extract(extractValues []db.IntegrationExtractValue, r *http.Request) (result map[string]string) { func Extract(extractValues []db.IntegrationExtractValue, r *http.Request, payload []byte) (result map[string]string) {
result = make(map[string]string) result = make(map[string]string)
for _, extractValue := range extractValues { for _, extractValue := range extractValues {
@ -272,19 +272,12 @@ func Extract(extractValues []db.IntegrationExtractValue, r *http.Request) (resul
case db.IntegrationExtractHeaderValue: case db.IntegrationExtractHeaderValue:
result[extractValue.Variable] = r.Header.Get(extractValue.Key) result[extractValue.Variable] = r.Header.Get(extractValue.Key)
case db.IntegrationExtractBodyValue: case db.IntegrationExtractBodyValue:
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
log.Fatal(err)
return
}
var body = string(bodyBytes)
switch extractValue.BodyDataType { switch extractValue.BodyDataType {
case db.IntegrationBodyDataJSON: case db.IntegrationBodyDataJSON:
result[extractValue.Variable] = var extractedResult = fmt.Sprintf("%v", gojsonq.New().JSONString(string(payload)).Find(extractValue.Key))
fmt.Sprintf("%v", gojsonq.New().JSONString(body).Find(extractValue.Key)) result[extractValue.Variable] = extractedResult
case db.IntegrationBodyDataString: case db.IntegrationBodyDataString:
result[extractValue.Variable] = body result[extractValue.Variable] = string(payload)
} }
} }
} }

View File

@ -66,6 +66,7 @@ func GetMigrations() []Migration {
{Version: "2.9.61"}, {Version: "2.9.61"},
{Version: "2.9.62"}, {Version: "2.9.62"},
{Version: "2.9.70"}, {Version: "2.9.70"},
{Version: "2.9.97"},
} }
} }

View File

@ -20,6 +20,8 @@ type Task struct {
DryRun bool `db:"dry_run" json:"dry_run"` DryRun bool `db:"dry_run" json:"dry_run"`
Diff bool `db:"diff" json:"diff"` Diff bool `db:"diff" json:"diff"`
IntegrationID *int `db:"integration_id" json:"integration_id"`
// override variables // override variables
Playbook string `db:"playbook" json:"playbook"` Playbook string `db:"playbook" json:"playbook"`
Environment string `db:"environment" json:"environment"` Environment string `db:"environment" json:"environment"`

View File

@ -0,0 +1 @@
alter table `task` add `integration_id` int null references project__integration(`id`) on delete set null;

View File

@ -23,10 +23,13 @@
<v-col> <v-col>
<v-list two-line subheader class="pa-0"> <v-list two-line subheader class="pa-0">
<v-list-item class="pa-0"> <v-list-item class="pa-0">
<v-list-item-content> <v-list-item-content v-if="item.user_id != null">
<v-list-item-title>{{ $t('author') }}</v-list-item-title> <v-list-item-title>{{ $t('author') }}</v-list-item-title>
<v-list-item-subtitle>{{ user.name || '-' }}</v-list-item-subtitle> <v-list-item-subtitle>{{ user.name || '-' }}</v-list-item-subtitle>
</v-list-item-content> </v-list-item-content>
<v-list-item-content v-else-if="item.integration_id != null">
<v-list-item-title>{{ $t('integration') }}</v-list-item-title>
</v-list-item-content>
</v-list-item> </v-list-item>
</v-list> </v-list>
</v-col> </v-col>

View File

@ -238,5 +238,5 @@ export default {
Run: 'Run', Run: 'Run',
CreateDemoProject: 'Create Demo Project', CreateDemoProject: 'Create Demo Project',
LeaveProject: 'Leave Project', LeaveProject: 'Leave Project',
integration: 'Integration',
}; };

View File

@ -185,7 +185,7 @@
:tooltip="item.last_task.message" :tooltip="item.last_task.message"
/> />
<div style="color: gray; font-size: 14px;"> <div style="color: gray; font-size: 14px;">
{{ $t('by', {user_name: item.last_task.user_name}) }} {{ $t('by', {user_name: item.last_task.user_name }) }}
</div> </div>
</div> </div>
</template> </template>