Beispiel #1
0
func Store(m *Merge) (string, error) {
	data, err := json.Marshal(m)
	if err != nil {
		return "", err
	}
	gist := Gist{
		Description: "a bit o' spruce, from http://play.spruce.cf",
		Public:      true,
		Files: map[string]GistFile{
			"spruce.json": GistFile{Content: string(data)},
		},
	}
	encoded, err := json.Marshal(gist)
	if err != nil {
		return "", err
	}
	body := strings.NewReader(string(encoded))
	c := client.New()
	resp, err := c.Post("https://api.github.com/gists", "application/json", body)
	log.Printf("got a response: %v", resp)
	if err != nil {
		return "", err
	}
	if resp.StatusCode != 201 {
		return "", fmt.Errorf("failed to create gist: API returned %s", resp.Status)
	}

	created, err := parseGist(resp)
	if err != nil {
		return "", err
	}
	return created.ID, nil
}
Beispiel #2
0
func Retrieve(key string) (*Merge, error) {
	c := client.New()
	resp, err := c.Get(fmt.Sprintf("https://api.github.com/gists/%s", key))
	if err != nil {
		return nil, err
	}

	if resp.StatusCode != 200 {
		return nil, fmt.Errorf("not found")
	}

	gist, err := parseGist(resp)
	if err != nil {
		return nil, err
	}

	f, ok := gist.Files["spruce.json"]
	if !ok {
		return nil, err
	}

	var m Merge
	err = json.Unmarshal([]byte(f.Content), &m)
	if err != nil {
		return nil, err
	}
	return &m, nil
}
Beispiel #3
0
func Releases(owner, repo string) ([]string, error) {
	c := client.New()
	r, err := c.Get(fmt.Sprintf("https://api.github.com/repos/%s/%s/tags", owner, repo))
	if err != nil {
		return nil, err
	}
	if r.StatusCode != 200 {
		return nil, fmt.Errorf("API %s", r.Status)
	}

	b, err := ioutil.ReadAll(r.Body)
	if err != nil {
		return nil, err
	}

	var data []struct{ Name string }
	err = json.Unmarshal(b, &data)
	if err != nil {
		return nil, err
	}

	versions := make([]string, 0)
	for _, tag := range data {
		if VersionMatch.MatchString(tag.Name) {
			versions = append(versions, tag.Name[1:])
		}
	}
	return versions, nil
}