Skip to content

Commit

Permalink
merge: Permits PRs for metadata and profiles update
Browse files Browse the repository at this point in the history
  • Loading branch information
geaaru committed Jan 6, 2025
1 parent 253318d commit ae1b27f
Show file tree
Hide file tree
Showing 6 changed files with 243 additions and 16 deletions.
88 changes: 83 additions & 5 deletions pkg/kit/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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)
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/kit/merge_bump.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
11 changes: 4 additions & 7 deletions pkg/kit/merge_eclasses.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/kit/merge_fixups.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
75 changes: 72 additions & 3 deletions pkg/kit/merge_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand Down
74 changes: 73 additions & 1 deletion pkg/kit/merge_profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit ae1b27f

Please sign in to comment.