Skip to content

Commit

Permalink
Add new action for PipelineRun - Edit and Run
Browse files Browse the repository at this point in the history
  • Loading branch information
marniks7 committed Jan 15, 2023
1 parent 09ef68e commit 57d42b0
Show file tree
Hide file tree
Showing 20 changed files with 603 additions and 45 deletions.
78 changes: 78 additions & 0 deletions packages/e2e/cypress/e2e/run/actions-pipelinerun.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
Copyright 2022 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

const namespace = 'e2e-actions';
describe('Edit and run Pipeline Run', () => {
before(() => {
cy.exec('kubectl version --client');
cy.exec(`kubectl create namespace ${namespace} || true`);
});

after(() => {
cy.exec(`kubectl delete namespace ${namespace} || true`);
});

it('should create pipelinerun on edit and run', function () {
// given pipeline
const uniqueNumber = Date.now();
const pipelineName = `sp-${uniqueNumber}`;
const pipeline = `apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
name: ${pipelineName}
namespace: ${namespace}
spec:
tasks:
- name: hello
taskSpec:
steps:
- name: echo
image: busybox
script: |
#!/bin/ash
echo "Hello World!"
`;
cy.exec(`echo "${pipeline}" | kubectl apply -f -`);
// when create first pipeline run
cy.visit(
`/#/pipelineruns/create?namespace=${namespace}&pipelineName=${pipelineName}`
);
cy.get('[id=create-pipelinerun--namespaces-dropdown]').should(
'have.value',
namespace
);
cy.get('[id=create-pipelinerun--pipelines-dropdown]').should(
'have.value',
pipelineName
);
cy.contains('button', 'Create').click();

cy.get(
`td:has(.bx--link[title*=${pipelineName}-run]) + td:has(.tkn--status[data-reason=Succeeded])`,
{ timeout: 15000 }
).should('have.length', 1);

// when edit and run to create second pipeline run
cy.contains(`${pipelineName}-run`).parent().click();
cy.contains('button', 'Actions').click();
cy.contains('button', 'Edit and run').click();
cy.get('.cm-content').contains(`name: ${pipelineName}`);
cy.contains('button', 'Create').click();

// then
cy.get(
`td:has(.bx--link[title*=${pipelineName}-run]) + td:has(.tkn--status[data-reason=Succeeded])`,
{ timeout: 15000 }
).should('have.length', 2);
});
});
8 changes: 8 additions & 0 deletions packages/utils/src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,14 @@ export function getGenerateNamePrefixForRerun(name) {
return `${root}${rerunIdentifier}`;
}

export function getGenerateNamePrefixForNewRun(name) {
let generateName = name;
if (!name.endsWith('-')) {
generateName = `${name}-`;
}
return `${generateName}`;
}

/*
getParams required to support 3rd-party consumers of certain dashboard
components (e.g. PipelineRun) while they migrate to the Tekton beta
Expand Down
72 changes: 52 additions & 20 deletions src/api/pipelineRuns.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { getGenerateNamePrefixForRerun } from '@tektoncd/dashboard-utils';
import {
getGenerateNamePrefixForNewRun,
getGenerateNamePrefixForRerun
} from '@tektoncd/dashboard-utils';
import deepClone from 'lodash.clonedeep';

import { deleteRequest, get, patch, post } from './comms';
Expand Down Expand Up @@ -170,30 +173,46 @@ export function createPipelineRun({
return post(uri, payload).then(({ body }) => body);
}

export function rerunPipelineRun(pipelineRun) {
const { annotations, labels, name, namespace } = pipelineRun.metadata;
export function generateNewPipelineRunPayload(pipelineRun, rerun) {
const { annotations, labels, name, namespace, generateName } =
pipelineRun.metadata;

const payload = deepClone(pipelineRun);
payload.apiVersion =
payload.apiVersion || `tekton.dev/${getTektonPipelinesAPIVersion()}`;
payload.kind = payload.kind || 'PipelineRun';

function getGenerateName() {
if (rerun) {
return getGenerateNamePrefixForRerun(name);
}

return generateName || getGenerateNamePrefixForNewRun(name);
}

payload.metadata = {
annotations: annotations || {},
generateName: getGenerateNamePrefixForRerun(name),
labels: {
...labels,
'dashboard.tekton.dev/rerunOf': name
},
annotations,
generateName: getGenerateName(),
labels,
namespace
};
if (rerun) {
payload.metadata.labels = {
...labels,
'dashboard.tekton.dev/rerunOf': name
};
}

Object.keys(payload.metadata.labels).forEach(label => {
if (label.startsWith('tekton.dev/')) {
delete payload.metadata.labels[label];
}
});
if (payload.metadata.labels) {
Object.keys(payload.metadata.labels).forEach(label => {
if (label.startsWith('tekton.dev/')) {
delete payload.metadata.labels[label];
}
});
}

/*
if (payload.metadata.annotations) {
/*
This is used by Tekton Pipelines as part of the conversion between v1beta1
and v1 resources. Creating a run with this in place prevents it from actually
executing and instead adopts the status of the original TaskRuns.
Expand All @@ -204,14 +223,27 @@ export function rerunPipelineRun(pipelineRun) {
When v1beta1 has been fully removed from Tekton Pipelines we can revisit this
and remove all remaining `tekton.dev/*` annotations.
*/
delete payload.metadata.annotations['tekton.dev/v1beta1TaskRuns'];
delete payload.metadata.annotations[
'kubectl.kubernetes.io/last-applied-configuration'
];
*/
delete payload.metadata.annotations['tekton.dev/v1beta1TaskRuns'];
delete payload.metadata.annotations[
'kubectl.kubernetes.io/last-applied-configuration'
];
}
Object.keys(payload.metadata).forEach(
i => payload.metadata[i] === undefined && delete payload.metadata[i]
);

delete payload.status;

delete payload.spec?.status;
return { namespace, payload };
}

export function rerunPipelineRun(pipelineRun) {
const { namespace, payload } = generateNewPipelineRunPayload(
pipelineRun,
true
);

const uri = getTektonAPI('pipelineruns', { namespace });
return post(uri, payload).then(({ body }) => body);
Expand Down
Loading

0 comments on commit 57d42b0

Please sign in to comment.