diff --git a/pkg/kit/merge.go b/pkg/kit/merge.go index b187441..5af6306 100644 --- a/pkg/kit/merge.go +++ b/pkg/kit/merge.go @@ -32,11 +32,13 @@ type MergeBot struct { IsANewBranch bool WorkDir string - hasCommit bool - files4Commit map[string][]string - manifestFiles map[string][]specs.RepoScanFile - fixupBranches map[string]*specs.MergeKitFixupInclude - eclassUpdate bool + hasCommit bool + files4Commit map[string][]string + manifestFiles map[string][]specs.RepoScanFile + fixupBranches map[string]*specs.MergeKitFixupInclude + eclassUpdate bool + profilesUpdate bool + metadataUpdate bool GithubClient *github.Client } @@ -90,6 +92,8 @@ func NewMergeBot(c *specs.MarkDevkitConfig) *MergeBot { fixupBranches: make(map[string]*specs.MergeKitFixupInclude, 0), GithubClient: nil, eclassUpdate: false, + profilesUpdate: false, + metadataUpdate: false, } } @@ -435,6 +439,80 @@ func (m *MergeBot) Push(mkit *specs.MergeKit, opts *MergeBotOpts) error { targetKit.Name, pr.GetHTMLURL())) } + if m.profilesUpdate { + + prBranchName := fmt.Sprintf( + "%s%s", + prBranchPrefix, "profiles-update", + ) + + err = PushBranch(kitDir, prBranchName, pushOpts) + if err != nil { + return err + } + + pr, err := CreatePullRequest(m.GithubClient, ctx, + // title + "mark-devkit: Update/Add Profiles", + // source branch + prBranchName, + // target branch + targetKit.Branch, + // body + fmt.Sprintf( + "Automatic update for add/update profiles to branch %s for specfile %s by mark-bot", + targetKit.Branch, mkit.File), + // github User + opts.GithubUser, + // github target repository + targetKit.Name, + ) + + if err != nil { + return err + } + + m.Logger.Info(fmt.Sprintf("[%s] Created correctly PR for profiles: %s", + targetKit.Name, pr.GetHTMLURL())) + } + + if m.metadataUpdate { + + prBranchName := fmt.Sprintf( + "%s%s", + prBranchPrefix, "metadata-update", + ) + + err = PushBranch(kitDir, prBranchName, pushOpts) + if err != nil { + return err + } + + pr, err := CreatePullRequest(m.GithubClient, ctx, + // title + "mark-devkit: Update/Add Metadata", + // source branch + prBranchName, + // target branch + targetKit.Branch, + // body + fmt.Sprintf( + "Automatic update for add/update metadata to branch %s for specfile %s by mark-bot", + targetKit.Branch, mkit.File), + // github User + opts.GithubUser, + // github target repository + targetKit.Name, + ) + + if err != nil { + return err + } + + m.Logger.Info(fmt.Sprintf("[%s] Created correctly PR for profiles: %s", + targetKit.Name, pr.GetHTMLURL())) + } + } else { err = Push(kitDir, pushOpts) } diff --git a/pkg/kit/merge_bump.go b/pkg/kit/merge_bump.go index e1dadba..03b09f2 100644 --- a/pkg/kit/merge_bump.go +++ b/pkg/kit/merge_bump.go @@ -64,6 +64,13 @@ func (m *MergeBot) BumpAtoms(mkit *specs.MergeKit, opts *MergeBotOpts) error { m.Logger.InfoC(fmt.Sprintf( "[%s] PR branch already present. Nothing to do.", pkg)) + + // Restore committed files in order to avoid + // that the same changes will be added in new commit. + err = m.restoreFiles(kitDir, files, opts, worktree) + if err != nil { + return err + } continue } diff --git a/pkg/kit/merge_eclasses.go b/pkg/kit/merge_eclasses.go index 01e1a88..3d80e74 100644 --- a/pkg/kit/merge_eclasses.go +++ b/pkg/kit/merge_eclasses.go @@ -94,6 +94,10 @@ func (m *MergeBot) MergeEclasses(mkit *specs.MergeKit, opts *MergeBotOpts) error prBranchPrefix, kit.Branch, "eclasses", ) + // Restore committed files in order to avoid + // that the same changes will be added in new commit. + defer m.restoreFiles(kitDir, files, opts, worktree) + prBranchExists, err := BranchExists(kit.Url, prBranchName) if err != nil { return err @@ -152,13 +156,6 @@ func (m *MergeBot) MergeEclasses(mkit *specs.MergeKit, opts *MergeBotOpts) error return err } - // Restore committed files in order to avoid - // that the same changes will be added in new commit. - err = m.restoreFiles(kitDir, files, opts, worktree) - if err != nil { - return err - } - m.eclassUpdate = true } diff --git a/pkg/kit/merge_fixups.go b/pkg/kit/merge_fixups.go index 096faf4..b8e7c73 100644 --- a/pkg/kit/merge_fixups.go +++ b/pkg/kit/merge_fixups.go @@ -83,6 +83,10 @@ func (m *MergeBot) MergeFixups(mkit *specs.MergeKit, opts *MergeBotOpts) error { m.Logger.InfoC(fmt.Sprintf( "[%s] PR branch already present for fixup %s. Nothing to do.", prBranchName, name)) + err = m.restoreFiles(kitDir, files, opts, worktree) + if err != nil { + return err + } continue } diff --git a/pkg/kit/merge_metadata.go b/pkg/kit/merge_metadata.go index 1eb2592..021eb77 100644 --- a/pkg/kit/merge_metadata.go +++ b/pkg/kit/merge_metadata.go @@ -15,6 +15,7 @@ import ( "github.com/macaroni-os/mark-devkit/pkg/specs" "github.com/go-git/go-git/v5" + "github.com/go-git/go-git/v5/plumbing" "github.com/macaroni-os/macaronictl/pkg/utils" ) @@ -94,8 +95,58 @@ func (m *MergeBot) prepareMetadataDir(mkit *specs.MergeKit, return err } - commitHash, err := m.commitFiles(kitDir, []string{layoutConf}, - cMsg, opts, worktree) + headRef, err := repo.Head() + if err != nil { + return err + } + + prBranchName := fmt.Sprintf( + "%s%s", + prBranchPrefix, "metadata-update", + ) + + // Restore committed files in order to avoid + // that the same changes will be added in new commit. + files := []string{layoutConf} + defer m.restoreFiles(kitDir, files, opts, worktree) + + if opts.PullRequest { + // NOTE: pull request for a new branch it doesn't make sense + // Probably we need to add a check. + prBranchExists, err := BranchExists(kit.Url, prBranchName) + if err != nil { + return err + } + + if prBranchExists { + // PR is already been pushed. + m.Logger.InfoC(fmt.Sprintf( + "[%s] PR branch already present for metadata. Nothing to do.", + prBranchName)) + return nil + } + + branchRef := plumbing.NewBranchReferenceName(prBranchName) + ref := plumbing.NewHashReference(branchRef, headRef.Hash()) + // The created reference is saved in the storage. + err = repo.Storer.SetReference(ref) + if err != nil { + return err + } + + // Creating the new branch for the PR. + branchCoOpts := git.CheckoutOptions{ + Branch: plumbing.ReferenceName(branchRef), + Create: false, + Keep: true, + } + + if err := worktree.Checkout(&branchCoOpts); err != nil { + return err + } + } + + commitHash, err := m.commitFiles(kitDir, files, cMsg, opts, worktree) if err != nil { return err } @@ -105,7 +156,25 @@ func (m *MergeBot) prepareMetadataDir(mkit *specs.MergeKit, m.Logger.InfoC(fmt.Sprintf("%s", commit)) } - m.hasCommit = true + if opts.PullRequest { + + // Return to working branch + targetBranchRef := plumbing.NewBranchReferenceName(kit.Branch) + branchCoOpts := git.CheckoutOptions{ + Branch: plumbing.ReferenceName(targetBranchRef), + Create: false, + Keep: true, + } + err := worktree.Checkout(&branchCoOpts) + if err != nil { + return err + } + + m.metadataUpdate = true + + } else { + m.hasCommit = true + } } return nil diff --git a/pkg/kit/merge_profiles.go b/pkg/kit/merge_profiles.go index f9c90df..3ffa412 100644 --- a/pkg/kit/merge_profiles.go +++ b/pkg/kit/merge_profiles.go @@ -16,6 +16,7 @@ import ( "github.com/macaroni-os/mark-devkit/pkg/specs" "github.com/go-git/go-git/v5" + "github.com/go-git/go-git/v5/plumbing" "github.com/macaroni-os/macaronictl/pkg/utils" ) @@ -137,6 +138,60 @@ func (m *MergeBot) prepareProfilesDir(mkit *specs.MergeKit, return err } + headRef, err := repo.Head() + if err != nil { + return err + } + + prBranchName := fmt.Sprintf( + "%s%s", + prBranchPrefix, "profiles-update", + ) + + // Restore committed files in order to avoid + // that the same changes will be added in new commit. + files := []string{} + for f := range files4Commit { + files = append(files, f) + } + defer m.restoreFiles(kitDir, files, opts, worktree) + + if opts.PullRequest { + // NOTE: pull request for a new branch it doesn't make sense + // Probably we need to add a check. + prBranchExists, err := BranchExists(kit.Url, prBranchName) + if err != nil { + return err + } + + if prBranchExists { + // PR is already been pushed. + m.Logger.InfoC(fmt.Sprintf( + "[%s] PR branch already present for profiles. Nothing to do.", + prBranchName)) + return nil + } + + branchRef := plumbing.NewBranchReferenceName(prBranchName) + ref := plumbing.NewHashReference(branchRef, headRef.Hash()) + // The created reference is saved in the storage. + err = repo.Storer.SetReference(ref) + if err != nil { + return err + } + + // Creating the new branch for the PR. + branchCoOpts := git.CheckoutOptions{ + Branch: plumbing.ReferenceName(branchRef), + Create: false, + Keep: true, + } + + if err := worktree.Checkout(&branchCoOpts); err != nil { + return err + } + } + for f, cMsg := range files4Commit { commitHash, err := m.commitFiles(kitDir, []string{f}, cMsg, opts, worktree) @@ -150,7 +205,24 @@ func (m *MergeBot) prepareProfilesDir(mkit *specs.MergeKit, } } - m.hasCommit = true + if opts.PullRequest { + // Return to working branch + targetBranchRef := plumbing.NewBranchReferenceName(kit.Branch) + branchCoOpts := git.CheckoutOptions{ + Branch: plumbing.ReferenceName(targetBranchRef), + Create: false, + Keep: true, + } + err := worktree.Checkout(&branchCoOpts) + if err != nil { + return err + } + + m.profilesUpdate = true + + } else { + m.hasCommit = true + } } return nil