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

feat: Handle restrict pr #1296

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
19 changes: 19 additions & 0 deletions app/components/pipeline/event/card/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ export default class PipelineEventCardComponent extends Component {

@tracked showEventHistoryModal;

@tracked showStartEventModal;

queueName;

userSettings;
Expand Down Expand Up @@ -296,6 +298,13 @@ export default class PipelineEventCardComponent extends Component {
return `View event history for ${groupId}`;
}

get isRestrictPR() {
const prRestriction =
this.args.pipeline.annotations?.['screwdriver.cd/restrictPR'];

return prRestriction ? prRestriction !== 'none' : false;
}

@action
openParametersModal() {
this.showParametersModal = true;
Expand Down Expand Up @@ -325,4 +334,14 @@ export default class PipelineEventCardComponent extends Component {
closeEventHistoryModal() {
this.showEventHistoryModal = false;
}

@action
openStartEventModal() {
this.showStartEventModal = true;
}

@action
closeStartEventModal() {
this.showStartEventModal = false;
}
}
3 changes: 2 additions & 1 deletion app/components/pipeline/event/card/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@
}
}

.abort-event-button {
.abort-event-button,
.start-event-button {
margin-left: auto;
background-color: colors.$sd-white;
}
Expand Down
25 changes: 25 additions & 0 deletions app/components/pipeline/event/card/template.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,31 @@
{{/if}}
</BsButton>
{{/if}}
{{else}}
{{#if (and this.isPR this.isRestrictPR)}}
<BsButton
class="start-event-button"
@type="primary"
@outline={{true}}
@onClick={{fn this.openStartEventModal}}
title="Start PR event"
>
<FaIcon
@icon="play-circle"
@fixedWidth="true"
@prefix="far"
/>
{{#if this.showStartEventModal}}
<Pipeline::Modal::StartEvent
@pipeline={{@pipeline}}
@jobs={{@jobs}}
@event={{@event}}
@isRestrictPR={{true}}
@closeModal={{this.closeStartEventModal}}
/>
{{/if}}
</BsButton>
{{/if}}
{{/if}}
</div>

Expand Down
23 changes: 14 additions & 9 deletions app/components/pipeline/modal/start-event/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,34 +56,39 @@ export default class PipelineModalStartEventComponent extends Component {
async startBuild() {
this.isAwaitingResponse = true;

const { isRestrictPR } = this.args;
const prNum = isRestrictPR ? this.args.event.prNum : null;
const event = isRestrictPR ? this.args.event : null;

const data = buildPostBody(
this.session.data.authenticated.username,
this.args.pipeline.id,
null,
null,
event,
this.parameters,
false,
null
null,
prNum
);

await this.shuttle
.fetchFromApi('post', '/events', data)
.then(event => {
.then(newEvent => {
this.args.closeModal();

if (this.router.currentRouteName === 'v2.pipeline.events.show') {
this.router.transitionTo('v2.pipeline.events.show', {
event,
newEvent,
reloadEventRail: true,
id: event.id
id: newEvent.id
});
} else if (this.router.currentRouteName === 'v2.pipeline.pulls.show') {
this.router.transitionTo('v2.pipeline.pulls.show', {
event,
newEvent,
reloadEventRail: true,
id: event.prNum,
pull_request_number: event.prNum,
sha: event.sha
id: newEvent.prNum,
pull_request_number: newEvent.prNum,
sha: newEvent.sha
});
}
})
Expand Down
15 changes: 12 additions & 3 deletions app/utils/pipeline/modal/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* @param parameters
* @param isFrozen
* @param reason
* @param prNum
* @returns {{causeMessage: string, pipelineId}}
*/
export function buildPostBody( // eslint-disable-line import/prefer-default-export
Expand All @@ -16,17 +17,25 @@ export function buildPostBody( // eslint-disable-line import/prefer-default-expo
event,
parameters,
isFrozen,
reason
reason,
prNum
) {
const data = {
pipelineId,
causeMessage: `Manually started by ${username}`
};

if (event && job) {
data.startFrom = job.name;
if (event) {
data.groupEventId = event.groupEventId;
data.parentEventId = event.id;

if (job) {
data.startFrom = job.name;
}
if (prNum) {
data.startFrom = '~pr';
data.prNum = prNum;
}
} else {
data.startFrom = '~commit';
}
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/utils/pipeline/modal/request-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,29 @@ module('Unit | Utility | pipeline/modal/request', function () {
);
});

test('buildPostBody sets correct values for new PR event', function (assert) {
assert.deepEqual(
buildPostBody(
'foobar',
123,
null,
{ id: 9, groupEventId: 2 },
null,
false,
null,
5
),
{
pipelineId: 123,
causeMessage: 'Manually started by foobar',
startFrom: '~pr',
groupEventId: 2,
parentEventId: 9,
prNum: 5
}
);
});

test('buildPostBody sets correct values for new event starting from job', function (assert) {
assert.deepEqual(
buildPostBody(
Expand Down