grim/youtrack-import

Parents 186525273046
Children 5e6c52f18f2b
Make sure trac descriptions are always at the end of a comment and clean up the way fields are displayed
  • +67 -25
    trac/tickets.go
  • --- a/trac/tickets.go Mon Jul 27 22:58:56 2020 -0500
    +++ b/trac/tickets.go Tue Jul 28 01:07:24 2020 -0500
    @@ -138,40 +138,57 @@
    // as we iterate the comments and then only "commit" the change when it has
    // a new author/timestamp or when we're looking at the last one.
    var comment *youtrack.Comment
    + var description string
    for _, change := range t.Changes {
    - newComment := &youtrack.Comment{
    - Author: convertUsername(change.Author),
    - Created: convertTime(change.Time),
    - Updated: convertTime(change.Time),
    - Markdown: true,
    - }
    + if comment != nil && convertTime(change.Time) != comment.Created {
    + // Add the description at the end to make sure all the field changes
    + // are always listed before it.
    + if description != "" {
    + if comment.Text != "" {
    + comment.Text += "\n\n"
    + }
    + comment.Text += description
    + description = ""
    + }
    - // There's some oddness in our trac where we have a few changes where
    - // the author is null, so if it's empty string, set it to the
    - // unknownUser.
    - if newComment.Author == "" {
    - newComment.Author = unknownUser
    + // our changes were loaded from the database ordered by timestamp,
    + // so if we hit a new time stamp, we're in a new change.
    + issue.Comments = append(issue.Comments, comment)
    + comment = nil
    }
    if comment == nil {
    - comment = newComment
    - } else {
    - if comment.Author != newComment.Author || comment.Created != newComment.Created {
    - issue.Comments = append(issue.Comments, comment)
    - comment = newComment
    + comment = &youtrack.Comment{
    + Author: convertUsername(change.Author),
    + Created: convertTime(change.Time),
    + Updated: convertTime(change.Time),
    + Markdown: true,
    + }
    +
    + // There's some oddness in our trac where we have a few changes where
    + // the author is null, so if it's empty string, set it to the
    + // unknownUser.
    + if comment.Author == "" {
    + comment.Author = unknownUser
    }
    }
    text := ""
    switch change.Field {
    case "comment":
    - // if the comment already has text, add a newline to help make the
    - // actual comment text stand out.
    - if comment.Text != "" {
    - text += "\n"
    + description = convertString(change.NewValue)
    +
    + // if the comment is a reply, we strip off the first line as there's
    + // now way to replicated this properly in youtrack. Note trac uses
    + // `\r` like a bunch of weirdos...
    + if strings.Contains(convertString(change.OldValue), ".") {
    + newLine := strings.Index(description, "\r")
    + if newLine != -1 {
    + description = description[newLine+1:]
    + }
    }
    - text += convertString(change.NewValue)
    + continue
    case "description":
    issue.UpdatedBy = convertUsername(change.Author)
    text = "description: modified\n"
    @@ -184,17 +201,42 @@
    }
    if text == "" {
    + oldValue := convertString(change.OldValue)
    + if oldValue == "" {
    + oldValue = "None"
    + }
    +
    + newValue := convertString(change.NewValue)
    + if newValue == "" {
    + newValue = "None"
    + }
    +
    + fieldName := change.Field
    + switch fieldName {
    + case "owner":
    + fieldName = "Assignee"
    + }
    +
    text = fmt.Sprintf(
    - "%s: %s -> %s\n",
    - change.Field,
    - convertString(change.OldValue),
    - convertString(change.NewValue),
    + "%s: %s ⟶ %s\n",
    + fieldName,
    + oldValue,
    + newValue,
    )
    }
    comment.Text += text
    }
    + // Add the description at the end to make sure all the field changes are
    + // always listed before it.
    + if description != "" {
    + if comment.Text != "" {
    + comment.Text += "\n\n"
    + }
    + comment.Text += description
    + }
    +
    if comment != nil {
    issue.Comments = append(issue.Comments, comment)
    }