Skip to content

Commit

Permalink
#1140 Invitations.java
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Dec 17, 2016
1 parent c1e972a commit 9c9feff
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 18 deletions.
90 changes: 90 additions & 0 deletions src/main/java/com/rultor/agents/github/Invitations.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* Copyright (c) 2009-2016, rultor.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met: 1) Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer. 2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution. 3) Neither the name of the rultor.com nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.rultor.agents.github;

import com.jcabi.github.Github;
import com.jcabi.github.RtPagination;
import com.jcabi.http.Request;
import com.jcabi.http.response.RestResponse;
import com.jcabi.log.Logger;
import java.io.IOException;
import java.net.HttpURLConnection;
import javax.json.JsonObject;

/**
* GitHub invitations.
*
* @author Yegor Bugayenko (yegor256@gmail.com)
* @version $Id$
* @since 1.62
*/
final class Invitations {

/**
* Github client.
*/
private final transient Github github;

/**
* Ctor.
* @param ghb Github client
*/
Invitations(final Github ghb) {
this.github = ghb;
}

/**
* Accept them all.
* @throws IOException If fails
*/
public void accept() throws IOException {
// @checkstyle MultipleStringLiteralsCheck (2 lines)
final Request entry = this.github.entry().reset("Accept").header(
"Accept", "application/vnd.github.swamp-thing-preview+json"
);
final Iterable<JsonObject> all = new RtPagination<>(
entry.uri().path("/user/repository_invitations").back(),
RtPagination.COPYING
);
for (final JsonObject json : all) {
entry.uri().path("/user/repository_invitations/")
.path(Integer.toString(json.getInt("id"))).back()
.method(Request.PATCH)
.fetch()
.as(RestResponse.class)
.assertStatus(HttpURLConnection.HTTP_NO_CONTENT);
Logger.info(
this, "invitation to %s accepted",
json.getJsonObject("repository").getString("full_name")
);
}
}

}
20 changes: 2 additions & 18 deletions src/main/java/com/rultor/agents/github/StartsTalks.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public StartsTalks(final Github ghub) {
}

@Override
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
public void execute(final Talks talks) throws IOException {
final String since = new Time(
DateUtils.addMinutes(new Date(), -Tv.THREE)
Expand All @@ -99,7 +100,7 @@ public void execute(final Talks talks) throws IOException {
if ("mention".equals(reason)) {
names.add(this.activate(talks, event));
} else if ("invitation".equals(reason)) {
this.accept(event);
new Invitations(this.github).accept();
}
}
req.uri()
Expand All @@ -115,23 +116,6 @@ public void execute(final Talks talks) throws IOException {
);
}

/**
* Accept invitation to the repo.
* @param json The event
* @throws IOException If fails
*/
private void accept(final JsonObject json) throws IOException {
this.github.entry()
.uri().path("/user/repository_invitations/")
.path(json.getString("id")).back()
.method(Request.PATCH)
.header("Accept", "application/vnd.github.swamp-thing-preview+json")
.fetch()
.as(RestResponse.class)
.assertStatus(HttpURLConnection.HTTP_NO_CONTENT);
Logger.info(this, "invitation accepted to %s", this.coords(json));
}

/**
* Activate talk.
* @param talks Talks
Expand Down

0 comments on commit 9c9feff

Please sign in to comment.