Skip to content

Instantly share code, notes, and snippets.

@tchap
Last active August 31, 2015 10:13
Show Gist options
  • Save tchap/1295c0ea186013774fd8 to your computer and use it in GitHub Desktop.
Save tchap/1295c0ea186013774fd8 to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"errors"
"fmt"
"io"
"os"
"strconv"
"github.com/salsita/go-pivotaltracker/v5/pivotal"
)
func main() {
if err := run(); err != nil {
fmt.Fprintln(os.Stderr, "\nError:", err)
}
}
func run() error {
args := os.Args[1:]
if len(args) != 1 {
fmt.Fprintln(os.Stderr, "Usage: migrate PT_PROJECT_ID")
return errors.New("missing the project ID argument")
}
projectId, err := strconv.Atoi(args[0])
if err != nil {
return err
}
token := os.Getenv("PT_TOKEN")
if token == "" {
return fmt.Errorf("Environment variable PT_TOKEN is not set")
}
client := pivotal.NewClient(token)
cursor, err := client.Stories.Iterate(projectId, "state:started label:implemented")
if err != nil {
return err
}
var stories []*pivotal.Story
for {
story, err := cursor.Next()
if err != nil {
if err == io.EOF {
break
}
return err
}
stories = append(stories, story)
}
fmt.Println("The following stories are going to be modified:")
fmt.Println()
for _, story := range stories {
fmt.Printf(" %v\n", story.URL)
}
fmt.Println()
reader := bufio.NewReader(os.Stdin)
fmt.Print("Press ENTER to continue... ")
if _, err := reader.ReadString('\n'); err != nil {
return err
}
fmt.Println()
for _, story := range stories {
fmt.Printf("Updating story %v\n", story.Id)
_, _, err = client.Stories.Update(projectId, story.Id, &pivotal.StoryRequest{
State: pivotal.StoryStateFinished,
Labels: dropImplementedLabel(story.Labels),
})
if err != nil {
return err
}
}
fmt.Println("---> Success")
fmt.Println()
return nil
}
func dropImplementedLabel(in []*pivotal.Label) (out *[]*pivotal.Label) {
labels := make([]*pivotal.Label, 0, len(in)-1)
for _, label := range in {
if label.Name != "implemented" {
labels = append(labels, &pivotal.Label{
Id: label.Id,
})
}
}
return &labels
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment