|
| 1 | +const YAML = require('js-yaml'); |
| 2 | + |
| 3 | +const { getHash, logger } = require('../util'); |
| 4 | + |
| 5 | +module.exports = (rawSources) => { |
| 6 | + logger(__filename, 'purgeSources'); |
| 7 | + |
| 8 | + const sources = YAML.load(rawSources); |
| 9 | + |
| 10 | + // List all repository URLs |
| 11 | + const allRepositories = sources |
| 12 | + .filter((source) => source.repository) |
| 13 | + .map((source) => source.repository); |
| 14 | + |
| 15 | + // Collect up any repositories that have been moved as a pointer ready for merging them with the |
| 16 | + // the new GitHub URL |
| 17 | + const newRepositoryLocations = sources |
| 18 | + .filter((source) => source.repositoryMetadata && source.repositoryMetadata.moved) |
| 19 | + .reduce((output, source) => { |
| 20 | + if (output[source.repositoryMetadata.newUrl]) { |
| 21 | + // eslint-disable-next-line no-param-reassign |
| 22 | + output[source.repositoryMetadata.newUrl] = output[source.repositoryMetadata.newUrl] |
| 23 | + .concat(source.repository); |
| 24 | + |
| 25 | + return output; |
| 26 | + } |
| 27 | + |
| 28 | + return Object.assign( |
| 29 | + output, |
| 30 | + { [source.repositoryMetadata.newUrl]: [source.repository] }, |
| 31 | + ); |
| 32 | + }, {}); |
| 33 | + |
| 34 | + // Remove anything that is not found or has been moved. If a repository has been moved but the |
| 35 | + // new location is not in source then change the value of the source.repository property |
| 36 | + return YAML.dump( |
| 37 | + sources |
| 38 | + .map((source) => { |
| 39 | + const updatedSource = source; |
| 40 | + const newRepositoryLocation = newRepositoryLocations[source.repository]; |
| 41 | + |
| 42 | + if (newRepositoryLocation) { |
| 43 | + // Source repository is updated to include old locations for tracking purposes |
| 44 | + |
| 45 | + // eslint-disable-next-line no-param-reassign |
| 46 | + updatedSource.oldLocations = [...new Set((source.oldLocations || []) |
| 47 | + .concat(newRepositoryLocations[source.repository]))]; |
| 48 | + } else if ((source.repositoryMetadata || {}).moved |
| 49 | + && allRepositories.indexOf(source.repositoryMetadata.newUrl) === -1) { |
| 50 | + // Source repository has been moved but there is no data in the repository to move it to |
| 51 | + // Update the repository property with the new URL. This will then be ignored on the |
| 52 | + // next run and the updated repository value retained in the tools.yaml |
| 53 | + |
| 54 | + logger(`Moving repository metadata: ${source.repository} to new URL reference: ${updatedSource.repositoryMetadata.newUrl}`); |
| 55 | + |
| 56 | + updatedSource.oldLocations = [source.repository]; |
| 57 | + updatedSource.repository = updatedSource.repositoryMetadata.newUrl; |
| 58 | + updatedSource.id = getHash(source.repository); |
| 59 | + |
| 60 | + delete updatedSource.repositoryMetadata.newUrl; |
| 61 | + delete updatedSource.repositoryMetadata.moved; |
| 62 | + } |
| 63 | + |
| 64 | + return updatedSource; |
| 65 | + }) |
| 66 | + .filter((source) => { |
| 67 | + const { repositoryMetadata } = source; |
| 68 | + |
| 69 | + if (!repositoryMetadata || (!repositoryMetadata.notFound && !repositoryMetadata.moved)) { |
| 70 | + return true; |
| 71 | + } |
| 72 | + |
| 73 | + if (repositoryMetadata.notFound) { |
| 74 | + logger(`Removing repository as not found at target location: ${source.repository}`); |
| 75 | + return false; |
| 76 | + } |
| 77 | + |
| 78 | + logger(`Removing repository as it has been moved: ${source.repository} and is already catalogued at new URL reference: ${source.repositoryMetadata.newUrl}`); |
| 79 | + return false; |
| 80 | + }), |
| 81 | + ); |
| 82 | +}; |
0 commit comments