> {
40 |
41 | private static final Logger LOGGER = Logger.getLogger(ComputedFolderWebHookTrigger.class.getName());
42 |
43 |
44 |
45 |
46 | private final String token;
47 |
48 |
49 | public String getToken() {
50 | return token;
51 | }
52 |
53 |
54 |
55 | @DataBoundConstructor
56 | public ComputedFolderWebHookTrigger(String token) {
57 | this.token = token;
58 | LOGGER.info("setting token:"+token);
59 |
60 | }
61 |
62 |
63 | public ComputedFolderWebHookTriggerResult trigger() {
64 | if(job == null)
65 | return null;
66 | Queue.Item queueItem = job.scheduleBuild2(0);
67 | return new ComputedFolderWebHookTriggerResult(queueItem);
68 | }
69 |
70 |
71 |
72 |
73 |
74 | /**
75 | * Our {@link hudson.model.Descriptor}
76 | */
77 | @Extension
78 | @SuppressWarnings("unused") // instantiated by Jenkins
79 | public static class DescriptorImpl extends TriggerDescriptor {
80 | /**
81 | * {@inheritDoc}
82 | */
83 | public boolean isApplicable(Item item) {
84 | return item instanceof ComputedFolder;
85 | }
86 |
87 | /**
88 | * {@inheritDoc}
89 | */
90 | public String getDisplayName() {
91 | return "Scan by webhook";
92 | }
93 | }
94 |
95 |
96 | }
97 |
--------------------------------------------------------------------------------
/src/main/java/com/igalg/jenkins/plugins/mswt/trigger/ComputedFolderWebHookTriggerResult.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2019 igalg
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 | package com.igalg.jenkins.plugins.mswt.trigger;
25 |
26 | import hudson.model.Queue.Item;
27 |
28 | public class ComputedFolderWebHookTriggerResult {
29 |
30 | private final String url;
31 | private final long id;
32 | private final boolean triggered;
33 |
34 | public ComputedFolderWebHookTriggerResult(Item queueItem) {
35 | if (queueItem != null) {
36 | this.url = queueItem.getUrl();
37 | this.id = queueItem.getId();
38 | this.triggered = true;
39 | } else {
40 | this.url = null;
41 | this.id = 0;
42 | this.triggered = false;
43 | }
44 | }
45 |
46 | public boolean isTriggered() {
47 | return triggered;
48 | }
49 |
50 | public long getId() {
51 | return id;
52 | }
53 |
54 | public String getUrl() {
55 | return url;
56 | }
57 |
58 | }
--------------------------------------------------------------------------------
/src/main/resources/com/igalg/jenkins/plugins/mswt/trigger/ComputedFolderWebHookTrigger/config.jelly:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/src/main/resources/com/igalg/jenkins/plugins/mswt/trigger/ComputedFolderWebHookTrigger/help-token.html:
--------------------------------------------------------------------------------
1 |
24 |
25 | The token to match with webhook token.
26 | Receive any HTTP request, JENKINS_URL/multibranch-webhook-trigger/invoke?token=[Trigger token]
27 | If a token match, than a multibranch scan will bi triggered.
28 |
--------------------------------------------------------------------------------
/src/main/resources/com/igalg/jenkins/plugins/mswt/trigger/ComputedFolderWebHookTrigger/help.html:
--------------------------------------------------------------------------------
1 |
24 |
25 |
26 | Allows Multibranch Scan Webhook Trigger to trigger scan of this multibranch job.
27 |
28 |
--------------------------------------------------------------------------------
/src/main/resources/index.jelly:
--------------------------------------------------------------------------------
1 |
24 |
25 |
26 | Trigger that can receive any HTTP request and trigger a multibranch job scan when token matched.
27 |
28 |
--------------------------------------------------------------------------------
/src/test/java/com/igalg/jenkins/plugins/mswt/ComputedFolderWebHookRequestReceiverTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2019 igalg
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 | package com.igalg.jenkins.plugins.mswt;
25 |
26 | import static com.google.common.collect.ImmutableMap.of;
27 | import static com.google.common.collect.Lists.newArrayList;
28 | import static com.google.common.collect.Maps.newHashMap;
29 | import static org.assertj.core.api.Assertions.assertThat;
30 |
31 | import java.util.List;
32 | import java.util.Map;
33 |
34 | import org.junit.Before;
35 | import org.junit.Test;
36 |
37 | public class ComputedFolderWebHookRequestReceiverTest {
38 |
39 | ComputedFolderWebHookRequestReceiver computedFolderWebHookRequestReceiver;
40 |
41 | @Before
42 | public void init() {
43 | computedFolderWebHookRequestReceiver = new ComputedFolderWebHookRequestReceiver();
44 | }
45 |
46 | @Test
47 | public void testThatNoTokenReturnsNull() {
48 |
49 | Map> headers = newHashMap();
50 | Map parameterMap = newHashMap();
51 | String token = computedFolderWebHookRequestReceiver.getGivenToken(headers, parameterMap);
52 | assertThat(token).isNull();
53 | }
54 |
55 | @Test
56 | public void testThatParameterTokenReturnsThatToken() {
57 | Map> headers = newHashMap();
58 | Map parameterMap = of("token", new String[] {"token-parameter"});
59 | final String token = computedFolderWebHookRequestReceiver.getGivenToken(headers, parameterMap);
60 | assertThat(token).isEqualTo("token-parameter");
61 | }
62 |
63 | @Test
64 | public void testThatHeaderTokenReturnsThatToken() {
65 | Map> headers = of("token", (List) newArrayList("token-header"));
66 | Map parameterMap = newHashMap();
67 | String token = computedFolderWebHookRequestReceiver.getGivenToken(headers, parameterMap);
68 | assertThat(token).isEqualTo("token-header");
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/src/test/java/com/igalg/jenkins/plugins/mswt/locator/ComputedFolderWebHookTriggerLocatorTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2019 igalg
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 | package com.igalg.jenkins.plugins.mswt.locator;
25 |
26 | import static org.assertj.core.api.Assertions.assertThat;
27 | import static org.mockito.Mockito.mock;
28 | import static org.mockito.Mockito.when;
29 |
30 | import java.util.ArrayList;
31 | import java.util.HashMap;
32 | import java.util.List;
33 | import java.util.Map;
34 |
35 | import org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject;
36 | import org.junit.Before;
37 | import org.junit.Test;
38 | import org.junit.runner.RunWith;
39 | import org.powermock.api.mockito.PowerMockito;
40 | import org.powermock.core.classloader.annotations.PrepareForTest;
41 | import org.powermock.modules.junit4.PowerMockRunner;
42 |
43 | import com.cloudbees.hudson.plugins.folder.computed.ComputedFolder;
44 | import com.igalg.jenkins.plugins.mswt.trigger.ComputedFolderWebHookTrigger;
45 |
46 | import hudson.triggers.Trigger;
47 | import hudson.triggers.TriggerDescriptor;
48 |
49 | @SuppressWarnings("rawtypes")
50 | @RunWith(PowerMockRunner.class)
51 | @PrepareForTest({WorkflowMultiBranchProject.class})
52 | public class ComputedFolderWebHookTriggerLocatorTest {
53 |
54 | private List computedFolderList;
55 | private final WorkflowMultiBranchProject multibranchJobWithNoToken = createJobWorkflowMultiBranchProject("abc");
56 | private final WorkflowMultiBranchProject multibranchJobWithToken = createJobWorkflowMultiBranchProject("token");
57 | private final WorkflowMultiBranchProject multibranchJobWithNoTrigger = createJobWorkflowMultiBranchProjectWithouTrigger();
58 | private ComputedFolderLocator computedFolderLocator;
59 |
60 | @Before
61 | public void before() {
62 | computedFolderList = new ArrayList<>();
63 | computedFolderList.add(multibranchJobWithNoToken);
64 | computedFolderList.add(multibranchJobWithToken);
65 | computedFolderList.add(multibranchJobWithNoTrigger);
66 | computedFolderLocator = new ComputedFolderLocator() {
67 |
68 | @Override
69 | public List getAllComputedFolders(){
70 | return computedFolderList;
71 | }
72 | };
73 | }
74 |
75 | @Test
76 | public void testThatJobsWithMatchedTokenFoundedAndNotMatchNotFounded() {
77 | final String givenToken = "token";
78 |
79 | List jobsToTrigger = getJobNameList(ComputedFolderWebHookTriggerLocator.locateJobsWithTrigger(computedFolderLocator, givenToken));
80 |
81 | assertThat(jobsToTrigger.contains(multibranchJobWithToken.getFullName())).isTrue();
82 | assertThat(jobsToTrigger.contains(multibranchJobWithNoToken.getFullName())).isFalse();
83 | }
84 |
85 | @Test
86 | public void testThatJobsWithoutTriggerAreNotLocated() {
87 | final String givenToken = "token";
88 |
89 | List jobsToTrigger = getJobNameList(ComputedFolderWebHookTriggerLocator.locateJobsWithTrigger(computedFolderLocator, givenToken));
90 |
91 |
92 | assertThat(jobsToTrigger.contains(multibranchJobWithNoTrigger.getFullName())).isFalse();
93 |
94 | }
95 |
96 |
97 | private WorkflowMultiBranchProject createJobWorkflowMultiBranchProjectWithouTrigger() {
98 | WorkflowMultiBranchProject mock = PowerMockito.mock(WorkflowMultiBranchProject.class);
99 | PowerMockito.when(mock.getFullName())
100 | .thenReturn("WorkflowMultiBranchProject_noTrigger");
101 | return mock;
102 | }
103 | private WorkflowMultiBranchProject createJobWorkflowMultiBranchProject(String token) {
104 | WorkflowMultiBranchProject mock = PowerMockito.mock(WorkflowMultiBranchProject.class);
105 | PowerMockito.when(mock.getFullName()).thenReturn("WorkflowMultiBranchProject_" + token);
106 | Map> triggers = new HashMap<>();
107 | TriggerDescriptor typeDescr = mock(TriggerDescriptor.class);
108 | ComputedFolderWebHookTrigger trigger = new ComputedFolderWebHookTrigger(token);
109 | triggers.put(typeDescr, trigger);
110 | when(mock.getTriggers()).thenReturn(triggers);
111 | return mock;
112 | }
113 |
114 |
115 |
116 | private List getJobNameList(List jobsToTrigger) {
117 | List names = new ArrayList<>();
118 | for ( LocatedComputedFolder locatedComputedFolder : jobsToTrigger)
119 | names.add(locatedComputedFolder.getFullName());
120 | return names;
121 | }
122 |
123 |
124 |
125 | }
126 |
--------------------------------------------------------------------------------