Skip to content

Commit afac47a

Browse files
committed
Expose hook script into documentation
1 parent 09b878f commit afac47a

File tree

3 files changed

+28
-19
lines changed

3 files changed

+28
-19
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ result in less work for Jenkins to determine the projects that need to
5454
be rebuilt. This new hook is achieved by adding `branch` and
5555
`changsetId` parameters to the notification URL. Newer versions of
5656
Mercurial can achieve this with an in-process hook such as
57-
that shown in test sources in `MercurialRule.registerHook`.
57+
that shown in test sources in [hook.py](https://github.com/jenkinsci/mercurial-plugin/tree/master/src/test/resources/hook.py)
5858

5959
## Windows/TortoiseHG Integration 
6060

src/test/java/hudson/plugins/mercurial/MercurialRule.java

+16-18
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
import java.util.Set;
2121
import java.util.TreeSet;
2222
import static org.junit.Assert.*;
23+
24+
import org.apache.commons.io.IOUtils;
25+
import org.apache.commons.lang.StringUtils;
2326
import org.junit.Assume;
2427
import org.junit.AssumptionViolatedException;
2528
import org.junit.rules.ExternalResource;
@@ -189,26 +192,21 @@ public void registerHook(FilePath repo) throws Exception {
189192
FilePath hgDir = repo.child(".hg");
190193
FilePath enforcePython3 = hgDir.child("enforce-python3.py");
191194
enforcePython3.write(
192-
"import urllib.request, urllib.parse\n" +
193-
"def precommit(**kwargs):\n" +
194-
" urllib.request.Request('http://nowhere.net/')\n", null);
195+
"import urllib.request, urllib.parse\n" +
196+
"def precommit(**kwargs):\n" +
197+
" urllib.request.Request('http://nowhere.net/')\n", null);
198+
195199
FilePath hook = hgDir.child("hook.py");
196-
hook.write(
197-
"import urllib.request, urllib.parse\n" +
198-
"def commit(ui, repo, node, **kwargs):\n" +
199-
" data = {\n" +
200-
" 'url': '" + repo.toURI().toString() + "',\n" +
201-
" 'branch': repo[node].branch(),\n" +
202-
" 'changesetId': node,\n" +
203-
" }\n" +
204-
" req = urllib.request.Request('" + j.getURL() + "mercurial/notifyCommit')\n" +
205-
" rsp = urllib.request.urlopen(req, urllib.parse.urlencode(data).encode(\"utf-8\"))\n" +
206-
" # TODO gives some error about bytes vs. str: ui.warn('Notify Commit hook response: %s\\n' % rsp.read())\n" +
207-
" pass\n", null);
200+
201+
String hook_text = IOUtils.toString(this.getClass().getResourceAsStream("hook.py"), "UTF-8");
202+
StringUtils.replace(hook_text, "@JENKINS_URL@", j.getURL().toString());
203+
StringUtils.replace(hook_text, "@REPO_URL@", repo.toURI().toString());
204+
hook.write(hook_text, null);
205+
208206
hgDir.child("hgrc").write(
209-
"[hooks]\n" +
210-
"precommit.enforce-python3 = python:" + enforcePython3.getRemote() + ":precommit\n" +
211-
"commit.jenkins = python:" + hook.getRemote() + ":commit", null);
207+
"[hooks]\n" +
208+
"precommit.enforce-python3 = python:" + enforcePython3.getRemote() + ":precommit\n" +
209+
"commit.jenkins = python:" + hook.getRemote() + ":commit", null);
212210
}
213211

214212
}

src/test/resources/hook.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import urllib.request, urllib.parse
2+
def commit(ui, repo, node, **kwargs):
3+
data = {
4+
'url': '@REPO_URL@',
5+
'branch': repo[node].branch(),
6+
'changesetId': node,
7+
}
8+
req = urllib.request.Request('@JENKINS_URL@mercurial/notifyCommit')
9+
rsp = urllib.request.urlopen(req, urllib.parse.urlencode(data).encode("utf-8"))
10+
# TODO gives some error about bytes vs. str: ui.warn('Notify Commit hook response: %s\n' % rsp.read())
11+
pass

0 commit comments

Comments
 (0)