11package handlers
22
33import (
4+ "context"
45 "fmt"
56 "net/http"
67
@@ -17,44 +18,46 @@ import (
1718)
1819
1920// PostV3Templates triggers a new template build
20- func (a * APIStore ) PostV3Templates (ctx * gin.Context ) {
21- body , err := apiutils .ParseBody [api.TemplateBuildRequestV3 ](ctx , ctx )
21+ func (a * APIStore ) PostV3Templates (c * gin.Context ) {
22+ ctx := c .Request .Context ()
23+
24+ body , err := apiutils .ParseBody [api.TemplateBuildRequestV3 ](ctx , c )
2225 if err != nil {
23- a .sendAPIStoreError (ctx , http .StatusBadRequest , fmt .Sprintf ("Invalid request body: %s" , err ))
26+ a .sendAPIStoreError (c , http .StatusBadRequest , fmt .Sprintf ("Invalid request body: %s" , err ))
2427 telemetry .ReportCriticalError (ctx , "invalid request body" , err )
2528
2629 return
2730 }
2831
29- t := requestTemplateBuild (ctx , a , body )
32+ t := requestTemplateBuild (ctx , c , a , body )
3033 if t != nil {
31- ctx .JSON (http .StatusAccepted , t )
34+ c .JSON (http .StatusAccepted , t )
3235 }
3336}
3437
35- func requestTemplateBuild (ctx * gin.Context , a * APIStore , body api.TemplateBuildRequestV3 ) * api.TemplateRequestResponseV3 {
36- telemetry .ReportEvent (ctx , "started environment build" )
38+ func requestTemplateBuild (ctx context. Context , c * gin.Context , a * APIStore , body api.TemplateBuildRequestV3 ) * api.TemplateRequestResponseV3 {
39+ telemetry .ReportEvent (c , "started environment build" )
3740
3841 // Prepare info for rebuilding env
39- team , apiErr := a .GetTeamAndLimits (ctx , body .TeamID )
42+ team , apiErr := a .GetTeamAndLimits (ctx , c , body .TeamID )
4043 if apiErr != nil {
41- a .sendAPIStoreError (ctx , apiErr .Code , apiErr .ClientMsg )
42- telemetry .ReportCriticalError (ctx , "error when getting team, limits" , apiErr .Err )
44+ a .sendAPIStoreError (c , apiErr .Code , apiErr .ClientMsg )
45+ telemetry .ReportCriticalError (c , "error when getting team, limits" , apiErr .Err )
4346
4447 return nil
4548 }
4649
4750 // Create the build, find the template ID by alias or generate a new one
48- _ , span := tracer .Start (ctx , "find-template-alias" )
51+ _ , span := tracer .Start (c , "find-template-alias" )
4952 defer span .End ()
5053 templateID := id .Generate ()
5154 public := false
52- templateAlias , err := a .sqlcDB .GetTemplateAliasByAlias (ctx , body .Alias )
55+ templateAlias , err := a .sqlcDB .GetTemplateAliasByAlias (c , body .Alias )
5356 switch {
5457 case err == nil :
5558 if templateAlias .TeamID != team .ID {
56- a .sendAPIStoreError (ctx , http .StatusBadRequest , fmt .Sprintf ("Alias `%s` is already taken" , body .Alias ))
57- telemetry .ReportError (ctx , "template alias is already taken" , nil , telemetry .WithTemplateID (templateAlias .EnvID ), telemetry .WithTeamID (team .ID .String ()), attribute .String ("alias" , body .Alias ))
59+ a .sendAPIStoreError (c , http .StatusBadRequest , fmt .Sprintf ("Alias `%s` is already taken" , body .Alias ))
60+ telemetry .ReportError (c , "template alias is already taken" , nil , telemetry .WithTemplateID (templateAlias .EnvID ), telemetry .WithTeamID (team .ID .String ()), attribute .String ("alias" , body .Alias ))
5861
5962 return nil
6063 }
@@ -64,17 +67,17 @@ func requestTemplateBuild(ctx *gin.Context, a *APIStore, body api.TemplateBuildR
6467 case dberrors .IsNotFoundError (err ):
6568 // Alias is available and not used
6669 default :
67- a .sendAPIStoreError (ctx , http .StatusInternalServerError , fmt .Sprintf ("Error when getting template alias: %s" , err ))
68- telemetry .ReportCriticalError (ctx , "error when getting template alias" , err )
70+ a .sendAPIStoreError (c , http .StatusInternalServerError , fmt .Sprintf ("Error when getting template alias: %s" , err ))
71+ telemetry .ReportCriticalError (c , "error when getting template alias" , err )
6972
7073 return nil
7174 }
7275 span .End ()
7376
74- builderNodeID , err := a .templateManager .GetAvailableBuildClient (ctx , apiutils .WithClusterFallback (team .ClusterID ))
77+ builderNodeID , err := a .templateManager .GetAvailableBuildClient (c , apiutils .WithClusterFallback (team .ClusterID ))
7578 if err != nil {
76- a .sendAPIStoreError (ctx , http .StatusInternalServerError , "Error when getting available build client" )
77- telemetry .ReportCriticalError (ctx , "error when getting available build client" , err , telemetry .WithTemplateID (templateID ))
79+ a .sendAPIStoreError (c , http .StatusInternalServerError , "Error when getting available build client" )
80+ telemetry .ReportCriticalError (c , "error when getting available build client" , err , telemetry .WithTemplateID (templateID ))
7881
7982 return nil
8083 }
@@ -91,17 +94,17 @@ func requestTemplateBuild(ctx *gin.Context, a *APIStore, body api.TemplateBuildR
9194 Version : templates .TemplateV2LatestVersion ,
9295 }
9396
94- template , apiError := template .RegisterBuild (ctx , a .templateBuildsCache , a .db , buildReq )
97+ template , apiError := template .RegisterBuild (c , a .templateBuildsCache , a .db , buildReq )
9598 if apiError != nil {
96- a .sendAPIStoreError (ctx , apiError .Code , apiError .ClientMsg )
97- telemetry .ReportCriticalError (ctx , "build template register failed" , apiError .Err )
99+ a .sendAPIStoreError (c , apiError .Code , apiError .ClientMsg )
100+ telemetry .ReportCriticalError (c , "build template register failed" , apiError .Err )
98101
99102 return nil
100103 }
101104
102- _ , span = tracer .Start (ctx , "posthog-analytics" )
105+ _ , span = tracer .Start (c , "posthog-analytics" )
103106 defer span .End ()
104- properties := a .posthog .GetPackageToPosthogProperties (& ctx .Request .Header )
107+ properties := a .posthog .GetPackageToPosthogProperties (& c .Request .Header )
105108 a .posthog .IdentifyAnalyticsTeam (team .ID .String (), team .Name )
106109 a .posthog .CreateAnalyticsTeamEvent (team .ID .String (), "submitted environment build request" , properties .
107110 Set ("environment" , template .TemplateID ).
0 commit comments