Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new action for PipelineRun - Edit and Run #2633

Merged
merged 1 commit into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 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,81 @@
/*
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();

marniks7 marked this conversation as resolved.
Show resolved Hide resolved
cy.contains('h1', 'PipelineRuns');

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('a', `${pipelineName}-run`).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();
AlanGreene marked this conversation as resolved.
Show resolved Hide resolved
cy.contains('h1', 'PipelineRuns');

// then
cy.get(
`td:has(.bx--link[title*=${pipelineName}-run]) + td:has(.tkn--status[data-reason=Succeeded])`,
{ timeout: 15000 }
).should('have.length', 2);
});
});
52 changes: 37 additions & 15 deletions src/api/pipelineRuns.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,22 +170,32 @@ 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 || `${name}-`;
}

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

Object.keys(payload.metadata.labels).forEach(label => {
if (label.startsWith('tekton.dev/')) {
Expand All @@ -194,24 +204,36 @@ export function rerunPipelineRun(pipelineRun) {
});

/*
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.
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.

Ideally we would just delete all `tekton.dev/*` annotations as we do with labels but
`tekton.dev/v1beta1Resources` is required for pipelines that use PipelineResources,
and there may be other similar annotations that are still required.
Ideally we would just delete all `tekton.dev/*` annotations as we do with labels but
`tekton.dev/v1beta1Resources` is required for pipelines that use PipelineResources,
and there may be other similar annotations that are still required.

When v1beta1 has been fully removed from Tekton Pipelines we can revisit this
and remove all remaining `tekton.dev/*` annotations.
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'
];
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,
rerun: true
});

const uri = getTektonAPI('pipelineruns', { namespace });
return post(uri, payload).then(({ body }) => body);
Expand Down
176 changes: 176 additions & 0 deletions src/api/pipelineRuns.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import yaml from 'js-yaml';
import * as API from './pipelineRuns';
import * as utils from './utils';
import { rest, server } from '../../config_frontend/msw';
import { generateNewPipelineRunPayload } from './pipelineRuns';

it('cancelPipelineRun', () => {
const name = 'foo';
Expand Down Expand Up @@ -326,3 +328,177 @@ it('startPipelineRun', () => {
}
);
});

it('rerun. generate new pipeline run with minimum possible fields', () => {
const pipelineRun = {
apiVersion: 'tekton.dev/v1beta1',
kind: 'PipelineRun',
metadata: {
name: 'test',
namespace: 'test-namespace'
},
spec: {
pipelineRef: {
name: 'simple'
}
}
};
const { namespace, payload } = generateNewPipelineRunPayload({
pipelineRun,
rerun: true
});
const expected = `apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
annotations: {}
generateName: test-r-
labels:
dashboard.tekton.dev/rerunOf: test
namespace: test-namespace
spec:
pipelineRef:
name: simple
`;
expect(namespace).toEqual('test-namespace');
expect(yaml.dump(payload)).toEqual(expected);
});

it('rerun. generate new pipeline run with all processed fields', () => {
const pipelineRun = {
apiVersion: 'tekton.dev/v1beta1',
kind: 'PipelineRun',
metadata: {
name: 'test',
namespace: 'test-namespace',
annotations: {
keya: 'valuea',
'kubectl.kubernetes.io/last-applied-configuration':
'{"apiVersion": "tekton.dev/v1beta1", "keya": "valuea"}'
},
labels: {
key1: 'valuel',
key2: 'value2',
'tekton.dev/pipeline': 'foo'
},
uid: '111-233-33',
resourceVersion: 'aaaa'
},
spec: {
pipelineRef: {
name: 'simple'
},
params: [{ name: 'param-1' }, { name: 'param-2' }]
},
status: { startTime: '0' }
};
const { namespace, payload } = generateNewPipelineRunPayload({
pipelineRun,
rerun: true
});
const expected = `apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
annotations:
keya: valuea
generateName: test-r-
labels:
key1: valuel
key2: value2
dashboard.tekton.dev/rerunOf: test
namespace: test-namespace
spec:
pipelineRef:
name: simple
params:
- name: param-1
- name: param-2
`;
expect(namespace).toEqual('test-namespace');
expect(yaml.dump(payload)).toEqual(expected);
});

it('edit. generate new pipeline run with minimum possible fields', () => {
const pipelineRun = {
apiVersion: 'tekton.dev/v1beta1',
kind: 'PipelineRun',
metadata: {
name: 'test',
namespace: 'test-namespace'
},
spec: {
pipelineRef: {
name: 'simple'
}
}
};
const { namespace, payload } = generateNewPipelineRunPayload({
pipelineRun,
rerun: false
});
const expected = `apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
annotations: {}
generateName: test-
labels: {}
namespace: test-namespace
spec:
pipelineRef:
name: simple
`;
expect(namespace).toEqual('test-namespace');
expect(yaml.dump(payload)).toEqual(expected);
});
it('edit. generate new pipeline run with all processed fields', () => {
const pipelineRun = {
apiVersion: 'tekton.dev/v1beta1',
kind: 'PipelineRun',
metadata: {
annotations: {
keya: 'valuea',
'kubectl.kubernetes.io/last-applied-configuration':
'{"apiVersion": "tekton.dev/v1beta1", "keya": "valuea"}'
},
labels: {
key1: 'valuel',
key2: 'value2',
'tekton.dev/pipeline': 'foo',
'tekton.dev/run': 'bar'
},
name: 'test',
namespace: 'test-namespace',
uid: '111-233-33',
resourceVersion: 'aaaa'
},
spec: {
pipelineRef: {
name: 'simple'
},
params: [{ name: 'param-1' }, { name: 'param-2' }]
},
status: { startTime: '0' }
};
const { namespace, payload } = generateNewPipelineRunPayload({
pipelineRun,
rerun: false
});
const expected = `apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
annotations:
keya: valuea
generateName: test-
labels:
key1: valuel
key2: value2
namespace: test-namespace
spec:
pipelineRef:
name: simple
params:
- name: param-1
- name: param-2
`;
expect(namespace).toEqual('test-namespace');
expect(yaml.dump(payload)).toEqual(expected);
});
Loading