Skip to content

Commit

Permalink
initial commit for a mercurial plugin. wip
Browse files Browse the repository at this point in the history
  • Loading branch information
bradrydzewski committed Oct 27, 2015
1 parent 557a2eb commit feedb68
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 0 deletions.
31 changes: 31 additions & 0 deletions .drone.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
build:
image: golang:1.5
environment:
- GO15VENDOREXPERIMENT=1
- GOOS=linux
- GOARCH=amd64
- CGO_ENABLED=0
commands:
- go get
- go build
- go test

publish:
docker:
username: drone
password: $$DOCKER_PASS
email: $$DOCKER_EMAIL
repo: plugins/drone-hg
when:
branch: master

plugin:
name: Mercurial
desc: Clones a mercurial repository.
type: clone
image: plugins/drone-hg
labels:
- scm
- vcs
- hg

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ _testmain.go
*.exe
*.test
*.prof

drone-hg
13 changes: 13 additions & 0 deletions DOCS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Use the Mercurial plugin to clone a mercurial repository. Note that Drone uses this plugin
by default for all Bitbucket mercurial repositories, without any configuration required. You can override
the default configuration with the following parameters:

* `path` relative path inside /drone/src where the repository is cloned

The following is a sample Git clone configuration in your .drone.yml file:

```yaml
clone:
image: hg
path: bitbucket.org/foo/bar
```
9 changes: 9 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Docker image for Drone's git-clone plugin
#
# CGO_ENABLED=0 go build -a -tags netgo
# docker build --rm=true -t plugins/drone-hg .

FROM alpine:3.2
RUN apk add -U ca-certificates git openssh curl perl && rm -rf /var/cache/apk/*
ADD drone-hg /bin/
ENTRYPOINT ["/bin/drone-hg"]
28 changes: 28 additions & 0 deletions logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
134 changes: 134 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package main

import (
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"os/user"
"path/filepath"
"strings"

"github.com/drone/drone-plugin-go/plugin"
)

var hgrcFile = `
[auth]
bb.prefix = https://bitbucket.org
bb.username = %s
bb.password = %s
`

// Params stores the git clone parameters used to
// configure and customzie the git clone behavior.
type Params struct {
}

func main() {
v := new(Params)
r := new(plugin.Repo)
b := new(plugin.Build)
w := new(plugin.Workspace)
plugin.Param("repo", r)
plugin.Param("build", b)
plugin.Param("workspace", w)
plugin.Param("vargs", &v)
plugin.MustParse()

err := run(r, b, w, v)
if err != nil {
os.Exit(1)
}
}

// run clones the repository and build revision
// into the build workspace.
func run(r *plugin.Repo, b *plugin.Build, w *plugin.Workspace, v *Params) error {

err := os.MkdirAll(w.Path, 0777)
if err != nil {
fmt.Printf("Error creating directory %s. %s\n", w.Path, err)
return err
}

// generate the .hgrc file
if err := writeHgrc(w); err != nil {
fmt.Fprintln(os.Stderr, err)
return err
}

var cmds []*exec.Cmd
cmds = append(cmds, clone(b))
cmds = append(cmds, update(b))

for _, cmd := range cmds {
cmd.Dir = w.Path
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
trace(cmd)
err := cmd.Run()
if err != nil {
return err
}
}

return nil
}

func clone(b *plugin.Build) *exec.Cmd {
return exec.Command(
"hg",
"clone",
"--branch",
b.Branch,
)
}

func update(b *plugin.Build) *exec.Cmd {
return exec.Command(
"hg",
"update",
b.Commit,
)
}

// Trace writes each command to standard error (preceded by a ‘$ ’) before it
// is executed. Used for debugging your build.
func trace(cmd *exec.Cmd) {
fmt.Println("$", strings.Join(cmd.Args, " "))
}

// Writes the hgrc file.
func writeHgrc(in *plugin.Workspace) error {
if in.Netrc == nil || len(in.Netrc.Machine) == 0 {
return nil
}
out := fmt.Sprintf(
hgrcFile,
in.Netrc.Machine, // TODO this may require adding http(s) prefix
in.Netrc.Login,
in.Netrc.Password,
)
home := "/root/.hg"
u, err := user.Current()
if err == nil {
home = u.HomeDir
}
path := filepath.Join(home, "hgrc")
return ioutil.WriteFile(path, []byte(out), 0600)
}

func isDirEmpty(name string) bool {
f, err := os.Open(name)
if err != nil {
return true
}
defer f.Close()

_, err = f.Readdir(1)
if err == io.EOF {
return true
}
return false
}

0 comments on commit feedb68

Please sign in to comment.