Skip to content

Commit

Permalink
Fix timeout to apply for the whole request, not just each single REST…
Browse files Browse the repository at this point in the history
… call

Add paramter to specify file limit for PR
  • Loading branch information
Gamer025 committed Oct 16, 2021
1 parent 6eea345 commit 2ea34a2
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CodeOwnersParser/ActionInputs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ public string Name
Default = 10000)]
public int timeout { get; set; } = 0;

[Option('l', "fileLimit",
Required = false,
HelpText = "The workspace directory, or repository root directory. Use `/github/workspace`.",
Default = 1000)]
public int fileLimit { get; set; } = 0;

[Option('k', "token",
Required = false,
HelpText = "The workspace directory, or repository root directory. Use `/github/workspace`.")]
Expand Down
31 changes: 29 additions & 2 deletions CodeOwnersParser/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using CodeOwnersParser;
using System.Collections.Generic;
using Octokit;
using System.Threading.Tasks;

var parser = Default.ParseArguments<ActionInputs>(() => new(), args);
parser.WithNotParsed(
Expand All @@ -27,14 +28,40 @@ static void NotifyOwners(ActionInputs inputs)
if (!String.IsNullOrEmpty(inputs.token))
ghclient.Credentials = new Credentials(inputs.token);

PullRequest PR = ghclient.PullRequest.Get(inputs.Owner, inputs.Name, inputs.pullID).Result;
if (PR.ChangedFiles > inputs.fileLimit)
{
Console.WriteLine($"PR exceeded file limit. Limit: {inputs.fileLimit} files, PR files: {PR.ChangedFiles}");
Environment.Exit(1);
}

Console.WriteLine($"Getting PR files for PR with ID {inputs.pullID}");
List<Octokit.PullRequestFile> modifiedFiles = new List<Octokit.PullRequestFile>(ghclient.PullRequest.Files(inputs.Owner, inputs.Name, inputs.pullID).Result);
var modifiedFilesTask = ghclient.PullRequest.Files(inputs.Owner, inputs.Name, inputs.pullID);
List<Octokit.PullRequestFile> modifiedFiles;
if (modifiedFilesTask.Wait(inputs.timeout))
{
modifiedFiles = new List<PullRequestFile>(modifiedFilesTask.Result);
}
else
{
throw new TimeoutException("Timeout while getting PR files");
}

List<string> ownersWithModifiedFiles = Helpers.GetOwnersWithModifiedFiles(codeowners, modifiedFiles);
//If we were provided a botname parse its comments and find already notifed owners
if (inputs.botname is not null)
{
List<Octokit.IssueComment> PRcomments = new List<IssueComment>(ghclient.Issue.Comment.GetAllForIssue(inputs.Owner, inputs.Name, inputs.pullID).Result);
var PRcommentsTask = ghclient.Issue.Comment.GetAllForIssue(inputs.Owner, inputs.Name, inputs.pullID);
List<Octokit.IssueComment> PRcomments;
if (PRcommentsTask.Wait(inputs.timeout))
{
PRcomments = new List<IssueComment>(PRcommentsTask.Result);
}
else
{
throw new TimeoutException("Timeout while getting PR comments");
}

List<string> notifiedOwners = Helpers.GetMentionedOwners(PRcomments, inputs.botname, inputs.prefix, inputs.sufix, inputs.separator);
ownersWithModifiedFiles = ownersWithModifiedFiles.Except(notifiedOwners).ToList();
}
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Name | Required | Default | Description
owner | no | github.repository_owner | Name of repo owner
name | no | github.repository | Name of the repo
timeout | no | '10000' | Timeout for Github REST API calls
fileLimit | no | '1000' | Maximum amount of file a PR might have, if exceeded it won't be processed and the action will fail. Setting this to a high value might result in rate limiting.
token | no | github.token | Token used for REST calls. Only needed to increase rate limits, can be replaced with empty string, but might lead to rate limit errors.
workspace | no | '/github/workspace' | Location of the repo in the workflow. Normally '/github/workspace' for Docker workflows.
file | no | '/.github/CODEOWNERS' | Relative location of the CODEOWNERS file in the repo. Useful if you want a separate owner file for this action.
Expand Down
7 changes: 7 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ inputs:
"Timeout for Github REST API requests."
required: false
default: '10000'
fileLimit:
description:
"Maximum amount of files to process, PRs with more files will not be processed."
required: false
default: '1000'
token:
description:
"Github token used for REST API. Only needed to increase rate limit, may not be provided."
Expand Down Expand Up @@ -75,6 +80,8 @@ runs:
- ${{ inputs.name }
- '-t'
- ${{ inputs.timeout }}
- '-l'
- ${{ inputs.fileLimit }}
- '-k'
- ${{ inputs.token }}
- '-w'
Expand Down

0 comments on commit 2ea34a2

Please sign in to comment.