Ejemplo n.º 1
0
// DeleteRepo deletes a repository configuration.
func DeleteRepo(c *gin.Context) {
	var (
		owner = c.Param("owner")
		name  = c.Param("repo")
		user  = session.User(c)
	)
	repo, err := store.GetRepoOwnerName(c, owner, name)
	if err != nil {
		logrus.Errorf("Error getting repository %s. %s", name, err)
		c.AbortWithStatus(404)
		return
	}
	err = store.DeleteRepo(c, repo)
	if err != nil {
		logrus.Errorf("Error deleting repository %s. %s", name, err)
		c.AbortWithStatus(500)
		return
	}
	link := fmt.Sprintf(
		"%s/hook",
		httputil.GetURL(c.Request),
	)
	err = remote.DelHook(c, user, repo, link)
	if err != nil {
		logrus.Errorf("Error deleting repository hook for %s. %s", name, err)
	}
	c.String(200, "")
}
Ejemplo n.º 2
0
// PostRepo activates a new repository.
func PostRepo(c *gin.Context) {
	var (
		owner = c.Param("owner")
		name  = c.Param("repo")
		user  = session.User(c)
	)

	// verify repo doesn't already exist
	if _, err := store.GetRepoOwnerName(c, owner, name); err == nil {
		c.AbortWithStatus(409)
		c.String(409, "Error activating a repository that is already active.")
		return
	}

	repo, err := remote.GetRepo(c, user, owner, name)
	if err != nil {
		c.String(404, "Error finding repository in GitHub. %s")
		return
	}
	repo.UserID = user.ID
	repo.Secret = model.Rand()

	// creates a token to authorize the link callback url
	t := token.New(token.HookToken, repo.Slug)
	sig, err := t.Sign(repo.Secret)
	if err != nil {
		c.String(500, "Error activating repository. %s")
		return
	}

	// create the hook callback url
	link := fmt.Sprintf(
		"%s/hook?access_token=%s",
		httputil.GetURL(c.Request),
		sig,
	)
	err = remote.SetHook(c, user, repo, link)
	if err != nil {
		c.String(500, "Error creating hook. %s", err)
		return
	}

	err = store.CreateRepo(c, repo)
	if err != nil {
		c.String(500, "Error activating the repository. %s", err)
		return
	}
	c.JSON(200, repo)
}
Ejemplo n.º 3
0
func (g *Github) GetUser(res http.ResponseWriter, req *http.Request) (*model.User, error) {

	var config = &oauth2.Config{
		ClientID:     g.Client,
		ClientSecret: g.Secret,
		RedirectURL:  fmt.Sprintf("%s/login", httputil.GetURL(req)),
		Endpoint: oauth2.Endpoint{
			AuthURL:  fmt.Sprintf("%s/login/oauth/authorize", g.URL),
			TokenURL: fmt.Sprintf("%s/login/oauth/access_token", g.URL),
		},
		Scopes: g.Scopes,
	}

	// get the oauth code from the incoming request. if no code is present
	// redirec the user to GitHub login to retrieve a code.
	var code = req.FormValue("code")
	if len(code) == 0 {
		state := fmt.Sprintln(time.Now().Unix())
		http.Redirect(res, req, config.AuthCodeURL(state), http.StatusSeeOther)
		return nil, nil
	}

	// exchanges the oauth2 code for an access token
	token, err := config.Exchange(oauth2.NoContext, code)
	if err != nil {
		return nil, fmt.Errorf("Error exchanging token. %s", err)
	}

	// get the currently authenticated user details for the access token
	client := setupClient(g.API, token.AccessToken)
	user, _, err := client.Users.Get("")
	if err != nil {
		return nil, fmt.Errorf("Error fetching user. %s", err)
	}

	return &model.User{
		Login:  *user.Login,
		Token:  token.AccessToken,
		Avatar: *user.AvatarURL,
	}, nil
}