-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
305287b
commit b2565d3
Showing
9 changed files
with
689 additions
and
464 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,9 +13,15 @@ import ( | |
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"github.com/go-git/go-git/v5" | ||
"github.com/go-git/go-git/v5/plumbing" | ||
"github.com/go-git/go-git/v5/plumbing/object" | ||
"github.com/go-git/go-git/v5/plumbing/transport/http" | ||
"io" | ||
"log" | ||
"net/url" | ||
"os" | ||
"path" | ||
"slices" | ||
"strconv" | ||
"strings" | ||
|
@@ -66,6 +72,8 @@ type ( | |
CommentLint(context.Context, *TriggerContext, *atlasexec.SummaryReport) error | ||
// CommentPlan comments on the pull request with the schema plan. | ||
CommentPlan(context.Context, *TriggerContext, *atlasexec.SchemaPlan) error | ||
// IsCoAuthored checks if the given commit is co-authored by a bot. | ||
IsCoAuthored(ctx context.Context, commit string) (bool, error) | ||
} | ||
Logger interface { | ||
// Infof logs an info message. | ||
|
@@ -92,6 +100,8 @@ type ( | |
MigrateDown(context.Context, *atlasexec.MigrateDownParams) (*atlasexec.MigrateDown, error) | ||
// MigrateLintError runs the `migrate lint` command and fails if there are lint errors. | ||
MigrateLintError(context.Context, *atlasexec.MigrateLintParams) error | ||
// MigrateHash runs the `migrate lint` command and fails if there are lint errors. | ||
MigrateHash(context.Context, *atlasexec.MigrateHashParams) error | ||
// MigratePush runs the `migrate push` command. | ||
MigratePush(context.Context, *atlasexec.MigratePushParams) (string, error) | ||
// MigrateTest runs the `migrate test` command. | ||
|
@@ -474,10 +484,118 @@ func (a *Actions) MigrateLint(ctx context.Context) error { | |
if dirName == "" { | ||
return errors.New("atlasaction: missing required parameter dir-name") | ||
} | ||
dirURL := a.GetInput("dir") | ||
tc, err := a.GetTriggerContext(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
a.Infof("Branch: %s", tc.Branch) | ||
if a.GetBoolInput("auto-hash") && tc.PullRequest != nil && strings.HasPrefix(dirURL, "file://") { | ||
a.Infof("Checking if scm client exists") | ||
scm, err := tc.SCMClient() | ||
switch { | ||
case errors.Is(err, ErrNoSCM): | ||
case err != nil: | ||
return err | ||
default: | ||
dirPath := strings.TrimPrefix(dirURL, "file://") | ||
a.Infof("SCM client exists. checking if commit is co-authored") | ||
coAuthored, err := scm.IsCoAuthored(ctx, tc.PullRequest.Commit) | ||
if err != nil { | ||
return err | ||
} | ||
if !coAuthored { | ||
break | ||
} | ||
a.Infof("commit is co-authored. running migrate hash") | ||
if err := a.Atlas.MigrateHash(ctx, &atlasexec.MigrateHashParams{ | ||
DirURL: dirURL, | ||
ConfigURL: a.GetInput("config"), | ||
Env: a.GetInput("env"), | ||
Vars: a.GetVarsInput("vars"), | ||
}); err != nil { | ||
return err | ||
} | ||
r, err := git.PlainOpen(".") | ||
if err != nil { | ||
return err | ||
} | ||
w, err := r.Worktree() | ||
if err != nil { | ||
return err | ||
} | ||
a.Infof("checking out branch %s", plumbing.NewBranchReferenceName(tc.Branch)) | ||
if err := w.Checkout(&git.CheckoutOptions{ | ||
Branch: plumbing.NewBranchReferenceName(tc.Branch), | ||
Create: true, | ||
Force: true, | ||
}); err != nil { | ||
return err | ||
} | ||
a.Infof("Pulling from remote") | ||
auth := &http.BasicAuth{Username: "Atlas Action", Password: tc.Act.Getenv("GITHUB_TOKEN")} | ||
if err := w.PullContext(ctx, &git.PullOptions{RemoteName: "origin", ReferenceName: plumbing.NewBranchReferenceName(tc.Branch), Auth: auth}); err != nil { | ||
return err | ||
} | ||
status, err := w.Status() | ||
if err != nil { | ||
return err | ||
} | ||
a.Infof("git status: %s", status.String()) | ||
if f := status.File(path.Join(dirPath, "atlas.sum")); f == nil || f.Worktree != git.Modified { | ||
break | ||
} | ||
a.Infof("staging file") | ||
if _, err := w.Add(path.Join(dirPath, "atlas.sum")); err != nil { | ||
return err | ||
} | ||
status, err = w.Status() | ||
if err != nil { | ||
return err | ||
} | ||
a.Infof("git status: %s", status.String()) | ||
a.Infof("setting git config") | ||
conf, err := r.Config() | ||
if err != nil { | ||
return err | ||
} | ||
conf.Author.Name = "Atlas Action" | ||
conf.Author.Email = "[email protected]" | ||
if err := r.SetConfig(conf); err != nil { | ||
return err | ||
} | ||
a.Infof("committing changes") | ||
if _, err := w.Commit("fix directory sum file [skip ci]", &git.CommitOptions{}); err != nil { | ||
return err | ||
} | ||
status, err = w.Status() | ||
if err != nil { | ||
return err | ||
} | ||
a.Infof("git status: %s", status.String()) | ||
headRef, err := r.Head() | ||
if err != nil { | ||
log.Fatalf("Failed to get HEAD reference: %v", err) | ||
} | ||
log.Printf("Current branch: %s\n", headRef.Name().Short()) | ||
commitIter, _ := r.Log(&git.LogOptions{}) | ||
commitCount := 0 | ||
commitIter.ForEach(func(c *object.Commit) error { | ||
commitCount++ | ||
log.Println("Commit:", c.Hash, c.Message) | ||
return nil | ||
}) | ||
log.Printf("Total commits: %d", commitCount) | ||
remotes, _ := r.Remotes() | ||
for _, remote := range remotes { | ||
log.Println("Remote:", remote.Config().URLs) | ||
} | ||
a.Infof("pushing changes") | ||
if err := r.PushContext(ctx, &git.PushOptions{RemoteName: "origin", Auth: auth}); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
var ( | ||
resp bytes.Buffer | ||
isLintErr bool | ||
|
@@ -487,7 +605,7 @@ func (a *Actions) MigrateLint(ctx context.Context) error { | |
switch err := a.Atlas.MigrateLintError(ctx, &atlasexec.MigrateLintParams{ | ||
Context: rc, | ||
DevURL: a.GetInput("dev-url"), | ||
DirURL: a.GetInput("dir"), | ||
DirURL: dirURL, | ||
ConfigURL: a.GetInput("config"), | ||
Env: a.GetInput("env"), | ||
Base: a.GetAtlasURLInput("dir-name", "tag"), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.