فهرست منبع

add query for detecting procfile on backend

Alexander Belanger 5 سال پیش
والد
کامیت
bdcd849a31

+ 20 - 0
dashboard/src/components/repo-selector/ContentsList.tsx

@@ -86,6 +86,26 @@ export default class ContentsList extends Component<PropsType, StateType> {
 
         this.setState({ loading: false, error: true });
       });
+
+      api
+      .getProcfileContents(
+        "<token>",
+        { path: "./Procfile" },
+        {
+          project_id: currentProject.id,
+          git_repo_id: actionConfig.git_repo_id,
+          kind: "github",
+          owner: actionConfig.git_repo.split("/")[0],
+          name: actionConfig.git_repo.split("/")[1],
+          branch: branch,
+        }
+      )
+      .then((res) => {
+        console.log(res.data)      
+      })
+      .catch((err) => {
+        console.log(err);
+      });
   };
 
   renderContentList = () => {

+ 17 - 0
dashboard/src/shared/api.tsx

@@ -297,6 +297,22 @@ const getBranchContents = baseApi<
   return `/api/projects/${pathParams.project_id}/gitrepos/${pathParams.git_repo_id}/repos/${pathParams.kind}/${pathParams.owner}/${pathParams.name}/${pathParams.branch}/contents`;
 });
 
+const getProcfileContents = baseApi<
+  {
+    path: string;
+  },
+  {
+    project_id: number;
+    git_repo_id: number;
+    kind: string;
+    owner: string;
+    name: string;
+    branch: string;
+  }
+>("GET", (pathParams) => {
+  return `/api/projects/${pathParams.project_id}/gitrepos/${pathParams.git_repo_id}/repos/${pathParams.kind}/${pathParams.owner}/${pathParams.name}/${pathParams.branch}/procfile`;
+});
+
 const getBranches = baseApi<
   {},
   {
@@ -821,6 +837,7 @@ export default {
   getNamespaces,
   getNGINXIngresses,
   getOAuthIds,
+  getProcfileContents,
   getProjectClusters,
   getProjectRegistries,
   getProjectRepos,

+ 0 - 2
server/api/git_action_handler.go

@@ -151,8 +151,6 @@ func (app *App) createGitActionFromForm(
 		return nil
 	}
 
-	fmt.Println("GIT ACTIONB BRANCH IS", gitAction.GitBranch)
-
 	// create the commit in the git repo
 	gaRunner := &actions.GithubActions{
 		GitIntegration: gr,

+ 61 - 0
server/api/git_repo_handler.go

@@ -6,7 +6,9 @@ import (
 	"fmt"
 	"net/http"
 	"net/url"
+	"regexp"
 	"strconv"
+	"strings"
 
 	"golang.org/x/oauth2"
 
@@ -202,6 +204,65 @@ func (app *App) HandleGetBranchContents(w http.ResponseWriter, r *http.Request)
 	json.NewEncoder(w).Encode(res)
 }
 
+type GetProcfileContentsResp map[string]string
+
+var procfileRegex = regexp.MustCompile("^([A-Za-z0-9_]+):\\s*(.+)$")
+
+// HandleGetProcfileContents retrieves the contents of a procfile in a github repo
+func (app *App) HandleGetProcfileContents(w http.ResponseWriter, r *http.Request) {
+	tok, err := app.githubTokenFromRequest(r)
+
+	if err != nil {
+		app.handleErrorInternal(err, w)
+		return
+	}
+
+	client := github.NewClient(app.GithubProjectConf.Client(oauth2.NoContext, tok))
+	owner := chi.URLParam(r, "owner")
+	name := chi.URLParam(r, "name")
+	branch := chi.URLParam(r, "branch")
+
+	queryParams, err := url.ParseQuery(r.URL.RawQuery)
+
+	if err != nil {
+		app.handleErrorFormDecoding(err, ErrReleaseDecode, w)
+		return
+	}
+
+	resp, _, _, err := client.Repositories.GetContents(
+		context.TODO(),
+		owner,
+		name,
+		queryParams["path"][0],
+		&github.RepositoryContentGetOptions{
+			Ref: branch,
+		},
+	)
+
+	if err != nil {
+		app.handleErrorInternal(err, w)
+		return
+	}
+
+	fileData, err := resp.GetContent()
+
+	if err != nil {
+		app.handleErrorInternal(err, w)
+		return
+	}
+
+	parsedContents := make(GetProcfileContentsResp)
+
+	// parse the procfile information
+	for _, line := range strings.Split(fileData, "\n") {
+		if matches := procfileRegex.FindStringSubmatch(line); matches != nil {
+			parsedContents[matches[1]] = matches[2]
+		}
+	}
+
+	json.NewEncoder(w).Encode(parsedContents)
+}
+
 // finds the github token given the git repo id and the project id
 func (app *App) githubTokenFromRequest(
 	r *http.Request,

+ 14 - 0
server/router/router.go

@@ -1053,6 +1053,20 @@ func New(a *api.App) *chi.Mux {
 			),
 		)
 
+		r.Method(
+			"GET",
+			"/projects/{project_id}/gitrepos/{git_repo_id}/repos/{kind}/{owner}/{name}/{branch}/procfile",
+			auth.DoesUserHaveProjectAccess(
+				auth.DoesUserHaveGitRepoAccess(
+					requestlog.NewHandler(a.HandleGetProcfileContents, l),
+					mw.URLParam,
+					mw.URLParam,
+				),
+				mw.URLParam,
+				mw.ReadAccess,
+			),
+		)
+
 		// /api/projects/{project_id}/deploy routes
 		r.Method(
 			"POST",