-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ability to download repos from SCC added
- Loading branch information
Showing
6 changed files
with
206 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package get | ||
|
||
import ( | ||
"encoding/base64" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
// SCCURLs returns URLs for repos in SCC | ||
func SCCURLs(baseURL string, username string, password string, nameFilters []string, descriptionFilters []string) (urls []string, err error) { | ||
urls = []string{} | ||
|
||
token := base64.URLEncoding.EncodeToString([]byte(username + ":" + password)) | ||
|
||
fmt.Println("Repos available in SCC follow:") | ||
next := baseURL + "/connect/organizations/repositories" | ||
for { | ||
var page []byte | ||
page, next, err = _downloadPaged(next, token) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
type Repo struct { | ||
URL string | ||
Name string | ||
Description string | ||
DistroTarget string `json:"distro_target"` | ||
} | ||
|
||
var repos []Repo | ||
err := json.Unmarshal(page, &repos) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, repo := range repos { | ||
fmt.Printf(" %s: %s\n", repo.Name, repo.Description) | ||
if _matches(repo.Name, repo.Description, nameFilters, descriptionFilters) { | ||
urls = append(urls, repo.URL) | ||
} | ||
} | ||
|
||
if next == "" { | ||
break | ||
} | ||
} | ||
fmt.Println(urls) | ||
|
||
return | ||
} | ||
|
||
func _matches(name string, description string, nameFilters []string, descriptionFilters []string) bool { | ||
for _, nameFilter := range nameFilters { | ||
if strings.Contains(name, nameFilter) { | ||
for _, descriptionFilter := range descriptionFilters { | ||
if strings.Contains(description, descriptionFilter) { | ||
return true | ||
} | ||
} | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func _downloadPaged(url string, token string) (page []byte, next string, err error) { | ||
req, err := http.NewRequest("GET", url, nil) | ||
if err != nil { | ||
return | ||
} | ||
|
||
req.Header.Add("Authorization", fmt.Sprintf("Basic %s", token)) | ||
req.Header.Add("Accept", "application/vnd.scc.suse.com.v4+json") | ||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return | ||
} | ||
|
||
if resp.StatusCode != 200 { | ||
err = &UnexpectedStatusCodeError{url, resp.StatusCode} | ||
return | ||
} | ||
|
||
page, err = ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return | ||
} | ||
|
||
re := regexp.MustCompile("<([^>]+)>; rel=\"next\"") | ||
matches := re.FindStringSubmatch(resp.Header["Link"][0]) | ||
if matches != nil { | ||
next = matches[1] | ||
} | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package get | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
) | ||
|
||
func TestSCCURLs(t *testing.T) { | ||
// Respond to http://localhost:8080/test with "Hello, World" | ||
http.HandleFunc("/connect/organizations/repositories", func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Link", "<http://localhost:8080/connect/organizations/repositories2>; rel=\"next\"") | ||
fmt.Fprintf(w, "[{\"url\" : \"test\", \"name\" : \"test\", \"description\" : \"test\"}]") | ||
}) | ||
|
||
http.HandleFunc("/connect/organizations/repositories2", func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Link", "") | ||
fmt.Fprintf(w, "[{\"url\" : \"test2\", \"name\" : \"test2\", \"description\" : \"test2\"}]") | ||
}) | ||
|
||
urls, err := SCCURLs("http://localhost:8080", "user", "pass", []string{"test2"}, []string{""}) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
if len(urls) != 1 { | ||
t.Error("expected 1 url") | ||
} | ||
|
||
if urls[0] != "test2" { | ||
t.Error("expected test2, got " + urls[0]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters