|
49 | 49 | import org.gitlab4j.api.models.ProjectUser;
|
50 | 50 | import org.gitlab4j.api.models.PushRules;
|
51 | 51 | import org.gitlab4j.api.models.Snippet;
|
| 52 | +import org.gitlab4j.api.models.Variable; |
52 | 53 | import org.gitlab4j.api.models.Visibility;
|
53 | 54 |
|
54 | 55 | /**
|
@@ -2395,4 +2396,152 @@ public Project setProjectAvatar(Object projectIdOrPath, File avatarFile) throws
|
2395 | 2396 | Response response = putUpload(Response.Status.OK, "avatar", avatarFile, "projects", getProjectIdOrPath(projectIdOrPath));
|
2396 | 2397 | return (response.readEntity(Project.class));
|
2397 | 2398 | }
|
| 2399 | + |
| 2400 | + /** |
| 2401 | + * Get list of a project's variables. |
| 2402 | + * |
| 2403 | + * <pre><code>GitLab Endpoint: GET /groups/:id/variables</code></pre> |
| 2404 | + * |
| 2405 | + * @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required |
| 2406 | + * @return a list of variables belonging to the specified project |
| 2407 | + * @throws GitLabApiException if any exception occurs |
| 2408 | + */ |
| 2409 | + public List<Variable> getVariables(Object projectIdOrPath) throws GitLabApiException { |
| 2410 | + return (getVariables(projectIdOrPath, getDefaultPerPage()).all()); |
| 2411 | + } |
| 2412 | + |
| 2413 | + /** |
| 2414 | + * Get a list of variables for the specified project in the specified page range. |
| 2415 | + * |
| 2416 | + * <pre><code>GitLab Endpoint: GET /projects/:id/variables</code></pre> |
| 2417 | + * |
| 2418 | + * @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required |
| 2419 | + * @param page the page to get |
| 2420 | + * @param perPage the number of Variable instances per page |
| 2421 | + * @return a list of variables belonging to the specified project in the specified page range |
| 2422 | + * @throws GitLabApiException if any exception occurs |
| 2423 | + */ |
| 2424 | + public List<Variable> getVariables(Object projectIdOrPath, int page, int perPage) throws GitLabApiException { |
| 2425 | + Response response = get(Response.Status.OK, getPageQueryParams(page, perPage), "projects", getProjectIdOrPath(projectIdOrPath), "variables"); |
| 2426 | + return (response.readEntity(new GenericType<List<Variable>>() {})); |
| 2427 | + } |
| 2428 | + |
| 2429 | + /** |
| 2430 | + * Get a Pager of variables belonging to the specified project. |
| 2431 | + * |
| 2432 | + * <pre><code>GitLab Endpoint: GET /projects/:id/variables</code></pre> |
| 2433 | + * |
| 2434 | + * @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required |
| 2435 | + * @param itemsPerPage the number of Variable instances that will be fetched per page |
| 2436 | + * @return a Pager of variables belonging to the specified project |
| 2437 | + * @throws GitLabApiException if any exception occurs |
| 2438 | + */ |
| 2439 | + public Pager<Variable> getVariables(Object projectIdOrPath, int itemsPerPage) throws GitLabApiException { |
| 2440 | + return (new Pager<Variable>(this, Variable.class, itemsPerPage, null, "projects", getProjectIdOrPath(projectIdOrPath), "variables")); |
| 2441 | + } |
| 2442 | + |
| 2443 | + /** |
| 2444 | + * Get a Stream of variables belonging to the specified project. |
| 2445 | + * |
| 2446 | + * <pre><code>GitLab Endpoint: GET /projects/:id/variables</code></pre> |
| 2447 | + * |
| 2448 | + * @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required |
| 2449 | + * @return a Stream of variables belonging to the specified project |
| 2450 | + * @throws GitLabApiException if any exception occurs |
| 2451 | + */ |
| 2452 | + public Stream<Variable> getVariablesStream(Object projectIdOrPath) throws GitLabApiException { |
| 2453 | + return (getVariables(projectIdOrPath, getDefaultPerPage()).stream()); |
| 2454 | + } |
| 2455 | + |
| 2456 | + /** |
| 2457 | + * Get the details of a project variable. |
| 2458 | + * |
| 2459 | + * <pre><code>GitLab Endpoint: GET /projects/:id/variables/:key</code></pre> |
| 2460 | + * |
| 2461 | + * @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required |
| 2462 | + * @param key the key of an existing variable, required |
| 2463 | + * @return the Variable instance for the specified variable |
| 2464 | + * @throws GitLabApiException if any exception occurs |
| 2465 | + */ |
| 2466 | + public Variable getVariable(Object projectIdOrPath, String key) throws GitLabApiException { |
| 2467 | + Response response = get(Response.Status.OK, null, "projects", getProjectIdOrPath(projectIdOrPath), "variables", key); |
| 2468 | + return (response.readEntity(Variable.class)); |
| 2469 | + } |
| 2470 | + |
| 2471 | + /** |
| 2472 | + * Get the details of a variable as an Optional instance. |
| 2473 | + * |
| 2474 | + * <pre><code>GitLab Endpoint: GET /projects/:id/variables/:key</code></pre> |
| 2475 | + * |
| 2476 | + * @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required |
| 2477 | + * @param key the key of an existing variable, required |
| 2478 | + * @return the Variable for the specified variable as an Optional instance |
| 2479 | + */ |
| 2480 | + public Optional<Variable> getOptionalVariable(Object projectIdOrPath, String key) { |
| 2481 | + try { |
| 2482 | + return (Optional.ofNullable(getVariable(projectIdOrPath, key))); |
| 2483 | + } catch (GitLabApiException glae) { |
| 2484 | + return (GitLabApi.createOptionalFromException(glae)); |
| 2485 | + } |
| 2486 | + } |
| 2487 | + |
| 2488 | + /** |
| 2489 | + * Create a new project variable. |
| 2490 | + * |
| 2491 | + * <pre><code>GitLab Endpoint: POST /projects/:id/variables</code></pre> |
| 2492 | + * |
| 2493 | + * @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required |
| 2494 | + * @param key the key of a variable; must have no more than 255 characters; only A-Z, a-z, 0-9, and _ are allowed, required |
| 2495 | + * @param value the value for the variable, required |
| 2496 | + * @param isProtected whether the variable is protected, optional |
| 2497 | + * @param environmentScope the environment_scope of the variable, optional |
| 2498 | + * @return a Variable instance with the newly created variable |
| 2499 | + * @throws GitLabApiException if any exception occurs during execution |
| 2500 | + */ |
| 2501 | + public Variable createVariable(Object projectIdOrPath, String key, String value, Boolean isProtected, String environmentScope) throws GitLabApiException { |
| 2502 | + |
| 2503 | + GitLabApiForm formData = new GitLabApiForm() |
| 2504 | + .withParam("key", key, true) |
| 2505 | + .withParam("value", value, true) |
| 2506 | + .withParam("protected", isProtected) |
| 2507 | + .withParam("environment_scope", environmentScope); |
| 2508 | + Response response = post(Response.Status.CREATED, formData, "projects", getProjectIdOrPath(projectIdOrPath), "variables"); |
| 2509 | + return (response.readEntity(Variable.class)); |
| 2510 | + } |
| 2511 | + |
| 2512 | + /** |
| 2513 | + * Update a project variable. |
| 2514 | + * |
| 2515 | + * <pre><code>GitLab Endpoint: PUT /projects/:id/variables/:key</code></pre> |
| 2516 | + * |
| 2517 | + * @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required |
| 2518 | + * @param key the key of an existing variable, required |
| 2519 | + * @param value the value for the variable, required |
| 2520 | + * @param isProtected whether the variable is protected, optional |
| 2521 | + * @param environmentScope the environment_scope of the variable, optional |
| 2522 | + * @return a Variable instance with the updated variable |
| 2523 | + * @throws GitLabApiException if any exception occurs during execution |
| 2524 | + */ |
| 2525 | + public Variable updateVariable(Object projectIdOrPath, String key, String value, Boolean isProtected, String environmentScope) throws GitLabApiException { |
| 2526 | + |
| 2527 | + GitLabApiForm formData = new GitLabApiForm() |
| 2528 | + .withParam("value", value, true) |
| 2529 | + .withParam("protected", isProtected) |
| 2530 | + .withParam("environment_scope", environmentScope); |
| 2531 | + Response response = putWithFormData(Response.Status.OK, formData, "projects", getProjectIdOrPath(projectIdOrPath), "variables", key); |
| 2532 | + return (response.readEntity(Variable.class)); |
| 2533 | + } |
| 2534 | + |
| 2535 | + /** |
| 2536 | + * Deletes a project variable. |
| 2537 | + * |
| 2538 | + * <pre><code>DELETE /projects/:id/variables/:key</code></pre> |
| 2539 | + * |
| 2540 | + * @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required |
| 2541 | + * @param key the key of an existing variable, required |
| 2542 | + * @throws GitLabApiException if any exception occurs |
| 2543 | + */ |
| 2544 | + public void deleteVariable(Object projectIdOrPath, String key) throws GitLabApiException { |
| 2545 | + delete(Response.Status.NO_CONTENT, null, "projects", getProjectIdOrPath(projectIdOrPath), "variables", key); |
| 2546 | + } |
2398 | 2547 | }
|
0 commit comments