Commit e42d54df authored by Tim Olshansky's avatar Tim Olshansky Committed by GitHub

Merge pull request #156 from zakyvit/master

Build variables implementation
parents 78d599c2 b22fb80a
......@@ -2241,4 +2241,134 @@ public class GitlabAPI {
+ GitlabNote.URL + noteId + GitlabAward.URL + "/" + award.getId();
retrieve().method("DELETE").to(tailUrl, Void.class);
}
/**
* Gets build variables associated with a project.
* @param projectId The ID of the project.
* @return A non-null list of variables.
* @throws IOException
*/
public List<GitlabBuildVariable> getBuildVariables(Integer projectId)
throws IOException {
String tailUrl = GitlabProject.URL + "/" + projectId + GitlabBuildVariable.URL;
GitlabBuildVariable[] variables = retrieve().to(tailUrl, GitlabBuildVariable[].class);
return Arrays.asList(variables);
}
/**
* Gets build variables associated with a project.
* @param project The project associated with variables.
* @return A non-null list of variables.
* @throws IOException
*/
public List<GitlabBuildVariable> getBuildVariables(GitlabProject project)
throws IOException {
return getBuildVariables(project.getId());
}
/**
* Gets build variable associated with a project and key.
* @param projectId The ID of the project.
* @param key The key of the variable.
* @return A variable.
* @throws IOException
*/
public GitlabBuildVariable getBuildVariable(Integer projectId, String key)
throws IOException {
String tailUrl = GitlabProject.URL + "/" +
projectId +
GitlabBuildVariable.URL +
key;
return retrieve().to(tailUrl, GitlabBuildVariable.class);
}
/**
* Gets build variable associated with a project and key.
* @param project The project associated with the variable.
* @return A variable.
* @throws IOException
*/
public GitlabBuildVariable getBuildVariable(GitlabProject project, String key)
throws IOException {
return getBuildVariable(project.getId(), key);
}
/**
* Creates a new build variable.
* @param projectId The ID of the project containing the new variable.
* @param key The key of the variable.
* @param value The value of the variable
* @return The newly created variable.
* @throws IOException
*/
public GitlabBuildVariable createBuildVariable(
Integer projectId,
String key,
String value) throws IOException {
String tailUrl = GitlabProject.URL + "/" + projectId + GitlabBuildVariable.URL;
return dispatch().with("key", key)
.with("value", value)
.to(tailUrl, GitlabBuildVariable.class);
}
/**
* Creates a new variable.
* @param projectId The ID of the project containing the variable.
* @param variable The variable to create.
* @return The newly created variable.
*/
public GitlabBuildVariable createBuildVariable(Integer projectId, GitlabBuildVariable variable)
throws IOException {
String key = variable.getKey();
String value = variable.getValue();
return createBuildVariable(projectId, key, value);
}
/**
* Deletes an existing variable.
* @param projectId The ID of the project containing the variable.
* @param key The key of the variable to delete.
* @throws IOException
*/
public void deleteBuildVariable(Integer projectId, String key)
throws IOException {
String tailUrl = GitlabProject.URL + "/" +
projectId +
GitlabBuildVariable.URL +
key;
retrieve().method("DELETE").to(tailUrl, Void.class);
}
/**
* Deletes an existing variable.
* @param projectId The ID of the project containing the variable.
* @param variable The variable to delete.
* @throws IOException
*/
public void deleteBuildVariable(Integer projectId, GitlabBuildVariable variable)
throws IOException {
deleteBuildVariable(projectId, variable.getKey());
}
/**
* Updates an existing variable.
* @param projectId The ID of the project containing the variable.
* @param key The key of the variable to update.
* @param newValue The updated value.
* @return The updated, deserialized variable.
* @throws IOException
*/
public GitlabBuildVariable updateBuildVariable(Integer projectId,
String key,
String newValue) throws IOException {
String tailUrl = GitlabProject.URL + "/" +
projectId +
GitlabBuildVariable.URL +
key;
GitlabHTTPRequestor requestor = retrieve().method("PUT");
if (newValue != null) {
requestor = requestor.with("value", newValue);
}
return requestor.to(tailUrl, GitlabBuildVariable.class);
}
}
package org.gitlab.api.models;
import com.fasterxml.jackson.annotation.JsonProperty;
/**
* @author Vitezslav Zak
*/
public class GitlabBuildVariable {
public final static String URL = "/variables/";
public GitlabBuildVariable() {
}
public GitlabBuildVariable(String key, String value) {
this.key = key;
this.value = value;
}
@JsonProperty("key")
private String key;
@JsonProperty("value")
private String value;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
package org.gitlab.api;
import org.gitlab.api.models.GitlabBuildVariable;
import org.gitlab.api.models.GitlabProject;
import org.gitlab.api.models.GitlabGroup;
import org.gitlab.api.models.GitlabUser;
import org.junit.Before;
......@@ -65,6 +67,51 @@ public class GitlabAPITest {
assertEquals(expected + "/", api.getUrl("").toString());
}
@Test
public void testCreateUpdateDeleteVariable() throws IOException {
String key = randVal("key");
String value = randVal("value");
String newValue = randVal("new_value");
String projectName = randVal("project");
GitlabProject project = api.createProject(projectName);
assertNotNull(project);
GitlabBuildVariable variable = api.createBuildVariable(project.getId(), key, value);
assertNotNull(variable);
GitlabBuildVariable refetched = api.getBuildVariable(project.getId(), key);
assertNotNull(refetched);
assertEquals(refetched.getKey(), variable.getKey());
assertEquals(refetched.getValue(), variable.getValue());
api.updateBuildVariable(project.getId(), key, newValue);
GitlabBuildVariable postUpdate = api.getBuildVariable(project.getId(), key);
assertNotNull(postUpdate);
assertEquals(postUpdate.getKey(), variable.getKey());
assertNotEquals(postUpdate.getValue(), variable.getValue());
assertEquals(postUpdate.getValue(), newValue);
api.deleteBuildVariable(project.getId(), key);
// expect a 404, but we have no access to it
try {
GitlabBuildVariable shouldNotExist = api.getBuildVariable(project.getId(), key);
assertNull(shouldNotExist);
} catch (FileNotFoundException thisIsSoOddForAnRESTApiClient) {
assertTrue(true); // expected
}
api.deleteProject(project.getId());
}
@Test
public void testCreateUpdateDeleteUser() throws IOException {
......@@ -142,6 +189,6 @@ public class GitlabAPITest {
}
private String randVal(String postfix) {
return rand + "-" + postfix;
return rand + "_" + postfix;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment