func setup() {
	mux = http.NewServeMux()
	server = httptest.NewServer(mux)

	client = octokat.NewClient()
	client.BaseURL = server.URL
}
Example #2
0
func main() {

	app := cli.NewApp()

	app.Name = "pulls"
	app.Usage = "Manage github pull requests for project maintainers"
	app.Version = "0.0.1"

	client := gh.NewClient()

	org, name, err := gordon.GetOriginUrl()
	if err != nil {
		fmt.Fprintf(os.Stderr, "The current directory is not a valid git repository.\n")
		os.Exit(1)
	}
	t, err := gordon.NewMaintainerManager(client, org, name)
	if err != nil {
		gordon.WriteError("%s", err)
	}
	m = t

	loadCommands(app)

	app.Run(os.Args)
}
Example #3
0
func voteIssuesFilter(issues []*gh.Issue, numVotes int, err error) ([]*gh.Issue, error) {
	if err != nil {
		return nil, err
	}

	out := []*gh.Issue{}
	for _, issue := range issues {
		fmt.Printf(".")
		client := gh.NewClient()
		org, name, err := gordon.GetOriginUrl()
		if err != nil {
			panic(err)
		}
		t, err := gordon.NewMaintainerManager(client, org, name)
		if err != nil {
			panic(err)
		}
		comments, err := t.GetComments(strconv.Itoa(issue.Number))
		if err != nil {
			return nil, err
		}
		issue.Comments = 0
		for _, comment := range comments {
			if strings.Contains(comment.Body, "+1") {
				issue.Comments += 1
			}
		}
		if issue.Comments >= numVotes {
			out = append(out, issue)
		}
	}
	return out, nil
}
Example #4
0
func noMergePullRequestsFilter(prs []*gh.PullRequest, err error) ([]*gh.PullRequest, error) {
	if err != nil {
		return nil, err
	}

	out := []*gh.PullRequest{}
	for _, pr := range prs {
		fmt.Printf(".")
		// We have to fetch the single pr to get the merge state
		// it sucks but we have to do it
		client := gh.NewClient()
		org, name, err := gordon.GetOriginUrl()
		if err != nil {
			panic(err)
		}
		t, err := gordon.NewMaintainerManager(client, org, name)
		if err != nil {
			panic(err)
		}
		pr, _, err := t.GetPullRequest(strconv.Itoa(pr.Number), false)
		if err != nil {
			return nil, err
		}
		if !pr.Mergeable {
			out = append(out, pr)
		}
	}
	return out, nil
}
Example #5
0
func (c Config) updateGithubStatus(repoName, context, sha, state, desc, buildURL string) error {
	// parse git repo for username
	// and repo name
	r := strings.SplitN(repoName, "/", 2)
	if len(r) < 2 {
		return fmt.Errorf("repo name could not be parsed: %s", repoName)
	}

	// initialize github client
	gh := octokat.NewClient()
	gh = gh.WithToken(c.GHToken)
	repo := octokat.Repo{
		Name:     r[1],
		UserName: r[0],
	}

	status := &octokat.StatusOptions{
		State:       state,
		Description: desc,
		URL:         buildURL,
		Context:     context,
	}
	if _, err := gh.SetStatus(repo, sha, status); err != nil {
		return fmt.Errorf("setting status for repo: %s, sha: %s failed: %v", repoName, sha, err)
	}

	logrus.Infof("Setting status on %s %s to %s for %s succeeded", repoName, sha, state, context)
	return nil
}
Example #6
0
func lgtmPullRequestsFilter(prs []*gh.PullRequest, err error) ([]*gh.PullRequest, error) {
	if err != nil {
		return nil, err
	}

	for _, pr := range prs {
		fmt.Printf(".")
		client := gh.NewClient()
		org, name, err := gordon.GetOriginUrl()
		if err != nil {
			panic(err)
		}
		t, err := gordon.NewMaintainerManager(client, org, name)
		if err != nil {
			panic(err)
		}
		comments, err := t.GetComments(strconv.Itoa(pr.Number))
		if err != nil {
			return nil, err
		}
		pr.ReviewComments = 0
		maintainersOccurrence := map[string]bool{}
		for _, comment := range comments {
			// We should check it this LGTM is by a user in
			// the maintainers file
			userName := comment.User.Login
			if strings.Contains(comment.Body, "LGTM") && t.IsMaintainer(userName) && !maintainersOccurrence[userName] {
				maintainersOccurrence[userName] = true
				pr.ReviewComments += 1
			}
		}
	}
	return prs, nil
}
Example #7
0
func (c Config) getFailedPRs(context, repoName string) (nums []int, err error) {
	// parse git repo for username
	// and repo name
	r := strings.SplitN(repoName, "/", 2)
	if len(r) < 2 {
		return nums, fmt.Errorf("repo name could not be parsed: %s", repoName)
	}

	// initialize github client
	gh := octokat.NewClient()
	gh = gh.WithToken(c.GHToken)
	repo := octokat.Repo{
		Name:     r[1],
		UserName: r[0],
	}

	// get pull requests
	prs, err := gh.PullRequests(repo, &octokat.Options{
		Params: map[string]string{
			"state":    "open",
			"per_page": "100",
		},
	})
	if err != nil {
		return nums, fmt.Errorf("requesting open repos for %s failed: %v", repoName, err)
	}

	for _, pr := range prs {
		if !hasStatus(gh, repo, pr.Head.Sha, context) {
			nums = append(nums, pr.Number)
		}
	}

	return nums, nil
}
Example #8
0
func (c Config) getShas(owner, name, context string, number int) (shas []string, pr *octokat.PullRequest, err error) {
	// initialize github client
	gh := octokat.NewClient()
	gh = gh.WithToken(c.GHToken)
	repo := octokat.Repo{
		Name:     name,
		UserName: owner,
	}

	// get the pull request so we can get the commits
	pr, err = gh.PullRequest(repo, strconv.Itoa(number), &octokat.Options{})
	if err != nil {
		return shas, pr, fmt.Errorf("getting pull request %d for %s/%s failed: %v", number, owner, name, err)
	}

	// check which commits we want to get
	// from the original flag --build-commits
	if c.BuildCommits == "all" || c.BuildCommits == "new" {

		// get the commits url
		req, err := http.Get(pr.CommitsURL)
		if err != nil {
			return shas, pr, err
		}
		defer req.Body.Close()

		// parse the response
		var commits []Commit
		decoder := json.NewDecoder(req.Body)
		if err := decoder.Decode(&commits); err != nil {
			return shas, pr, fmt.Errorf("parsing the response from %s failed: %v", pr.CommitsURL, err)
		}

		// append the commit shas
		for _, commit := range commits {
			// if we only want the new shas
			// check to make sure the status
			// has not been set before appending
			if c.BuildCommits == "new" {
				if hasStatus(gh, repo, commit.Sha, context) {
					continue
				}
			}

			shas = append(shas, commit.Sha)
		}
	} else {
		// this is the case where buildCommits == "last"
		// just get the sha of the pr
		shas = append(shas, pr.Head.Sha)
	}

	return shas, pr, nil
}
Example #9
0
func FilterIssues(c *cli.Context, issues []*gh.Issue) ([]*gh.Issue, error) {
	var (
		yesterday      = time.Now().Add(-24 * time.Hour)
		out            = []*gh.Issue{}
		client         = gh.NewClient()
		org, name, err = gordon.GetRemoteUrl(c.String("remote"))
	)
	if err != nil {
		return nil, err
	}
	t, err := gordon.NewMaintainerManager(client, org, name)
	if err != nil {
		return nil, err
	}

	for _, issue := range issues {
		fmt.Printf(".")

		if c.Bool("new") && !issue.CreatedAt.After(yesterday) {
			continue
		}

		if milestone := c.String("milestone"); milestone != "" && issue.Milestone.Title != milestone {
			continue
		}

		if numVotes := c.Int("votes"); numVotes > 0 {
			comments, err := t.GetComments(strconv.Itoa(issue.Number))
			if err != nil {
				return nil, err
			}
			issue.Comments = 0
			for _, comment := range comments {
				if strings.Contains(comment.Body, "+1") {
					issue.Comments += 1
				}
			}
			if issue.Comments < numVotes {
				continue
			}
		}

		if c.Bool("proposals") && !strings.HasPrefix(issue.Title, "Proposal") {
			continue
		}

		out = append(out, issue)
	}
	return out, nil

}
Example #10
0
func before(c *cli.Context) error {
	client := gh.NewClient()

	org, name, err := gordon.GetRemoteUrl(c.String("remote"))
	if err != nil {
		return fmt.Errorf("The current directory is not a valid git repository (%s).\n", err)
	}
	t, err := gordon.NewMaintainerManager(client, org, name)
	if err != nil {
		return err
	}
	m = t
	return nil
}
Example #11
0
// Client initializes the authorization with the GitHub API
func (g GitHub) Client() *octokat.Client {
	var cache httpcache.Cache
	if cachePath := os.Getenv("GITHUB_CACHE_PATH"); cachePath != "" {
		cache = diskcache.New(cachePath)
	} else {
		cache = httpcache.NewMemoryCache()
	}
	tr := httpcache.NewTransport(cache)

	c := &http.Client{Transport: tr}

	gh := octokat.NewClient()
	gh = gh.WithToken(g.AuthToken)
	gh = gh.WithHTTPClient(c)
	return gh
}
Example #12
0
File: main.go Project: askb/gordon
func before(c *cli.Context) error {
	client := gh.NewClient()

	// set up the git remote to be used
	org, name, err := gordon.GetRemoteUrl(c.String("remote"))
	if err != nil {
		return fmt.Errorf("The current directory is not a valid git repository (%s).\n", err)
	}
	t, err := gordon.NewMaintainerManager(client, org, name)
	if err != nil {
		return err
	}
	m = t

	// Set verbosity
	gordon.VerboseOutput = c.Bool("verbose")

	return nil
}
Example #13
0
func main() {
	// set log level
	if debug {
		logrus.SetLevel(logrus.DebugLevel)
	}

	if version {
		fmt.Println(VERSION)
		return
	}

	// set up github token auth
	gh := octokat.NewClient()
	gh = gh.WithToken(ghtoken)

	// process the queue
	if err := ProcessQueue(&Handler{gh}, QueueOptsFromContext(topic, channel, lookupd)); err != nil {
		logrus.Fatal(err)
	}
}
Example #14
0
func main() {
	app := cli.NewApp()

	app.Name = "issues"
	app.Usage = "Manage github issues"
	app.Version = "0.0.1"

	client := gh.NewClient()

	org, name, err := pulls.GetOriginUrl()
	if err != nil {
		panic(err)
	}
	t, err := pulls.NewMaintainer(client, org, name)
	if err != nil {
		panic(err)
	}
	m = t

	loadCommands(app)

	app.Run(os.Args)
}
Example #15
0
func (c Config) addGithubComment(repoName, pr, comment string) error {
	// parse git repo for username
	// and repo name
	r := strings.SplitN(repoName, "/", 2)
	if len(r) < 2 {
		return fmt.Errorf("repo name could not be parsed: %s", repoName)
	}

	// initialize github client
	gh := octokat.NewClient()
	gh = gh.WithToken(c.GHToken)
	repo := octokat.Repo{
		Name:     r[1],
		UserName: r[0],
	}

	// add comment to the PR
	if _, err := gh.AddComment(repo, pr, comment); err != nil {
		return fmt.Errorf("adding comment to %s#%s failed: %v", repoName, pr, err)
	}

	return nil
}
Example #16
0
func main() {

	app := cli.NewApp()

	app.Name = "pulls"
	app.Usage = "Manage github pull requests for project maintainers"
	app.Version = "0.0.1"

	client := gh.NewClient()

	org, name, err := pulls.GetOriginUrl()
	if err != nil {
		pulls.WriteError("%s", err)
	}
	t, err := pulls.NewMaintainer(client, org, name)
	if err != nil {
		pulls.WriteError("%s", err)
	}
	m = t

	loadCommands(app)

	app.Run(os.Args)
}
Example #17
0
// Client initializes the authorization with the GitHub API
func (g GitHub) Client() *octokat.Client {
	gh := octokat.NewClient()
	gh = gh.WithToken(g.AuthToken)
	return gh
}
Example #18
0
func newHandler(ghToken string) *Handler {
	gh := octokat.NewClient()
	gh = gh.WithToken(ghToken)

	return &Handler{gh}
}