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

     1  package model
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  )
     7  
     8  const (
     9  	ScmGit = "git"
    10  	ScmHg  = "hg"
    11  	ScmSvn = "svn"
    12  )
    13  
    14  const (
    15  	HostBitbucket = "bitbucket.org"
    16  	HostGoogle    = "code.google.com"
    17  	HostCustom    = "custom"
    18  )
    19  
    20  const (
    21  	DefaultBranchGit = "master"
    22  	DefaultBranchHg  = "default"
    23  	DefaultBranchSvn = "trunk"
    24  )
    25  
    26  const (
    27  	githubRepoPattern           = "git://%s/%s/%s.git"
    28  	githubRepoPatternPrivate    = "git@%s:%s/%s.git"
    29  	bitbucketRepoPattern        = "https://bitbucket.org/%s/%s.git"
    30  	bitbucketRepoPatternPrivate = "git@bitbucket.org:%s/%s.git"
    31  )
    32  
    33  type Repo struct {
    34  	ID int64 `meddler:"id,pk" json:"id"`
    35  
    36  	// the full, canonical name of the repository, for example:
    37  	// github.com/bradrydzewski/go.stripe
    38  	Slug string `meddler:"slug" json:"slug"`
    39  
    40  	// the hosting service where the repository is stored,
    41  	// such as github.com, bitbucket.org, etc
    42  	Host string `meddler:"host" json:"host"`
    43  
    44  	// the owner of the repository on the host system.
    45  	// for example, the Github username.
    46  	Owner string `meddler:"owner" json:"owner"`
    47  
    48  	// URL-friendly version of a repository name on the
    49  	// host system.
    50  	Name string `meddler:"name" json:"name"`
    51  
    52  	// A value of True indicates the repository is closed source,
    53  	// while a value of False indicates the project is open source.
    54  	Private bool `meddler:"private" json:"private"`
    55  
    56  	// A value of True indicates the repository is disabled and
    57  	// no builds should be executed
    58  	Disabled bool `meddler:"disabled" json:"disabled"`
    59  
    60  	// A value of True indicates that pull requests are disabled
    61  	// for the repository and no builds will be executed
    62  	DisabledPullRequest bool `meddler:"disabled_pr" json:"disabled_pr"`
    63  
    64  	// indicates the type of repository, such as
    65  	// Git, Mercurial, Subversion or Bazaar.
    66  	SCM string `meddler:"scm" json:"scm"`
    67  
    68  	// the repository URL, for example:
    69  	// git://github.com/bradrydzewski/go.stripe.git
    70  	URL string `meddler:"url" json:"url"`
    71  
    72  	// username and password requires to authenticate
    73  	// to the repository
    74  	Username string `meddler:"username" json:"username"`
    75  	Password string `meddler:"password" json:"password"`
    76  
    77  	// RSA key pair that will injected into the virtual machine
    78  	// .ssh/id_rsa and .ssh/id_rsa.pub files.
    79  	PublicKey  string `meddler:"public_key"  json:"public_key"`
    80  	PrivateKey string `meddler:"private_key" json:"public_key"`
    81  
    82  	// Parameters stored external to the repository in YAML
    83  	// format, injected into the Build YAML at runtime.
    84  	Params map[string]string `meddler:"params,gob" json:"-"`
    85  
    86  	// the amount of time, in seconds the build will execute
    87  	// before exceeding its timelimit and being killed.
    88  	Timeout int64 `meddler:"timeout" json:"timeout"`
    89  
    90  	// Indicates the build should be executed in privileged
    91  	// mode. This could, for example, be used to run Docker in Docker.
    92  	Privileged bool `meddler:"privileged" json:"privileged"`
    93  
    94  	// Foreign keys signify the User that created
    95  	// the repository and team account linked to
    96  	// the repository.
    97  	UserID int64 `meddler:"user_id"  json:"user_id"`
    98  	TeamID int64 `meddler:"team_id"  json:"team_id"`
    99  
   100  	Created time.Time `meddler:"created,utctime" json:"created"`
   101  	Updated time.Time `meddler:"updated,utctime" json:"updated"`
   102  }
   103  
   104  // Creates a new repository
   105  func NewRepo(host, owner, name, scm, url string) (*Repo, error) {
   106  	repo := Repo{}
   107  	repo.URL = url
   108  	repo.SCM = scm
   109  	repo.Host = host
   110  	repo.Owner = owner
   111  	repo.Name = name
   112  	repo.Slug = fmt.Sprintf("%s/%s/%s", host, owner, name)
   113  	key, err := generatePrivateKey()
   114  	if err != nil {
   115  		return nil, err
   116  	}
   117  
   118  	repo.PublicKey = marshalPublicKey(&key.PublicKey)
   119  	repo.PrivateKey = marshalPrivateKey(key)
   120  	return &repo, nil
   121  }
   122  
   123  // Creates a new GitHub repository
   124  func NewGitHubRepo(domain, owner, name string, private bool) (*Repo, error) {
   125  	var url string
   126  	switch private {
   127  	case false:
   128  		url = fmt.Sprintf(githubRepoPattern, domain, owner, name)
   129  	case true:
   130  		url = fmt.Sprintf(githubRepoPatternPrivate, domain, owner, name)
   131  	}
   132  	return NewRepo(domain, owner, name, ScmGit, url)
   133  }
   134  
   135  // Creates a new Bitbucket repository
   136  func NewBitbucketRepo(owner, name string, private bool) (*Repo, error) {
   137  	var url string
   138  	switch private {
   139  	case false:
   140  		url = fmt.Sprintf(bitbucketRepoPattern, owner, name)
   141  	case true:
   142  		url = fmt.Sprintf(bitbucketRepoPatternPrivate, owner, name)
   143  	}
   144  	return NewRepo(HostBitbucket, owner, name, ScmGit, url)
   145  }
   146  
   147  func (r *Repo) DefaultBranch() string {
   148  	switch r.SCM {
   149  	case ScmGit:
   150  		return DefaultBranchGit
   151  	case ScmHg:
   152  		return DefaultBranchHg
   153  	case ScmSvn:
   154  		return DefaultBranchSvn
   155  	default:
   156  		return DefaultBranchGit
   157  	}
   158  }