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

Fix: add stale build label if it is removed before updating branch #235

Merged
merged 35 commits into from
Jun 29, 2021

Conversation

nlok5923
Copy link
Contributor

@nlok5923 nlok5923 commented Mar 28, 2021

Fixes: #225

Explanation

Added script to add stale build label if it was removed from PR before updating the pr.

Checklist

  • I have successfully deployed my own instance of Oppiabot.
    • You can find instructions for doing this here.
  • I have manually tested all the changes made in this PR following the manual tests matrix.

@nlok5923
Copy link
Contributor Author

@vojtechjelinek @jameesjohn sir please review this pr.

@nlok5923
Copy link
Contributor Author

manual testing video:

bot.mp4

@nlok5923 nlok5923 changed the title Fix: add stale build label if if it removed before updating branch Fix: add stale build label if it is removed before updating branch Mar 28, 2021
@vojtechjelinek vojtechjelinek requested review from jameesjohn and vojtechjelinek and removed request for jameesjohn March 28, 2021 16:42
Copy link
Member

@vojtechjelinek vojtechjelinek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Left a few comments.

const commentParams = context.issue({
body:
'Hi @' + user.login + ', the build of this pr is ' +
' stale please do not remove ' + [OLD_BUILD_LABEL] +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
' stale please do not remove ' + [OLD_BUILD_LABEL] +
' stale please do not remove \'' + OLD_BUILD_LABEL + '\''

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed in latest commits

Comment on lines 382 to 388
const commentParams = context.issue({
body:
'Hi @' + user.login + ', the build of this pr is ' +
' stale please do not remove ' + [OLD_BUILD_LABEL] +
' label. ',
});
await context.github.issues.createComment(commentParams);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const commentParams = context.issue({
body:
'Hi @' + user.login + ', the build of this pr is ' +
' stale please do not remove ' + [OLD_BUILD_LABEL] +
' label. ',
});
await context.github.issues.createComment(commentParams);
const commentParams = context.issue({
body:
'Hi @' + user.login + ', the build of this pr is ' +
' stale please do not remove ' + [OLD_BUILD_LABEL] +
' label. ',
});
await context.github.issues.createComment(commentParams);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed in latest commits

lib/checkPullRequestLabels.js Show resolved Hide resolved
Comment on lines 370 to 371
* This function checks and prevent the removal of critical label
* by unauthorised users.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* This function checks and prevent the removal of critical label
* by unauthorised users.
* This function checks and prevent the removal of stale build label
* by unauthorised users.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed in latest commits

@vojtechjelinek vojtechjelinek removed their assignment Mar 28, 2021
Copy link
Contributor

@jameesjohn jameesjohn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @nlok5923. I left some comments.

Comment on lines 382 to 388
const commentParams = context.issue({
body:
'Hi @' + user.login + ', the build of this pr is ' +
' stale please do not remove \'' + OLD_BUILD_LABEL + '\'' +
' label. ',
});
await context.github.issues.createComment(commentParams);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before commenting on this PR, we need to actually check if the build of the PR is stale.

See

const {data: lastCommit} = await context.github.repos.getCommit(
context.repo({
ref: pullRequest.head.sha
})
);
const lastCommitDate = new Date(lastCommit.commit.author.date);
// Commit is older than 2 days
if (utils.MIN_BUILD_DATE > lastCommitDate) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes sir i agree with you i had added the check in the latest commit.

*
* @param {import('probot').Context} context
*/
module.exports.checkBuildLabelRemoval = async function (context) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
module.exports.checkBuildLabelRemoval = async function (context) {
module.exports.checkStaleBuildLabelRemoval = async function (context) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changes addressed in latest commit.

index.js Outdated
@@ -115,6 +115,11 @@ const runChecks = async (context, checkEvent) => {
checkPullRequestLabelsModule.checkCriticalLabel(context)
);
break;
case constants.staleBuildLabelCheck:
callable.push(
checkPullRequestLabelsModule.checkBuildLabelRemoval(context)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
checkPullRequestLabelsModule.checkBuildLabelRemoval(context)
checkPullRequestLabelsModule.checkStaleBuildLabelRemoval(context)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changes addressed in latest commit.

@nlok5923
Copy link
Contributor Author

@vojtechjelinek @jameesjohn sir please review this pull request.

Copy link
Contributor

@jameesjohn jameesjohn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, two extra comments.

*
* @param {import('probot').Context} context
*/
module.exports.checkStaleBuildLabelRemoval = async function (context) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No tests are written for this function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added test in latest commit

Comment on lines 383 to 391
const {data: lastCommit} = await context.github.repos.getCommit(
context.repo({
ref: pullRequest.head.sha
})
);

const lastCommitDate = new Date(lastCommit.commit.author.date);
// Commit is older than 2 days
if (MIN_BUILD_DATE > lastCommitDate) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are making this check at multiple locations, this can be extracted into a utility function that accepts the pull request and context as parameters and returns a boolean value if the commit is older than the minimum build date.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed in latest commit.

Copy link
Contributor Author

@nlok5923 nlok5923 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also moved the build check repetative script to utils

*
* @param {import('probot').Context} context
*/
module.exports.checkStaleBuildLabelRemoval = async function (context) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added test in latest commit

Copy link
Member

@vojtechjelinek vojtechjelinek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Left a few comments.

*
* @param {import('probot').Context} context
*/
module.exports.checkStaleBuildLabelRemoval = async function (context) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
module.exports.checkStaleBuildLabelRemoval = async function (context) {
module.exports.checkStaleBuildLabelRemoved = async function (context) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed function name in latest commit.

Comment on lines 383 to 387
/*const {data: lastCommit} = await context.github.repos.getCommit(
context.repo({
ref: pullRequest.head.sha
})
);*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not keep code that is commented.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes removed commented code.

lib/utils.js Outdated
Comment on lines 69 to 79
const checkBuild = async (pullRequest, context) => {
const {data: lastCommit} = await context.github.repos.getCommit(
context.repo({
ref: pullRequest.head.sha
})
);

const lastCommitDate = new Date(lastCommit.commit.author.date);
// Commit is older than 2 days
return (MIN_BUILD_DATE > lastCommitDate)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const checkBuild = async (pullRequest, context) => {
const {data: lastCommit} = await context.github.repos.getCommit(
context.repo({
ref: pullRequest.head.sha
})
);
const lastCommitDate = new Date(lastCommit.commit.author.date);
// Commit is older than 2 days
return (MIN_BUILD_DATE > lastCommitDate)
}
const checkPrIsNotStale = async (pullRequest, context) => {
const {data: lastCommit} = await context.github.repos.getCommit(
context.repo({
ref: pullRequest.head.sha
})
);
const lastCommitDate = new Date(lastCommit.commit.author.date);
// Commit is older than 2 days
return (MIN_BUILD_DATE > lastCommitDate)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indented

})
);*/

// const lastCommitDate = new Date(lastCommit.commit.author.date);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

);*/

// const lastCommitDate = new Date(lastCommit.commit.author.date);
// Commit is older than 2 days
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment can easily become stale, since the number of days depends on the checkBuild method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done sir.

if (checkBuild(pullRequest, context)) {
const commentParams = context.issue({
body:
'Hi @' + user.login + ', the build of this pr is ' +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'Hi @' + user.login + ', the build of this pr is ' +
'Hi @' + user.login + ', the build of this PR is ' +

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed in latest commit.

Copy link
Contributor

@jameesjohn jameesjohn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, took another pass.

You can run the tests locally to be sure your changes didn't break a previous implementation.

// Commit is older than 2 days, this can happen when 'synchronize' event
// got triggered as a result of a merge to the main branch. In this case
// we don't want to remove the label.
if (utils.MIN_BUILD_DATE > lastCommitDate) {
if (utils.checkBuild(pullRequest, context)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no utils.checkBuild function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extremely sorry sir for such mistake fixing it in latest commit.

const lastCommitDate = new Date(lastCommit.commit.author.date);
// Commit is older than 2 days
if (utils.MIN_BUILD_DATE > lastCommitDate) {
if (utils.checkBuild(pullRequest, context)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no utils.checkBuild function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extremely sorry sir for such mistake fixing it in latest commit.

@jameesjohn jameesjohn removed their assignment Apr 4, 2021
@@ -22,7 +22,10 @@ const checkCriticalPullRequestModule =
require('../lib/checkCriticalPullRequest');
const checkPullRequestTemplateModule =
require('../lib/checkPullRequestTemplate');
const checkStaleBuildLabelRemovedModule =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This module has been imported line 21

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed

Comment on lines 68 to 74
getCommit: jasmine.createSpy('getCommit').and.resolveTo({
commit: {
author: {
date: '2021-04-12T18:33:45Z'
}
}
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
getCommit: jasmine.createSpy('getCommit').and.resolveTo({
commit: {
author: {
date: '2021-04-12T18:33:45Z'
}
}
})
getCommit: jasmine.createSpy('getCommit').and.resolveTo({
data: {
commit: {
author: {
date: '2021-04-12T18:33:45Z'
}
}
}
})

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, move this spy to the different describe blocks that need it so that you will be able to update the data appropriately

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed.

Comment on lines 606 to 613
beforeEach(async () => {
payloadData.payload.action = 'unlabeled';
payloadData.payload.label = label;
payloadData.payload.sender.login = 'seanlip';
spyOn(
checkStaleBuildLabelRemovedModule, 'checkStaleBuildLabelRemoved'
).and.callThrough();
await robot.receive(payloadData);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mock getCommit here to signify that the pull request has been updated recently.
You can set the commit date to be (new Date).toISOString(), which will make sure that the commit date will always be more than the minimum date.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do this in every other place that requires it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed in latest commit.

@oppiabot
Copy link

oppiabot bot commented Jun 12, 2021

Unassigning @jameesjohn since the review is done.

@nlok5923
Copy link
Contributor Author

@jameesjohn can you please provide a little review over the latest commit as my current changes do not reducing the no of failed tests. also please can you comment on why this pr is experiencing merge conflicts.

@nlok5923
Copy link
Contributor Author

nlok5923 commented Jun 16, 2021

@jameesjohn please review this pr i had tried to bring all the changes suggested by you but still i am receiving test failing you can see log here. one more thing is due to recent merge the specs are not running while npm test

@nlok5923 nlok5923 assigned jameesjohn and unassigned nlok5923 Jun 16, 2021
Comment on lines 72 to 80
getCommit: jasmine.createSpy('getCommit').and.resolveTo({
data: {
commit: {
author: {
date: '2021-04-12T18:33:45Z'
}
}
}
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be removed from here and only added to places where it is required.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment on lines 571 to 591
await robot.receive(payloadData);
github = {
issues: {
createComment: jasmine.createSpy('createComment').and.returnValue({}),
addAssignees: jasmine.createSpy('addAssignees').and.resolveTo({}),
removeLabel: jasmine.createSpy('removeLabel').and.resolveTo({}),
addLabels: jasmine.createSpy('addLabels').and.resolveTo({}),
},
repos: {
getCommit: jasmine.createSpy('getCommit').and.resolveTo({
lastCommit: {
commit: {
author: {
date: (new Date).toISOString()
}
}
}
})
}
};
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
await robot.receive(payloadData);
github = {
issues: {
createComment: jasmine.createSpy('createComment').and.returnValue({}),
addAssignees: jasmine.createSpy('addAssignees').and.resolveTo({}),
removeLabel: jasmine.createSpy('removeLabel').and.resolveTo({}),
addLabels: jasmine.createSpy('addLabels').and.resolveTo({}),
},
repos: {
getCommit: jasmine.createSpy('getCommit').and.resolveTo({
lastCommit: {
commit: {
author: {
date: (new Date).toISOString()
}
}
}
})
}
};
});
github.repos.getCommit = jasmine.createSpy('getCommit').and.resolveTo({
data: {
commit: {
author: {
date: '2021-04-12T18:33:45Z'
}
}
}
});
await robot.receive(payloadData);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed in latest commit

it('check if pr is stale', () => {
expect(github.repos.getCommit).toHaveBeenCalled();
expect(github.repos.getCommit).toHaveBeenCalledWith({
ref: payloadData.payload.pull_request.head.sha
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ref: payloadData.payload.pull_request.head.sha
owner: 'oppia',
repo: 'oppia',
ref: payloadData.payload.pull_request.head.sha

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed

Comment on lines 645 to 664
await robot.receive(payloadData);
github = {
issues: {
createComment: jasmine.createSpy('createComment').and.returnValue({}),
addAssignees: jasmine.createSpy('addAssignees').and.resolveTo({}),
removeLabel: jasmine.createSpy('removeLabel').and.resolveTo({}),
addLabels: jasmine.createSpy('addLabels').and.resolveTo({}),
},
repos: {
getCommit: jasmine.createSpy('getCommit').and.resolveTo({
lastCommit: {
commit: {
author: {
date: '2021-04-12T18:33:45Z'
}
}
}
})
}
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
await robot.receive(payloadData);
github = {
issues: {
createComment: jasmine.createSpy('createComment').and.returnValue({}),
addAssignees: jasmine.createSpy('addAssignees').and.resolveTo({}),
removeLabel: jasmine.createSpy('removeLabel').and.resolveTo({}),
addLabels: jasmine.createSpy('addLabels').and.resolveTo({}),
},
repos: {
getCommit: jasmine.createSpy('getCommit').and.resolveTo({
lastCommit: {
commit: {
author: {
date: '2021-04-12T18:33:45Z'
}
}
}
})
}
};
github.repos.getCommit = jasmine.createSpy('getCommit').and.resolveTo({
data: {
commit: {
author: {
date: (new Date).toString()
}
}
}
});

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed

Comment on lines 674 to 676
expect(github.repos.getCommit).toHaveBeenCalledWith({
ref: payloadData.payload.pull_request.head.sha
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
expect(github.repos.getCommit).toHaveBeenCalledWith({
ref: payloadData.payload.pull_request.head.sha
});
expect(github.repos.getCommit).toHaveBeenCalledWith({
repo: 'oppia',
owner: 'oppia',
ref: payloadData.payload.pull_request.head.sha
});

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed

@oppiabot
Copy link

oppiabot bot commented Jun 22, 2021

Unassigning @jameesjohn since the review is done.

@oppiabot
Copy link

oppiabot bot commented Jun 22, 2021

Hi @nlok5923, it looks like some changes were requested on this pull request by @jameesjohn. PTAL. Thanks!

@nlok5923
Copy link
Contributor Author

@jameesjohn thanks for guidance now all checks are passing.
image

@nlok5923 nlok5923 requested a review from jameesjohn June 22, 2021 14:13
@nlok5923 nlok5923 assigned jameesjohn and unassigned nlok5923 Jun 22, 2021
@jameesjohn jameesjohn merged commit eea2542 into oppia:master Jun 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Oppiabot should add PR Don't merge:STATE BUILD label again.
3 participants