Skip to content

Commit

Permalink
(#1) Connect to GitHub and create a release (#2)
Browse files Browse the repository at this point in the history
* Authentication via user token
* Find repo based on username/repo_name
* Create release with a given tag
* Set the name on the release created
  • Loading branch information
llorllale committed Mar 12, 2018
1 parent 0f48b6e commit 3520d14
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 1 deletion.
1 change: 1 addition & 0 deletions NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ This project includes:
Apache HttpCore under Apache License, Version 2.0
AspectJ runtime under Eclipse Public License - v 1.0
Bean Validation API under The Apache Software License, Version 2.0
cactoos under MIT
CDI APIs under Apache License, Version 2.0
Commons IO under The Apache Software License, Version 2.0
Extended StAX API under Dual license consisting of the CDDL v1.1 and GPL v2
Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,11 @@
<artifactId>jcabi-github</artifactId>
<version>0.33.1</version>
</dependency>
<dependency>
<groupId>org.cactoos</groupId>
<artifactId>cactoos</artifactId>
<version>0.29</version>
</dependency>
</dependencies>

<build>
Expand Down
70 changes: 69 additions & 1 deletion src/main/java/org/llorllale/mvn/plgn/releasecat/Upload.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,90 @@

package org.llorllale.mvn.plgn.releasecat;

import com.jcabi.github.Coordinates;
import com.jcabi.github.Release;
import com.jcabi.github.Repo;
import com.jcabi.github.RtGithub;
import java.io.IOException;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.cactoos.Scalar;
import org.cactoos.scalar.IoCheckedScalar;

/**
* Uploads releases to GitHub.
*
* @author George Aristy (george.aristy@gmail.com)
* @since 0.1.0
* @todo #1:30min Add description to releases. This will be useful for displaying changelogs
* directly in the release. Right now we just have the tag and the name.
* @todo #1:30min Add ability to specify whether it's a prerelease or a full release. This
* would be useful when uploading release candidates.
* @todo #1:30min Add ability to upload artifacts to releases. This will be useful when
* someone wants to include sources and / or binaries.
*/
@Mojo(name = "upload", defaultPhase = LifecyclePhase.DEPLOY)
public final class Upload extends AbstractMojo {
@Parameter(name = "token", property = "releasecat.token")
private String token;

@Parameter(name = "user", property = "releasecat.user")
private String user;

@Parameter(name = "repo", property = "releasecat.repo")
private String repo;

@Parameter(name = "tag", property = "releasecat.tag")
private String tag;

@Parameter(name = "name", property = "releasecat.name")
private String name;

private final IoCheckedScalar<Repo> githubRepo;

/**
* Ctor.
*
* @since 0.1.0
*/
public Upload() {
this.githubRepo = new IoCheckedScalar<>(
() -> new RtGithub(this.token).repos().get(new Coordinates.Simple(this.user, this.repo))
);
}

/**
* For testing purposes.
*
* @param tag the git tag
* @param name the release name
* @param repo the github repo
* @since 0.1.0
*/
Upload(String tag, String name, Scalar<Repo> repo) {
this.tag = tag;
this.name = name;
this.githubRepo = new IoCheckedScalar<>(repo);
}

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
throw new UnsupportedOperationException("Not supported yet.");
this.getLog().info(String.format(
"Creating release with name %s and tag %s at %s/%s",
this.name, this.tag, this.user, this.repo
));
try {
new Release.Smart(
this.githubRepo.value()
.releases()
.create(this.tag)
).name(this.name);
} catch (IOException | IllegalArgumentException e) {
throw new MojoFailureException("Error creating release", e);
}
}
}
102 changes: 102 additions & 0 deletions src/test/java/org/llorllale/mvn/plgn/releasecat/UploadTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright 2018 George Aristy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.llorllale.mvn.plgn.releasecat;

// @checkstyle AvoidStaticImport (3 lines)
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;

import com.jcabi.github.Release;
import com.jcabi.github.Releases;
import com.jcabi.github.Repo;
import com.jcabi.github.Repos;
import com.jcabi.github.mock.MkGithub;
import java.io.IOException;
import org.apache.maven.plugin.MojoFailureException;
import org.junit.Test;

/**
* Tests for {@link Upload}.
*
* @author George Aristy (george.aristy@gmail.com)
* @since 0.1.0
* @checkstyle MethodName (500 lines)
*/
@SuppressWarnings("checkstyle:MultipleStringLiterals")
public final class UploadTest {
/**
* Release should be created with the given tag.
*
* @throws Exception unexpected
* @since 0.1.0
*/
@Test
public void releaseIsCreated() throws Exception {
final Repo repo = new MkGithub().repos()
.create(new Repos.RepoCreate("my_user/my_project", false));
new Upload("Tag v1.0", "Name v1.0", () -> repo).execute();
assertNotNull(
new Releases.Smart(repo.releases()).find("Tag v1.0")
);
}

/**
* Release should be created with the given name.
*
* @throws Exception unexpected
* @since 0.1.0
*/
@Test
public void releaseHasGivenName() throws Exception {
final Repo repo = new MkGithub().repos()
.create(new Repos.RepoCreate("my_user/my_project", false));
new Upload("Tag v1.0", "Name v1.0", () -> repo).execute();
assertThat(
new Release.Smart(new Releases.Smart(repo.releases()).find("Tag v1.0")).name(),
is("Name v1.0")
);
}

/**
* Mojo should wrap IOException in MojoFailureException.
*
* @throws Exception MojoFailureException
* @since 0.1.0
*/
@Test(expected = MojoFailureException.class)
public void mojoFailureIfIoError() throws Exception {
new Upload(
"my_user", "wrong_project",
() -> {
throw new IOException();
}
).execute();
}

/**
* Mojo should fail if the configuration is not specified (ie. is null). This results in an
* internal IllegalArgumentException.
*
* @throws Exception MojoFailureException
* @since 0.1.0
*/
@Test(expected = MojoFailureException.class)
public void mojoFailureIfConfigNotSpecified() throws Exception {
new Upload().execute();
}
}
22 changes: 22 additions & 0 deletions src/test/java/org/llorllale/mvn/plgn/releasecat/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2018 George Aristy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* Tests.
*
* @since 0.1.0
*/
package org.llorllale.mvn.plgn.releasecat;

3 comments on commit 3520d14

@0pdd
Copy link

@0pdd 0pdd commented on 3520d14 Mar 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 1-fc28441b discovered in src/main/java/org/llorllale/mvn/plgn/releasecat/Upload.java and submitted as #3. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link

@0pdd 0pdd commented on 3520d14 Mar 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 1-fdf75b0d discovered in src/main/java/org/llorllale/mvn/plgn/releasecat/Upload.java and submitted as #4. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link

@0pdd 0pdd commented on 3520d14 Mar 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 1-3284a5e8 discovered in src/main/java/org/llorllale/mvn/plgn/releasecat/Upload.java and submitted as #5. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.