Skip to content

Commit

Permalink
Add --no-file-mode option
Browse files Browse the repository at this point in the history
  • Loading branch information
int128 committed Mar 11, 2019
1 parent 2726b4c commit 90e2d88
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 51 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Options:
-C, --directory string Change to directory before copy
--dry-run Upload files but do not update the branch actually
-m, --message string Commit message (mandatory)
--no-file-mode Ignore executable bit of file and treat as 0644
-u, --owner string GitHub repository owner (mandatory)
-r, --repo string GitHub repository name (mandatory)
--token string GitHub API token [$GITHUB_TOKEN]
Expand Down
3 changes: 3 additions & 0 deletions adaptors/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func (c *Cmd) Run(ctx context.Context, args []string) int {
f.StringVarP(&o.Branch, "branch", "b", "", "Branch name (default: default branch of repository)")
f.StringVarP(&o.Chdir, "directory", "C", "", "Change to directory before copy")
f.StringVar(&o.GitHubToken, "token", "", fmt.Sprintf("GitHub API token [$%s]", envGitHubToken))
f.BoolVar(&o.NoFileMode, "no-file-mode", false, "Ignore executable bit of file and treat as 0644")
f.BoolVar(&o.DryRun, "dry-run", false, "Upload files but do not update the branch actually")
f.BoolVar(&o.Debug, "debug", false, "Show debug logs")

Expand Down Expand Up @@ -114,6 +115,7 @@ func (c *Cmd) copy(ctx context.Context, o copyOptions) int {
CommitMessage: git.CommitMessage(o.CommitMessage),
BranchName: git.BranchName(o.Branch),
Paths: o.Paths,
NoFileMode: o.NoFileMode,
DryRun: o.DryRun,
}); err != nil {
c.Logger.Errorf("Could not copy files: %s", err)
Expand All @@ -128,6 +130,7 @@ type copyOptions struct {
CommitMessage string
Branch string // optional
Paths []string
NoFileMode bool
DryRun bool
}

Expand Down
36 changes: 36 additions & 0 deletions adaptors/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,42 @@ func TestCmd_Run(t *testing.T) {
}
})

t.Run("--no-file-mode", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

copyUseCase := mock_usecases.NewMockCopyUseCase(ctrl)
copyUseCase.EXPECT().
Do(ctx, usecases.CopyUseCaseIn{
Repository: git.RepositoryID{Owner: "owner", Name: "repo"},
CommitMessage: "commit-message",
Paths: []string{"file1", "file2"},
NoFileMode: true,
})

cmd := Cmd{
CopyUseCase: copyUseCase,
Env: mock_adaptors.NewMockEnv(ctrl),
Logger: mock_adaptors.NewLogger(t),
LoggerConfig: mock_adaptors.NewMockLoggerConfig(ctrl),
GitHubClientInit: newGitHubClientInit(ctrl, infrastructure.GitHubClientInitOptions{Token: "YOUR_TOKEN"}),
}
args := []string{
cmdName,
"--token", "YOUR_TOKEN",
"-u", "owner",
"-r", "repo",
"-m", "commit-message",
"--no-file-mode",
"file1",
"file2",
}
exitCode := cmd.Run(ctx, args)
if exitCode != exitCodeOK {
t.Errorf("exitCode wants %d but %d", exitCodeOK, exitCode)
}
})

t.Run("--dry-run", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
Expand Down
61 changes: 30 additions & 31 deletions usecases/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,37 @@ func (u *CopyUseCase) Do(ctx context.Context, in usecases.CopyUseCaseIn) error {
}
u.Logger.Infof("Logged in as %s", out.CurrentUserName)

gitFiles := make([]git.File, len(files))
for i, file := range files {
content, err := u.FileSystem.ReadAsBase64EncodedContent(file.Path)
if err != nil {
return errors.Wrapf(err, "error while reading file %s", file.Path)
}
blobSHA, err := u.GitHub.CreateBlob(ctx, git.NewBlob{
Repository: in.Repository,
Content: content,
})
if err != nil {
return errors.Wrapf(err, "error while creating a blob for %s", file.Path)
}
gitFile := git.File{
Filename: file.Path,
BlobSHA: blobSHA,
Executable: !in.NoFileMode && file.Executable,
}
gitFiles[i] = gitFile
u.Logger.Infof("Uploaded %s as blob %s", file.Path, blobSHA)
}

if in.BranchName == "" {
// copy to the default branch
if err := u.copyToExistingBranch(ctx, copyToExistingBranchIn{
Files: files,
CopyUseCaseIn: in,
Files: gitFiles,
Repository: out.Repository,
CommitMessage: in.CommitMessage,
BranchName: out.DefaultBranchName,
ParentCommitSHA: out.DefaultBranchCommitSHA,
ParentTreeSHA: out.DefaultBranchTreeSHA,
DryRun: in.DryRun,
}); err != nil {
return errors.WithStack(err)
}
Expand All @@ -58,55 +79,33 @@ func (u *CopyUseCase) Do(ctx context.Context, in usecases.CopyUseCaseIn) error {
return errors.Errorf("branch %s does not exist", in.BranchName)
}
if err := u.copyToExistingBranch(ctx, copyToExistingBranchIn{
Files: files,
CopyUseCaseIn: in,
Files: gitFiles,
Repository: out.Repository,
CommitMessage: in.CommitMessage,
BranchName: in.BranchName,
ParentCommitSHA: out.BranchCommitSHA,
ParentTreeSHA: out.BranchTreeSHA,
DryRun: in.DryRun,
}); err != nil {
return errors.WithStack(err)
}
return nil
}

type copyToExistingBranchIn struct {
Files []adaptors.File
usecases.CopyUseCaseIn

Files []git.File
Repository git.RepositoryID
CommitMessage git.CommitMessage
BranchName git.BranchName
ParentCommitSHA git.CommitSHA
ParentTreeSHA git.TreeSHA
DryRun bool
}

func (u *CopyUseCase) copyToExistingBranch(ctx context.Context, in copyToExistingBranchIn) error {
gitFiles := make([]git.File, len(in.Files))
for i, file := range in.Files {
content, err := u.FileSystem.ReadAsBase64EncodedContent(file.Path)
if err != nil {
return errors.Wrapf(err, "error while reading file %s", file.Path)
}
blobSHA, err := u.GitHub.CreateBlob(ctx, git.NewBlob{
Repository: in.Repository,
Content: content,
})
if err != nil {
return errors.Wrapf(err, "error while creating a blob for %s", file.Path)
}
gitFiles[i] = git.File{
Filename: file.Path,
BlobSHA: blobSHA,
Executable: file.Executable,
}
u.Logger.Infof("Uploaded %s as blob %s", file.Path, blobSHA)
}

treeSHA, err := u.GitHub.CreateTree(ctx, git.NewTree{
Repository: in.Repository,
BaseTreeSHA: in.ParentTreeSHA,
Files: gitFiles,
Files: in.Files,
})
if err != nil {
return errors.Wrapf(err, "error while creating a tree")
Expand Down
69 changes: 49 additions & 20 deletions usecases/copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func TestCopyUseCase_Do(t *testing.T) {
}
})

t.Run("DefaultBranch/NothingToCommit", func(t *testing.T) {
t.Run("NothingToCommit", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

Expand Down Expand Up @@ -170,7 +170,7 @@ func TestCopyUseCase_Do(t *testing.T) {
}
})

t.Run("DefaultBranch/DryRun", func(t *testing.T) {
t.Run("NoFileMode", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

Expand All @@ -187,7 +187,33 @@ func TestCopyUseCase_Do(t *testing.T) {
DefaultBranchCommitSHA: "masterCommitSHA",
DefaultBranchTreeSHA: "masterTreeSHA",
}, nil)
gitHubCreateBlobTree(gitHub, ctx, repositoryID, "masterTreeSHA")
gitHub.EXPECT().
CreateBlob(ctx, git.NewBlob{
Repository: repositoryID,
Content: "base64content1",
}).
Return(git.BlobSHA("blobSHA1"), nil)
gitHub.EXPECT().
CreateBlob(ctx, git.NewBlob{
Repository: repositoryID,
Content: "base64content2",
}).
Return(git.BlobSHA("blobSHA2"), nil)
gitHub.EXPECT().
CreateTree(ctx, git.NewTree{
Repository: repositoryID,
BaseTreeSHA: "masterTreeSHA",
Files: []git.File{
{
Filename: "file1",
BlobSHA: "blobSHA1",
}, {
Filename: "file2",
BlobSHA: "blobSHA2",
},
},
}).
Return(git.TreeSHA("treeSHA"), nil)
gitHub.EXPECT().
CreateCommit(ctx, git.NewCommit{
Repository: repositoryID,
Expand All @@ -204,6 +230,13 @@ func TestCopyUseCase_Do(t *testing.T) {
Return(&adaptors.QueryCommitOut{
ChangedFiles: 1,
}, nil)
gitHub.EXPECT().
UpdateBranch(ctx, git.NewBranch{
Repository: repositoryID,
BranchName: "master",
CommitSHA: "commitSHA",
}, false).
Return(nil)

useCase := CopyUseCase{
FileSystem: fileSystem,
Expand All @@ -214,14 +247,14 @@ func TestCopyUseCase_Do(t *testing.T) {
Repository: repositoryID,
CommitMessage: "message",
Paths: []string{"path"},
DryRun: true,
NoFileMode: true,
})
if err != nil {
t.Errorf("Do returned error: %+v", err)
}
})

t.Run("GivenBranch", func(t *testing.T) {
t.Run("DryRun", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

Expand All @@ -230,23 +263,20 @@ func TestCopyUseCase_Do(t *testing.T) {
gitHub.EXPECT().
QueryRepository(ctx, adaptors.QueryRepositoryIn{
Repository: repositoryID,
BranchName: "gh-pages",
}).
Return(&adaptors.QueryRepositoryOut{
CurrentUserName: "current",
Repository: repositoryID,
DefaultBranchName: "master",
DefaultBranchCommitSHA: "masterCommitSHA",
DefaultBranchTreeSHA: "masterTreeSHA",
BranchCommitSHA: "ghCommitSHA",
BranchTreeSHA: "ghTreeSHA",
}, nil)
gitHubCreateBlobTree(gitHub, ctx, repositoryID, "ghTreeSHA")
gitHubCreateBlobTree(gitHub, ctx, repositoryID, "masterTreeSHA")
gitHub.EXPECT().
CreateCommit(ctx, git.NewCommit{
Repository: repositoryID,
TreeSHA: "treeSHA",
ParentCommitSHA: "ghCommitSHA",
ParentCommitSHA: "masterCommitSHA",
Message: "message",
}).
Return(git.CommitSHA("commitSHA"), nil)
Expand All @@ -258,13 +288,6 @@ func TestCopyUseCase_Do(t *testing.T) {
Return(&adaptors.QueryCommitOut{
ChangedFiles: 1,
}, nil)
gitHub.EXPECT().
UpdateBranch(ctx, git.NewBranch{
Repository: repositoryID,
BranchName: "gh-pages",
CommitSHA: "commitSHA",
}, false).
Return(nil)

useCase := CopyUseCase{
FileSystem: fileSystem,
Expand All @@ -273,16 +296,16 @@ func TestCopyUseCase_Do(t *testing.T) {
}
err := useCase.Do(ctx, usecases.CopyUseCaseIn{
Repository: repositoryID,
BranchName: "gh-pages",
CommitMessage: "message",
Paths: []string{"path"},
DryRun: true,
})
if err != nil {
t.Errorf("Do returned error: %+v", err)
}
})

t.Run("GivenBranch/DryRun", func(t *testing.T) {
t.Run("GivenBranch", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

Expand Down Expand Up @@ -319,6 +342,13 @@ func TestCopyUseCase_Do(t *testing.T) {
Return(&adaptors.QueryCommitOut{
ChangedFiles: 1,
}, nil)
gitHub.EXPECT().
UpdateBranch(ctx, git.NewBranch{
Repository: repositoryID,
BranchName: "gh-pages",
CommitSHA: "commitSHA",
}, false).
Return(nil)

useCase := CopyUseCase{
FileSystem: fileSystem,
Expand All @@ -330,7 +360,6 @@ func TestCopyUseCase_Do(t *testing.T) {
BranchName: "gh-pages",
CommitMessage: "message",
Paths: []string{"path"},
DryRun: true,
})
if err != nil {
t.Errorf("Do returned error: %+v", err)
Expand Down
1 change: 1 addition & 0 deletions usecases/interfaces/usecases.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ type CopyUseCaseIn struct {
CommitMessage git.CommitMessage
BranchName git.BranchName // default branch if empty
Paths []string
NoFileMode bool
DryRun bool
}

0 comments on commit 90e2d88

Please sign in to comment.