From acf2a33a683fca30fee887284005238e7fcbc07c Mon Sep 17 00:00:00 2001 From: Jguer Date: Mon, 1 May 2017 02:23:03 +0100 Subject: [PATCH] Added repo parsing --- aur/github/github.go | 30 ++++++++++++++++++++++++++++++ aur/github/github_test.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 aur/github/github.go create mode 100644 aur/github/github_test.go diff --git a/aur/github/github.go b/aur/github/github.go new file mode 100644 index 0000000..b32a942 --- /dev/null +++ b/aur/github/github.go @@ -0,0 +1,30 @@ +package github + +import "strings" + +// Branches contains the information of a repository branch +type Branches []struct { + Name string `json:"name"` + Commit struct { + Sha string `json:"sha"` + URL string `json:"url"` + } `json:"commit"` +} + +const repoAPI = "https://api.github.com/repos/{USER}/{REPOSITORY}/branches" + +func parseSource(source string) (owner string, repo string) { + split := strings.Split(source, "github.com/") + if len(split) > 1 { + secondSplit := strings.Split(split[1], "/") + if len(secondSplit) > 1 { + owner = secondSplit[0] + thirdSplit := strings.Split(secondSplit[1], ".git") + if len(thirdSplit) > 0 { + repo = thirdSplit[0] + } + } + + } + return +} diff --git a/aur/github/github_test.go b/aur/github/github_test.go new file mode 100644 index 0000000..9f2027d --- /dev/null +++ b/aur/github/github_test.go @@ -0,0 +1,33 @@ +package github + +import ( + "testing" +) + +func TestParsing(t *testing.T) { + type source struct { + sourceurl string + owner string + repo string + } + + neovim := source{sourceurl: "git+https://github.com/neovim/neovim.git"} + neovim.owner, neovim.repo = parseSource(neovim.sourceurl) + + if neovim.owner != "neovim" || neovim.repo != "neovim" { + t.Fatalf("Expected to find neovim/neovim, found %+v/%+v", neovim.owner, neovim.repo) + } + + yay := source{sourceurl: "git://github.com/jguer/yay.git#branch=master"} + yay.owner, yay.repo = parseSource(yay.sourceurl) + if yay.owner != "jguer" || yay.repo != "yay" { + t.Fatalf("Expected to find jguer/yay, found %+v/%+v", yay.owner, yay.repo) + } + + ack := source{sourceurl: "git://github.com/davidgiven/ack"} + ack.owner, ack.repo = parseSource(ack.sourceurl) + if ack.owner != "davidgiven" || ack.repo != "ack" { + t.Fatalf("Expected to find davidgiven/ack, found %+v/%+v", ack.owner, ack.repo) + } + +}