Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Commit

Permalink
Merge pull request #13 from Thiht/main
Browse files Browse the repository at this point in the history
feat: support qovery-container-names input
  • Loading branch information
benjaminch authored May 5, 2023
2 parents 345b3b0 + d1927b9 commit 73a168c
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 6 deletions.
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ inputs:
qovery-container-ids:
description: 'Qovery container IDs, separated by `,`'
required: false
qovery-container-names:
description: 'Qovery container names, separated by `,`'
required: false
qovery-container-tags:
description: 'Qovery container tags, separated by `,`'
required: false
Expand All @@ -69,5 +72,6 @@ runs:
- --db-id=${{ inputs.qovery-database-id }}
- --db-name=${{ inputs.qovery-database-name }}
- --container-ids=${{ inputs.qovery-container-ids }}
- --container-names=${{ inputs.qovery-container-names }}
- --container-tags=${{ inputs.qovery-container-tags }}
- --api-token=${{ inputs.qovery-api-token }}
29 changes: 28 additions & 1 deletion github-action/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var (
databaseId = kingpin.Flag("db-id", "Qovery database ID").String()
databaseName = kingpin.Flag("db-name", "Qovery database name").String()
containerIds = kingpin.Flag("container-ids", "Qovery container ids separated by ,").String()
containerNames = kingpin.Flag("container-names", "Qovery container name(s)").String()
containerImageTags = kingpin.Flag("container-tags", "Qovery container image tags separated by ,").String()
apiToken = kingpin.Flag("api-token", "Qovery API token").Required().String()
)
Expand Down Expand Up @@ -97,6 +98,26 @@ func getApplicationIds(qoveryAPIClient pkg.QoveryAPIClient, envId string, id *st
return "", errors.New("'app-ids' or 'app-names' property must be defined")
}

func getContainerIds(qoveryAPIClient pkg.QoveryAPIClient, envId string, id *string, name *string) (string, error) {
if id != nil && *id != "" {
return sanitizeInputIDsList(*id), nil
}

if name != nil && *name != "" {
var ids []string
for _, sName := range strings.Split(sanitizeInputIDsList(*name), ",") {
id, err := qovery.GetContainerIdByName(qoveryAPIClient, envId, sName)
handleError(err)

ids = append(ids, id)
}

return strings.Join(ids, ","), nil
}

return "", errors.New("'container-ids' or 'container-names' property must be defined")
}

func getDatabaseId(qoveryAPIClient pkg.QoveryAPIClient, envId string, id *string, name *string) (string, error) {
if id != nil && *id != "" {
return *id, nil
Expand Down Expand Up @@ -127,7 +148,7 @@ func main() {

deployApp := (applicationIds != nil && *applicationIds != "") || (applicationNames != nil && *applicationNames != "")
deployDb := (databaseId != nil && *databaseId != "") || (databaseName != nil && *databaseName != "")
deployContainer := containerIds != nil && *containerIds != ""
deployContainer := (containerIds != nil && *containerIds != "") || (containerNames != nil && *containerNames != "")

if deployApp && (applicationCommitId == nil || *applicationCommitId == "") {
fmt.Println("error: commit ID shouldn't be empty: `app-commit-id` to be set in args or `GITHUB_SHA` env var to be set.")
Expand Down Expand Up @@ -188,6 +209,12 @@ func main() {
})
}

if deployContainer {
contIds, err := getContainerIds(qoveryAPIClient, environmentId, containerIds, containerNames)
handleError(err)
containerIds = &contIds
}

ids = strings.Split(sanitizeInputIDsList(*containerIds), ",")
tags := strings.Split(*containerImageTags, ",")
if len(ids) != len(tags) {
Expand Down
19 changes: 14 additions & 5 deletions github-action/pkg/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,23 @@ type Application struct {
}

type ApplicationResult struct {
Results []Application `json:results`
Results []Application `json:"results"`
}

type ApplicationDeployment struct {
ApplicationId string `json:"application_id"`
GitCommitId string `json:"git_commit_id"`
}

type ContainerResult struct {
Results []Container `json:"results"`
}

type Container struct {
ID string `json:"id"`
Name string `json:"name"`
}

type ContainerDeployment struct {
Id string `json:"id"`
ImageTag string `json:"image_tag"`
Expand All @@ -35,7 +44,7 @@ type Database struct {
}

type DatabaseResult struct {
Results []Database `json:results`
Results []Database `json:"results"`
}

type Environment struct {
Expand All @@ -44,7 +53,7 @@ type Environment struct {
}

type EnvironmentResult struct {
Results []Environment `json:results`
Results []Environment `json:"results"`
}

type Project struct {
Expand All @@ -53,7 +62,7 @@ type Project struct {
}

type ProjectResult struct {
Results []Project `json:results`
Results []Project `json:"results"`
}

type Organization struct {
Expand All @@ -62,5 +71,5 @@ type Organization struct {
}

type OrganizationResult struct {
Results []Organization `json:results`
Results []Organization `json:"results"`
}
34 changes: 34 additions & 0 deletions github-action/pkg/qovery_api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ type QoveryAPIClient interface {
ListProjects(organizationId string) ([]Project, error)
ListEnvironments(projectId string) ([]Environment, error)
ListApplications(environmentId string) ([]Application, error)
ListContainers(environmentId string) ([]Container, error)
ListDatabases(environmentId string) ([]Database, error)
}

Expand Down Expand Up @@ -422,6 +423,39 @@ func (a qoveryAPIClient) ListApplications(environmentId string) ([]Application,
}
}

func (a qoveryAPIClient) ListContainers(environmentId string) ([]Container, error) {
req, err := http.NewRequest("GET", a.baseURL+"/environment/"+environmentId+"/container", nil)
req.Header.Set("Authorization", "Token "+a.apiToken)
req.Header.Set("Content-Type", "application/json")
if err != nil {
return nil, err
}

resp, err := a.c.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

switch resp.StatusCode {
case 200:
jsonData, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}

res := ContainerResult{}
err = json.Unmarshal(jsonData, &res)
if err != nil {
return nil, err
}

return res.Results, nil
default:
return nil, fmt.Errorf("qovery API error, status code: %s", resp.Status)
}
}

func (a qoveryAPIClient) ListDatabases(environmentId string) ([]Database, error) {
req, err := http.NewRequest("GET", a.baseURL+"/environment/"+environmentId+"/database", nil)
req.Header.Set("Authorization", "Token "+a.apiToken)
Expand Down
21 changes: 21 additions & 0 deletions github-action/qovery/container.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package qovery

import (
"fmt"
"github-action/pkg"
)

func GetContainerIdByName(qoveryAPIClient pkg.QoveryAPIClient, environmentId string, name string) (string, error) {
containers, err := qoveryAPIClient.ListContainers(environmentId)
if err != nil {
return "", err
}

for _, container := range containers {
if container.Name == name {
return container.ID, nil
}
}

return "", fmt.Errorf("can't find container with name %v! (it's case sensitive)", name)
}

0 comments on commit 73a168c

Please sign in to comment.