Project

Profile

Help

Repository Commit Hooks » History » Sprint/Milestone 3

Jens Krämer, 10/16/2023 09:36 AM

1 1 Felix Schäfer
# Repository Commit Hooks
2
3 3 Jens Krämer
This guide explains how to set up subversion pre- and post-commit hooks with your Planio repository. Using pre-commit webhooks, project managers can influence the commit life cycles of repositories, by for example enforcing a certain commit message format, limiting commit rights for certain portions of the subversion repository, or by enforcing other custom rules. Post-commit hooks are useful to trigger actions like CI runs, that should happen after a successful commit.
4 1 Felix Schäfer
5
{{>toc}}
6
7 3 Jens Krämer
## Setting up your commit hooks
8 1 Felix Schäfer
9 3 Jens Krämer
In your project, navigate to **Settings** → **Repositories**. Find the repository you're concerned with and click **Edit**. This form has a new row labelled **Repository hooks**, where a click on **Edit** leads to the list of commit hooks for that repository. Please click **Add** in order to create a new hook.
10 1 Felix Schäfer
11 3 Jens Krämer
## Hook configuration
12 1 Felix Schäfer
13 3 Jens Krämer
First of all, choose the type of hook you intend to create. Currently, the following hooks can be created:
14 1 Felix Schäfer
15 3 Jens Krämer
| Type | Stage | Description |
16
| --- | --- | --- |
17
| Insert ticket subject | post-commit | Changes the commit message by inserting a referenced tickets' subject after the reference: `#1234` => `#1234 [Ticket subject]` |
18
| Require valid ticket reference | pre-commit | Denies commits that do not mention a valid ticket reference in their commit message. Valid ticket references are of the form `#1234`. The referenced ticket must be visible to the committing user. |
19
| Web hook | pre-commit | Sends an HTTP POST request with commit information in JSON format to a user defined URL. The commit is accepted or denied based on the remote servers' response. You can define which response codes should be considered successful. If the commit is rejected, the response body will be passed to the committing user *if* the response has a content type of `text/plain`. Otherwise, a generic error message ("Commit denied by repository configuration") will be given. |
20
| Web hook | post-commit | Sends an HTTP POST request with commit information in JSON format to a user defined URL |
21 1 Felix Schäfer
22
You can set up multiple hooks for the same repository, and reorder them using the drag handles in the table. Hook execution stops at the first failed hook, later hooks are not executed.
23
24
## Web hook payload
25
26 3 Jens Krämer
The `JSON` payload we `POST` to your configured web hook URL contains information about the project, repository, and the actual commit.
27 1 Felix Schäfer
28
Sample payload:
29
30
~~~json
31
{
32
  "project":{
33
    "name":"Demo",
34
    "identifier":"demo",
35
    "id":10
36
  },
37
  "hostname":"demo.planio.com",
38
  "repository":{
39
    "id":45,
40
    "name":"hooktest",
41
    "full_name":"demo.hooktest",
42
    "html_url":"https://demo.planio.com/projects/demo/repository/45",
43
    "url":"https://demo.planio.com/projects/demo/repository/45",
44
    "issues_url":"https://demo.planio.com/projects/demo/issues.json",
45
    "size":90824,
46
    "svn_url":"https://demo.planio.com/svn/demo.hooktest"
47
  },
48
  "before":"1",
49
  "size":1,
50
  "commits":[{
51
    "committer":"jk",
52
    "message":"test commit",
53
    "added":[],
54
    "modified":["changed/file.txt"],
55
    "removed":[],
56
    "prop_changed":[]
57
  }],
58
  "head_commit":{
59
    "committer":"jk",
60
    "message":"test commit",
61
    "added":[],
62
    "modified":["changed/file.txt"],
63
    "removed":[],
64
    "prop_changed":[]
65
  }
66
}
67
~~~
68
69
Elements in the `project` and `repository` structures should be self-explaining. The remaining top level elements are:
70
71
- `before` - highest revision number before this commit
72
- `head_commit` - the last (and in the case of SVN, only) commit that is contained in this payload.
73
- `commits` - list of all commits contained in this payload (always one for SVN)
74
- `size` - number of commits contained in the payload (for SVN this will be always 1)
75
76 3 Jens Krämer
For consistency reasons we decided to stick to the payload format of the post-receive web hooks which we already have in place for Git  repositories - that's why there are the seemingly redundant `commits` list and separate `head_commit` elements, which are more useful in the case of Git.
77 1 Felix Schäfer
78
For each commit, the login of the committing user, the log message as well as the list of added / removed / modified paths, as well as paths that had properties changed, are given.