Skip to content

Instantly share code, notes, and snippets.

@tmiller
Created March 4, 2013 22:48
Show Gist options
  • Save tmiller/5086379 to your computer and use it in GitHub Desktop.
Save tmiller/5086379 to your computer and use it in GitHub Desktop.
/*
Package pivotaltracker implements a wrapper around Pivotal Tracker's API.
*/
package main
import (
"encoding/xml"
"fmt"
"io/ioutil"
"net/http"
)
func main() {
projects := AllProjects()
found := make(chan Story)
for _, project := range projects {
go FindStory(project.Id, 43689065, found)
}
story := <-found
fmt.Printf("[#%d] %s\n\n\n%s\n", story.Id, story.Name, story.Url)
}
// Projects holds a collection of type Project unmarshalled from XML
type Projects struct {
XMLName xml.Name `xml:"projects"`
ProjectList []Project `xml:"project"`
}
// Project represents a Pivotal Tracker project
type Project struct {
Id int `xml:"id"`
}
// Story represents a Pivotal Tracker story
type Story struct {
Id int `xml:"id"`
Name string `xml:"name"`
Url string `xml:"url"`
}
// Calls Pivotal Tracker and finds a story for the given story_id and
// project_id.
func FindStory(projectId, storyId int, found chan Story) {
findStory := fmt.Sprintf("projects/%d/stories/%d", projectId, storyId)
response, err := callPivotalTracker(findStory)
if err != nil {
fmt.Println(err)
}
var story Story
xml.Unmarshal(response, &story)
if story.Id != 0 {
found <- story
}
}
// Calls Pivotal Tracker and returns a list of your projects.
func AllProjects() []Project {
response, err := callPivotalTracker("projects")
if err != nil {
fmt.Println(err)
}
var results Projects
xml.Unmarshal(response, &results)
return results.ProjectList
}
// Sends a command to Pivotal Tracker and returns XML representation of the
// response.
func callPivotalTracker(command string) (response []byte, err error) {
client := new(http.Client)
url := "https://www.pivotaltracker.com/services/v3/" + command
request, err := http.NewRequest("GET", url, nil)
if err != nil {
return
}
apiKey := "API KEY HERE"
request.Header.Add("X-TrackerToken", apiKey)
resp, err := client.Do(request)
if err != nil {
return
}
defer resp.Body.Close()
response, err = ioutil.ReadAll(resp.Body)
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment