coder / backstage-plugins

Official Coder plugins for the Backstage platform
34 stars 2 forks source link

Devcontainers plugin: Plugin is not able to process GitLab URLs correctly #117

Closed Parkreiner closed 4 months ago

Parkreiner commented 5 months ago

Issue comes from a user at a prospective company.

Problem

Likely issue

Other notes

Brandon provided a sample catalog-info file that isn't working (though he obviously can't share the full repo):

apiVersion: backstage.io/v1alpha1
kind: Component
metadata:
  namespace: default
  annotations:
    backstage.io/managed-by-location: url:https://my.gitlab.com/devexportal/sandbox/myreactapp/-/blob/main/catalog-info.yaml
    backstage.io/managed-by-origin-location: url:https://my.gitlab.com/devexportal/sandbox/myreactapp/-/blob/main/catalog-info.yaml
    backstage.io/view-url: https://my.gitlab.com/devexportal/sandbox/myreactapp/-/blob/main/catalog-info.yaml
    backstage.io/edit-url: https://my.gitlab.com/devexportal/sandbox/myreactapp/-/edit/main/catalog-info.yaml
    backstage.io/source-location: url:https://my.gitlab.com/devexportal/sandbox/myreactapp/-/blob/main/
    swf-backstage-dev.com/template-source-url: https://my.gitlab.com/devexportal/templates/simple-react-app/-/blob/0.5.0/
    swf-backstage-dev.com/template-source-name: simple-react-app
    gitlab.com/project-slug: devexportal/sandbox/myreactapp
    backstage.io/techdocs-ref: dir:.
    argocd/app-name: myreactapp-main
    argocd/proxy-url: /argocd/api
    backstage.io/kubernetes-namespace: myreactapp
    backstage.io/kubernetes-label-selector: app.kubernetes.io/component=myreactapp
    gitlab.com/instance: my.gitlab.com
  title: myReactApp
  name: myreactapp
  description: Creating React App from Template
  tags:
    - helm
    - kubernetes
    - react
    - typescript
    - vite
    - devcontainers
  links:
    - url: https://my.gitlab.com/devexportal/templates/simple-react-app/-/blob/0.5.0/
      title: Template Source Url
      type: templates
  uid: e375af26-1e87-44aa-b02f-78498e879628
  etag: d26e609289dbb3d09c7550f5c57d17c3f45a9c3e
relations:
  - type: ownedBy
    targetRef: group:default/gitlab-users
    target:
      kind: group
      namespace: default
      name: gitlab-users
  - type: scaffoldedFrom
    targetRef: template:default/simple-react-app
    target:
      kind: template
      namespace: default
      name: simple-react-app
spec:
  type: website
  lifecycle: experimental
  owner: group:default/my-group
  dependsOn: []
  consumesApis: []
  coder:
    templateName: docker
    mode: auto
    params:
      repo: custom
      region: us-east-1
  createdDate: 2024-04-17T17:08:58.204Z

The culprit might be the fact that the backstage.io/source-location property has a value of url:https://my.gitlab.com/devexportal/sandbox/myreactapp/-/blob/main/, and that the /~/blob/main/ at the end shouldn't be there?

We aren't working with this property directly, but it might be shared with the APIs we are using

code-asher commented 5 months ago

This reminds me of the work we are doing on the Coder git-clone module, we have to parse git URLs there as well.

My idea was to have a set of matchers we can use for figuring out the base repository URL and branch, so it could look something like this:

github = [ "{owner}/{repo}/tree/{branch}" "{owner}/{repo}/blob/{branch}" ]
gitlab = [ "{owner}/{repo}/-/tree/{branch}" "{owner}/{repo}/-/blob/{branch}"  ]
bitbucket = [
  "{owner}/{repo}/src/{ignore}?at={branch}"
  "{owner}/{repo}/src/{branch}"
]

Each matcher is tried in order, this syntax is inspired by go-chi but we could do :var if we wanted it Express-like. Or we could just directly write them as regular expressions.

code-asher commented 5 months ago

Or we use something like https://github.com/IonicaBizau/git-url-parse