-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ensure unique GH_PROJECT/GH_TAGNAME (ports 241637)
- Loading branch information
Showing
11 changed files
with
225 additions
and
68 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package apis | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
) | ||
|
||
func get(url string) ([]byte, error) { | ||
resp, err := http.Get(url) | ||
if err != nil { | ||
return nil, fmt.Errorf("GET %s: %v", url, err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
switch resp.StatusCode { | ||
case http.StatusOK: | ||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, fmt.Errorf("GET %s: %v", url, err) | ||
} | ||
return body, nil | ||
default: | ||
body, _ := ioutil.ReadAll(resp.Body) | ||
return nil, fmt.Errorf("GET %s: %d, body: %v", url, resp.StatusCode, string(body)) | ||
} | ||
} |
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,28 @@ | ||
package apis | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/url" | ||
) | ||
|
||
type GithubCommit struct { | ||
ID string `json:"sha"` | ||
} | ||
|
||
func GetGithubCommit(account, project, ref string) (*GithubCommit, error) { | ||
projectID := fmt.Sprintf("%s/%s", url.PathEscape(account), url.PathEscape(project)) | ||
url := fmt.Sprintf("https://api.github.com/repos/%s/commits/%s", projectID, ref) | ||
|
||
resp, err := get(url) | ||
if err != nil { | ||
return nil, fmt.Errorf("error getting commit %s for %s/%s: %v", ref, account, project, err) | ||
} | ||
|
||
var ret GithubCommit | ||
if err := json.Unmarshal(resp, &ret); err != nil { | ||
return nil, fmt.Errorf("error unmarshalling: %v, resp: %v", err, string(resp)) | ||
} | ||
|
||
return &ret, nil | ||
} |
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,24 @@ | ||
// +build online | ||
|
||
package apis | ||
|
||
import "testing" | ||
|
||
func TestGetGithubCommit(t *testing.T) { | ||
examples := []struct { | ||
site, account, project, ref, ID string | ||
}{ | ||
{"https://gitlab.com", "dmgk", "modules2tuple", "v1.9.0", "fc09878b93db35aafc74311f7ea6684ac08a3b83"}, | ||
{"https://gitlab.com", "dmgk", "modules2tuple", "a0cdb416ca2c", "a0cdb416ca2cbf6d3dad67a97f4fdcfac954503e"}, | ||
} | ||
|
||
for i, x := range examples { | ||
c, err := GetGithubCommit(x.account, x.project, x.ref) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if x.ID != c.ID { | ||
t.Errorf("expected commit ID %s, got %s (example %d)", x.ID, c.ID, i) | ||
} | ||
} | ||
} |
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,28 @@ | ||
package apis | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/url" | ||
) | ||
|
||
type GitlabCommit struct { | ||
ID string `json:"id"` | ||
} | ||
|
||
func GetGitlabCommit(site, account, project, commit string) (*GitlabCommit, error) { | ||
projectID := url.PathEscape(fmt.Sprintf("%s/%s", account, project)) | ||
url := fmt.Sprintf("%s/api/v4/projects/%s/repository/commits/%s", site, projectID, commit) | ||
|
||
resp, err := get(url) | ||
if err != nil { | ||
return nil, fmt.Errorf("error getting commit %s for %s/%s: %v", commit, account, project, err) | ||
} | ||
|
||
var ret GitlabCommit | ||
if err := json.Unmarshal(resp, &ret); err != nil { | ||
return nil, fmt.Errorf("error unmarshalling: %v, resp: %v", err, string(resp)) | ||
} | ||
|
||
return &ret, nil | ||
} |
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 |
---|---|---|
@@ -1,24 +1,24 @@ | ||
// +build online | ||
|
||
package gitlab | ||
package apis | ||
|
||
import "testing" | ||
|
||
func TestGetCommit(t *testing.T) { | ||
func TestGetGitlabCommit(t *testing.T) { | ||
examples := []struct { | ||
site, account, project, tag, commitID string | ||
site, account, project, ref, ID string | ||
}{ | ||
{"https://gitlab.com", "gitlab-org", "gitaly-proto", "v1.32.0", "f4db5d05d437abe1154d7308ca044d3577b5ccba"}, | ||
{"https://gitlab.com", "gitlab-org", "labkit", "0c3fc7cdd57c", "0c3fc7cdd57c57da5ab474aa72b6640d2bdc9ebb"}, | ||
} | ||
|
||
for i, x := range examples { | ||
c, err := GetCommit(x.site, x.account, x.project, x.tag) | ||
c, err := GetGitlabCommit(x.site, x.account, x.project, x.ref) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if x.commitID != c.ID { | ||
t.Errorf("expected commitID %s, got %s (example %d)", x.commitID, c.ID, i) | ||
if x.ID != c.ID { | ||
t.Errorf("expected commit ID %s, got %s (example %d)", x.ID, c.ID, i) | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module github.com/dmgk/modules2tuple | ||
|
||
go 1.12 | ||
go 1.13 |
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,28 @@ | ||
// +build online | ||
|
||
package tuple | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestUniqueProjectAndTag(t *testing.T) { | ||
given := ` | ||
# github.com/json-iterator/go v1.1.7 | ||
# github.com/ugorji/go v1.1.7` | ||
|
||
expected := `GH_TUPLE= \ | ||
json-iterator:go:v1.1.7:json_iterator_go/vendor/github.com/json-iterator/go \ | ||
ugorji:go:23ab95ef5dc3:ugorji_go/vendor/github.com/ugorji/go | ||
` | ||
|
||
tt, err := NewParser("vendor", false).Read(strings.NewReader(given)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
out := tt.String() | ||
if out != expected { | ||
t.Errorf("expected output %s, got %s", expected, out) | ||
} | ||
} |
Oops, something went wrong.