Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Take back control of hooks #1006

Merged
merged 7 commits into from
Feb 23, 2017
Merged

Conversation

lunny
Copy link
Member

@lunny lunny commented Feb 22, 2017

This PR depends on go-gitea/git#34. On this PR, we changed the hooks directory structure.

Now we put all the server-side hooks(pre-receive, update, post-receive) on hooks/pre-receive.d/, hooks/update.d/ and hooks/post-receive.d/.
Gitea's hook will be at hooks/pre-receive.d/gitea, hooks/update.d/gitea and hooks/post-receive.d/gitea.
And the old user hooks will be at hooks/pre-receive.d/pre-receive and hooks/post-receive.d/post-receive. Of course, we will support multiple hooks on the UI sometime, it's will be easy if this PR is merged.

@lunny lunny added type/enhancement An improvement of existing functionality type/feature Completely new functionality. Can only be merged if feature freeze is not active. labels Feb 22, 2017
@lunny lunny added this to the 1.1.0 milestone Feb 22, 2017
Copy link
Member

@bkcsoft bkcsoft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise LGTM

cmd/hook.go Outdated
CmdHook = cli.Command{
Name: "hook",
Usage: "Delegate commands to corresponding Git hooks",
Description: "All sub-commands should only be called by Git",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"This should only be called by Git"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

models/repo.go Outdated
return x.Where("id > 0").Iterate(new(Repository),
func(idx int, bean interface{}) error {
if err := createDelegateHooks(bean.(*Repository).RepoPath()); err != nil {
return err
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe prefix the error with SyncRepositoryHook: %v ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

@bkcsoft
Copy link
Member

bkcsoft commented Feb 22, 2017

Of course, we will support multiple hooks on the UI sometime, it's will be easy if this PR is merged.

Agreed :)

@tboerger tboerger added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Feb 22, 2017
@lunny lunny added the pr/wip This PR is not ready for review label Feb 22, 2017
@lunny lunny changed the title Take control hooks back [WIP] Take control hooks back Feb 22, 2017
@bkcsoft bkcsoft changed the title [WIP] Take control hooks back [WIP] Take back control of hooks Feb 22, 2017
@lunny lunny removed the pr/wip This PR is not ready for review label Feb 22, 2017
@lunny lunny changed the title [WIP] Take back control of hooks Take back control of hooks Feb 22, 2017
@bkcsoft
Copy link
Member

bkcsoft commented Feb 22, 2017

LGTM

@tboerger tboerger added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Feb 22, 2017
if len(os.Getenv("SSH_ORIGINAL_COMMAND")) == 0 {
return nil
}
if err := setup("hooks/pre-receive.log"); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function name 'setup' is a bit of vague as to what it actually does. I guess it's about setting up a logger?
But I don't understand why it has anything to do with the database engine.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setup is to initialize the command, this function is for serval commands which the different is the log place.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it's resulted from factoring out the common 'preparing' part of all the commands. Maybe the Before callback is a suitable place for it.

https://sourcegraph.com/github.com/lunny/gitea@f132edf80f2de3b9335e99eec7d23abdfcc6670f/-/blob/vendor/github.com/urfave/cli/command.go#L32:2-32:8

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before should be the same actions, but this time we have a parameter - log name.

Copy link
Contributor

@typeless typeless Feb 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Before callback has the context parameter which has the needed (I guess) information like the command name to construct the path of the log file, if I wasn't misunderstanding something

Actually, I am not sure if putting it in Before is better or not. But there is the possibility. 😉

args := c.Args()
if len(args) != 3 {
fail("Arguments received are not equal to three", "Arguments received are not equal to three")
} else if len(args[0]) == 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose the two conditions are not mutually exclusive? Not sure if I understand it correctly, but the use of else if seems suspicious.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Here I just move codes from update.go to hook.go. I think it's no problem. Suppose the command hook update "" a b. The refName is empty.

}

func runHookUpdate(c *cli.Context) error {
if len(os.Getenv("SSH_ORIGINAL_COMMAND")) == 0 {
Copy link
Contributor

@typeless typeless Feb 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why the check is needed, with the nearby context.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the SSH_ORIGINAL_COMMAND is not set, that's because it is called by shell on manually.

@lunny lunny force-pushed the lunny/tack_control_hooks_back branch from f132edf to 39387ec Compare February 22, 2017 08:41
}
}

if err = ioutil.WriteFile(oldHookPath, []byte(hookTpls[i]), 0777); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is 0777 necessary? Least privilege is prefered in general.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. It seems 0755 is enough.

Copy link
Contributor

@typeless typeless Feb 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If 0700 is enough then 0700 is better. I cannot think of a scenario where 'others' need the permissions to R/W it.

Edit: Okay, it would be much convenient when debugging 😆

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. But I ls -l the repository's root dir, all the permission is 755. Don't know why.

@typeless
Copy link
Contributor

Otherwise LGTM and good job 👍

@lunny
Copy link
Member Author

lunny commented Feb 22, 2017

will fix #343

cmd/hook.go Outdated

"github.com/urfave/cli"

"code.gitea.io/gitea/models"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

coding style?

import (
  // stdlib
  "encoding/json"
  "fmt"

  // local packages
  "code.gitea.io/gitea/models"
  "code.gitea.io/sdk/gitea"

  // external packages
  "github.com/foo/bar"
  "gopkg.io/baz.v1"
)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

"github.com/Unknwon/com"
"github.com/go-xorm/xorm"

"code.gitea.io/gitea/modules/setting"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same above coding style.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

@appleboy
Copy link
Member

build fails

@appleboy
Copy link
Member

@lunny rebase base on master can fix build errors?

@lunny
Copy link
Member Author

lunny commented Feb 23, 2017

Yes. let me try.

@lunny lunny force-pushed the lunny/tack_control_hooks_back branch from f42099d to 9a9f14d Compare February 23, 2017 01:17
@appleboy
Copy link
Member

LGTM

@tboerger tboerger added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Feb 23, 2017
@lunny
Copy link
Member Author

lunny commented Feb 23, 2017

let L-G-T-M work

@lunny lunny merged commit 0e6b9ea into go-gitea:master Feb 23, 2017
lunny added a commit to lunny/gitea that referenced this pull request Feb 23, 2017
var (
hookNames = []string{"pre-receive", "update", "post-receive"}
hookTpls = []string{
fmt.Sprintf("#!/usr/bin/env %s\nORI_DIR=`pwd`\nSHELL_FOLDER=$(cd \"$(dirname \"$0\")\";pwd)\ncd \"$ORI_DIR\"\nfor i in `ls \"$SHELL_FOLDER/pre-receive.d\"`; do\n sh \"$SHELL_FOLDER/pre-receive.d/$i\"\ndone", setting.ScriptType),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's late but still want to comment it. You don't need the quotes for the ls and you don't need to call ls. Just something like for I in folder/*; do done. Beside that I would use source instead of sh to execute the scripts, than you can also define env variables in scripts that can be used in another script later on

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you are right since I'm not familiar with shell script. We also have runServe could define env variables before these script to be executed. So it's no need here to put another one.

@lunny lunny deleted the lunny/tack_control_hooks_back branch March 8, 2017 15:11
@zeripath zeripath mentioned this pull request Feb 3, 2020
7 tasks
@go-gitea go-gitea locked and limited conversation to collaborators Nov 23, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. type/enhancement An improvement of existing functionality type/feature Completely new functionality. Can only be merged if feature freeze is not active.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants