forked from drone-plugins/drone-hg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial commit for a mercurial plugin. wip
- Loading branch information
1 parent
557a2eb
commit feedb68
Showing
6 changed files
with
217 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,5 @@ _testmain.go | |
*.exe | ||
*.test | ||
*.prof | ||
|
||
drone-hg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |