Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(integrations): Use payload instead of re-reading closed Body #1987

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 11 additions & 18 deletions api/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func ReceiveIntegration(w http.ResponseWriter, r *http.Request) {
continue
}

RunIntegration(integration, project, r)
RunIntegration(integration, project, r, payload)
}

w.WriteHeader(http.StatusNoContent)
Expand Down Expand Up @@ -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))

Expand All @@ -241,9 +241,8 @@ func RunIntegration(integration db.Integration, project db.Project, r *http.Requ

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)
if err != nil {
log.Error(err)
Expand All @@ -252,9 +251,10 @@ func RunIntegration(integration db.Integration, project db.Project, r *http.Requ

var environmentJSONString = string(environmentJSONBytes)
var taskDefinition = db.Task{
TemplateID: integration.TemplateID,
ProjectID: integration.ProjectID,
Environment: environmentJSONString,
TemplateID: integration.TemplateID,
ProjectID: integration.ProjectID,
Environment: environmentJSONString,
IntegrationID: &integration.ID,
}

_, err = helpers.TaskPool(r).AddTask(taskDefinition, nil, integration.ProjectID)
Expand All @@ -264,27 +264,20 @@ 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)

for _, extractValue := range extractValues {
switch extractValue.ValueSource {
case db.IntegrationExtractHeaderValue:
result[extractValue.Variable] = r.Header.Get(extractValue.Key)
case db.IntegrationExtractBodyValue:
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
log.Fatal(err)
return
}
var body = string(bodyBytes)

switch extractValue.BodyDataType {
case db.IntegrationBodyDataJSON:
result[extractValue.Variable] =
fmt.Sprintf("%v", gojsonq.New().JSONString(body).Find(extractValue.Key))
var extractedResult = fmt.Sprintf("%v", gojsonq.New().JSONString(string(payload)).Find(extractValue.Key))
result[extractValue.Variable] = extractedResult
case db.IntegrationBodyDataString:
result[extractValue.Variable] = body
result[extractValue.Variable] = string(payload)
}
}
}
Expand Down
1 change: 1 addition & 0 deletions db/Migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func GetMigrations() []Migration {
{Version: "2.9.61"},
{Version: "2.9.62"},
{Version: "2.9.70"},
{Version: "2.9.97"},
}
}

Expand Down
2 changes: 2 additions & 0 deletions db/Task.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type Task struct {
DryRun bool `db:"dry_run" json:"dry_run"`
Diff bool `db:"diff" json:"diff"`

IntegrationID *int `db:"integration_id" json:"integration_id"`

// override variables
Playbook string `db:"playbook" json:"playbook"`
Environment string `db:"environment" json:"environment"`
Expand Down
1 change: 1 addition & 0 deletions db/sql/migrations/v2.9.97.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table `task` add `integration_id` int null references project__integration(`id`) on delete set null;
5 changes: 4 additions & 1 deletion web/src/components/TaskLogView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@
<v-col>
<v-list two-line subheader 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-subtitle>{{ user.name || '-' }}</v-list-item-subtitle>
</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>
</v-col>
Expand Down
2 changes: 1 addition & 1 deletion web/src/lang/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,5 +238,5 @@ export default {
Run: 'Run',
CreateDemoProject: 'Create Demo Project',
LeaveProject: 'Leave Project',

integration: 'Integration',
};
2 changes: 1 addition & 1 deletion web/src/views/project/Templates.vue
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@
:tooltip="item.last_task.message"
/>
<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>
</template>
Expand Down