-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
225 additions
and
9 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,51 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
) | ||
|
||
type Assets struct { | ||
Name string `json:"name"` | ||
AssetId int `json:"id"` | ||
} | ||
|
||
type DeleteTarget Assets | ||
|
||
const ( | ||
// GET /repos/:owner/:repo/releases/:id/assets | ||
LIST_ASSET_URL = "https://api.github.com/repos/%s/%s/releases/%d/assets" | ||
) | ||
|
||
func listAssetsURL(info *Info) string { | ||
return fmt.Sprintf(LIST_ASSET_URL, info.OwnerName, info.RepoName, info.ID) | ||
} | ||
|
||
// DeleteTargets extract asset IDs which is already uploaded | ||
// It decide `already uploaded` based on filename to upload | ||
func SearchDeleteTargets(r io.Reader, uploads []string) ([]DeleteTarget, error) { | ||
|
||
targets := []DeleteTarget{} | ||
body, err := ioutil.ReadAll(r) | ||
if err != nil { | ||
return targets, err | ||
} | ||
|
||
var assetsUploaded []Assets | ||
err = json.Unmarshal(body, &assetsUploaded) | ||
if err != nil { | ||
return targets, err | ||
} | ||
|
||
for _, upload := range uploads { | ||
for _, asset := range assetsUploaded { | ||
if upload == asset.Name { | ||
targets = append(targets, | ||
DeleteTarget{Name: asset.Name, AssetId: asset.AssetId}) | ||
} | ||
} | ||
} | ||
return targets, 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,61 @@ | ||
package main | ||
|
||
import ( | ||
. "github.com/onsi/gomega" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestListAssetURL(t *testing.T) { | ||
RegisterTestingT(t) | ||
|
||
info := &Info{ | ||
OwnerName: "tcnksm", | ||
RepoName: "ghr", | ||
ID: 1234, | ||
} | ||
|
||
url := listAssetsURL(info) | ||
Expect(url).To(Equal("https://api.github.com/repos/tcnksm/ghr/releases/1234/assets")) | ||
} | ||
|
||
func TestExtractDeleteTarget(t *testing.T) { | ||
RegisterTestingT(t) | ||
|
||
uploads := []string{"example1.zip", "example3.zip"} | ||
|
||
json := ` | ||
[ | ||
{ | ||
"url": "https://api.github.com/repos/octocat/Hello-World/releases/assets/1", | ||
"browser_download_url": "https://github.com/octocat/Hello-World/releases/download/v1.0.0/example1.zip", | ||
"id": 1, | ||
"name": "example1.zip", | ||
"label": "short description", | ||
"state": "uploaded" | ||
}, | ||
{ | ||
"url": "https://api.github.com/repos/octocat/Hello-World/releases/assets/2", | ||
"browser_download_url": "https://github.com/octocat/Hello-World/releases/download/v1.0.0/example2.zip", | ||
"id": 2, | ||
"name": "example2.zip", | ||
"label": "short description", | ||
"state": "uploaded" | ||
}, | ||
{ | ||
"url": "https://api.github.com/repos/octocat/Hello-World/releases/assets/3", | ||
"browser_download_url": "https://github.com/octocat/Hello-World/releases/download/v1.0.0/example3.zip", | ||
"id": 3, | ||
"name": "example3.zip", | ||
"label": "short description", | ||
"state": "uploaded" | ||
} | ||
] | ||
` | ||
targets, err := SearchDeleteTargets(strings.NewReader(json), uploads) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(targets).To( | ||
Equal([]DeleteTarget{ | ||
{Name: "example1.zip", AssetId: 1}, | ||
{Name: "example3.zip", AssetId: 3}})) | ||
} |
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
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