Skip to content

Commit

Permalink
Merge pull request #579 from martinvanzijl/issue_512_methods_to_updat…
Browse files Browse the repository at this point in the history
…e_milestones

Add methods to update and delete milestones.
  • Loading branch information
bitwiseman authored Oct 30, 2019
2 parents d4ddf45 + 03edacf commit 6956922
Show file tree
Hide file tree
Showing 20 changed files with 1,119 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/main/java/org/kohsuke/github/GHMilestone.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,29 @@ public void reopen() throws IOException {
edit("state", "open");
}

/**
* Deletes this milestone.
*/
public void delete() throws IOException {
root.retrieve().method("DELETE").to(getApiRoute());
}

private void edit(String key, Object value) throws IOException {
new Requester(root)._with(key, value).method("PATCH").to(getApiRoute());
}

public void setTitle(String title) throws IOException {
edit("title", title);
}

public void setDescription(String description) throws IOException {
edit("description", description);
}

public void setDueOn(Date dueOn) throws IOException {
edit("due_on", GitHub.printDate(dueOn));
}

protected String getApiRoute() {
return "/repos/"+owner.getOwnerName()+"/"+owner.getName()+"/milestones/"+number;
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/kohsuke/github/GitHub.java
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,9 @@ public Reader renderMarkdown(String text) throws IOException {
}

/*package*/ static String printDate(Date dt) {
return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(dt);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
df.setTimeZone(TimeZone.getTimeZone("GMT"));
return df.format(dt);
}

/*package*/ static final ObjectMapper MAPPER = new ObjectMapper();
Expand Down
64 changes: 64 additions & 0 deletions src/test/java/org/kohsuke/github/GHMilestoneTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.kohsuke.github;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.Date;

/**
* @author Martin van Zijl
*/
public class GHMilestoneTest extends AbstractGitHubWireMockTest {

@Before
@After
public void cleanUp() throws Exception {
// Cleanup is only needed when proxying
if (!mockGitHub.isUseProxy()) {
return;
}

for (GHMilestone milestone : getRepository(gitHubBeforeAfter).listMilestones(GHIssueState.ALL)) {
if ("Original Title".equals(milestone.getTitle()) ||
"Updated Title".equals(milestone.getTitle())) {
milestone.delete();
}
}
}

@Test
public void testUpdateMilestone() throws Exception {
GHRepository repo = getRepository();
GHMilestone milestone = repo.createMilestone("Original Title",
"To test the update methods");

String NEW_TITLE = "Updated Title";
String NEW_DESCRIPTION = "Updated Description";
Date NEW_DUE_DATE = GitHub.parseDate("2020-10-05T13:00:00Z");
Date OUTPUT_DUE_DATE = GitHub.parseDate("2020-10-05T07:00:00Z");

milestone.setTitle(NEW_TITLE);
milestone.setDescription(NEW_DESCRIPTION);
milestone.setDueOn(NEW_DUE_DATE);

// Force reload.
milestone = repo.getMilestone(milestone.getNumber());

assertEquals(NEW_TITLE, milestone.getTitle());
assertEquals(NEW_DESCRIPTION, milestone.getDescription());

// The time is truncated when sent to the server, but still part of the returned value
// 07:00 midnight PDT
assertEquals(OUTPUT_DUE_DATE, milestone.getDueOn());
}

protected GHRepository getRepository() throws IOException {
return getRepository(gitHub);
}

private GHRepository getRepository(GitHub gitHub) throws IOException {
return gitHub.getOrganization("github-api-test-org").getRepository("github-api");
}
}
72 changes: 72 additions & 0 deletions src/test/java/org/kohsuke/github/GitHubStaticTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.kohsuke.github;

import org.junit.Assert;
import org.junit.Test;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.TimeZone;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.not;

/**
* Unit test for {@link GitHub} static helpers.
*
* @author Liam Newman
*/
public class GitHubStaticTest extends Assert {

@Test
public void timeRoundTrip() throws Exception {
Instant instantNow = Instant.now();

Date instantSeconds = Date.from(instantNow.truncatedTo(ChronoUnit.SECONDS));
Date instantMillis = Date.from(instantNow.truncatedTo(ChronoUnit.MILLIS));

// TODO: other formats
String instantFormatSlash = formatDate(instantMillis, "yyyy/MM/dd HH:mm:ss ZZZZ");
String instantFormatDash = formatDate(instantMillis, "yyyy-MM-dd'T'HH:mm:ss'Z'");
String instantFormatMillis = formatDate(instantMillis, "yyyy-MM-dd'T'HH:mm:ss.S'Z'");
String instantSecondsFormatMillis = formatDate(instantSeconds, "yyyy-MM-dd'T'HH:mm:ss.S'Z'");
String instantBadFormat = formatDate(instantMillis, "yy-MM-dd'T'HH:mm'Z'");


assertThat(GitHub.parseDate(GitHub.printDate(instantSeconds)),
equalTo(GitHub.parseDate(GitHub.printDate(instantMillis))));

assertThat(instantSeconds,
equalTo(GitHub.parseDate(GitHub.printDate(instantSeconds))));

assertThat(instantMillis,
not(equalTo(GitHub.parseDate(GitHub.printDate(instantMillis)))));

assertThat(instantSeconds,
equalTo(GitHub.parseDate(instantFormatSlash)));

assertThat(instantSeconds,
equalTo(GitHub.parseDate(instantFormatDash)));

assertThat(instantMillis,
equalTo(GitHub.parseDate(instantFormatMillis)));

assertThat(instantSeconds,
equalTo(GitHub.parseDate(instantSecondsFormatMillis)));

try {
GitHub.parseDate(instantBadFormat);
fail("Bad time format should throw.");
} catch (IllegalStateException e) {
assertThat(e.getMessage(), equalTo("Unable to parse the timestamp: " + instantBadFormat));
}
}

static String formatDate(Date dt, String format) {
SimpleDateFormat df = new SimpleDateFormat(format);
df.setTimeZone(TimeZone.getTimeZone("GMT"));
return df.format(dt);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"login": "github-api-test-org",
"id": 7544739,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
"url": "https://api.github.com/orgs/github-api-test-org",
"repos_url": "https://api.github.com/orgs/github-api-test-org/repos",
"events_url": "https://api.github.com/orgs/github-api-test-org/events",
"hooks_url": "https://api.github.com/orgs/github-api-test-org/hooks",
"issues_url": "https://api.github.com/orgs/github-api-test-org/issues",
"members_url": "https://api.github.com/orgs/github-api-test-org/members{/member}",
"public_members_url": "https://api.github.com/orgs/github-api-test-org/public_members{/member}",
"avatar_url": "https://avatars3.githubusercontent.com/u/7544739?v=4",
"description": null,
"is_verified": false,
"has_organization_projects": true,
"has_repository_projects": true,
"public_repos": 10,
"public_gists": 0,
"followers": 0,
"following": 0,
"html_url": "https://github.com/github-api-test-org",
"created_at": "2014-05-10T19:39:11Z",
"updated_at": "2015-04-20T00:42:30Z",
"type": "Organization",
"total_private_repos": 0,
"owned_private_repos": 0,
"private_gists": 0,
"disk_usage": 132,
"collaborators": 0,
"billing_email": "[email protected]",
"default_repository_permission": "none",
"members_can_create_repositories": false,
"two_factor_requirement_enabled": false,
"plan": {
"name": "free",
"space": 976562499,
"private_repos": 0,
"filled_seats": 7,
"seats": 0
}
}
Loading

0 comments on commit 6956922

Please sign in to comment.