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

Change default branch to main #46

Merged
merged 16 commits into from
Feb 21, 2024
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
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 12
node-version: 20
- run: npm ci
- run: npm run lint
- run: npm test
2 changes: 1 addition & 1 deletion .gitpod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ tasks:
github:
prebuilds:
# enable for the default branch (defaults to true)
master: true
main: true
# enable for all branches in this repo (defaults to false)
branches: true
# enable for pull requests coming from this repo (defaults to true)
Expand Down
41 changes: 26 additions & 15 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@

# Run pipeline on master branch, pull-requests into master, and tags
# Run pipeline on main branch, pull-requests into main, and tags
trigger:
branches:
include:
- master
- main
- refs/tags/*

pr:
- master
- main

pool:
vmImage: ubuntu-latest
Expand All @@ -21,37 +21,48 @@ stages:
- task: NodeTool@0
displayName: 'Install Node.js'
inputs:
versionSpec: '10.x'
versionSpec: '20.x'

- script: npm ci
displayName: 'Install'

- task: SonarQubePrepare@5
displayName: 'Prepare analysis on SonarQube'
- task: SonarCloudPrepare@1
displayName: 'Prepare analysis on SonarCloud'
inputs:
SonarQube: 'sonarqube-connection'
SonarCloud: 'SonarCloud'
organization: 'nhsdigital'
scannerMode: 'CLI'
configMode: 'manual'
cliProjectName: 'User Feedback Store'
cliProjectKey: 'UserFeedbackStore'
projectKey: 'nhsuk-user-feedback-store'
projectName: 'User Feedback Store'
extraProperties: |
sonar.projectKey=nhsuk-user-feedback-store
sonar.exclusions=$(System.DefaultWorkingDirectory)/dependency-scan-results/**
sonar.javascript.lcov.reportPaths=coverage/lcov.info
sonar.coverage.exclusions=**/*.config.js,**/coverage/**
sonar.exclusions=node_modules/**
sonar.dependencyCheck.htmlReportPath=$(System.DefaultWorkingDirectory)/dependency-scan-results/dependency-check-report.html
sonar.language=js

- script: npm run test
displayName: 'Test'

- task: SonarQubeAnalyze@5
displayName: 'Run code analysis'
- task: SonarCloudAnalyze@1
displayName: 'Run SonarCloud Code Analysis'

- task: SonarQubePublish@5
displayName: 'Publish quality gate result'
- task: SonarCloudPublish@1
displayName: 'Publish Quality Gate Result'
inputs:
pollingTimeoutSec: '300'

- task: sonarcloud-buildbreaker@2
displayName: "Break the build if the quality gate fails"
inputs:
SonarCloud: 'SonarCloud'
organization: 'nhsdigital'

- stage: DevDeployment
displayName: 'Dev Deployment'
condition: and(succeeded(), startsWith(variables['Build.SourceBranch'], 'refs/heads/master'))
condition: and(succeeded(), startsWith(variables['Build.SourceBranch'], 'refs/heads/main'))
dependsOn:
- Test
jobs:
Expand Down
2 changes: 1 addition & 1 deletion bin/averages.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { getSatisfactionPerURL, closeConnection } = require('../lib/database.js');
const { getSatisfactionPerURL, closeConnection } = require('../lib/database');

module.exports = async (dateSince) => {
const averages = await getSatisfactionPerURL(dateSince);
Expand Down
8 changes: 4 additions & 4 deletions bin/averages.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* global expect jest */

const averages = require('./averages.js');
const averages = require('./averages');

jest.mock('../lib/database.js');
jest.mock('../lib/database');

const { getSatisfactionPerURL } = require('../lib/database.js');
const { getSatisfactionPerURL } = require('../lib/database');

const { mockStdout } = require('../tests/mocks.js');
const { mockStdout } = require('../tests/mocks');

describe('Print a CSV of average satisfaction scores', () => {
let stdout;
Expand Down
2 changes: 1 addition & 1 deletion bin/comments.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { getAllComments, closeConnection } = require('../lib/database.js');
const { getAllComments, closeConnection } = require('../lib/database');

const arrayToCSVRow = (array) => {
const values = array
Expand Down
8 changes: 4 additions & 4 deletions bin/comments.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* global expect jest */

const comments = require('./comments.js');
const comments = require('./comments');

jest.mock('../lib/database.js');
jest.mock('../lib/database');

const { getAllComments } = require('../lib/database.js');
const { getAllComments } = require('../lib/database');

const { mockStdout } = require('../tests/mocks.js');
const { mockStdout } = require('../tests/mocks');

describe('Print a CSV of comments', () => {
let stdout;
Expand Down
4 changes: 2 additions & 2 deletions bin/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const yargs = require('yargs');

const getComments = require('./comments.js');
const getAverages = require('./averages.js');
const getComments = require('./comments');
const getAverages = require('./averages');

const setEnvVars = (args) => {
process.env.MONGO_CONNECTION_STRING = args['connection-string'];
Expand Down
6 changes: 3 additions & 3 deletions bin/index.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* global expect jest */

const cli = require('./index.js');
const cli = require('./index');

jest.mock('./comments.js');
jest.mock('./averages.js');

const getComments = require('./comments.js');
const getAverages = require('./averages.js');
const getComments = require('./comments');
const getAverages = require('./averages');

describe('CLI', () => {
it('returns help text by default', (done) => {
Expand Down
2 changes: 1 addition & 1 deletion comments/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const { handleComments } = require('../lib/index.js');
const { handleComments } = require('../lib/index');

module.exports = (context, req) => handleComments(context, req);
2 changes: 1 addition & 1 deletion healthcheck/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const { handleHealthcheck } = require('../lib/index.js');
const { handleHealthcheck } = require('../lib/index');

module.exports = async (context, req) => handleHealthcheck(context, req);
2 changes: 1 addition & 1 deletion host.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"version": "2.0",
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[1.*, 2.0.0)"
"version": "[1.*, 3.0.0)"
},
"extensions": {
"http": {
Expand Down
14 changes: 6 additions & 8 deletions lib/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
@module lib/database
*/

const { MongoClient, ObjectID } = require('mongodb');
const { uuid } = require('uuidv4');
const { MongoClient, ObjectId } = require('mongodb');
const { v4: uuid } = require('uuid');

const DB_NAME = 'userfeedback';
const COLLECTION_NAME = 'feedback';

const { validateInitialResponse, validateTextComments } = require('./validation.js');
const { validateInitialResponse, validateTextComments } = require('./validation');

let dbClient;

Expand All @@ -18,9 +18,7 @@ let dbClient;
const getDb = async () => {
const CONNECTION_STRING = process.env.MONGO_CONNECTION_STRING;
if (!dbClient) {
dbClient = await MongoClient.connect(CONNECTION_STRING, {
useUnifiedTopology: true,
});
dbClient = await MongoClient.connect(CONNECTION_STRING);
}
return dbClient.db(DB_NAME);
};
Expand Down Expand Up @@ -98,7 +96,7 @@ const update = async (data) => {
};

const getSinceDateQuery = (sinceDate) => {
const minObjectId = ObjectID.createFromTime(sinceDate.getTime() / 1000);
const minObjectId = ObjectId.createFromTime(sinceDate.getTime() / 1000);
return {
_id: {
$gt: minObjectId,
Expand All @@ -121,7 +119,7 @@ const getAllData = async (sinceDate = null, extraQuery = {}) => {
const data = await collection.find(query).project(fields).toArray();
return data.map((row) => ({
...row,
timestamp: ObjectID(row._id).getTimestamp(), // eslint-disable-line no-underscore-dangle
timestamp: new ObjectId(row._id).getTimestamp(), // eslint-disable-line no-underscore-dangle
}));
};

Expand Down
101 changes: 56 additions & 45 deletions lib/database.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@

const { MongoClient } = require('mongodb');

const database = require('./database.js');
const database = require('./database');

let connection;
let db;

beforeAll(async () => {
connection = await MongoClient.connect(
global.__MONGO_URI__, // eslint-disable-line no-underscore-dangle
{
useNewUrlParser: true,
}
global.__MONGO_URI__ // eslint-disable-line no-underscore-dangle
);
db = connection.db('userfeedback');
});
Expand Down Expand Up @@ -86,23 +83,28 @@ describe('database select', () => {
// record the time of DB insert for use in tests. This time is only approximate
insertTime = new Date();

await db.collection('feedback').insert([{
comments: 'This is comment 1',
isSatisfied: true,
url: 'https://example.com/1',
}, {
comments: 'This is comment 2',
isSatisfied: false,
unusedField: 'this is not needed',
url: 'https://example.com/2',
}, {
isSatisfied: true,
url: 'https://example.com/3',
}, {
comments: '',
isSatisfied: true,
url: 'https://example.com/4',
}]);
await db.collection('feedback').insertMany([
{
comments: 'This is comment 1',
isSatisfied: true,
url: 'https://example.com/1',
},
{
comments: 'This is comment 2',
isSatisfied: false,
unusedField: 'this is not needed',
url: 'https://example.com/2',
},
{
isSatisfied: true,
url: 'https://example.com/3',
},
{
comments: '',
isSatisfied: true,
url: 'https://example.com/4',
},
]);
});

describe('getAll', () => {
Expand Down Expand Up @@ -161,22 +163,28 @@ describe('database select', () => {
describe('getSatisfactionPerURL', () => {
beforeAll(async () => {
await db.collection('feedback').deleteMany({});
await db.collection('feedback').insert([{
isSatisfied: true,
url: 'https://example.com/1?query=a',
}, {
isSatisfied: false,
url: 'https://example.com/1?query=b&foo=bar',
}, {
isSatisfied: true,
url: 'https://example.com/2?query=c#an-anchor',
}, {
isSatisfied: true,
url: 'https://example.com/2?query=d',
}, {
isSatisfied: true,
url: 'https://example.com/2#an-anchor',
}]);
await db.collection('feedback').insertMany([
{
isSatisfied: true,
url: 'https://example.com/1?query=a',
},
{
isSatisfied: false,
url: 'https://example.com/1?query=b&foo=bar',
},
{
isSatisfied: true,
url: 'https://example.com/2?query=c#an-anchor',
},
{
isSatisfied: true,
url: 'https://example.com/2?query=d',
},
{
isSatisfied: true,
url: 'https://example.com/2#an-anchor',
},
]);
});

it('returns one row per unique url', async () => {
Expand Down Expand Up @@ -214,13 +222,16 @@ describe('database select', () => {

it('URLs are lower-cased', async () => {
await db.collection('feedback').deleteMany({});
await db.collection('feedback').insert([{
isSatisfied: true,
url: 'https://EXAMPLE.COM/',
}, {
isSatisfied: true,
url: 'https://example.com/',
}]);
await db.collection('feedback').insertMany([
{
isSatisfied: true,
url: 'https://EXAMPLE.COM/',
},
{
isSatisfied: true,
url: 'https://example.com/',
},
]);

const data = await database.getSatisfactionPerURL();
expect(data).toHaveLength(1);
Expand Down
6 changes: 3 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const database = require('./database.js');
const { HttpError } = require('./validation.js');
const responses = require('./responses.js');
const database = require('./database');
const { HttpError } = require('./validation');
const responses = require('./responses');

const handleError = (err) => {
// only return error message in response if it is a HttpError
Expand Down
8 changes: 4 additions & 4 deletions lib/index.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/* global expect jest */

const { handleComments, handleSatisfied, handleHealthcheck } = require('./index.js');
const { ValidationError } = require('./validation.js');
const context = require('../tests/unit/defaultContext.js');
const { handleComments, handleSatisfied, handleHealthcheck } = require('./index');
const { ValidationError } = require('./validation');
const context = require('../tests/unit/defaultContext');

jest.mock('./database.js');
const database = require('./database.js');
const database = require('./database');

describe('http handler for /satisfied/', () => {
beforeEach(() => {
Expand Down
Loading
Loading