//GithubLoginCallback Called by github after authorization is granted
func GithubLoginCallback(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
	log.Println("GithubLoginCallback")
	state := r.FormValue("state")
	if state != oauth.RandomString {
		log.Printf("invalid oauth state, expected '%s', got '%s'\n", oauth.RandomString, state)
		http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
		return
	}

	code := r.FormValue("code")
	// Convert the auth code into a token
	token, err := oauth.Conf.Exchange(oauth2.NoContext, code)
	if err != nil {
		log.Printf("oauthConf.Exchange() failed with '%s'\n", err)
		http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
		return
	}

	// Get a client that uses the auth token
	oauthClient := oauth.Conf.Client(oauth2.NoContext, token)
	githubClient := github.NewClient(oauthClient)
	// Get the user info
	user, _, err := githubClient.Users.Get("")
	if err != nil {
		log.Printf("client.Users.Get() failed with '%s'\n", err)
		http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
		return
	}

	u, err := store.GetUser(*user.Login)
	if err != nil {
		log.Printf("store.GetUser failed with '%s'\n", err)
	}
	u.Username = *user.Login
	//	u.Email = *user.Email
	u.Token = *token
	u.AvatarURL = *user.AvatarURL
	u.GithubProfile = *user.HTMLURL
	err = store.SaveUser(u)

	session := sessions.GetSession(r)
	session.Set("username", *user.Login)

	log.Printf("Logged in as GitHub user: %s\n", *user.Login)
	http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
}
Exemple #2
0
// DeleteProject Deletes a project
func DeleteProject(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	var id = ps.ByName("id")
	user, err := session.GetUser(r)
	if err != nil {
		log.Println("Error getting the user from the session: ", err)
		http.Error(w, "Forbidden", http.StatusForbidden)
		return
	}

	if id == "" {
		log.Println("Missing project id!")
		http.NotFound(w, r)
		return
	}
	_, err = user.GetProject(id)
	if err != nil {
		log.Println("Error getting project: ", id, err)
		http.NotFound(w, r)
	} else {
		// TODO: sanitize id
		wd, _ := os.Getwd()
		err := os.Chdir(wd + "/repos/" + user.Username + "/")
		if err != nil {
			http.Error(w, "Unexpected Error", http.StatusInternalServerError)
			return
		}
		cmd.Run("rm", []string{"-rf", id})

		user.RemoveProject(id)

		err = os.Chdir(wd)
		err = store.SaveUser(user)
		log.Printf("DeleteProject %s!\n", id)
		w.WriteHeader(http.StatusNoContent)
	}
}
Exemple #3
0
// PostProject ...
func PostProject(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
	user, err := session.GetUser(r)
	if err != nil {
		log.Fatal(err)
	}
	projectName := r.PostFormValue("name")
	if projectName == "" {
		log.Println("Missing projectName")
		http.Redirect(w, r, "/", http.StatusSeeOther)
		return
	}
	repoDescription := "Repository for " + projectName + " created by Hugoku"
	ts := oauth2.StaticTokenSource(
		&oauth2.Token{AccessToken: user.Token.AccessToken},
	)
	tc := oauth2.NewClient(oauth2.NoContext, ts)
	client := github.NewClient(tc)

	log.Printf("Creating %s...", projectName)

	buildInfo, err := ci.Deploy(user.Username, projectName)

	log.Printf("Build duration: %s\n", buildInfo.BuildDuration)

	if err != nil {
		log.Fatalf("Error while trying to create project: %s", err)
	}

	if !repo.Exists(client, user.Username, projectName) {
		gitHubStartTime := time.Now()
		repo := &github.Repository{
			Name:        github.String(projectName),
			Private:     github.Bool(false),
			Description: github.String(repoDescription),
		}
		repo, _, err = client.Repositories.Create("", repo)
		if err != nil {
			log.Fatalf("Error while trying to create repo: %s", err)
		}

		// Push the repo
		wd, _ := os.Getwd()
		err := os.Chdir(wd + "/repos/" + user.Username + "/" + projectName + "/")
		if err != nil {
			log.Fatal(err)
		}
		cmd.Run("git", []string{"init", "--quiet"})
		cmd.Run("git", []string{"add", "."})
		cmd.Run("git", []string{"commit", "-m", "'initial source code'"})
		cmd.Run("git", []string{"remote", "add", "origin", "[email protected]:" + user.Username + "/" + projectName + ".git"})
		cmd.Run("git", []string{"push", "--quiet", "-u", "origin", "master"})
		err = os.Chdir(wd)
		if err != nil {
			log.Fatal(err)
		}
		githubTime := time.Since(gitHubStartTime)
		log.Printf("Git repo creation duration: %s\n", githubTime)
	}
	project := store.Project{Name: projectName, Description: repoDescription, BuildsInfo: []store.BuildInfo{buildInfo}, LastBuildInfo: buildInfo}
	user.Projects = append(user.Projects, project)

	err = store.SaveUser(user)

	http.Redirect(w, r, "/", http.StatusSeeOther)
}