2019-07-14 16:51:10 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-09-14 18:31:56 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2019-07-14 16:51:10 +00:00
|
|
|
"time"
|
2020-09-14 18:31:56 +00:00
|
|
|
|
|
|
|
"github.com/github-release/github-release/github"
|
2019-07-14 16:51:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-09-14 18:31:56 +00:00
|
|
|
// GET /repos/:owner/:repo/releases/assets/:id
|
|
|
|
// DELETE /repos/:owner/:repo/releases/assets/:id
|
|
|
|
ASSET_URI = "/repos/%s/%s/releases/assets/%d"
|
|
|
|
|
|
|
|
// API: https://developer.github.com/v3/repos/releases/#list-assets-for-a-release
|
|
|
|
// GET /repos/:owner/:repo/releases/:id/assets
|
|
|
|
ASSET_RELEASE_LIST_URI = "/repos/%s/%s/releases/%d/assets"
|
2019-07-14 16:51:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Asset struct {
|
|
|
|
Url string `json:"url"`
|
|
|
|
Id int `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
ContentType string `json:"content_type"`
|
|
|
|
State string `json:"state"`
|
|
|
|
Size uint64 `json:"size"`
|
|
|
|
Downloads uint64 `json:"download_count"`
|
|
|
|
Created time.Time `json:"created_at"`
|
|
|
|
Published time.Time `json:"published_at"`
|
|
|
|
}
|
|
|
|
|
2020-09-14 18:31:56 +00:00
|
|
|
// findAsset returns the asset if an asset with name can be found in assets,
|
|
|
|
// otherwise returns nil.
|
|
|
|
func findAsset(assets []Asset, name string) *Asset {
|
2019-07-14 16:51:10 +00:00
|
|
|
for _, asset := range assets {
|
|
|
|
if asset.Name == name {
|
2020-09-14 18:31:56 +00:00
|
|
|
return &asset
|
2019-07-14 16:51:10 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-14 18:31:56 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete sends a HTTP DELETE request for the given asset to Github. Returns
|
|
|
|
// nil if the asset was deleted OR there was nothing to delete.
|
|
|
|
func (a *Asset) Delete(user, repo, token string) error {
|
|
|
|
URL := nvls(EnvApiEndpoint, github.DefaultBaseURL) +
|
|
|
|
fmt.Sprintf(ASSET_URI, user, repo, a.Id)
|
|
|
|
resp, err := github.DoAuthRequest("DELETE", URL, "application/json", token, nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to delete asset %s (ID: %d), HTTP error: %b", a.Name, a.Id, err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusNoContent {
|
|
|
|
return fmt.Errorf("failed to delete asset %s (ID: %d), status: %s", a.Name, a.Id, resp.Status)
|
|
|
|
}
|
|
|
|
return nil
|
2019-07-14 16:51:10 +00:00
|
|
|
}
|