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(1769): Do not throw err if cannot get changed files #142

Merged
merged 1 commit into from
Oct 1, 2019
Merged

Conversation

tkyi
Copy link
Member

@tkyi tkyi commented Sep 30, 2019

Context

Sometimes when there are PRs with over 2500 files changed, the webhook will fail since the circuit breaker will timeout.

Objective

This PR does not throw errs when getChangedFiles or getOpenedPrs calls fail.

References

Short term fix for: screwdriver-cd/screwdriver#1769

License

I confirm that this contribution is made under the terms of the license found in the root directory of this repository's source tree and that I have the authority necessary to make this contribution on behalf of its copyright owner.

index.js Outdated
@@ -478,7 +478,7 @@ class GithubScm extends Scm {

/**
* Get a list of names and references of opened PRs
* @async _getOpenedPRs
* @async getOpenedPRs
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, _getOpenedPRs and _ getChangedFiles are the function names

Copy link
Member Author

Choose a reason for hiding this comment

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

it's not really consistent in the file 🤷‍♀

index.js Outdated
@@ -1236,8 +1238,9 @@ class GithubScm extends Scm {

/**
* Resolve a pull request object based on the config
* @async getPrRef
* @async getPrInfo
Copy link
Member

Choose a reason for hiding this comment

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

_getPrInfo

index.js Outdated
@@ -513,7 +513,8 @@ class GithubScm extends Scm {
}));
} catch (err) {
winston.error('Failed to getOpenedPRs: ', err);
throw err;

return [];
Copy link
Member

Choose a reason for hiding this comment

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

If we are not returning the error, then we can return []; in finally block

Copy link
Member

Choose a reason for hiding this comment

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

I was wrong about the previous comment. Return in finally block always override the return value in try block.

Maybe we can write something like

        let pullRequests = [];
        try {
           pullRequests = await this.breaker.runCommand({	            const pullRequests = await this.breaker.runCommand({
                action: 'list',	                action: 'list',
                scopeType: 'pulls',	                scopeType: 'pulls',
                token,	                token,
                params: {	                params: {
                    owner,	                    owner,
                    repo,	                    repo,
                    state: 'open'	                    state: 'open'
                }	                }
            });	            });
            return pullRequests.data.map(pullRequest => ({	            return pullRequests.data.map(pullRequest => ({
                name: `PR-${pullRequest.number}`,	                name: `PR-${pullRequest.number}`,
                ref: `pull/${pullRequest.number}/merge`,	                ref: `pull/${pullRequest.number}/merge`,
                username: pullRequest.user.login,	                username: pullRequest.user.login,
                title: pullRequest.title,	                title: pullRequest.title,
                createTime: pullRequest.created_at,	                createTime: pullRequest.created_at,
                url: pullRequest.html_url,	                url: pullRequest.html_url,
                userProfile: pullRequest.user.html_url	                userProfile: pullRequest.user.html_url
            }));	            }));
        } catch (err) {
            winston.error('Failed to getOpenedPRs: ', err);	            winston.error('Failed to getOpenedPRs: ', err);
            // throw err;
        } finally {
            return pullRequests;
	    }

Have a default value. 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it's better practice not to do return in finally

getting eslint error no-unsafe-finally when I try your suggestion
https://eslint.org/docs/rules/no-unsafe-finally

Copy link
Member

Choose a reason for hiding this comment

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

OK. Lets return in catch.

index.js Outdated
@@ -513,7 +513,8 @@ class GithubScm extends Scm {
}));
} catch (err) {
winston.error('Failed to getOpenedPRs: ', err);
throw err;

return [];
Copy link
Member

Choose a reason for hiding this comment

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

I was wrong about the previous comment. Return in finally block always override the return value in try block.

Maybe we can write something like

        let pullRequests = [];
        try {
           pullRequests = await this.breaker.runCommand({	            const pullRequests = await this.breaker.runCommand({
                action: 'list',	                action: 'list',
                scopeType: 'pulls',	                scopeType: 'pulls',
                token,	                token,
                params: {	                params: {
                    owner,	                    owner,
                    repo,	                    repo,
                    state: 'open'	                    state: 'open'
                }	                }
            });	            });
            return pullRequests.data.map(pullRequest => ({	            return pullRequests.data.map(pullRequest => ({
                name: `PR-${pullRequest.number}`,	                name: `PR-${pullRequest.number}`,
                ref: `pull/${pullRequest.number}/merge`,	                ref: `pull/${pullRequest.number}/merge`,
                username: pullRequest.user.login,	                username: pullRequest.user.login,
                title: pullRequest.title,	                title: pullRequest.title,
                createTime: pullRequest.created_at,	                createTime: pullRequest.created_at,
                url: pullRequest.html_url,	                url: pullRequest.html_url,
                userProfile: pullRequest.user.html_url	                userProfile: pullRequest.user.html_url
            }));	            }));
        } catch (err) {
            winston.error('Failed to getOpenedPRs: ', err);	            winston.error('Failed to getOpenedPRs: ', err);
            // throw err;
        } finally {
            return pullRequests;
	    }

Have a default value. 🤔

index.js Outdated
@@ -513,7 +513,8 @@ class GithubScm extends Scm {
}));
} catch (err) {
winston.error('Failed to getOpenedPRs: ', err);
throw err;

return [];
Copy link
Member

Choose a reason for hiding this comment

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

OK. Lets return in catch.

@tkyi tkyi changed the title fix(1769): Do not throw err if cannot get changed files or get opened prs fix(1769): Do not throw err if cannot get changed files Oct 1, 2019
@tkyi tkyi merged commit 2117fd2 into master Oct 1, 2019
@tkyi tkyi deleted the getPrs branch October 1, 2019 18:30
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.

3 participants