github.com/kvattikuti/drone@v0.2.1-0.20140603034306-d400229a327a/pkg/model/commit.go (about)

     1  package model
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  )
     7  
     8  type Commit struct {
     9  	ID          int64     `meddler:"id,pk"            json:"id"`
    10  	RepoID      int64     `meddler:"repo_id"          json:"-"`
    11  	Status      string    `meddler:"status"           json:"status"`
    12  	Started     time.Time `meddler:"started,utctime"  json:"started"`
    13  	Finished    time.Time `meddler:"finished,utctime" json:"finished"`
    14  	Duration    int64     `meddler:"duration"         json:"duration"`
    15  	Hash        string    `meddler:"hash"             json:"hash"`
    16  	Branch      string    `meddler:"branch"           json:"branch"`
    17  	PullRequest string    `meddler:"pull_request"     json:"pull_request"`
    18  	Author      string    `meddler:"author"           json:"author"`
    19  	Gravatar    string    `meddler:"gravatar"         json:"gravatar"`
    20  	Timestamp   string    `meddler:"timestamp"        json:"timestamp"`
    21  	Message     string    `meddler:"message"          json:"message"`
    22  
    23  	Created time.Time `meddler:"created,utctime"  json:"created"`
    24  	Updated time.Time `meddler:"updated,utctime"  json:"updated"`
    25  }
    26  
    27  // Returns the Short (--short) Commit Hash.
    28  func (c *Commit) HashShort() string {
    29  	if len(c.Hash) > 6 {
    30  		return c.Hash[:6]
    31  	} else {
    32  		return c.Hash
    33  	}
    34  }
    35  
    36  // Returns the Gravatar Image URL.
    37  func (c *Commit) Image() string      { return fmt.Sprintf(GravatarPattern, c.Gravatar, 58) }
    38  func (c *Commit) ImageSmall() string { return fmt.Sprintf(GravatarPattern, c.Gravatar, 32) }
    39  func (c *Commit) ImageLarge() string { return fmt.Sprintf(GravatarPattern, c.Gravatar, 160) }
    40  
    41  // Returns the Started Date as an ISO8601
    42  // formatted string.
    43  func (c *Commit) StartedString() string {
    44  	return c.Started.Format("2006-01-02T15:04:05Z")
    45  }
    46  
    47  // Returns the Created Date as an ISO8601
    48  // formatted string.
    49  func (c *Commit) CreatedString() string {
    50  	return c.Created.Format("2006-01-02T15:04:05Z")
    51  }
    52  
    53  // Returns the Started Date as an ISO8601
    54  // formatted string.
    55  func (c *Commit) FinishedString() string {
    56  	return c.Finished.Format("2006-01-02T15:04:05Z")
    57  }
    58  
    59  // Set the Author's email address and calculate the
    60  // Gravatar hash.
    61  func (c *Commit) SetAuthor(email string) {
    62  	c.Author = email
    63  	c.Gravatar = createGravatar(email)
    64  }
    65  
    66  // Combined Repository and Commit details
    67  type RepoCommit struct {
    68  	// Repo Details
    69  	Slug  string `meddler:"slug"  json:"slug"`
    70  	Host  string `meddler:"host"  json:"host"`
    71  	Owner string `meddler:"owner" json:"owner"`
    72  	Name  string `meddler:"name"  json:"name"`
    73  
    74  	// Commit Details
    75  	Status      string    `meddler:"status"           json:"status"`
    76  	Started     time.Time `meddler:"started,utctime"  json:"started"`
    77  	Finished    time.Time `meddler:"finished,utctime" json:"finished"`
    78  	Duration    int64     `meddler:"duration"         json:"duration"`
    79  	Hash        string    `meddler:"hash"             json:"hash"`
    80  	Branch      string    `meddler:"branch"           json:"branch"`
    81  	PullRequest string    `meddler:"pull_request"     json:"pull_request"`
    82  	Author      string    `meddler:"author"           json:"author"`
    83  	Gravatar    string    `meddler:"gravatar"         json:"gravatar"`
    84  	Timestamp   string    `meddler:"timestamp"        json:"timestamp"`
    85  	Message     string    `meddler:"message"          json:"message"`
    86  	Created     time.Time `meddler:"created,utctime"  json:"created"`
    87  	Updated     time.Time `meddler:"updated,utctime"  json:"updated"`
    88  }
    89  
    90  // Returns the Short (--short) Commit Hash.
    91  func (c *RepoCommit) HashShort() string {
    92  	if len(c.Hash) > 6 {
    93  		return c.Hash[:6]
    94  	} else {
    95  		return c.Hash
    96  	}
    97  }
    98  
    99  // Returns the Gravatar Image URL.
   100  func (c *RepoCommit) Image() string      { return fmt.Sprintf(GravatarPattern, c.Gravatar, 42) }
   101  func (c *RepoCommit) ImageSmall() string { return fmt.Sprintf(GravatarPattern, c.Gravatar, 32) }
   102  func (c *RepoCommit) ImageLarge() string { return fmt.Sprintf(GravatarPattern, c.Gravatar, 160) }
   103  
   104  // Returns the Started Date as an ISO8601
   105  // formatted string.
   106  func (c *RepoCommit) StartedString() string {
   107  	return c.Started.Format("2006-01-02T15:04:05Z")
   108  }
   109  
   110  // Returns the Created Date as an ISO8601
   111  // formatted string.
   112  func (c *RepoCommit) CreatedString() string {
   113  	return c.Created.Format("2006-01-02T15:04:05Z")
   114  }
   115  
   116  // Returns the Started Date as an ISO8601
   117  // formatted string.
   118  func (c *RepoCommit) FinishedString() string {
   119  	return c.Finished.Format("2006-01-02T15:04:05Z")
   120  }