Code Study: GitHub Desktop / lib / parse-app-url.ts

The code file defines the supported URL action types.

URLActionType

export type URLActionType =
  | IOAuthAction
  | IOpenRepositoryFromURLAction
  | IOpenRepositoryFromPathAction
  | IUnknownAction

export interface IOAuthAction {
  readonly name: 'oauth'
  readonly code: string
  readonly state: string
}

export interface IOpenRepositoryFromURLAction {
  readonly name: 'open-repository-from-url'
  /** the remote repository location associated with the "Open in Desktop" action */
  readonly url: string
  /** the optional branch name which should be checked out. use the default branch otherwise. */
  readonly branch: string | null
  /** the pull request number, if pull request originates from a fork of the repository */
  readonly pr: string | null
  /** the file to open after cloning the repository */
  readonly filepath: string | null
}

export interface IOpenRepositoryFromPathAction {
  readonly name: 'open-repository-from-path'
  /** The local path to open. */
  readonly path: string
}

export interface IUnknownAction {
  readonly name: 'unknown'
  readonly url: string
}

It also defines a parseAppURL function:

parseAppURL

The function parses a given url string to one of the URL action types with extracted parameters for each URL action type.

export function parseAppURL(url: string): URLActionType

The url is in the format of: <protocol>://<actionName>/<parsedPath>?<queryStrings>. Here are the format and example url strings of each URLActionType:

FormatExample URL stringURLActionType
x-github-client://oauth/?code=<code>&state=<state>x-github-desktop://oauth/?code=fjkdsjk&state=jfsdkfkdsIOAuthAction
x-github-client://openRepo/<remote-git-url]>?pr=<pr>&branch=<branch>&filepath=<local-path>x-github-client://openRepo/https://github.com/hello/world?branch=devIOpenRepositoryFromURLAction
x-github-client://openLocalRepo/<local-path>x-github-client://openLocalRepo/D:\hello\worldIOpenRepositoryFromPathAction
tags: GitHub Desktop - Electron