Commit feb0c3ba authored by krlwlfrt's avatar krlwlfrt
Browse files

feat: add check for protected tags to tidy task

Fixes #14
parent af49f4a4
Loading
Loading
Loading
Loading
Loading
+36 −8
Original line number Diff line number Diff line
@@ -13,14 +13,7 @@
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */
import {Api} from '@openstapps/gitlab-api';
import {
  AccessLevel,
  IssueState,
  MembershipScope,
  Milestone,
  Project,
  Scope,
} from '@openstapps/gitlab-api/lib/types';
import {AccessLevel, IssueState, MembershipScope, Milestone, Project, Scope} from '@openstapps/gitlab-api/lib/types';
import {asyncPool} from 'async-pool-native/dist/async-pool';
import {flatten2dArray, getProjects, logger} from '../common';
import {GROUPS, NEEDED_LABELS, NEEDED_MILESTONES, NOTE_PREFIX, PROTECTED_BRANCHES, SCHOOLS} from '../configuration';
@@ -228,6 +221,40 @@ export async function tidyProtectedBranches(api: Api, projects: Project[]): Prom
  logger.ok('Tidied protected branches.');
}

/**
 * Tidy protected tags
 *
 * @param api GitLab API instance to use for the requests
 * @param projects List of projects to tidy protected tags on
 */
export async function tidyProtectedTags(api: Api, projects: Project[]): Promise<void> {
  await asyncPool(2, projects, async (project) => {
    const protectedTags: Array<{
      create_access_levels: Array<{
        access_level: AccessLevel,
        access_level_description: string;
      }>;
      name: string;
    }> = await api.makeGitLabAPIRequest(`projects/${project.id}/protected_tags`);

    if (!protectedTags.find((protectedTag) => {
      return protectedTag.name === 'v*';
    })) {
      await api.makeGitLabAPIRequest(`projects/${project.id}/protected_tags`, {
        data: {
          create_access_level: AccessLevel.Maintainer,
          name: 'v*',
        },
        method: 'POST',
      });

      logger.log(`Added protected version tag in project '${project.name_with_namespace}'.`);
    }
  });

  logger.ok('Tidied protected tags.');
}

/**
 * Tidy "sub" group members
 *
@@ -320,6 +347,7 @@ export async function tidy(api: Api) {
    tidyLabels(api, projects),
    tidyMilestones(api, projects),
    tidyProtectedBranches(api, projects),
    tidyProtectedTags(api, projects),
    tidySubGroupMembers(api),
  ]);