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

Reduce duplication in badge regex/url patterns #2050

Closed
chris48s opened this issue Sep 4, 2018 · 4 comments
Closed

Reduce duplication in badge regex/url patterns #2050

chris48s opened this issue Sep 4, 2018 · 4 comments
Labels
core Server, BaseService, GitHub auth, Shared helpers

Comments

@chris48s
Copy link
Member

chris48s commented Sep 4, 2018

Can we use something like url-pattern or path-to-regexp to define routes in a nicer way so we don't have to define this twice?

relevant discussions:
#2042 (comment)
#1582 (comment)

@chris48s chris48s added the core Server, BaseService, GitHub auth, Shared helpers label Sep 4, 2018
@RedSparr0w
Copy link
Member

RedSparr0w commented Sep 23, 2018

👍 Sounds like a good idea, would definitely help keep everything consistent.

Edit: I like the look of path-to-regexp

@paulmelnikow
Copy link
Member

I played with path-to-regexp a bit back in March. This is based on what I did then:

const pathToRegexp = require('path-to-regexp')

const keys = []
const re = pathToRegexp('/appveyor/ci/:user/:repo/:branch?.:ext(png|gif)', keys, {
  delimiter: '.',
  strict: true,
  sensitive: true,
})

console.log(re)

const result = {}
re.exec('/appveyor/ci/foo/bar/baz.png').slice(1).forEach((match, i) => {
  result[keys[i].name] = match
})
console.log(result)

Output:

/^\/appveyor\/ci\/([^\/]+?)\/([^\/]+?)\/([^\/]+?)?\.(png|gif)$/
{ user: 'foo', repo: 'bar', branch: 'baz', ext: 'png' }

Cool stuff!

The compile() method would be useful in the frontend to build a badge URL from user input. I ran into some issues with that, IIRC. It'd be good to make sure that works before we settle on this library.

@paulmelnikow
Copy link
Member

compile() seems to be working now!

const toPath = pathToRegexp.compile('/appveyor/ci/:user/:repo/:branch?.:ext(png|gif)', {
  delimiter: '.',
  strict: true,
  sensitive: true,
})

console.log(toPath({ user: 'foo', repo: 'bar', branch: 'baz', ext: 'png' }))

What do you think? Should we move forward with this approach?

paulmelnikow added a commit that referenced this issue Nov 6, 2018
This reduces duplication in badge regex/url patterns, and reduce the need to understand regexes in order to create badges.

The badge examples could be harmonized to reduce the duplication even further, though that could be handled in a follow-on PR.

Ref: #2050
paulmelnikow added a commit that referenced this issue Nov 6, 2018
This reduces duplication in badge regex/url patterns, and reduce the need to understand regexes in order to create badges.

The badge examples could be harmonized to reduce the duplication even further, though that could be handled in a follow-on PR.

Ref: #2050
@paulmelnikow
Copy link
Member

#2279 is about ready for merge!

The next step would be to reduce duplication in defining examples, and maybe do this with an eye toward #701 and #776.

paulmelnikow added a commit that referenced this issue Nov 8, 2018
This reduces duplication in badge regex/url patterns, and reduces the need to understand regexes in order to create badges.

Ref: #2050
paulmelnikow added a commit that referenced this issue Nov 12, 2018
This continues the work from #2279, by allowing example badges to be specified using `namedParams`.

Using an object makes it possible for us to display these in form fields down the line. (#701)

Closes #2050.
paulmelnikow added a commit that referenced this issue Nov 17, 2018
This continues the work from #2279, by allowing example badges to be specified using `namedParams`. Using an object makes it possible for us to display these in form fields down the line. (#701)

I've called this the "preferred" way, and labeled the other ways deprecated. I've also added some doc to the `examples` property in BaseService. Then I realized we had some doc in the tutorial, though I think it's fine to have a short version in the tutorial, and the gory detail in BaseService.

I've also added a `pattern` keyword, and made `urlPattern` an alias.

Closes #2050.
@paulmelnikow paulmelnikow mentioned this issue Dec 10, 2018
12 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Server, BaseService, GitHub auth, Shared helpers
Projects
None yet
Development

No branches or pull requests

3 participants