99
1010 gb "github.com/gitploy-io/gitploy/internal/server/global"
1111 "github.com/gitploy-io/gitploy/model/ent"
12+ "github.com/gitploy-io/gitploy/model/ent/deployment"
1213 "github.com/gitploy-io/gitploy/model/extent"
1314 "github.com/gitploy-io/gitploy/pkg/e"
1415)
@@ -54,15 +55,7 @@ func (s *DeploymentAPI) Rollback(c *gin.Context) {
5455 return
5556 }
5657
57- d , err = s .i .Deploy (ctx , u , re ,
58- & ent.Deployment {
59- Type : d .Type ,
60- Env : d .Env ,
61- Ref : d .Ref ,
62- DynamicPayload : d .DynamicPayload ,
63- IsRollback : true ,
64- },
65- env )
58+ d , err = s .i .Deploy (ctx , u , re , s .buildDeploymentForRollback (d ), env )
6659 if err != nil {
6760 s .log .Check (gb .GetZapLogLevel (err ), "Failed to rollback." ).Write (zap .Error (err ))
6861 gb .ResponseWithError (c , err )
@@ -77,3 +70,25 @@ func (s *DeploymentAPI) Rollback(c *gin.Context) {
7770 s .log .Info ("Start to rollback." , zap .String ("repo" , re .GetFullName ()), zap .Int ("number" , d .Number ))
7871 gb .Response (c , http .StatusCreated , d )
7972}
73+
74+ func (s * DeploymentAPI ) buildDeploymentForRollback (d * ent.Deployment ) * ent.Deployment {
75+ // To avoid referencing the head of the branch,
76+ // server has to reference the commit SHA of the deployment.
77+ if d .Type == deployment .TypeBranch && d .Sha != "" {
78+ return & ent.Deployment {
79+ Type : deployment .TypeCommit ,
80+ Env : d .Env ,
81+ Ref : d .Sha ,
82+ DynamicPayload : d .DynamicPayload ,
83+ IsRollback : true ,
84+ }
85+ }
86+
87+ return & ent.Deployment {
88+ Type : d .Type ,
89+ Env : d .Env ,
90+ Ref : d .Ref ,
91+ DynamicPayload : d .DynamicPayload ,
92+ IsRollback : true ,
93+ }
94+ }
0 commit comments