Skip to content

Commit

Permalink
[TECH] Afficher le label de la team concernée et le lien vers la PR a…
Browse files Browse the repository at this point in the history
…ssociée au changement de config
  • Loading branch information
github-actions[bot] committed Sep 26, 2023
2 parents fd1d792 + bad1bf2 commit 67106a9
Show file tree
Hide file tree
Showing 4 changed files with 267 additions and 112 deletions.
15 changes: 13 additions & 2 deletions build/services/slack/view-submissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,23 @@ const slackPostMessageService = require('../../../common/services/slack/surfaces
module.exports = {
async submitReleaseTypeSelection(payload) {
const releaseType = payload.view.state.values['publish-release-type']['release-type-option'].selected_option.value;
const { hasConfigFileChanged, latestTag } = await githubService.hasConfigFileChangedSinceLatestRelease();
const { hasConfigFileChanged, latestTag, pullRequestsForCommitShaDetails } =
await githubService.hasConfigFileChangedSinceLatestRelease();

if (hasConfigFileChanged) {
let pRsAndTeamLabelsMessageList = 'Les Pr et équipes concernées sont : ';
pullRequestsForCommitShaDetails.forEach((pullRequestDetails) => {
pRsAndTeamLabelsMessageList = pRsAndTeamLabelsMessageList.concat(
`<${pullRequestDetails.html_url}|${pullRequestDetails.labels}> `,
);
});

const message =
':warning: Il y a eu des ajout(s)/suppression(s) ' +
`<https://github.com/1024pix/pix/compare/${latestTag}...dev|dans le fichier config.js>. ` +
"Pensez à vérifier que toutes les variables d'environnement sont bien à jour sur *Scalingo RECETTE*.";
"Pensez à vérifier que toutes les variables d'environnement sont bien à jour sur *Scalingo RECETTE*. " +
`${pRsAndTeamLabelsMessageList}`;

await slackPostMessageService.postMessage({
message,
channel: '#tech-releases',
Expand Down
49 changes: 48 additions & 1 deletion common/services/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,45 @@ const commentPullRequest = async ({ repositoryName, pullRequestId, comment }) =>
});
};

async function _getPullRequestsFromCommitShaFromGithub({ repoOwner, repoName, commitSha }) {
const { repos } = _createOctokit();
const { data } = await repos.listPullRequestsAssociatedWithCommit({
owner: repoOwner,
repo: repoName,
commit_sha: commitSha,
});

return data ? data : [];
}

async function _getPullRequestsDetailsByCommitShas({ repoOwner, repoName, commitsShaList }) {
let pullRequests = [];

await Promise.all(
commitsShaList.map(async (commitSha) => {
const pullRequestsByCommitSha = await _getPullRequestsFromCommitShaFromGithub({
repoOwner,
repoName,
commitSha,
});
pullRequestsByCommitSha.map((pullRequestDetails) => {
pullRequests.push(pullRequestDetails);
});
}),
);

const pullRequestsForCommitShaFilteredDetails = Array.from(new Set(pullRequests.map(JSON.stringify))).map(JSON.parse);

pullRequestsForCommitShaFilteredDetails.forEach((pullRequest) => {
pullRequest.labels = pullRequest.labels
.filter((label) => label.name.includes('team'))
.map((label) => label.name)
.join(',');
});

return pullRequestsForCommitShaFilteredDetails;
}

module.exports = {
async getPullRequests(label) {
const pullRequests = await _getPullReviewsFromGithub(label);
Expand Down Expand Up @@ -364,9 +403,17 @@ module.exports = {
const latestReleaseDate = await _getLatestReleaseDate(repoOwner, repoName);
const now = new Date().toISOString();
const commits = await _getCommitsWhereConfigFileHasChangedBetweenDate(repoOwner, repoName, latestReleaseDate, now);
const commitsShaList = commits.map((commit) => commit.sha);
const hasConfigFileChanged = commits.length > 0;
const latestTag = await _getLatestReleaseTagName(repoOwner, repoName);
return { hasConfigFileChanged, latestTag };

const pullRequestsForCommitShaDetails = await _getPullRequestsDetailsByCommitShas({
repoOwner,
repoName,
commitsShaList,
});

return { hasConfigFileChanged, latestTag, pullRequestsForCommitShaDetails };
},

async hasConfigFileChangedInLatestRelease(repoOwner = settings.github.owner, repoName = settings.github.repository) {
Expand Down
261 changes: 154 additions & 107 deletions test/acceptance/build/slack_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,134 +188,181 @@ describe('Acceptance | Build | Slack', function () {
});
});

it('returns the confirmation modal with a warning and sends a slack message to tech-releases', async function () {
// given
const tagNock = nock('https://api.github.com')
.get('/repos/github-owner/github-repository/tags')
.twice()
.reply(200, [
{
commit: {
url: 'https://api.github.com/repos/github-owner/github-repository/commits/1234',
it(
'returns the confirmation modal with a warning and sends a' + ' slack message to tech-releases',
async function () {
// given
const tagNock = nock('https://api.github.com')
.get('/repos/github-owner/github-repository/tags')
.twice()
.reply(200, [
{
commit: {
url: 'https://api.github.com/repos/github-owner/github-repository/commits/1234',
},
name: 'v6.6.6',
},
name: 'v6.6.6',
},
{
{
commit: {
url: 'https://api.github.com/repos/github-owner/github-repository/commits/456',
},
name: 'v6.6.5',
},
]);

const firstCommitNock = nock('https://api.github.com')
.get('/repos/github-owner/github-repository/commits/1234')
.reply(200, {
commit: {
url: 'https://api.github.com/repos/github-owner/github-repository/commits/456',
committer: {
date: '2021-04-14T12:40:50.326Z',
},
},
name: 'v6.6.5',
},
]);
});

const firstCommitNock = nock('https://api.github.com')
.get('/repos/github-owner/github-repository/commits/1234')
.reply(200, {
commit: {
committer: {
date: '2021-04-14T12:40:50.326Z',
const commitsNock = nock('https://api.github.com')
.filteringPath(
/since=\d{4}-\d{2}-\d{2}T\d{2}%3A\d{2}%3A\d{2}.\d{3}Z&until=\d{4}-\d{2}-\d{2}T\d{2}%3A\d{2}%3A\d{2}.\d{3}Z/g,
'since=XXXX&until=XXXX',
)
.get('/repos/github-owner/github-repository/commits?since=XXXX&until=XXXX&path=api%2Flib%2Fconfig.js')
.reply(200, [
{
sha: '6dcb09',
},
},
});
{
sha: '4193db5e',
},
]);

const commitsNock = nock('https://api.github.com')
.filteringPath(
/since=\d{4}-\d{2}-\d{2}T\d{2}%3A\d{2}%3A\d{2}.\d{3}Z&until=\d{4}-\d{2}-\d{2}T\d{2}%3A\d{2}%3A\d{2}.\d{3}Z/g,
'since=XXXX&until=XXXX',
)
.get('/repos/github-owner/github-repository/commits?since=XXXX&until=XXXX&path=api%2Flib%2Fconfig.js')
.reply(200, [{}]);
nock('https://api.github.com')
.get('/repos/github-owner/github-repository/commits/6dcb09/pulls')
.reply(200, [
{
number: 1327,
labels: [{ name: 'team-captains' }, { name: 'team-acces' }],
html_url: 'https://github.com/octocat/Hello-World/pull/1327',
},
{
number: 4567,
labels: [{ name: 'cross-team' }, { name: 'team-dev-com' }],
html_url: 'https://github.com/octocat/Hello-World/pull/4567',
},
]);

nock('https://api.github.com')
.get('/repos/github-owner/github-repository/commits/4193db5e/pulls')
.reply(200, [
{
number: 2438,
labels: [{ name: 'team-eval' }],
html_url: 'https://github.com/octocat/Hello-World/pull/2438',
},
{
number: 6934,
labels: [{ name: 'fake' }, { name: 'team-prescription' }],
html_url: 'https://github.com/octocat/Hello-World/pull/6934',
},
{
number: 1327,
labels: [{ name: 'team-captains' }, { name: 'team-acces' }],
html_url: 'https://github.com/octocat/Hello-World/pull/1327',
},
]);

const message =
':warning: Il y a eu des ajout(s)/suppression(s) ' +
'<https://github.com/1024pix/pix/compare/v6.6.6...dev|dans le fichier config.js>. ' +
"Pensez à vérifier que toutes les variables d'environnement sont bien à jour sur *Scalingo RECETTE*.";
const message =
':warning: Il y a eu des ajout(s)/suppression(s) ' +
'<https://github.com/1024pix/pix/compare/v6.6.6...dev|dans le fichier config.js>. ' +
"Pensez à vérifier que toutes les variables d'environnement sont bien à jour sur *Scalingo RECETTE*. " +
'Les Pr et équipes concernées sont : ' +
'<https://github.com/octocat/Hello-World/pull/1327|team-captains,team-acces> <https://github.com/octocat/Hello-World/pull/4567|cross-team,team-dev-com> <https://github.com/octocat/Hello-World/pull/2438|team-eval> <https://github.com/octocat/Hello-World/pull/6934|team-prescription> ';

const expectedRequestBody = {
channel: '#tech-releases',
text: message,
};
const expectedRequestBody = {
channel: '#tech-releases',
text: message,
};

const slackMessageNock = nock('https://slack.com')
.post('/api/chat.postMessage', (body) => {
expect(body).to.deep.equal(expectedRequestBody);
return true;
})
.reply(200, {
ok: true,
});
const slackMessageNock = nock('https://slack.com')
.post('/api/chat.postMessage', (body) => {
expect(body).to.deep.equal(expectedRequestBody);
return true;
})
.reply(200, {
ok: true,
});

const body = {
type: 'view_submission',
view: {
callback_id: 'release-type-selection',
state: {
values: {
'publish-release-type': {
'release-type-option': {
selected_option: {
value: 'major',
const body = {
type: 'view_submission',
view: {
callback_id: 'release-type-selection',
state: {
values: {
'publish-release-type': {
'release-type-option': {
selected_option: {
value: 'major',
},
},
},
},
},
},
},
};
};

// when
const res = await server.inject({
method: 'POST',
url: '/build/slack/interactive-endpoint',
headers: createSlackWebhookSignatureHeaders(JSON.stringify(body)),
payload: body,
});
// when
const res = await server.inject({
method: 'POST',
url: '/build/slack/interactive-endpoint',
headers: createSlackWebhookSignatureHeaders(JSON.stringify(body)),
payload: body,
});

//then
//then

expect(tagNock.isDone()).to.be.true;
expect(firstCommitNock.isDone()).to.be.true;
expect(commitsNock.isDone()).to.be.true;
expect(slackMessageNock.isDone()).to.be.true;
expect(tagNock.isDone()).to.be.true;
expect(firstCommitNock.isDone()).to.be.true;
expect(commitsNock.isDone()).to.be.true;
expect(slackMessageNock.isDone()).to.be.true;

expect(res.statusCode).to.equal(200);
expect(JSON.parse(res.payload)).to.deep.equal({
response_action: 'push',
view: {
type: 'modal',
callback_id: 'release-publication-confirmation',
private_metadata: 'major',
title: {
type: 'plain_text',
text: 'Confirmation',
},
submit: {
type: 'plain_text',
text: '🚀 Go !',
},
close: {
type: 'plain_text',
text: 'Annuler',
},
blocks: [
{
type: 'section',
text: {
type: 'mrkdwn',
text: ":warning: Il y a eu des ajout(s)/suppression(s) dans le fichier <https://github.com/1024pix/pix/compare/v6.6.6...dev|*config.js*>. Pensez à vérifier que toutes les variables d'environnement sont bien à jour sur *Scalingo RECETTE*.",
},
expect(res.statusCode).to.equal(200);
expect(JSON.parse(res.payload)).to.deep.equal({
response_action: 'push',
view: {
type: 'modal',
callback_id: 'release-publication-confirmation',
private_metadata: 'major',
title: {
type: 'plain_text',
text: 'Confirmation',
},
{
type: 'section',
text: {
type: 'mrkdwn',
text: 'Vous vous apprêtez à publier une version *major* et la déployer en recette. Êtes-vous sûr de vous ?',
},
submit: {
type: 'plain_text',
text: '🚀 Go !',
},
],
},
});
});
close: {
type: 'plain_text',
text: 'Annuler',
},
blocks: [
{
type: 'section',
text: {
type: 'mrkdwn',
text: ":warning: Il y a eu des ajout(s)/suppression(s) dans le fichier <https://github.com/1024pix/pix/compare/v6.6.6...dev|*config.js*>. Pensez à vérifier que toutes les variables d'environnement sont bien à jour sur *Scalingo RECETTE*.",
},
},
{
type: 'section',
text: {
type: 'mrkdwn',
text: 'Vous vous apprêtez à publier une version *major* et la déployer en recette. Êtes-vous sûr de vous ?',
},
},
],
},
});
},
);
});

describe('callback release-publication-confirmation', function () {
Expand Down
Loading

0 comments on commit 67106a9

Please sign in to comment.