github.com/LGUG2Z/story@v0.4.1/cli/prepare.go (about) 1 package cli 2 3 import ( 4 "fmt" 5 "os" 6 "sort" 7 "strings" 8 9 "github.com/LGUG2Z/story/git" 10 "github.com/LGUG2Z/story/manifest" 11 "github.com/spf13/afero" 12 "github.com/urfave/cli" 13 ) 14 15 // TODO: Add tests 16 func PrepareCmd(fs afero.Fs) cli.Command { 17 return cli.Command{ 18 Name: "prepare", 19 Usage: "Prepares a story for merges to trunk", 20 Action: cli.ActionFunc(func(c *cli.Context) error { 21 if !isStory { 22 return ErrNotWorkingOnAStory 23 } 24 25 if c.Args().Present() { 26 return ErrCommandTakesNoArguments 27 } 28 29 story, err := manifest.LoadStory(fs) 30 if err != nil { 31 return err 32 } 33 34 mergePrepMessage := fmt.Sprintf("[story prepare] Preparing '%s' for merge [skip ci]", story.Name) 35 36 // Update the story hashes 37 hashes, err := story.GetCommitHashes(fs) 38 if err != nil { 39 return err 40 } 41 42 story.Hashes = hashes 43 44 // Create the story folder if it doesn't exist 45 exists, err := afero.DirExists(fs, "story") 46 if err != nil { 47 return err 48 } 49 50 if !exists { 51 if err := fs.Mkdir("story", os.FileMode(0700)); err != nil { 52 return err 53 } 54 } 55 56 // Write the story .meta to the story folder and stage the file 57 storyNameWithoutSlash := strings.ReplaceAll(story.Name, "/", "-") 58 if err := story.WriteToLocation(fs, fmt.Sprintf("story/%s.json", storyNameWithoutSlash)); err != nil { 59 return err 60 } 61 62 _, err = git.Add(git.AddOpts{Project: "story", Files: []string{fmt.Sprintf("%s.json", storyNameWithoutSlash)}}) 63 if err != nil { 64 return err 65 } 66 67 // Recreate the .meta from the story .meta 68 m := manifest.Meta{Projects: story.AllProjects, Artifacts: story.Artifacts, Organisation: story.Orgranisation} 69 for artifact := range m.Artifacts { 70 m.Artifacts[artifact] = false 71 } 72 73 // Write the reconstructed .meta to the metarepo folder and stage the file 74 if err := m.Write(fs); err != nil { 75 return err 76 } 77 78 _, err = git.Add(git.AddOpts{Files: []string{".meta"}}) 79 if err != nil { 80 return err 81 } 82 83 // Format the hashes to the GitHub format to link to a specific commit 84 var hashMessages []string 85 for project, hash := range hashes { 86 commitUrl := fmt.Sprintf("https://github.com/%s/%s/commit/%s", story.Orgranisation, project, hash) 87 hashMessages = append(hashMessages, commitUrl) 88 } 89 90 sort.Strings(hashMessages) 91 92 // Commit on the metarepo 93 output, err := git.Commit(git.CommitOpts{Messages: []string{mergePrepMessage, strings.Join(hashMessages, "\n")}}) 94 if err != nil { 95 return err 96 } 97 98 printGitOutput(output, metarepo) 99 100 return nil 101 }), 102 } 103 }