From b61a01b48130172224c1a38504b9feef482e1933 Mon Sep 17 00:00:00 2001 From: Kamil Kowalski <kamil@fresha.com> Date: Thu, 22 Dec 2022 11:26:10 +0100 Subject: [PATCH] fix(manager/mix): fetching organization name for deps in mix files (#19128) --- lib/modules/manager/mix/__fixtures__/mix.exs | 5 +- .../mix/__snapshots__/extract.spec.ts.snap | 50 ----------- lib/modules/manager/mix/extract.spec.ts | 86 ++++++++++++++----- lib/modules/manager/mix/extract.ts | 50 ++++++----- lib/modules/manager/mix/index.ts | 8 +- 5 files changed, 106 insertions(+), 93 deletions(-) delete mode 100644 lib/modules/manager/mix/__snapshots__/extract.spec.ts.snap diff --git a/lib/modules/manager/mix/__fixtures__/mix.exs b/lib/modules/manager/mix/__fixtures__/mix.exs index 08fae9e3ca..fe1c24b7c7 100644 --- a/lib/modules/manager/mix/__fixtures__/mix.exs +++ b/lib/modules/manager/mix/__fixtures__/mix.exs @@ -21,8 +21,11 @@ defmodule MyProject.MixProject do # {:broadway_dashboard, "~> 0.2.2"}, {:postgrex, "~> 0.8.1"}, # {:broadway_dashboard, "~> 0.2.2"}, {:ecto, ">2.1.0 or <=3.0.0"}, - {:cowboy, github: "ninenines/cowboy"}, + {:cowboy, github: "ninenines/cowboy", tag: "v0.4.1"}, + {:phoenix, git: "https://github.com/phoenixframework/phoenix.git", branch: "main"}, + {:ecto, github: "elixir-ecto/ecto", ref: "795036d997c7503b21fb64d6bf1a89b83c44f2b5"}, {:secret, "~> 1.0", organization: "acme"}, + {:also_secret, "~> 1.0", only: [:dev, :test], organization: "acme", runtime: false}, {:ex_doc, ">2.1.0 and <=3.0.0"}, {:jason, ">= 1.0.0"}, {:jason, "~> 1.0", diff --git a/lib/modules/manager/mix/__snapshots__/extract.spec.ts.snap b/lib/modules/manager/mix/__snapshots__/extract.spec.ts.snap deleted file mode 100644 index c2682c143d..0000000000 --- a/lib/modules/manager/mix/__snapshots__/extract.spec.ts.snap +++ /dev/null @@ -1,50 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`modules/manager/mix/extract extractPackageFile() extracts all dependencies 1`] = ` -{ - "deps": [ - { - "currentValue": "~> 0.8.1", - "datasource": "hex", - "depName": "postgrex", - "packageName": "postgrex", - }, - { - "currentValue": ">2.1.0 or <=3.0.0", - "datasource": "hex", - "depName": "ecto", - "packageName": "ecto", - }, - { - "currentValue": "ninenines/cowboy", - "datasource": "github", - "depName": "cowboy", - "skipReason": "non-hex-dep-types", - }, - { - "currentValue": "~> 1.0", - "datasource": "hex", - "depName": "secret", - "packageName": "secret:acme", - }, - { - "currentValue": ">2.1.0 and <=3.0.0", - "datasource": "hex", - "depName": "ex_doc", - "packageName": "ex_doc", - }, - { - "currentValue": ">= 1.0.0", - "datasource": "hex", - "depName": "jason", - "packageName": "jason", - }, - { - "currentValue": "~> 1.0", - "datasource": "hex", - "depName": "jason", - "packageName": "jason", - }, - ], -} -`; diff --git a/lib/modules/manager/mix/extract.spec.ts b/lib/modules/manager/mix/extract.spec.ts index 530b2267aa..3ccc432018 100644 --- a/lib/modules/manager/mix/extract.spec.ts +++ b/lib/modules/manager/mix/extract.spec.ts @@ -15,27 +15,71 @@ describe('modules/manager/mix/extract', () => { it('extracts all dependencies', async () => { const res = await extractPackageFile(Fixtures.get('mix.exs'), 'mix.exs'); - expect(res).toMatchSnapshot({ - deps: [ - { depName: 'postgrex', currentValue: '~> 0.8.1' }, - { depName: 'ecto', currentValue: '>2.1.0 or <=3.0.0' }, - { - depName: 'cowboy', - currentValue: 'ninenines/cowboy', - datasource: 'github', - skipReason: 'non-hex-dep-types', - }, - { - depName: 'secret', - currentValue: '~> 1.0', - datasource: 'hex', - packageName: 'secret:acme', - }, - { depName: 'ex_doc', currentValue: '>2.1.0 and <=3.0.0' }, - { depName: 'jason', currentValue: '>= 1.0.0' }, - { depName: 'jason', currentValue: '~> 1.0' }, - ], - }); + expect(res?.deps).toEqual([ + { + currentValue: '~> 0.8.1', + datasource: 'hex', + depName: 'postgrex', + packageName: 'postgrex', + }, + { + currentValue: '>2.1.0 or <=3.0.0', + datasource: 'hex', + depName: 'ecto', + packageName: 'ecto', + }, + { + currentDigest: undefined, + currentValue: 'v0.4.1', + datasource: 'github-tags', + depName: 'cowboy', + packageName: 'ninenines/cowboy', + }, + { + currentDigest: undefined, + currentValue: 'main', + datasource: 'git-tags', + depName: 'phoenix', + packageName: 'https://github.com/phoenixframework/phoenix.git', + }, + { + currentDigest: '795036d997c7503b21fb64d6bf1a89b83c44f2b5', + currentValue: undefined, + datasource: 'github-tags', + depName: 'ecto', + packageName: 'elixir-ecto/ecto', + }, + { + currentValue: '~> 1.0', + datasource: 'hex', + depName: 'secret', + packageName: 'secret:acme', + }, + { + currentValue: '~> 1.0', + datasource: 'hex', + depName: 'also_secret', + packageName: 'also_secret:acme', + }, + { + currentValue: '>2.1.0 and <=3.0.0', + datasource: 'hex', + depName: 'ex_doc', + packageName: 'ex_doc', + }, + { + currentValue: '>= 1.0.0', + datasource: 'hex', + depName: 'jason', + packageName: 'jason', + }, + { + currentValue: '~> 1.0', + datasource: 'hex', + depName: 'jason', + packageName: 'jason', + }, + ]); }); }); }); diff --git a/lib/modules/manager/mix/extract.ts b/lib/modules/manager/mix/extract.ts index 4867af6315..54bd5e5717 100644 --- a/lib/modules/manager/mix/extract.ts +++ b/lib/modules/manager/mix/extract.ts @@ -1,13 +1,20 @@ import { logger } from '../../../logger'; import { findLocalSiblingOrParent, localPathExists } from '../../../util/fs'; import { newlineRegex, regEx } from '../../../util/regex'; +import { GitTagsDatasource } from '../../datasource/git-tags'; +import { GithubTagsDatasource } from '../../datasource/github-tags'; import { HexDatasource } from '../../datasource/hex'; import type { PackageDependency, PackageFile } from '../types'; const depSectionRegExp = regEx(/defp\s+deps.*do/g); const depMatchRegExp = regEx( - /{:(?<depName>\w+),\s*(?<datasource>[^:"]+)?:?\s*"(?<currentValue>[^"]+)",?\s*(?:organization: "(?<organization>.*)")?.*}/gm + /{:(?<app>\w+)(\s*,\s*"(?<requirement>[^"]+)")?(\s*,\s*(?<opts>[^}]+))?}/gm ); +const gitRegexp = regEx(/git:\s*"(?<value>[^"]+)"/); +const githubRegexp = regEx(/github:\s*"(?<value>[^"]+)"/); +const refRegexp = regEx(/ref:\s*"(?<value>[^"]+)"/); +const branchOrTagRegexp = regEx(/(?:branch|tag):\s*"(?<value>[^"]+)"/); +const organizationRegexp = regEx(/organization:\s*"(?<value>[^"]+)"/); const commentMatchRegExp = regEx(/#.*$/); export async function extractPackageFile( @@ -28,27 +35,30 @@ export async function extractPackageFile( } while (!contentArr[lineNumber].includes('end')); let depMatchGroups = depMatchRegExp.exec(depBuffer)?.groups; while (depMatchGroups) { - const { depName, datasource, currentValue, organization } = - depMatchGroups; + const { app, requirement, opts } = depMatchGroups; + const github = githubRegexp.exec(opts)?.groups?.value; + const git = gitRegexp.exec(opts)?.groups?.value; + const ref = refRegexp.exec(opts)?.groups?.value; + const branchOrTag = branchOrTagRegexp.exec(opts)?.groups?.value; + const organization = organizationRegexp.exec(opts)?.groups?.value; - const dep: PackageDependency = { - depName, - currentValue, - }; + let dep: PackageDependency; - dep.datasource = datasource || HexDatasource.id; - - if (dep.datasource === HexDatasource.id) { - dep.currentValue = currentValue; - dep.packageName = depName; - } - - if (organization) { - dep.packageName += ':' + organization; - } - - if (dep.datasource !== HexDatasource.id) { - dep.skipReason = 'non-hex-dep-types'; + if (git ?? github) { + dep = { + depName: app, + currentDigest: ref, + currentValue: branchOrTag, + datasource: git ? GitTagsDatasource.id : GithubTagsDatasource.id, + packageName: git ?? github, + }; + } else { + dep = { + depName: app, + currentValue: requirement, + datasource: HexDatasource.id, + packageName: organization ? `${app}:${organization}` : app, + }; } deps.push(dep); diff --git a/lib/modules/manager/mix/index.ts b/lib/modules/manager/mix/index.ts index 7820dcded9..f48c01ae25 100644 --- a/lib/modules/manager/mix/index.ts +++ b/lib/modules/manager/mix/index.ts @@ -1,4 +1,6 @@ import type { ProgrammingLanguage } from '../../../constants'; +import { GitTagsDatasource } from '../../datasource/git-tags'; +import { GithubTagsDatasource } from '../../datasource/github-tags'; import { HexDatasource } from '../../datasource/hex'; import * as hexVersioning from '../../versioning/hex'; @@ -12,4 +14,8 @@ export const defaultConfig = { versioning: hexVersioning.id, }; -export const supportedDatasources = [HexDatasource.id]; +export const supportedDatasources = [ + GithubTagsDatasource.id, + GitTagsDatasource.id, + HexDatasource.id, +]; -- GitLab