How-to do automation for "Set a component project as a catalog project "?
set gitlab-ci yml component in CICD Catalog ?
To use automation to enable this setting, you can use the mutationcatalogresourcescreate
GraphQL endpoint. Issue 463043 proposes to expose this in the REST API as well.
CI_API_GRAPHQL_URL=https://gitlab.xxx.es/api/graphql
PROJECT_ID="<project_id>"
PRIVATE_TOKEN="<your_access_token>"
curl --request POST \
--header "PRIVATE-TOKEN: $PRIVATE_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"query": "mutation { catalogResourcesCreate(input: { projectIds: [\"'$PROJECT_ID'\"] }) { errors } }"
}' \
$CI_API_GRAPHQL_URL
I get error:
GraphQL https://gitlab.xxxxxx.es/api/graphql
curl: (3) URL rejected: Malformed input to a URL function
I try
curl -s --request POST $CI_API_GRAPHQL_URL --header "PRIVATE-TOKEN: $USER_PAT_TOKEN" \
--header "Content-Type: application/json" \
--data '{"query": "mutation { catalogResourcesCreate(input: { projectIds: [\"'$PROJECT_ID'\"] }) }" }'
I get
{"errors":[{"message":"Field must have selections (field 'catalogResourcesCreate' returns CatalogResourcesCreatePayload but has no selections. Did you mean 'catalogResourcesCreate { ... }'?)","locations":[{"line":1,"column":12}],"path":["mutation","catalogResourcesCreate"],"extensions":{"code":"selectionMismatch","nodeName":"field 'catalogResourcesCreate'","typeName":"CatalogResourcesCreatePayload"}}]}
then, I try
curl -s --request POST $CI_API_GRAPHQL_URL --header "PRIVATE-TOKEN: $USER_DEVOPS_CICD_PAT_TOKEN" \
--header "Content-Type: application/json" \
--data '{"query": "mutation { catalogResourcesCreate(input: { projectPath: \"$PROJECT_PATH_GRAPHQL\" }) { errors } }" }'
I get now
{“errors”:[{“message”:“The resource that you are attempting to access does not exist or you don’t have permission to perform this action”,“locations”:[{“line”:1,“column”:12}],“path”:[“catalogResourcesCreate”]}],“data”:{“catalogResourcesCreate”:null}}
Troubleshooting:
USER_DEVOPS_CICD_PAT_TOKEN has scopes: api, read_api, read_user, read_repository, write_repository, read_registry, write_registry, sudo, admin_mode
curl -s --request POST $CI_API_GRAPHQL_URL \
--header "PRIVATE-TOKEN: $USER_DEVOPS_CICD_PAT_TOKEN" \
--header "Content-Type: application/json" \
--data '{"query": "{ project(fullPath: \"'$PROJECT_PATH_GRAPHQL'\") { id name } }"}'
curl -s --request POST $CI_API_GRAPHQL_URL \
--header "PRIVATE-TOKEN: $USER_DEVOPS_CICD_PAT_TOKEN" \
--header "Content-Type: application/json" \
--data '{"query": "{ currentUser { username } }"}'
echo -e "\n\n**********************************\n\n"
curl -s --request POST $CI_API_GRAPHQL_URL \
--header "PRIVATE-TOKEN: $USER_DEVOPS_CICD_PAT_TOKEN" \
--header "Content-Type: application/json" \
--data '{"query": "query { project(fullPath: \"'$PROJECT_PATH_GRAPHQL'\") { ciConfigPath ciCdSettings { keepLatestArtifactEnabled } features { issuesEnabled mergeRequestsEnabled buildsEnabled wikiEnabled } } }"}'
curl -s --request POST $CI_API_GRAPHQL_URL \
--header "PRIVATE-TOKEN: $USER_DEVOPS_CICD_PAT_TOKEN" \
--header "Content-Type: application/json" \
--data '{"query": "query { project(fullPath: \"'$PROJECT_PATH_GRAPHQL'\") { ciCatalogSettings { enabled } } }"}'
curl -s --request POST $CI_API_GRAPHQL_URL \
--header "PRIVATE-TOKEN: $USER_DEVOPS_CICD_PAT_TOKEN" \
--header "Content-Type: application/json" \
--data '{"query": "{ project(fullPath: \"'$PROJECT_PATH_GRAPHQL'\") { name id ciCdSettings { keepLatestArtifactEnabled } } }"}'
{“data”:{“project”:{“id”:“gid://gitlab/Project/124”,“name”:“publish”}}
{“data”:{“currentUser”:{“username”:“USER_DEVOPS_CICD”}}}
I get errors:
{“errors”:[{“message”:“Field ‘ciConfigPath’ doesn’t exist on type ‘Project’”,“locations”:[{“line”:1,“column”:74}],“path”:[“query”,“project”,“ciConfigPath”],“extensions”:{“code”:“undefinedField”,“typeName”:“Project”,“fieldName”:“ciConfigPath”}},{“message”:“Field ‘keepLatestArtifactEnabled’ doesn’t exist on type ‘ProjectCiCdSetting’”,“locations”:[{“line”:1,“column”:102}],“path”:[“query”,“project”,“ciCdSettings”,“keepLatestArtifactEnabled”],“extensions”:{“code”:“undefinedField”,“typeName”:“ProjectCiCdSetting”,“fieldName”:“keepLatestArtifactEnabled”}},{“message”:“Field ‘features’ doesn’t exist on type ‘Project’”,“locations”:[{“line”:1,“column”:130}],“path”:[“query”,“project”,“features”],“extensions”:{“code”:“undefinedField”,“typeName”:“Project”,“fieldName”:“features”}}]}
{“errors”:[{“message”:“Field ‘ciCatalogSettings’ doesn’t exist on type ‘Project’”,“locations”:[{“line”:1,“column”:74}],“path”:[“query”,“project”,“ciCatalogSettings”],“extensions”:{“code”:“undefinedField”,“typeName”:“Project”,“fieldName”:“ciCatalogSettings”}}]}
{“errors”:[{“message”:“Field ‘keepLatestArtifactEnabled’ doesn’t exist on type ‘ProjectCiCdSetting’”,“locations”:[{“line”:1,“column”:91}],“path”:[“query”,“project”,“ciCdSettings”,“keepLatestArtifactEnabled”],“extensions”:{“code”:“undefinedField”,“typeName”:“ProjectCiCdSetting”,“fieldName”:“keepLatestArtifactEnabled”}}]}