Skip to content

Commit

Permalink
[BUGFIX] Corriger la manière de s'assurer qu'une application est bien…
Browse files Browse the repository at this point in the history
… une RA

 #482
  • Loading branch information
github-actions[bot] authored Dec 10, 2024
2 parents 278caf0 + 44b0832 commit 35896da
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
3 changes: 2 additions & 1 deletion build/controllers/scalingo.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import slackPostMessageService from '../../common/services/slack/surfaces/messag
import { config } from '../../config.js';
import * as reviewAppRepository from '../repository/review-app-repository.js';
import githubService from '../../common/services/github.js';
import { ScalingoAppName } from '../../common/models/ScalingoAppName.js';

function getSlackMessageAttachments(payload) {
const appName = payload.app_name;
Expand Down Expand Up @@ -90,7 +91,7 @@ const scalingo = {
return h.response().code(200);
}

if (!appName.includes('review-pr')) {
if (!ScalingoAppName.isReviewApp(appName)) {
logger.error({ event, message: `The application ${appName} is not linked to a pull request.` });
return h.response().code(200);
}
Expand Down
5 changes: 5 additions & 0 deletions common/models/ScalingoAppName.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { config } from '../../config.js';
const alphanumericAndDashOnly = /^([a-zA-Z0-9]+-)+[a-zA-Z0-9]+$/;
const prefix = config.scalingo.validAppPrefix;

class ScalingoAppName {
static isApplicationNameValid(applicationName) {
const suffix = config.scalingo.validAppSuffix;
Expand All @@ -13,6 +14,10 @@ class ScalingoAppName {
const appNameEndsWithCorrectSuffix = suffix.includes(applicationName.split('-').slice(-1)[0]);
return appNameMatchesRegex && appNameHasCorrectLength && appNameStartsWithPix && appNameEndsWithCorrectSuffix;
}

static isReviewApp(applicationName) {
return Boolean(applicationName.match(/.*-pr\d+/g));
}
}

export { ScalingoAppName };
3 changes: 2 additions & 1 deletion common/services/scalingo-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as scalingo from 'scalingo';

import { config } from '../../config.js';
import { logger } from './logger.js';
import { ScalingoAppName } from '../models/ScalingoAppName.js';

const DEFAULT_OPTS = { withEnvSuffix: true };

Expand Down Expand Up @@ -170,7 +171,7 @@ class ScalingoClient {
}

async deleteReviewApp(appName) {
if (!appName.match(/.*-pr\d+/g)) {
if (!ScalingoAppName.isReviewApp(appName)) {
throw new Error(`Cannot call deleteReviewApp for the non review app ${appName}.`);
}

Expand Down
18 changes: 18 additions & 0 deletions test/unit/common/models/ScalingoAppName_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,22 @@ describe('Unit | Common | Models | ScalingoAppName', function () {
expect(result).to.be.true;
});
});

describe('#isReviewApp', function () {
describe('when the application is not a review app', function () {
['pix-api-production', 'pix-app-recette', 'pix-orga-integration'].forEach((appName) => {
it(`should return false for ${appName}`, function () {
expect(ScalingoAppName.isReviewApp(appName)).to.be.false;
});
});
});

describe('when the application is a review app', function () {
['pix-api-review-pr123', 'pix-integration-pr76656'].forEach((appName) => {
it(`should return false for ${appName}`, function () {
expect(ScalingoAppName.isReviewApp(appName)).to.be.true;
});
});
});
});
});

0 comments on commit 35896da

Please sign in to comment.