[go: up one dir, main page]

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

feat(webhook): support branch/tag delete events (JENKINS-66638). #36

Merged
merged 9 commits into from
Nov 2, 2021
135 changes: 135 additions & 0 deletions src/main/java/org/jenkinsci/plugin/gitea/GiteaDeleteSCMEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* The MIT License
*
* Copyright (c) 2021, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.jenkinsci.plugin.gitea;

import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.plugins.git.BranchSpec;
import hudson.plugins.git.GitSCM;
import hudson.plugins.git.GitStatus;
import hudson.plugins.git.extensions.impl.IgnoreNotifyCommit;
import hudson.scm.SCM;
import jenkins.scm.api.*;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.jgit.transport.URIish;
import org.jenkinsci.plugin.gitea.client.api.GiteaDeleteEvent;

import java.net.URISyntaxException;
import java.util.Collections;
import java.util.Map;

/**
* A {@link SCMHeadEvent} for a {@link GiteaDeleteEvent}.
*/
public class GiteaDeleteSCMEvent extends AbstractGiteaSCMHeadEvent<GiteaDeleteEvent> {
/**
* Constructor.
*
* @param payload the payload.
* @param origin the origin.
*/
public GiteaDeleteSCMEvent(@NonNull GiteaDeleteEvent payload, @CheckForNull String origin) {
super(Type.REMOVED, payload, origin);
}

/**
* {@inheritDoc}
*/
@Override
public String descriptionFor(@NonNull SCMNavigator navigator) {
String ref = getPayload().getRef();
ref = ref.startsWith(Constants.R_HEADS) ? ref.substring(Constants.R_HEADS.length()) : ref;
ref = ref.startsWith(Constants.R_TAGS) ? ref.substring(Constants.R_TAGS.length()) : ref;
return "Delete event for " + getPayload().getRefType() + " " + ref + " in repository " + getPayload().getRepository().getName();
}

/**
* {@inheritDoc}
*/
@Override
public String descriptionFor(SCMSource source) {
String ref = getPayload().getRef();
ref = ref.startsWith(Constants.R_HEADS) ? ref.substring(Constants.R_HEADS.length()) : ref;
ref = ref.startsWith(Constants.R_TAGS) ? ref.substring(Constants.R_TAGS.length()) : ref;
return "Delete event for " + getPayload().getRefType() + " " + ref;
}

/**
* {@inheritDoc}
*/
@Override
public String description() {
String ref = getPayload().getRef();
ref = ref.startsWith(Constants.R_HEADS) ? ref.substring(Constants.R_HEADS.length()) : ref;
ref = ref.startsWith(Constants.R_TAGS) ? ref.substring(Constants.R_TAGS.length()) : ref;
return "Delete event for " + getPayload().getRefType() + " " + ref + " in repository " +
getPayload().getRepository().getOwner().getUsername() + "/" +
getPayload().getRepository().getName();
}

/**
* {@inheritDoc}
*/
@NonNull
@Override
public Map<SCMHead, SCMRevision> headsFor(GiteaSCMSource source) {
String ref = getPayload().getRef();
ref = ref.startsWith(Constants.R_HEADS) ? ref.substring(Constants.R_HEADS.length()) : ref;
ref = ref.startsWith(Constants.R_TAGS) ? ref.substring(Constants.R_TAGS.length()) : ref;
if ("branch".equals(getPayload().getRefType())) {
BranchSCMHead h = new BranchSCMHead(ref);
return Collections.<SCMHead, SCMRevision>singletonMap(h, null);
} else if ("tag".equals(getPayload().getRefType())) {
TagSCMHead h = new TagSCMHead(ref, System.currentTimeMillis());
return Collections.<SCMHead, SCMRevision>singletonMap(h, null);
} else {
return Collections.<SCMHead, SCMRevision>emptyMap();
}
}

/**
* Our handler.
*/
@Extension
public static class HandlerImpl extends GiteaWebhookHandler<GiteaDeleteSCMEvent, GiteaDeleteEvent> {

/**
* {@inheritDoc}
*/
@Override
protected GiteaDeleteSCMEvent createEvent(GiteaDeleteEvent payload, String origin) {
return new GiteaDeleteSCMEvent(payload, origin);
}

/**
* {@inheritDoc}
*/
@Override
protected void process(GiteaDeleteSCMEvent event) {
SCMHeadEvent.fireNow(event);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* The MIT License
*
* Copyright (c) 2021, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.jenkinsci.plugin.gitea.client.api;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

/**
* Gitea {@link GiteaEventType#DELETE} event.
*/
@JsonIgnoreProperties(ignoreUnknown = Gitea.IGNORE_UNKNOWN_PROPERTIES)
public class GiteaDeleteEvent extends GiteaEvent {
private String ref;
private String refType;

@Override
public GiteaDeleteEvent clone() {
return (GiteaDeleteEvent) super.clone();
}

@Override
public String toString() {
return "GiteaDeleteEvent{" +
super.toString() +
", ref='" + ref + '\'' +
", refType='" + refType + '\'' +
'}';
}

public String getRef() {
return ref;
}

public void setRef(String ref) {
this.ref = ref;
}

public String getRefType() {
return refType;
}

@JsonProperty("ref_type")
public void setRefType(String refType) {
this.refType = refType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public enum GiteaEventType {
CREATE("create"),
PUSH("push"),
PULL_REQUEST("pull_request"),
REPOSITORY("repository");
REPOSITORY("repository"),
DELETE("delete");

private final String key;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package org.jenkinsci.plugin.gitea;

import jenkins.scm.api.SCMHead;
import jenkins.scm.api.SCMRevision;
import jenkins.scm.impl.mock.MockSCMNavigator;
import jenkins.scm.impl.mock.MockSCMSource;
import org.jenkinsci.plugin.gitea.client.api.GiteaDeleteEvent;
import org.jenkinsci.plugin.gitea.client.api.GiteaOwner;
import org.jenkinsci.plugin.gitea.client.api.GiteaRepository;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Map;

import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.*;

public class GiteaDeleteSCMEventTest {

GiteaSCMSource giteaSCMSource = new GiteaSCMSource("", "", "");
MockSCMSource scmSource = new MockSCMSource("controllerId", "giteaRepo", new ArrayList<>());
MockSCMNavigator scmNavigator = new MockSCMNavigator("controllerId", new ArrayList<>());
GiteaDeleteSCMEvent branchDeleteEvent = new GiteaDeleteSCMEvent(withGiteaDeleteEvent("feature/branch", "branch"), "?");
GiteaDeleteSCMEvent tagDeleteEvent = new GiteaDeleteSCMEvent(withGiteaDeleteEvent("mytag", "tag"), "?");

@Test
public void descriptionForSCMNavigator_withBranchDeleteEvent() {
assertThat(branchDeleteEvent.descriptionFor(scmNavigator), is("Delete event for branch feature/branch in repository giteaRepo"));
}

@Test
public void descriptionForSCMNavigator_withTagDeleteEvent() {
assertThat(tagDeleteEvent.descriptionFor(scmNavigator), is("Delete event for tag mytag in repository giteaRepo"));
}

@Test
public void descriptionForSCMSource_withBranchDeleteEvent() {
assertThat(branchDeleteEvent.descriptionFor(scmSource), is("Delete event for branch feature/branch"));
}

@Test
public void descriptionForSCMSource_withTagDeleteEvent() {
assertThat(tagDeleteEvent.descriptionFor(scmSource), is("Delete event for tag mytag"));
}

@Test
public void description_withBranchDeleteEvent() {
assertThat(branchDeleteEvent.description(), is("Delete event for branch feature/branch in repository ownerUser/giteaRepo"));
}

@Test
public void description_withTagDeleteEvent() {
assertThat(tagDeleteEvent.description(), is("Delete event for tag mytag in repository ownerUser/giteaRepo"));
}

@Test
public void headsFor_withBranchDeleteEvent() {
Map<SCMHead, SCMRevision> headsFor = branchDeleteEvent.headsFor(giteaSCMSource);

assertThat(headsFor.size(), is(1));
SCMHead scmHead = headsFor.keySet().iterator().next();
assertThat(scmHead.getName(), is("feature/branch"));
SCMRevision scmRevision = headsFor.values().iterator().next();
assertThat(scmRevision, nullValue());

}

@Test
public void headsFor_withTagDeleteEvent() {
Map<SCMHead, SCMRevision> headsFor = tagDeleteEvent.headsFor(giteaSCMSource);

assertThat(headsFor.size(), is(1));
SCMHead scmHead = headsFor.keySet().iterator().next();
assertThat(scmHead.getName(), is("mytag"));
SCMRevision scmRevision = headsFor.values().iterator().next();
assertThat(scmRevision, nullValue());
}

GiteaDeleteEvent withGiteaDeleteEvent(String ref, String refType) {
GiteaDeleteEvent event = new GiteaDeleteEvent();
event.setRef(ref);
event.setRefType(refType);
GiteaRepository repository = new GiteaRepository();
repository.setName("giteaRepo");
GiteaOwner owner = new GiteaOwner();
owner.setUsername("ownerUser");
repository.setOwner(owner);
event.setRepository(repository);
return event;
}

}