Skip to content

Commit

Permalink
feat: Add ignorePrerelease option
Browse files Browse the repository at this point in the history
  • Loading branch information
uanid committed Jul 12, 2024
1 parent fc388b0 commit 512598c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,14 @@ If you are using this plugin on GitLab CI/CD, you don't need to set any environm

### GitLab Monorepo Plugin Options

| Option | Description | Default |
|---------------|--------------------------------------------------------|------------------------------------------------------------------------------------------|
| `gitlabUrl` | *Optional.* The GitLab URL. | `CI_SERVER_URL` environment variable or `https://gitlab.com` |
| `projectId` | *Optional.* The GitLab project ID. | `CI_PROJECT_ID` or `CI_PROJECT_PATH` environment variable or read from git `origin` url. |
| `commitTitle` | *Optional.* The commit title message to pushed assets. | `chore(release): ${nextRelease.name} [skip ci]` |
| `branchName` | *Optional.* The branch name to merge each assets. | `assets/${commit.short}` |
| `assets` | *Optional.* The assets to be pushed. | `[]` |
| Option | Description | Default |
|--------------------|--------------------------------------------------------|------------------------------------------------------------------------------------------|
| `gitlabUrl` | *Optional.* The GitLab URL. | `CI_SERVER_URL` environment variable or `https://gitlab.com` |
| `projectId` | *Optional.* The GitLab project ID. | `CI_PROJECT_ID` or `CI_PROJECT_PATH` environment variable or read from git `origin` url. |
| `commitTitle` | *Optional.* The commit title message to pushed assets. | `chore(release): ${nextRelease.name} [skip ci]` |
| `branchName` | *Optional.* The branch name to merge each assets. | `assets/${commit.short}` |
| `assets` | *Optional.* The assets to be pushed. | `[]` |
| `ignorePrerelease` | *Optional.* Ignore prerelease version. | `true` |

## commitTitle and branchName template

Expand Down
16 changes: 10 additions & 6 deletions lib/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface UserConfig {
assets?: Assets[] | undefined;
commitTitle?: string | undefined;
branchName?: string | undefined;
ignorePrerelease?: boolean | undefined;
}

export interface Assets {
Expand All @@ -24,22 +25,24 @@ export interface PluginConfig {
assets: Assets[];
commitTitle: string;
branchName: string;
ignorePrerelease: boolean;
}

export function resolvePluginConfig(userConfig: UserConfig, context: VerifyConditionsContext): PluginConfig {
assertUserConfig(userConfig);
const gitlabUrl = userConfig.gitlabUrl || context.env['CI_SERVER_URL'] || 'https://gitlab.com';
let projectId = userConfig.projectId || context.env['CI_PROJECT_ID'] || context.env['CI_PROJECT_PATH'] || getProjectId(getOriginUrl(context));
const gitlabUrl = userConfig.gitlabUrl ?? context.env['CI_SERVER_URL'] ?? 'https://gitlab.com';
let projectId = userConfig.projectId ?? context.env['CI_PROJECT_ID'] ?? context.env['CI_PROJECT_PATH'] ?? getProjectId(getOriginUrl(context));
if (projectId.includes('/')) {
projectId = encodeURIComponent(projectId);
}
const gitlabToken: string | undefined = context.env['GITLAB_TOKEN'] || context.env['GITLAB_ACCESS_TOKEN'];
const gitlabToken: string | undefined = context.env['GITLAB_TOKEN'] ?? context.env['GITLAB_ACCESS_TOKEN'];
if (!gitlabToken) {
throw new Error('GITLAB_TOKEN or GITLAB_ACCESS_TOKEN env is required');
}
const assets = userConfig.assets || [];
const commitTitle = userConfig.commitTitle || 'chore(release): ${nextRelease.name} [skip ci]';
const branchName = userConfig.branchName || 'assets/${commit.short}';
const assets = userConfig.assets ?? [];
const commitTitle = userConfig.commitTitle ?? 'chore(release): ${nextRelease.name} [skip ci]';
const branchName = userConfig.branchName ?? 'assets/${commit.short}';
const ignorePrerelease = userConfig.ignorePrerelease ?? true;

const pluginConfig: PluginConfig = {
gitlabBaseUrl: urlJoin(gitlabUrl, 'api/v4', 'projects', projectId),
Expand All @@ -48,6 +51,7 @@ export function resolvePluginConfig(userConfig: UserConfig, context: VerifyCondi
assets: assets,
commitTitle: commitTitle,
branchName: branchName,
ignorePrerelease: ignorePrerelease,
}
return assertPluginConfig(pluginConfig);
}
Expand Down
8 changes: 6 additions & 2 deletions lib/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export async function publish(userConfig: UserConfig, context: PublishContext):
context.logger.log("No assets defined to publish.");
return;
}
if (context.branch.prerelease && config.ignorePrerelease) {
context.logger.log("Ignoring prerelease branch. Cause of ignorePrerelease is true.");
return;
}

const body = await makeCommitRequestBody(config, context);
context.logger.log(`Extract ${body.actions.length} assets.`);
Expand All @@ -29,7 +33,7 @@ export async function publish(userConfig: UserConfig, context: PublishContext):
body.start_branch = body.branch;
}
}
context.logger.log(`Published assets to ${body.branch} successfully .`);
context.logger.log(`Published assets to ${body.branch} successfully.`);
}

enum PushCommitResult {
Expand Down Expand Up @@ -144,7 +148,7 @@ function resolve(filePath: string, cwd: string | undefined): string {
if (filePath.startsWith("/")) {
return path.resolve(filePath);
} else {
return path.resolve(cwd || process.cwd(), filePath);
return path.resolve(cwd ?? process.cwd(), filePath);
}
}
}

0 comments on commit 512598c

Please sign in to comment.