Skip to content

Commit

Permalink
Add before_commit hook
Browse files Browse the repository at this point in the history
  • Loading branch information
davegaeddert committed May 31, 2021
1 parent 6042de9 commit 4354879
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
8 changes: 7 additions & 1 deletion docs/content/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,13 @@ dependencies:

## Injecting commands (hooks)

WIP
```yaml
version: 3
dependencies:
- type: js
settings:
before_commit: npm run compile # Only runs in CI
```

## Environment variables

Expand Down
36 changes: 36 additions & 0 deletions internal/hooks/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package hooks

import (
"os"
"os/exec"
"strings"

"github.com/dropseed/deps/internal/output"
"github.com/dropseed/deps/internal/pullrequest"
)

func RunPullrequestHook(pr pullrequest.PullrequestAdapter, hookName string) error {
hookCmd := pr.GetSetting(hookName)

if hookCmd == nil {
return nil
}

output.Event("Executing %s hook", hookName)

hookCmdLines := strings.Split(hookCmd.(string), "\n")

for _, line := range hookCmdLines {
cmd := exec.Command("sh", "-c", line)
// specific env too? would be pr.Config.Env?
cmd.Env = os.Environ()
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
return err
}
}

return nil
}
7 changes: 5 additions & 2 deletions internal/runner/ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/dropseed/deps/internal/ci"
"github.com/dropseed/deps/internal/config"
"github.com/dropseed/deps/internal/git"
"github.com/dropseed/deps/internal/hooks"
"github.com/dropseed/deps/internal/output"
"github.com/dropseed/deps/internal/pullrequest"
"github.com/dropseed/deps/internal/schemaext"
Expand Down Expand Up @@ -227,15 +228,17 @@ func runUpdate(update *Update, base, head string, existingUpdate bool) error {
return errors.New("Update didn't generate any changes to commit")
}

if err := hooks.RunPullrequestHook(pr, "before_commit"); err != nil {
return err
}

git.Add()
git.Commit(schemaext.TitleForDeps(outputDeps))
// TODO try adding more lines for dependency breakdown,
// especially on lockfiles

git.PushBranch(head)

// TODO hooks or what do you do otherwise?

if pr != nil {
output.Debug("Waiting a second for the push to be processed by the host")
time.Sleep(2 * time.Second)
Expand Down

0 comments on commit 4354879

Please sign in to comment.