From b9bbbbeac3a34ac51501d4b0d5dce09f23b06eef Mon Sep 17 00:00:00 2001 From: Junaid Shaikh <64411220+genos1998@users.noreply.github.com> Date: Wed, 23 Oct 2024 22:10:30 +0530 Subject: [PATCH] feat: save state (#14) --- august2024august2024v2/queries.graphql | 19 +++ august2024august2024v2/schema-generated.go | 172 +++++++++++++++++++++ august2024august2024v2/scoring.go | 15 +- august2024august2024v2/upgradeSteps.go | 8 +- august2024v2september2024/upgradeSteps.go | 8 +- common/upgradeSteps.go | 12 +- june2024june2024v2/upgradeSteps.go | 8 +- 7 files changed, 232 insertions(+), 10 deletions(-) diff --git a/august2024august2024v2/queries.graphql b/august2024august2024v2/queries.graphql index 7a39cd0..854460d 100644 --- a/august2024august2024v2/queries.graphql +++ b/august2024august2024v2/queries.graphql @@ -104,4 +104,23 @@ mutation UpdateArtifactScanData($artifactScanDataId: String!) { } } } +} + +mutation DeleteArtifactScanData($id: String) { + deleteArtifactScanData(filter: { id: { eq: $id } }) { + msg + numUids + } +} + +mutation DeleteArtifactScanDataForInprogress { + deleteArtifactScanData(filter: { vulnScanState: { eq: "inprogress" } }) { + numUids + } +} + +mutation DeleteArtifactScanDataNotHaveArtifact { + deleteArtifactScanData(filter: { not: { has: artifactDetails } }) { + numUids + } } \ No newline at end of file diff --git a/august2024august2024v2/schema-generated.go b/august2024august2024v2/schema-generated.go index 0d81889..0b611f1 100644 --- a/august2024august2024v2/schema-generated.go +++ b/august2024august2024v2/schema-generated.go @@ -9,6 +9,72 @@ import ( "github.com/Khan/genqlient/graphql" ) +// DeleteArtifactScanDataDeleteArtifactScanDataDeleteArtifactScanDataPayload includes the requested fields of the GraphQL type DeleteArtifactScanDataPayload. +type DeleteArtifactScanDataDeleteArtifactScanDataDeleteArtifactScanDataPayload struct { + Msg string `json:"msg"` + NumUids *int `json:"numUids"` +} + +// GetMsg returns DeleteArtifactScanDataDeleteArtifactScanDataDeleteArtifactScanDataPayload.Msg, and is useful for accessing the field via an interface. +func (v *DeleteArtifactScanDataDeleteArtifactScanDataDeleteArtifactScanDataPayload) GetMsg() string { + return v.Msg +} + +// GetNumUids returns DeleteArtifactScanDataDeleteArtifactScanDataDeleteArtifactScanDataPayload.NumUids, and is useful for accessing the field via an interface. +func (v *DeleteArtifactScanDataDeleteArtifactScanDataDeleteArtifactScanDataPayload) GetNumUids() *int { + return v.NumUids +} + +// DeleteArtifactScanDataForInprogressDeleteArtifactScanDataDeleteArtifactScanDataPayload includes the requested fields of the GraphQL type DeleteArtifactScanDataPayload. +type DeleteArtifactScanDataForInprogressDeleteArtifactScanDataDeleteArtifactScanDataPayload struct { + NumUids *int `json:"numUids"` +} + +// GetNumUids returns DeleteArtifactScanDataForInprogressDeleteArtifactScanDataDeleteArtifactScanDataPayload.NumUids, and is useful for accessing the field via an interface. +func (v *DeleteArtifactScanDataForInprogressDeleteArtifactScanDataDeleteArtifactScanDataPayload) GetNumUids() *int { + return v.NumUids +} + +// DeleteArtifactScanDataForInprogressResponse is returned by DeleteArtifactScanDataForInprogress on success. +type DeleteArtifactScanDataForInprogressResponse struct { + DeleteArtifactScanData *DeleteArtifactScanDataForInprogressDeleteArtifactScanDataDeleteArtifactScanDataPayload `json:"deleteArtifactScanData"` +} + +// GetDeleteArtifactScanData returns DeleteArtifactScanDataForInprogressResponse.DeleteArtifactScanData, and is useful for accessing the field via an interface. +func (v *DeleteArtifactScanDataForInprogressResponse) GetDeleteArtifactScanData() *DeleteArtifactScanDataForInprogressDeleteArtifactScanDataDeleteArtifactScanDataPayload { + return v.DeleteArtifactScanData +} + +// DeleteArtifactScanDataNotHaveArtifactDeleteArtifactScanDataDeleteArtifactScanDataPayload includes the requested fields of the GraphQL type DeleteArtifactScanDataPayload. +type DeleteArtifactScanDataNotHaveArtifactDeleteArtifactScanDataDeleteArtifactScanDataPayload struct { + NumUids *int `json:"numUids"` +} + +// GetNumUids returns DeleteArtifactScanDataNotHaveArtifactDeleteArtifactScanDataDeleteArtifactScanDataPayload.NumUids, and is useful for accessing the field via an interface. +func (v *DeleteArtifactScanDataNotHaveArtifactDeleteArtifactScanDataDeleteArtifactScanDataPayload) GetNumUids() *int { + return v.NumUids +} + +// DeleteArtifactScanDataNotHaveArtifactResponse is returned by DeleteArtifactScanDataNotHaveArtifact on success. +type DeleteArtifactScanDataNotHaveArtifactResponse struct { + DeleteArtifactScanData *DeleteArtifactScanDataNotHaveArtifactDeleteArtifactScanDataDeleteArtifactScanDataPayload `json:"deleteArtifactScanData"` +} + +// GetDeleteArtifactScanData returns DeleteArtifactScanDataNotHaveArtifactResponse.DeleteArtifactScanData, and is useful for accessing the field via an interface. +func (v *DeleteArtifactScanDataNotHaveArtifactResponse) GetDeleteArtifactScanData() *DeleteArtifactScanDataNotHaveArtifactDeleteArtifactScanDataDeleteArtifactScanDataPayload { + return v.DeleteArtifactScanData +} + +// DeleteArtifactScanDataResponse is returned by DeleteArtifactScanData on success. +type DeleteArtifactScanDataResponse struct { + DeleteArtifactScanData *DeleteArtifactScanDataDeleteArtifactScanDataDeleteArtifactScanDataPayload `json:"deleteArtifactScanData"` +} + +// GetDeleteArtifactScanData returns DeleteArtifactScanDataResponse.DeleteArtifactScanData, and is useful for accessing the field via an interface. +func (v *DeleteArtifactScanDataResponse) GetDeleteArtifactScanData() *DeleteArtifactScanDataDeleteArtifactScanDataDeleteArtifactScanDataPayload { + return v.DeleteArtifactScanData +} + // GetArtifactRiskGetArtifactRisk includes the requested fields of the GraphQL type ArtifactRisk. type GetArtifactRiskGetArtifactRisk struct { SourceCodeAlertsScore *int `json:"sourceCodeAlertsScore"` @@ -434,6 +500,14 @@ func (v *UpdateArtifactScanDataUpdateArtifactScanDataUpdateArtifactScanDataPaylo return v.Id } +// __DeleteArtifactScanDataInput is used internally by genqlient +type __DeleteArtifactScanDataInput struct { + Id string `json:"id"` +} + +// GetId returns __DeleteArtifactScanDataInput.Id, and is useful for accessing the field via an interface. +func (v *__DeleteArtifactScanDataInput) GetId() string { return v.Id } + // __GetArtifactRiskInput is used internally by genqlient type __GetArtifactRiskInput struct { Id *string `json:"id"` @@ -496,6 +570,104 @@ func (v *__UpdateArtifactScanDataRiskScoreAndStatusInput) GetDeploymentAlertsSco return v.DeploymentAlertsScore } +// The query or mutation executed by DeleteArtifactScanData. +const DeleteArtifactScanData_Operation = ` +mutation DeleteArtifactScanData ($id: String) { + deleteArtifactScanData(filter: {id:{eq:$id}}) { + msg + numUids + } +} +` + +func DeleteArtifactScanData( + ctx_ context.Context, + client_ graphql.Client, + id string, +) (*DeleteArtifactScanDataResponse, error) { + req_ := &graphql.Request{ + OpName: "DeleteArtifactScanData", + Query: DeleteArtifactScanData_Operation, + Variables: &__DeleteArtifactScanDataInput{ + Id: id, + }, + } + var err_ error + + var data_ DeleteArtifactScanDataResponse + resp_ := &graphql.Response{Data: &data_} + + err_ = client_.MakeRequest( + ctx_, + req_, + resp_, + ) + + return &data_, err_ +} + +// The query or mutation executed by DeleteArtifactScanDataForInprogress. +const DeleteArtifactScanDataForInprogress_Operation = ` +mutation DeleteArtifactScanDataForInprogress { + deleteArtifactScanData(filter: {vulnScanState:{eq:"inprogress"}}) { + numUids + } +} +` + +func DeleteArtifactScanDataForInprogress( + ctx_ context.Context, + client_ graphql.Client, +) (*DeleteArtifactScanDataForInprogressResponse, error) { + req_ := &graphql.Request{ + OpName: "DeleteArtifactScanDataForInprogress", + Query: DeleteArtifactScanDataForInprogress_Operation, + } + var err_ error + + var data_ DeleteArtifactScanDataForInprogressResponse + resp_ := &graphql.Response{Data: &data_} + + err_ = client_.MakeRequest( + ctx_, + req_, + resp_, + ) + + return &data_, err_ +} + +// The query or mutation executed by DeleteArtifactScanDataNotHaveArtifact. +const DeleteArtifactScanDataNotHaveArtifact_Operation = ` +mutation DeleteArtifactScanDataNotHaveArtifact { + deleteArtifactScanData(filter: {not:{has:artifactDetails}}) { + numUids + } +} +` + +func DeleteArtifactScanDataNotHaveArtifact( + ctx_ context.Context, + client_ graphql.Client, +) (*DeleteArtifactScanDataNotHaveArtifactResponse, error) { + req_ := &graphql.Request{ + OpName: "DeleteArtifactScanDataNotHaveArtifact", + Query: DeleteArtifactScanDataNotHaveArtifact_Operation, + } + var err_ error + + var data_ DeleteArtifactScanDataNotHaveArtifactResponse + resp_ := &graphql.Response{Data: &data_} + + err_ = client_.MakeRequest( + ctx_, + req_, + resp_, + ) + + return &data_, err_ +} + // The query or mutation executed by GetArtifactRisk. const GetArtifactRisk_Operation = ` query GetArtifactRisk ($id: ID!) { diff --git a/august2024august2024v2/scoring.go b/august2024august2024v2/scoring.go index a93b98b..845f579 100644 --- a/august2024august2024v2/scoring.go +++ b/august2024august2024v2/scoring.go @@ -14,6 +14,16 @@ func calculateScoring(prodDgraphClient graphql.Client) error { ctx := context.Background() + _, err := DeleteArtifactScanDataForInprogress(ctx, prodDgraphClient) + if err != nil { + return fmt.Errorf("error in DeleteArtifactScanDataForInprogress: %s", err.Error()) + } + + _, err = DeleteArtifactScanDataNotHaveArtifact(ctx, prodDgraphClient) + if err != nil { + return fmt.Errorf("error in DeleteArtifactScanDataNotHaveArtifact: %s", err.Error()) + } + prodArtifactScanDataIds, err := GetArtifactScanDataId(ctx, prodDgraphClient) if err != nil { return fmt.Errorf("error: could'nt query prod artifact scan data id: %s", err.Error()) @@ -53,7 +63,10 @@ func calculateScoring(prodDgraphClient graphql.Client) error { } if len(artifactRunHistories.QueryArtifactScanData) == 0 { - logger.Sl.Debugf("Skipping iteration of scandata id: %s as runhistory record not found", eachArtifactScanData.Id) + logger.Sl.Debugf("Deleting iteration of scandata id: %s as runhistory record not found", eachArtifactScanData.Id) + if _, err := DeleteArtifactScanData(context.Background(), prodDgraphClient, eachArtifactScanData.Id); err != nil { + return fmt.Errorf("error: DeleteArtifactScanData: artifact scan data id: %s err: %s", eachArtifactScanData.Id, err.Error()) + } continue } diff --git a/august2024august2024v2/upgradeSteps.go b/august2024august2024v2/upgradeSteps.go index 65a5206..acd9430 100644 --- a/august2024august2024v2/upgradeSteps.go +++ b/august2024august2024v2/upgradeSteps.go @@ -9,7 +9,7 @@ import ( "github.com/Khan/genqlient/graphql" ) -func UpgradeToAugust2024v2(prodGraphUrl, prodToken string, prodDgraphClient graphql.Client) error { +func UpgradeToAugust2024v2(prodGraphUrl, prodToken, restoreServiceUrl string, prodDgraphClient graphql.Client) error { logger.Logger.Info("--------------Starting Artifact Score Calculation------------------") @@ -21,6 +21,12 @@ func UpgradeToAugust2024v2(prodGraphUrl, prodToken string, prodDgraphClient grap return fmt.Errorf("UpgradeToAugust2024v2: calculateScoring: %s", err.Error()) } + if restoreServiceUrl != "" { + if err := graphqlfunc.BackupAndRestoreDgraph(prodGraphUrl, restoreServiceUrl); err != nil { + return fmt.Errorf("UpgradeToAugust2024v2: BackupAndRestoreDgraph: %s", err.Error()) + } + } + logger.Logger.Info("--------------Completed UpgradeToAugust2024v2------------------") return nil diff --git a/august2024v2september2024/upgradeSteps.go b/august2024v2september2024/upgradeSteps.go index 8734d16..e14711e 100644 --- a/august2024v2september2024/upgradeSteps.go +++ b/august2024v2september2024/upgradeSteps.go @@ -9,7 +9,7 @@ import ( "github.com/Khan/genqlient/graphql" ) -func UpgradeToSeptember2024(prodGraphUrl, prodToken string, prodDgraphClient graphql.Client) error { +func UpgradeToSeptember2024(prodGraphUrl, prodToken, restoreServiceUrl string, prodDgraphClient graphql.Client) error { logger.Logger.Info("--------------Starting UpgradeToSeptember2024------------------") @@ -45,6 +45,12 @@ func UpgradeToSeptember2024(prodGraphUrl, prodToken string, prodDgraphClient gra return fmt.Errorf("UpgradeToSeptember2024: setSummaryNodeForSecurityIssue: %s", err.Error()) } + if restoreServiceUrl != "" { + if err := graphqlfunc.BackupAndRestoreDgraph(prodGraphUrl, restoreServiceUrl); err != nil { + return fmt.Errorf("UpgradeToSeptember2024: BackupAndRestoreDgraph: %s", err.Error()) + } + } + logger.Logger.Info("--------------Completed UpgradeToSeptember2024------------------") return nil diff --git a/common/upgradeSteps.go b/common/upgradeSteps.go index dba071d..11d037e 100644 --- a/common/upgradeSteps.go +++ b/common/upgradeSteps.go @@ -119,9 +119,9 @@ func beginProcessOfUpgrade(upgradeTo SchemaOrder, isSecondDgraphRequired, isLast expGraphqlClient := graphqlfunc.NewClient(Conf.ExpGraphQLAddr, Conf.ExpDgraphToken) - return june2024june2024v2.UpgradeToJune2024V2(Conf.ExpGraphQLAddr, Conf.ExpDgraphToken, expGraphqlClient) + return june2024june2024v2.UpgradeToJune2024V2(Conf.ExpGraphQLAddr, Conf.ExpDgraphToken, Conf.RemoteDgraphRestoreUrl, expGraphqlClient) } - return june2024june2024v2.UpgradeToJune2024V2(Conf.ProdGraphQLAddr, Conf.ProdDgraphToken, prodGraphqlClient) + return june2024june2024v2.UpgradeToJune2024V2(Conf.ProdGraphQLAddr, Conf.ProdDgraphToken, "", prodGraphqlClient) case July2024Version: if err := allChecksForExpDgraph(July2024Version); err != nil { @@ -150,9 +150,9 @@ func beginProcessOfUpgrade(upgradeTo SchemaOrder, isSecondDgraphRequired, isLast expGraphqlClient := graphqlfunc.NewClient(Conf.ExpGraphQLAddr, Conf.ExpDgraphToken) - return august2024august2024v2.UpgradeToAugust2024v2(Conf.ExpGraphQLAddr, Conf.ExpDgraphToken, expGraphqlClient) + return august2024august2024v2.UpgradeToAugust2024v2(Conf.ExpGraphQLAddr, Conf.ExpDgraphToken, Conf.RemoteDgraphRestoreUrl, expGraphqlClient) } - return august2024august2024v2.UpgradeToAugust2024v2(Conf.ProdGraphQLAddr, Conf.ProdDgraphToken, prodGraphqlClient) + return august2024august2024v2.UpgradeToAugust2024v2(Conf.ProdGraphQLAddr, Conf.ProdDgraphToken, "", prodGraphqlClient) case September2024Version: if isSecondDgraphRequired && !isLastStep { @@ -163,10 +163,10 @@ func beginProcessOfUpgrade(upgradeTo SchemaOrder, isSecondDgraphRequired, isLast expGraphqlClient := graphqlfunc.NewClient(Conf.ExpGraphQLAddr, Conf.ExpDgraphToken) - return august2024v2september2024.UpgradeToSeptember2024(Conf.ExpGraphQLAddr, Conf.ExpDgraphToken, expGraphqlClient) + return august2024v2september2024.UpgradeToSeptember2024(Conf.ExpGraphQLAddr, Conf.ExpDgraphToken, Conf.RemoteDgraphRestoreUrl, expGraphqlClient) } - return august2024v2september2024.UpgradeToSeptember2024(Conf.ProdGraphQLAddr, Conf.ProdDgraphToken, prodGraphqlClient) + return august2024v2september2024.UpgradeToSeptember2024(Conf.ProdGraphQLAddr, Conf.ProdDgraphToken, "", prodGraphqlClient) } diff --git a/june2024june2024v2/upgradeSteps.go b/june2024june2024v2/upgradeSteps.go index c16a146..cdb8fc3 100644 --- a/june2024june2024v2/upgradeSteps.go +++ b/june2024june2024v2/upgradeSteps.go @@ -9,7 +9,7 @@ import ( "github.com/Khan/genqlient/graphql" ) -func UpgradeToJune2024V2(prodGraphUrl, prodToken string, prodDgraphClient graphql.Client) error { +func UpgradeToJune2024V2(prodGraphUrl, prodToken, restoreServiceUrl string, prodDgraphClient graphql.Client) error { logger.Logger.Info("--------------Starting UpgradeToJune2024V2------------------") @@ -17,6 +17,12 @@ func UpgradeToJune2024V2(prodGraphUrl, prodToken string, prodDgraphClient graphq return fmt.Errorf("UpgradeToJune2024: UpdateSchema: %s", err.Error()) } + if restoreServiceUrl != "" { + if err := graphqlfunc.BackupAndRestoreDgraph(prodGraphUrl, restoreServiceUrl); err != nil { + return fmt.Errorf("UpgradeToJune2024V2: BackupAndRestoreDgraph: %s", err.Error()) + } + } + logger.Logger.Info("--------------Completed UpgradeToJune2024V2------------------") return nil