Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TECH] Utiliser la version de release qui vient d'être crée plutôt que d'appeler l'API Github #236

Merged
merged 10 commits into from
Mar 27, 2023
3 changes: 1 addition & 2 deletions build/services/slack/view-submissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ module.exports = {
const releaseType = payload.view.private_metadata;

async function _publishAndDeploy({ releaseType, environment }) {
await publish(releaseType);
const latestReleaseTag = await githubService.getLatestReleaseTag();
const latestReleaseTag = await publish(releaseType);
return deploy(environment, latestReleaseTag);
}

Expand Down
3 changes: 2 additions & 1 deletion common/services/releases.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ module.exports = {
const branchName = await github.getDefaultBranch(config.github.owner, sanitizedRepoName);
const repositoryURL = `https://${config.github.token}@github.com/${config.github.owner}/${sanitizedRepoName}.git`;
const args = [config.github.owner, sanitizedRepoName, sanitizedReleaseType, branchName, repositoryURL];
await _runScriptWithArgument(RELEASE_PIX_SCRIPT, ...args);
const newPackageVersion = await _runScriptWithArgument(RELEASE_PIX_SCRIPT, ...args);
return newPackageVersion;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Retourner newPackageVersion en dehors du try/catch.
Mieux: supprimer le try/catch et laisser HapiJs gérer l'erreur non catchée, ou poster un message dans Slack pour dire que la release a fail

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Il manque un test sur la valeur retournée dans test/unit/common/services/releases_test.js - "#publishPixRepo"

Copy link
Contributor

@francois2metz francois2metz Mar 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Retourner newPackageVersion en dehors du try/catch.

Je ne vois pas ce que cela changerais. Peut tu expliciter ? (même après merge)

Mieux: supprimer le try/catch et laisser HapiJs gérer l'erreur non catchée, ou poster un message dans Slack pour dire que la release a fail

Il s'agit d'un tout autre problème a traiter hélas, hors du scope de cette PR.

} catch (err) {
console.error(err);
throw err;
Expand Down
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ require('dotenv').config();
const config = require('./config');
const server = require('./server');
const { createCronJob } = require('./common/services/cron-job');
const githubServices = require('./common/services/github');
const sendInBlueReport = require('./run/services/sendinblue-report');
const { deploy } = require('./run/services/deploy');
const ecoModeService = require('./build/services/eco-mode-service');
Expand All @@ -13,8 +14,10 @@ const init = async () => {
createCronJob('SendInBlue Report', sendInBlueReport.getReport, config.thirdServicesUsageReport.schedule);
createCronJob(
'Deploy Pix site',
() => {
deploy(config.PIX_SITE_REPO_NAME, config.PIX_SITE_APPS);
async () => {
const repoName = config.PIX_SITE_REPO_NAME;
const releaseTag = await githubServices.getLatestReleaseTag(repoName);
deploy(repoName, config.PIX_SITE_APPS, releaseTag);
},
config.pixSiteDeploy.schedule
);
Expand Down
6 changes: 4 additions & 2 deletions run/controllers/deploy-sites.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const config = require('../../config');
const { deploy } = require('../services/deploy');
const githubServices = require('../../common/services/github');

const Boom = require('@hapi/boom');

Expand All @@ -9,8 +10,9 @@ module.exports = {
if (payload.secret !== config.prismic.secret) {
throw Boom.unauthorized('Secret is missing or is incorrect');
}

const releaseTag = await deploy(config.PIX_SITE_REPO_NAME, config.PIX_SITE_APPS);
const repoName = config.PIX_SITE_REPO_NAME;
const releaseTag = await githubServices.getLatestReleaseTag(repoName);
await deploy(repoName, config.PIX_SITE_APPS, releaseTag);

return `pix.fr and pro.pix.fr deployments ${releaseTag} are in progress. Check deployment status on Scalingo`;
},
Expand Down
4 changes: 1 addition & 3 deletions run/services/deploy.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const githubServices = require('../../common/services/github');
const releasesService = require('../../common/services/releases');

async function deploy(repoName, appNamesList) {
const releaseTag = await githubServices.getLatestReleaseTag(repoName);
async function deploy(repoName, appNamesList, releaseTag) {
const environment = 'production';
await Promise.all(
appNamesList.map((appName) => releasesService.deployPixRepo(repoName, appName, releaseTag, environment))
Expand Down
61 changes: 31 additions & 30 deletions run/services/slack/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ function getErrorAppMessage(appName) {
function _isReleaseTypeInvalid(releaseType) {
return !['major', 'minor', 'patch'].includes(releaseType);
}
async function publishAndDeployPixUI(repoName, releaseType, responseUrl) {

async function _publishPixUI(repoName, releaseType, responseUrl) {
if (_isReleaseTypeInvalid(releaseType)) {
releaseType = 'minor';
}
const releaseTagBeforeRelease = await githubServices.getLatestReleaseTag(repoName);
await releasesService.publishPixRepo(repoName, releaseType);
const releaseTagAfterRelease = await githubServices.getLatestReleaseTag(repoName);
const releaseTagAfterRelease = await releasesService.publishPixRepo(repoName, releaseType);

if (releaseTagBeforeRelease === releaseTagAfterRelease) {
sendResponse(responseUrl, getErrorReleaseMessage(releaseTagAfterRelease, repoName));
Expand All @@ -80,13 +80,12 @@ async function publishAndDeployPixUI(repoName, releaseType, responseUrl) {
}
}

async function publishAndDeployEmberTestingLibrary(repoName, releaseType, responseUrl) {
async function _publishAndDeployEmberTestingLibrary(repoName, releaseType, responseUrl) {
if (_isReleaseTypeInvalid(releaseType)) {
releaseType = 'minor';
}
const releaseTagBeforeRelease = await githubServices.getLatestReleaseTag(repoName);
await releasesService.publishPixRepo(repoName, releaseType);
const releaseTagAfterRelease = await githubServices.getLatestReleaseTag(repoName);
const releaseTagAfterRelease = await releasesService.publishPixRepo(repoName, releaseType);

if (releaseTagBeforeRelease === releaseTagAfterRelease) {
sendResponse(responseUrl, getErrorReleaseMessage(releaseTagAfterRelease, repoName));
Expand All @@ -96,42 +95,44 @@ async function publishAndDeployEmberTestingLibrary(repoName, releaseType, respon
}
}

async function publishAndDeployRelease(repoName, appNamesList = [], releaseType, responseUrl) {
async function _publishAndDeployRelease(repoName, appNamesList = [], releaseType, responseUrl) {
try {
if (_isReleaseTypeInvalid(releaseType)) {
releaseType = 'minor';
}
await releasesService.publishPixRepo(repoName, releaseType);
const releaseTag = await deploy(repoName, appNamesList);
const releaseTagAfterRelease = await releasesService.publishPixRepo(repoName, releaseType);
await deploy(repoName, appNamesList, releaseTagAfterRelease);

sendResponse(responseUrl, getSuccessMessage(releaseTag, appNamesList.join(', ')));
sendResponse(responseUrl, getSuccessMessage(releaseTagAfterRelease, appNamesList.join(', ')));
} catch (e) {
sendResponse(responseUrl, getErrorAppMessage(appNamesList.join(', ')));
}
}

async function publishAndDeployReleaseWithAppsByEnvironment(repoName, appsByEnv, releaseType, responseUrl) {
async function _publishAndDeployReleaseWithAppsByEnvironment(repoName, appsByEnv, releaseType, responseUrl) {
if (_isReleaseTypeInvalid(releaseType)) {
releaseType = 'minor';
}
await releasesService.publishPixRepo(repoName, releaseType);
const releaseTag = await githubServices.getLatestReleaseTag(repoName);

const releaseTagAfterRelease = await releasesService.publishPixRepo(repoName, releaseType);

await Promise.all(
Object.keys(appsByEnv).map(async (scalingoEnv) => {
const scalingoInstance = await ScalingoClient.getInstance(scalingoEnv);

await Promise.all(
appsByEnv[scalingoEnv].map((scalingoAppName) => {
return scalingoInstance.deployFromArchive(scalingoAppName, releaseTag, repoName, { withEnvSuffix: false });
return scalingoInstance.deployFromArchive(scalingoAppName, releaseTagAfterRelease, repoName, {
withEnvSuffix: false,
});
})
);
})
);
sendResponse(responseUrl, getSuccessMessage(releaseTag, Object.values(appsByEnv).join(', ')));
sendResponse(responseUrl, getSuccessMessage(releaseTagAfterRelease, Object.values(appsByEnv).join(', ')));
}

async function getAndDeployLastVersion({ appName }) {
async function _getAndDeployLastVersion({ appName }) {
const lastReleaseTag = await githubServices.getLatestReleaseTag(PIX_REPO_NAME);
const sanitizedAppName = appName.trim().toLowerCase();

Expand All @@ -158,7 +159,7 @@ function _isAppFromPixRepo({ appName }) {
return appNamePrefix === 'pix' && PIX_APPS.includes(shortAppName) && PIX_APPS_ENVIRONMENTS.includes(environment);
}

async function deployFromBranch(repoName, appNames, branch) {
async function _deployFromBranch(repoName, appNames, branch) {
const client = await ScalingoClient.getInstance('production');
return Promise.all(
appNames.map((appName) => {
Expand All @@ -183,19 +184,19 @@ module.exports = {
},

async deployGraviteeAPIM() {
await deployFromBranch(PIX_GRAVITEE_APIM_REPO_NAME, PIX_GRAVITEE_APIM_APPS_NAME, 'main');
await _deployFromBranch(PIX_GRAVITEE_APIM_REPO_NAME, PIX_GRAVITEE_APIM_APPS_NAME, 'main');
},

async deployGeoAPI() {
await deployFromBranch(PIX_GEOAPI_REPO_NAME, [PIX_GEOAPI_APP_NAME], 'main');
await _deployFromBranch(PIX_GEOAPI_REPO_NAME, [PIX_GEOAPI_APP_NAME], 'main');
},

async deployPix360() {
await deployFromBranch(PIX_360_REPO_NAME, [PIX_360_APP_NAME], 'main');
await _deployFromBranch(PIX_360_REPO_NAME, [PIX_360_APP_NAME], 'main');
},

async createAndDeployPixLCMS(payload) {
await publishAndDeployReleaseWithAppsByEnvironment(
await _publishAndDeployReleaseWithAppsByEnvironment(
PIX_LCMS_REPO_NAME,
PIX_LCMS_APPS,
payload.text,
Expand All @@ -204,19 +205,19 @@ module.exports = {
},

async createAndDeployPixUI(payload) {
await publishAndDeployPixUI(PIX_UI_REPO_NAME, payload.text, payload.response_url);
await _publishPixUI(PIX_UI_REPO_NAME, payload.text, payload.response_url);
},

async createAndDeployEmberTestingLibrary(payload) {
await publishAndDeployEmberTestingLibrary(PIX_EMBER_TESTING_LIBRARY_REPO_NAME, payload.text, payload.response_url);
await _publishAndDeployEmberTestingLibrary(PIX_EMBER_TESTING_LIBRARY_REPO_NAME, payload.text, payload.response_url);
},

async createAndDeployPixSiteRelease(payload) {
await publishAndDeployRelease(PIX_SITE_REPO_NAME, PIX_SITE_APPS, payload.text, payload.response_url);
await _publishAndDeployRelease(PIX_SITE_REPO_NAME, PIX_SITE_APPS, payload.text, payload.response_url);
},

async createAndDeployPixDatawarehouse(payload) {
await publishAndDeployRelease(
await _publishAndDeployRelease(
PIX_DATAWAREHOUSE_REPO_NAME,
PIX_DATAWAREHOUSE_APPS_NAME,
payload.text,
Expand All @@ -225,7 +226,7 @@ module.exports = {
},

async createAndDeployPixBotRelease(payload) {
await publishAndDeployReleaseWithAppsByEnvironment(
await _publishAndDeployReleaseWithAppsByEnvironment(
PIX_BOT_REPO_NAME,
PIX_BOT_APPS,
payload.text,
Expand All @@ -234,18 +235,18 @@ module.exports = {
},

async getAndDeployLastVersion({ appName }) {
await getAndDeployLastVersion({ appName });
await _getAndDeployLastVersion({ appName });
},

async createAndDeployDbStats(payload) {
await publishAndDeployRelease(PIX_DB_STATS_REPO_NAME, PIX_DB_STATS_APPS_NAME, payload.text, payload.response_url);
await _publishAndDeployRelease(PIX_DB_STATS_REPO_NAME, PIX_DB_STATS_APPS_NAME, payload.text, payload.response_url);
},

async deployMetabase() {
await deployFromBranch(PIX_METABASE_REPO_NAME, PIX_METABASE_APPS_NAME, 'master');
await _deployFromBranch(PIX_METABASE_REPO_NAME, PIX_METABASE_APPS_NAME, 'master');
},

async createAndDeployPixTutosRelease(payload) {
await publishAndDeployRelease(PIX_TUTOS_REPO_NAME, [PIX_TUTOS_APP_NAME], payload.text, payload.response_url);
await _publishAndDeployRelease(PIX_TUTOS_REPO_NAME, [PIX_TUTOS_APP_NAME], payload.text, payload.response_url);
},
};
1 change: 1 addition & 0 deletions scripts/release-pix-repo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ tag_release_commit
push_commit_and_tag_to_remote

echo -e "Release publication for ${GITHUB_OWNER}/${GITHUB_REPOSITORY} ${GREEN}succeeded${RESET_COLOR} (${VERSION_TYPE})."
echo "v${NEW_PACKAGE_VERSION}"
1 change: 1 addition & 0 deletions test/acceptance/scripts/release-pix-repo_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ describe('Acceptance | Scripts | release-pix-repo.sh', function () {
'Created annotated tag',
'Pushed release commit to the origin',
'Release publication for 1024pix/pix-bot-publish-test \x1B[1;32msucceeded\x1B[0m (minor).',
'v0.2.0',
'',
];
const expectedStderr = [/^Cloning into/, /To (.+)/, /dev -> dev/, /v0.2.0 -> v0.2.0/, ''];
Expand Down
12 changes: 11 additions & 1 deletion test/unit/common/services/releases_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,12 @@ describe('releases', function () {
});

describe('#publishPixRepo', function () {
it("should call the release pix script with 'minor'", async function () {
beforeEach(function () {
// given
sinon.stub(github, 'getDefaultBranch').resolves('dev');
});

it("should call the release pix script with 'minor'", async function () {
//when
await releasesService.publishPixRepo('pix-site', 'minor');

Expand All @@ -123,5 +125,13 @@ describe('releases', function () {
)
);
});

it('should retrieve new package version', async function () {
//when
const newPackageVersion = await releasesService.publishPixRepo('pix-site', 'minor');

// then
expect(newPackageVersion).to.equal('3.14.0');
});
});
});
Loading