Skip to content
Open
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
61 changes: 61 additions & 0 deletions .github/workflows/adapter-naming.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: Check adapter naming conventions
on:
pull_request:
types: [opened, synchronize, reopened, ready_for_review]
permissions:
contents: read
jobs:
check-names:
name: Check adapter naming conventions
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v7
with:
fetch-depth: 0
- name: install dependencies
uses: ./.github/actions/npm-ci
- name: Update metadata
id: update
continue-on-error: true
run: |
npx gulp update-metadata --no-fetch
- name: Adapter code does not match file name
if: ${{ steps.update.outcome != 'success' }}
run: |
echo '{"issue_number": ${{ github.event.pull_request.number }}, "body": "This PR includes an adapter whose code does not match its file name. Bid adapter modules should be named `<bidderCode>BidAdapter`, userId `<userIdCode>IdSystem`, RTD `<rtdCode>RtdProvider`, and analytics `<analyticsCode>Analytics`."}' > ${{ runner.temp }}/comment.json
- name: Calculate diff
if: ${{ steps.update.outcome == 'success' }}
run: |
git diff --name-only $(git merge-base HEAD ${{ github.event.pull_request.base.sha }})..HEAD > ${{runner.temp}}/changed_files.txt
- name: Check naming
if: ${{ steps.update.outcome == 'success' }}
uses: actions/github-script@v9
id: check
with:
result-encoding: string
script: |
const fs = require('fs');
const { getViolationsSummary, formatViolationsSummary } = require('./metadata/validateNaming.mjs');
const diff = fs.readFileSync('${{ runner.temp }}/changed_files.txt').toString().split('\n').map(s => s.trim());
const modules = new Set(diff.map(filename => /^modules\/([^\/.]+)/.exec(filename)?.[1]).filter(fn => fn != null));
const violations = Object.fromEntries(
Object.entries(await getViolationsSummary())
.filter(([moduleName]) => modules.has(moduleName))
);
const formatted = formatViolationsSummary(violations);
if (formatted != null) {
fs.writeFileSync('${{ runner.temp }}/comment.json', JSON.stringify({
issue_number: ${{ github.event.pull_request.number }},
body: `Some adapters in this PR do not follow Prebid naming conventions.\n${formatted}`
}));
return 'true';
}
return 'false';

- name: Upload comment data
if: ${{ steps.update.outcome != 'success' || steps.check.outputs.result == 'true' }}
uses: actions/upload-artifact@v7
with:
name: comment
path: ${{ runner.temp }}/comment.json
1 change: 1 addition & 0 deletions .github/workflows/comment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
workflows:
- Check for Duplicated Code
- Check for linter warnings / exceptions
- Check adapter naming conventions
types:
- completed

Expand Down
6 changes: 6 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -578,3 +578,9 @@ gulp.task('compile-metadata', function (done) {
});
gulp.task('update-metadata', gulp.series('build', 'extract-metadata', 'compile-metadata'));
module.exports = nodeBundle;

gulp.task('validate-names', function (done) {
import('./metadata/validateNaming.mjs').then(({ validateNaming }) => {
validateNaming().then(done, done);
});
});
109 changes: 109 additions & 0 deletions metadata/validateNaming.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import fs from 'node:fs/promises';
import path from 'node:path';

async function readMetadata() {
return Object.fromEntries(
await Promise.all(
(await fs.readdir(path.resolve(import.meta.dirname, 'modules')))
.map(async name => {
const components = JSON.parse((await fs.readFile(path.resolve(import.meta.dirname, 'modules', name))).toString()).components;
return [name.replace(/\.json$/, ''), components];
})
)
);
}

function conflictDetector(metadata) {
function getKey({ componentType, componentName }) {
return componentType === 'bidder' ? `bidder.${componentName.substring(0, 6).toLowerCase()}` : `${componentType}.${componentName.toLowerCase()}`;
}

const conflictMap = Object.entries(metadata).reduce((memo, [moduleName, components]) => {
components
.forEach(({ componentType, componentName, aliasOf }) => {
const key = getKey({ componentType, componentName });
if (!memo.hasOwnProperty(key)) {
memo[key] = [];
}
memo[key].push({
moduleName: moduleName,
componentType,
componentName,
aliasOf
});
});
return memo;
}, {});
return function (moduleName, component) {
return conflictMap[getKey(component)]?.filter((entry) => entry.moduleName !== moduleName) ?? [];
};
}

function checkName({ componentType, componentName }) {
if (componentType === 'bidder' && !/^[a-z0-9_]+$/.test(componentName)) {
return 'contains uppercase or non-alphanumeric characters';
}
}

export async function getViolationsSummary() {
const meta = await readMetadata();
const checkForConflicts = conflictDetector(meta);
return Object.entries(meta)
.reduce((memo, [moduleName, components]) => {
components.forEach(cmp => {
const conflicts = checkForConflicts(moduleName, cmp);
const nameViolation = checkName(cmp);
if (conflicts.length > 0 || nameViolation != null) {
if (!memo.hasOwnProperty(moduleName)) {
memo[moduleName] = [];
}
const entry = {
component: {
componentType: cmp.componentType,
componentName: cmp.componentName,
aliasOf: cmp.aliasOf
}
};
if (conflicts.length > 0) {
entry.conflicts = conflicts;
}
if (nameViolation != null) {
entry.name = nameViolation;
}
memo[moduleName].push(entry);
}
});
return memo;
}, {});
}

export function formatViolationsSummary(violations) {
const naming = [];
const conflicting = [];

function declaration(component) {
return `${component.componentType} ${component.aliasOf ? 'alias' : 'code'} \`${component.componentName}\``;
}

Object.entries(violations).forEach(([moduleName, entries]) => {
entries.forEach(({ component, name, conflicts }) => {
if (name) {
naming.push(`Module \`${moduleName}\` defines ${declaration(component)}, which ${name}`);
}
if (conflicts) {
conflicting.push(`* Module \`${moduleName}\` defines ${declaration(component)}, which conflicts with:`);
conflicts.forEach(conflict => conflicting.push(` * ${declaration(conflict)} defined in module \`${conflict.moduleName}\``));
}
});
});

return naming.concat(['']).concat(conflicting).join('\n');
}

export async function validateNaming() {
const warn = formatViolationsSummary(await getViolationsSummary());
if (warn != null) {
console.warn(warn);
throw new Error('Some adapters do not follow naming conventions');
}
}
1 change: 1 addition & 0 deletions modules/adkernelBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import { getBidFloor } from '../libraries/adkernelUtils/adkernelUtils.js';

/**
* touch

Check failure on line 25 in modules/adkernelBidAdapter.js

View workflow job for this annotation

GitHub Actions / Run linter

Trailing spaces not allowed
* In case you're AdKernel whitelable platform's client who needs branded adapter to
* work with Adkernel platform - DO NOT COPY THIS ADAPTER UNDER NEW NAME
*
Expand Down Expand Up @@ -332,9 +333,9 @@
if (pbVideo.playerSize) {
sizes = pbVideo.playerSize[0];
typedImp.video = Object.assign(typedImp.video, parseGPTSingleSizeArrayToRtbSize(sizes) || {});
} else if (pbVideo.w && pbVideo.h) {
typedImp.video.w = pbVideo.w;
typedImp.video.h = pbVideo.h;

Check warning on line 338 in modules/adkernelBidAdapter.js

View workflow job for this annotation

GitHub Actions / Coverage

336-338 lines are not covered with tests
}
initImpBidfloor(typedImp, bidRequest, sizes, isMultiformat ? '*' : VIDEO);
result.push(typedImp);
Expand All @@ -342,8 +343,8 @@

if (mediaTypes?.native) {
if (isMultiformat) {
typedImp = { ...imp };
typedImp.id = typedImp.id + MULTI_FORMAT_SUFFIX_NATIVE;

Check warning on line 347 in modules/adkernelBidAdapter.js

View workflow job for this annotation

GitHub Actions / Coverage

346-347 lines are not covered with tests
} else {
typedImp = imp;
}
Expand Down Expand Up @@ -431,7 +432,7 @@
if (isEmpty(appConfig)) {
return { site: createSite(refererInfo, fpd) };
} else {
return { app: appConfig };

Check warning on line 435 in modules/adkernelBidAdapter.js

View workflow job for this annotation

GitHub Actions / Coverage

435 line is not covered with tests
}
}

Expand Down Expand Up @@ -499,10 +500,10 @@
'tmax': parseInt(bidderRequest.timeout)
};
if (!isEmpty(fpd.bcat)) {
request.bcat = fpd.bcat;

Check warning on line 503 in modules/adkernelBidAdapter.js

View workflow job for this annotation

GitHub Actions / Coverage

503 line is not covered with tests
}
if (!isEmpty(fpd.badv)) {
request.badv = fpd.badv;

Check warning on line 506 in modules/adkernelBidAdapter.js

View workflow job for this annotation

GitHub Actions / Coverage

506 line is not covered with tests
}
return request;
}
Expand Down Expand Up @@ -540,7 +541,7 @@
makeSyncInfo(bidderRequest)
);
if (schain) {
deepSetValue(req, 'source.ext.schain', schain);

Check warning on line 544 in modules/adkernelBidAdapter.js

View workflow job for this annotation

GitHub Actions / Coverage

544 line is not covered with tests
}
return req;
}
Expand All @@ -564,7 +565,7 @@
};
mergeDeep(site, fpd.site);
if (refInfo.ref != null) {
site.ref = refInfo.ref;

Check warning on line 568 in modules/adkernelBidAdapter.js

View workflow job for this annotation

GitHub Actions / Coverage

568 line is not covered with tests
} else {
delete site.ref;
}
Expand Down Expand Up @@ -604,7 +605,7 @@
*/
function validateNativeImageSize(img) {
if (!img) {
return true;

Check warning on line 608 in modules/adkernelBidAdapter.js

View workflow job for this annotation

GitHub Actions / Coverage

608 line is not covered with tests
}
if (img.sizes) {
return isArrayOfNums(img.sizes, 2);
Expand All @@ -612,7 +613,7 @@
if (isArray(img.aspect_ratios)) {
return img.aspect_ratios.length > 0 && img.aspect_ratios[0].min_height && img.aspect_ratios[0].min_width;
}
return true;

Check warning on line 616 in modules/adkernelBidAdapter.js

View workflow job for this annotation

GitHub Actions / Coverage

616 line is not covered with tests
}

/**
Expand Down
Loading