Skip to content

Commit

Permalink
Handle Gitea repos more intelligently
Browse files Browse the repository at this point in the history
  • Loading branch information
deanishe committed May 1, 2019
1 parent e13bbf1 commit fb48145
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 18 deletions.
6 changes: 3 additions & 3 deletions alfred.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/deanishe/awgo/util"
)


// JXA scripts to call Alfred.
const (
scriptSearch = "Application(%s).search(%s);"
Expand Down Expand Up @@ -45,7 +44,7 @@ type Alfred struct {
// For testing. Set to true to save JXA script to lastScript
// instead of running it.
noRunScripts bool
lastScript string
lastScript string
}

// NewAlfred creates a new Alfred from the environment.
Expand Down Expand Up @@ -152,9 +151,10 @@ func (a *Alfred) runScript(script string, arg ...interface{}) error {

// Name of JXA Application for running Alfred
func scriptAppName() string {
// Alfred 3
if strings.HasPrefix(os.Getenv(EnvVarAlfredVersion), "3") {
return "Alfred 3"
}
// Alfred 4+
return "com.runningwithcrayons.Alfred"
}

4 changes: 2 additions & 2 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/*
Package aw is a "plug-and-play" workflow development library/framework for
Alfred 3 (https://www.alfredapp.com/)
Alfred 3 & 4 (https://www.alfredapp.com/)
It combines features for interacting with Alfred (feedback, settings,
AppleScript API) with simple APIs for common workflow functionality
Expand Down Expand Up @@ -45,7 +45,7 @@ of AwGo and useful workflow idioms.
Features
As of AwGo 0.15, all applicable features of Alfred 3.7 are supported.
As of AwGo 0.15, all applicable features of Alfred 4.0 are supported.
The main features are:
Expand Down
14 changes: 10 additions & 4 deletions env.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
# Workflow environment variables
# These variables create an Alfred-like environment
# root="$( dirname "$0" )"

root="$( git rev-parse --show-toplevel )"
testdir="${root}/testenv"

# Absolute bare-minimum for AwGo to function...
export alfred_workflow_bundleid="net.deanishe.awgo"
export alfred_workflow_cache="$HOME/Library/Caches/com.runningwithcrayons.Alfred/Workflow Data/net.deanishe.awgo"
export alfred_workflow_data="$HOME/Library/Application Support/Alfred/Workflow Data/net.deanishe.awgo"
export alfred_workflow_data="${testdir}/data"
export alfred_workflow_cache="${testdir}/cache"
# export alfred_workflow_cache="$HOME/Library/Caches/com.runningwithcrayons.Alfred/Workflow Data/net.deanishe.awgo"
# export alfred_workflow_data="$HOME/Library/Application Support/Alfred/Workflow Data/net.deanishe.awgo"

test -f "$HOME/Library/Preferences/com.runningwithcrayons.Alfred.plist" || {
export alfred_version="3.8.1"
export alfred_workflow_cache="$HOME/Library/Caches/com.runningwithcrayons.Alfred-3/Workflow Data/net.deanishe.awgo"
export alfred_workflow_data="$HOME/Library/Application Support/Alfred 3/Workflow Data/net.deanishe.awgo"
# export alfred_workflow_cache="$HOME/Library/Caches/com.runningwithcrayons.Alfred-3/Workflow Data/net.deanishe.awgo"
# export alfred_workflow_data="$HOME/Library/Application Support/Alfred 3/Workflow Data/net.deanishe.awgo"
}

# Expected by ExampleNew
Expand Down
2 changes: 1 addition & 1 deletion run-tests.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env zsh

root="$( cd "$( dirname "$0" )"; pwd )"
root="$( git rev-parse --show-toplevel )"
testdir="${root}/testenv"
iplist="${root}/info.plist"
covfile="${root}/coverage.out"
Expand Down
25 changes: 24 additions & 1 deletion update/gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,33 @@ func (gr *giteaReleaser) Releases() ([]*Release, error) {
}

func (gr *giteaReleaser) url() *url.URL {
u, _ := url.Parse(gr.Repo + "/releases")
if gr.Repo == "" {
return nil
}
u, err := url.Parse(gr.Repo)
if err != nil {
return nil
}
// If no scheme is specified, assume HTTPS and re-parse URL.
// This is necessary because URL.Host isn't present on URLs
// without a scheme (hostname is added to path)
if u.Scheme == "" {
u.Scheme = "https"
u, err = url.Parse(u.String())
if err != nil {
return nil
}
}
if u.Host == "" {
return nil
}
path := strings.Split(strings.Trim(u.Path, "/"), "/")
if len(path) != 2 {
return nil
}

u.Path = fmt.Sprintf("/api/v1/repos/%s/%s/releases", path[0], path[1])

return u
}

Expand Down
35 changes: 28 additions & 7 deletions update/gitea_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,34 @@ func makeGiteaReleaser() *giteaReleaser {
func TestGiteaURL(t *testing.T) {
t.Parallel()

var (
gr = &giteaReleaser{Repo: "git.deanishe.net/deanishe/nonexistent"}
x = "https://git.deanishe.net/deanishe/nonexistent/releases"
v = gr.url().String()
)
if v != x {
t.Errorf("Bad repo URL. Expected=%v, Got=%v", x, v)
data := []struct {
repo string
url string
}{
// Invalid input
{"", ""},
{"https://git.deanishe.net/api/v1/repos/deanishe/nonexistent/releases", ""},
{"git.deanishe.net/deanishe", ""},
// Valid URLs
{"git.deanishe.net/deanishe/nonexistent", "https://git.deanishe.net/api/v1/repos/deanishe/nonexistent/releases"},
{"https://git.deanishe.net/deanishe/nonexistent", "https://git.deanishe.net/api/v1/repos/deanishe/nonexistent/releases"},
{"http://git.deanishe.net/deanishe/nonexistent", "http://git.deanishe.net/api/v1/repos/deanishe/nonexistent/releases"},
}

for _, td := range data {
gr := &giteaReleaser{Repo: td.repo}
u := gr.url()
if u == nil {
if td.url != "" {
t.Errorf("Bad API URL for %q. Expected=%q, Got=nil", td.repo, td.url)
}
continue
}

v := gr.url().String()
if v != td.url {
t.Errorf("Bad API URL. Expected=%v, Got=%v", td.url, v)
}
}
}

Expand Down

0 comments on commit fb48145

Please sign in to comment.