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

Checkout back to the original branch to reset the filesystem preventing ingest cache from corrupting #2160

Merged
merged 1 commit into from
Jan 22, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions internal/engine/actions/remediate/pull_request/pull_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,16 @@ func (r *Remediator) runGit(
return fmt.Errorf("cannot get primary email: %w", err)
}

currentHeadReference, err := repo.Head()
if err != nil {
return fmt.Errorf("cannot get current HEAD: %w", err)
}
currHeadName := currentHeadReference.Name()

// This resets the worktree so we don't corrupt the ingest cache (at least the main/originally-fetched branch).
// This also makes sure, all new remediations check out from main branch rather than prev remediation branch.
defer checkoutToOriginallyFetchedBranch(&logger, wt, currHeadName)

logger.Debug().Str("branch", branchBaseName(title)).Msg("Checking out branch")
err = wt.Checkout(&git.CheckoutOptions{
Branch: plumbing.NewBranchReferenceName(branchBaseName(title)),
Expand Down Expand Up @@ -508,3 +518,20 @@ func getPrimaryEmail(ctx context.Context, cli provifv1.GitHub) (string, error) {

return fallback, nil
}

func checkoutToOriginallyFetchedBranch(
logger *zerolog.Logger,
wt *git.Worktree,
originallyFetchedBranch plumbing.ReferenceName,
) {
err := wt.Checkout(&git.CheckoutOptions{
Branch: originallyFetchedBranch,
})
if err != nil {
logger.Err(err).Msg(
"unable to checkout to the previous head, this can corrupt the ingest cache, should not happen",
)
} else {
logger.Info().Msg(fmt.Sprintf("checked out back to %s branch", originallyFetchedBranch))
}
}
1 change: 0 additions & 1 deletion internal/engine/ingester/git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package rule provides the CLI subcommand for managing rules

package git_test

Expand Down
Loading