View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2013-2025 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.jcabi.github;
6   
7   import com.jcabi.log.Logger;
8   import java.io.IOException;
9   import org.apache.commons.lang3.RandomStringUtils;
10  
11  /**
12   * Utility class which provides convenient methods for repo managing.
13   * @since 0.1
14   */
15  public final class RepoRule {
16  
17      /**
18       * Create new repo for tests.
19       * @param repos Repos
20       * @return Repo
21       * @throws IOException If error occurred.
22       */
23      public Repo repo(final Repos repos) throws IOException {
24          final Repos.RepoCreate settings = new Repos.RepoCreate(
25              "foo",
26              false
27          ).withAutoInit(true);
28          int attempts = 0;
29          Repo repo = null;
30          while (repo == null) {
31              final Repos.RepoCreate request = settings.withName(
32                  RandomStringUtils.secure().nextAlphanumeric(20)
33              );
34              try {
35                  repo = repos.create(request);
36              } catch (final AssertionError ex) {
37                  Logger.warn(
38                      this, "Create repository failed. Message: %s",
39                      ex.getMessage()
40                  );
41                  ++attempts;
42                  if (attempts > 5) {
43                      throw new IllegalStateException(
44                          String.format(
45                              "Failed to created repository %s",
46                              request.name()
47                          ),
48                          ex
49                      );
50                  }
51              }
52          }
53          return repo;
54      }
55  }