Skip to content

Commit

Permalink
feat: bulk update issues and network concurrency limit
Browse files Browse the repository at this point in the history
  • Loading branch information
DevSide authored and DevSide committed Sep 24, 2019
1 parent fd99acf commit 2bebfd4
Show file tree
Hide file tree
Showing 6 changed files with 3,651 additions and 71 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,8 @@ interface Config {
// defaults to `v${version}` which results in a version that is named like `v1.0.0`
// ex: `Semantic Release v${version}` results in `Semantic Release v1.0.0`
releaseNameTemplate?: string;

// The number of maximum parallel network calls
networkConcurrency?: number;
}
```
74 changes: 42 additions & 32 deletions lib/success.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import JiraClient, { Version } from 'jira-connector';
import * as _ from 'lodash';
import pLimit from 'p-limit';

import { makeClient } from './jira';
import { GenerateNotesContext, PluginConfig } from './types';
Expand Down Expand Up @@ -62,6 +63,39 @@ async function findOrCreateVersion(config: PluginConfig, context: GenerateNotesC
return newVersion;
}

async function editIssueFixVersions(config: PluginConfig, context: GenerateNotesContext, jira: JiraClient, newVersionName: string, releaseVersionId: string, issueKey: string): Promise<void> {
try {
context.logger.info(`Adding issue ${issueKey} to '${newVersionName}'`);
if (!config.dryRun) {
await jira.issue.editIssue({
issueKey,
issue: {
update: {
fixVersions: [{
add: { id: releaseVersionId },
}],
},
},
});
}
} catch (err) {
const allowedStatusCodes = [400, 404];
let { statusCode } = err;
if (typeof err === 'string') {
try {
err = JSON.parse(err);
statusCode = statusCode || err.statusCode;
} catch (err) {
// it's not json :shrug:
}
}
if (allowedStatusCodes.indexOf(statusCode) === -1) {
throw err;
}
context.logger.error(`Unable to update issue ${issueKey} statusCode: ${statusCode}`);
}
}

export async function success(config: PluginConfig, context: GenerateNotesContext): Promise<void> {
const tickets = getTickets(config, context);

Expand All @@ -77,37 +111,13 @@ export async function success(config: PluginConfig, context: GenerateNotesContex
const project = await jira.project.getProject({ projectIdOrKey: config.projectId });
const releaseVersion = await findOrCreateVersion(config, context, jira, project.id, newVersionName);

for (const issueKey of tickets) {
try {
context.logger.info(`Adding issue ${issueKey} to '${newVersionName}'`);
if (!config.dryRun) {
await jira.issue.editIssue({
issueKey,
issue: {
update: {
fixVersions: [{
add: { id: releaseVersion.id },
}],
},
},
});
}
} catch (err) {
const allowedStatusCodes = [400, 404];
let { statusCode } = err;
if (typeof err === 'string') {
try {
err = JSON.parse(err);
statusCode = statusCode || err.statusCode;
} catch (err) {
// it's not json :shrug:
}
}
if (allowedStatusCodes.indexOf(statusCode) === -1) {
throw err;
}
context.logger.error(`Unable to update issue ${issueKey} statusCode: ${statusCode}`);
}
}
const concurrentLimit = pLimit(config.networkConcurrency || 10);

const edits = tickets.map(issueKey =>
concurrentLimit(() =>
editIssueFixVersions(config, context, jira, newVersionName, releaseVersion.id, issueKey),
),
);

await Promise.all(edits);
}
1 change: 1 addition & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,5 @@ export interface PluginConfig extends BaseConfig {
projectId: string;
releaseNameTemplate?: string;
jiraHost: string;
networkConcurrency?: number;
}
6 changes: 6 additions & 0 deletions lib/verifyConditions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { makeClient } from './jira';
import { PluginConfig, PluginContext } from './types';

export async function verifyConditions(config: PluginConfig, context: PluginContext): Promise<void> {
const { networkConcurrency } = config;

if (typeof config.jiraHost !== 'string') {
throw new SemanticReleaseError(`config.jiraHost must be a string`);
}
Expand Down Expand Up @@ -40,6 +42,10 @@ export async function verifyConditions(config: PluginConfig, context: PluginCont
}
}

if (networkConcurrency && (typeof networkConcurrency !== 'number' || networkConcurrency < 1)) {
throw new SemanticReleaseError(`config.networkConcurrency must be an number greater than 0`);
}

if (!context.env.JIRA_AUTH) {
throw new SemanticReleaseError(`JIRA_AUTH must be a string`);
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@semantic-release/error": "^2.2.0",
"jira-connector": "^2.16.0",
"lodash": "^4.17.11",
"p-limit": "^2.2.1",
"tslib": "^1.9.2"
},
"devDependencies": {
Expand Down
Loading

0 comments on commit 2bebfd4

Please sign in to comment.