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

[BUGFIX] Utiliser les commits entre les 2 releases pour afficher une notification de changement de config lors du déploiement #137

Merged
merged 1 commit into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions common/services/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ async function getLatestRelease(repoOwner, repoName) {
return tags[0];
}

async function getSecondToLastRelease(repoOwner, repoName) {
const tags = await _getTags(repoOwner, repoName);
return tags[1];
}

async function _getTags(repoOwner, repoName) {
const { repos } = _createOctokit();
const { data } = await repos.listTags({
Expand Down Expand Up @@ -182,6 +187,11 @@ async function _getLatestReleaseTagUrl(repoOwner, repoName) {
return latestReleaseTag.commit.url;
}

async function _getSecondToLastReleaseTagUrl(repoOwner, repoName) {
const secondToLastReleaseTag = await getSecondToLastRelease(repoOwner, repoName);
return secondToLastReleaseTag.commit.url;
}

async function _getLatestReleaseTagName(repoOwner, repoName) {
const latestReleaseTag = await getLatestRelease(repoOwner, repoName);
return latestReleaseTag.name;
Expand All @@ -200,12 +210,19 @@ async function _getLatestReleaseDate(repoOwner, repoName) {
return commit.committer.date;
}

async function _getCommitsWhereConfigFileHasChangedSinceDate(repoOwner, repoName, date) {
async function _getSecondToLastReleaseDate(repoOwner, repoName) {
const secondToLastTagUrl = await _getSecondToLastReleaseTagUrl(repoOwner, repoName);
const commit = await _getCommitAtURL(secondToLastTagUrl);
return commit.committer.date;
}

async function _getCommitsWhereConfigFileHasChangedBetweenDate(repoOwner, repoName, sinceDate, untilDate) {
const { repos } = _createOctokit();
const { data } = await repos.listCommits({
owner: repoOwner,
repo: repoName,
since: date,
since: sinceDate,
until: untilDate,
path: 'api/lib/config.js',
});

Expand Down Expand Up @@ -288,7 +305,15 @@ module.exports = {

async hasConfigFileChangedSinceLatestRelease(repoOwner = settings.github.owner, repoName = settings.github.repository) {
const latestReleaseDate = await _getLatestReleaseDate(repoOwner, repoName);
const commits = await _getCommitsWhereConfigFileHasChangedSinceDate(repoOwner, repoName, latestReleaseDate);
const now = new Date().toISOString();
const commits = await _getCommitsWhereConfigFileHasChangedBetweenDate(repoOwner, repoName, latestReleaseDate, now);
return commits.length > 0;
},

async hasConfigFileChangedInLatestRelease(repoOwner = settings.github.owner, repoName = settings.github.repository) {
const latestReleaseDate = await _getLatestReleaseDate(repoOwner, repoName);
const secondToLastReleaseDate = await _getSecondToLastReleaseDate(repoOwner, repoName);
const commits = await _getCommitsWhereConfigFileHasChangedBetweenDate(repoOwner, repoName, secondToLastReleaseDate, latestReleaseDate);
return commits.length > 0;
},

Expand Down
2 changes: 1 addition & 1 deletion run/services/slack/view-submissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const postSlackMessage = require('../../../common/services/slack/surfaces/messag
module.exports = {
async submitReleaseTagSelection(payload) {
const releaseTag = payload.view.state.values['deploy-release-tag']['release-tag-value'].value;
const hasConfigFileChanged = await githubService.hasConfigFileChangedSinceLatestRelease();
const hasConfigFileChanged = await githubService.hasConfigFileChangedInLatestRelease();
return openModalReleaseDeploymentConfirmation(releaseTag, hasConfigFileChanged);
},

Expand Down
1 change: 1 addition & 0 deletions test/acceptance/run/slack_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const server = require('../../../server');

describe('Acceptance | Run | Slack', function() {
describe('POST /run/slack/interactive-endpoint', function() {

it('responds with 204', async () => {
const body = {
type: 'view_closed'
Expand Down
62 changes: 50 additions & 12 deletions test/test-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,48 +51,86 @@ function createSlackWebhookSignatureHeaders(body) {
function nockGithubWithNoConfigChanges() {
nock('https://api.github.com')
.get('/repos/github-owner/github-repository/tags')
.reply(200, [{
'commit': {
'url': 'https://api.github.com/repos/github-owner/github-repository/commits/1234',
.twice()
.reply(200, [
{
'commit': {
'url': 'https://api.github.com/repos/github-owner/github-repository/commits/1234',
},
},
}]);
{
'commit': {
'url': 'https://api.github.com/repos/github-owner/github-repository/commits/456',
},
}
]);

nock('https://api.github.com')
.get('/repos/github-owner/github-repository/commits/1234')
.reply(200, {
commit: {
'committer': {
'date': '2011-04-14'
'date': '2021-04-14T12:40:50.326Z'
},
}
});

nock('https://api.github.com')
.get('/repos/github-owner/github-repository/commits/456')
.reply(200, {
commit: {
'committer': {
'date': '2021-04-10T12:40:50.326Z'
},
}
});

nock('https://api.github.com')
.get('/repos/github-owner/github-repository/commits?since=2011-04-14&path=api%2Flib%2Fconfig.js')
.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')
Copy link
Member

Choose a reason for hiding this comment

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

🤯

Copy link
Member Author

Choose a reason for hiding this comment

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

L'enfer mdr

.get('/repos/github-owner/github-repository/commits?since=XXXX&until=XXXX&path=api%2Flib%2Fconfig.js')
.reply(200, []);
}

function nockGithubWithConfigChanges() {
nock('https://api.github.com')
.get('/repos/github-owner/github-repository/tags')
.reply(200, [{
'commit': {
'url': 'https://api.github.com/repos/github-owner/github-repository/commits/1234',
.twice()
.reply(200, [
{
'commit': {
'url': 'https://api.github.com/repos/github-owner/github-repository/commits/1234',
},
},
}]);
{
'commit': {
'url': 'https://api.github.com/repos/github-owner/github-repository/commits/456',
},
}
]);

nock('https://api.github.com')
.get('/repos/github-owner/github-repository/commits/1234')
.reply(200, {
commit: {
'committer': {
'date': '2011-04-14'
'date': '2021-04-14T12:40:50.326Z'
},
}
});

nock('https://api.github.com')
.get('/repos/github-owner/github-repository/commits/456')
.reply(200, {
commit: {
'committer': {
'date': '2021-04-10T12:40:50.326Z'
},
}
});

nock('https://api.github.com')
.get('/repos/github-owner/github-repository/commits?since=2011-04-14&path=api%2Flib%2Fconfig.js')
.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, [{}]);
}

Expand Down
92 changes: 89 additions & 3 deletions test/unit/build/services/github_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { describe, it } = require('mocha');
const { expect } = require('chai');
const { nock, createGithubWebhookSignatureHeader } = require('../../../test-helper');
const { nock, createGithubWebhookSignatureHeader, sinon } = require('../../../test-helper');
const githubService = require('../../../../common/services/github');

describe('#getPullRequests', function() {
Expand Down Expand Up @@ -267,12 +267,23 @@ describe('#hasConfigFileChangedSinceLatestRelease', () => {
const repoOwner = 'github-owner';
const repoName = 'github-repository';

let clock, now;

beforeEach(() => {
now = new Date();
clock = sinon.useFakeTimers(now);
});

afterEach(() => {
clock.restore();
});

context('should return true when config file has been changed', ()=> {
it('should call Github "commits list" API', async () => {
// given
nock('https://api.github.com')
.get('/repos/github-owner/github-repository/commits')
.query({ since: latestReleaseDate, path: 'api/lib/config.js' })
.query({ since: latestReleaseDate, until: now.toISOString(), path: 'api/lib/config.js' })
.reply(200,
[{
sha: '5ec2f42',
Expand All @@ -299,7 +310,7 @@ describe('#hasConfigFileChangedSinceLatestRelease', () => {
// given
nock('https://api.github.com')
.get('/repos/github-owner/github-repository/commits')
.query({ since: latestReleaseDate, path: 'api/lib/config.js' })
.query({ since: latestReleaseDate, until: now.toISOString(), path: 'api/lib/config.js' })
.reply(200,
[]
);
Expand All @@ -320,6 +331,81 @@ describe('#hasConfigFileChangedSinceLatestRelease', () => {
});
});

describe('#hasConfigFileChangedInLatestRelease', () => {
const latestReleaseDate= '2020-08-13T04:45:06Z';
const secondToLastReleaseDate = '2020-08-10T12:45:06Z';
const repoOwner = 'github-owner';
const repoName = 'github-repository';

context('should return true when config file has been changed', ()=> {
it('should call Github "commits list" API', async () => {
// given
nock('https://api.github.com')
.get('/repos/github-owner/github-repository/commits')
.query({ since: secondToLastReleaseDate, until: latestReleaseDate, path: 'api/lib/config.js' })
.reply(200,
[{
sha: '5ec2f42',
}]
);

nock('https://api.github.com')
.get('/repos/github-owner/github-repository/tags')
.twice()
.reply(200, [
{ commit: { url: '/latest_tag_commit_url' } },
{ commit: { url: '/second-to-last_tag_commit_url' } },
]);

nock('https://api.github.com')
.get('/latest_tag_commit_url')
.reply(200, {commit: {committer: {date: latestReleaseDate}}});

nock('https://api.github.com')
.get('/second-to-last_tag_commit_url')
.reply(200, {commit: {committer: {date: secondToLastReleaseDate}}});

// when
const response = await githubService.hasConfigFileChangedInLatestRelease(repoOwner, repoName);
// then
expect(response).to.be.true;
});
});

context('should return false when config file did not changed changed', ()=> {
it('should call Github "commits list" API', async () => {
// given
nock('https://api.github.com')
.get('/repos/github-owner/github-repository/commits')
.query({ since: secondToLastReleaseDate, until: latestReleaseDate, path: 'api/lib/config.js' })
.reply(200,
[]
);

nock('https://api.github.com')
.get('/repos/github-owner/github-repository/tags')
.twice()
.reply(200, [
{ commit: { url: '/latest_tag_commit_url' } },
{ commit: { url: '/second-to-last_tag_commit_url' } },
]);

nock('https://api.github.com')
.get('/latest_tag_commit_url')
.reply(200, {commit: {committer: {date: latestReleaseDate}}});

nock('https://api.github.com')
.get('/second-to-last_tag_commit_url')
.reply(200, {commit: {committer: {date: secondToLastReleaseDate}}});

// when
const response = await githubService.hasConfigFileChangedInLatestRelease(repoOwner, repoName);
// then
expect(response).to.be.false;
});
});
});

describe('#verifyWebhookSignature', function() {
it('return true when the signature match', function() {
const body = {};
Expand Down