• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

necojackarc / auto-request-review / 5059573801

pending completion
5059573801

push

github

GitHub
Fix the stub to return in the expected format (#93)

59 of 70 branches covered (84.29%)

Branch coverage included in aggregate %.

155 of 171 relevant lines covered (90.64%)

7.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.78
/src/github.js
1
'use strict';
2

3
const core = require('@actions/core');
1✔
4
const fs = require('fs');
1✔
5
const github = require('@actions/github');
1✔
6
const partition = require('lodash/partition');
1✔
7
const yaml = require('yaml');
1✔
8
const { LOCAL_FILE_MISSING } = require('./constants');
1✔
9

10
class PullRequest {
11
  // ref: https://developer.github.com/v3/pulls/#get-a-pull-request
12
  constructor(pull_request_paylaod) {
13
    // "ncc" doesn't yet support private class fields as of 29 Aug. 2020
14
    // ref: https://github.com/vercel/ncc/issues/499
15
    this._pull_request_paylaod = pull_request_paylaod;
1✔
16
  }
17

18
  get author() {
19
    return this._pull_request_paylaod.user.login;
1✔
20
  }
21

22
  get title() {
23
    return this._pull_request_paylaod.title;
1✔
24
  }
25

26
  get is_draft() {
27
    return this._pull_request_paylaod.draft;
1✔
28
  }
29
}
30

31
function get_pull_request() {
32
  const context = get_context();
1✔
33

34
  return new PullRequest(context.payload.pull_request);
1✔
35
}
36

37
async function fetch_config() {
38
  const context = get_context();
1✔
39
  const octokit = get_octokit();
1✔
40
  const config_path = get_config_path();
1✔
41
  const useLocal = get_use_local();
1✔
42
  let content = '';
1✔
43

44
  if (!useLocal) {
1!
45
    const { data: response_body } = await octokit.repos.getContent({
1✔
46
      owner: context.repo.owner,
47
      repo: context.repo.repo,
48
      path: config_path,
49
      ref: context.ref,
50
    });
51

52
    content = Buffer.from(response_body.content, response_body.encoding).toString();
1✔
53
  } else {
54
    try {
×
55
      content = fs.readFileSync(config_path).toString();
×
56

57
      if (!content) {
×
58
        throw new Error();
×
59
      }
60
    } catch (error) {
61
      core.debug(`Error when reading local file: ${error}`);
×
62

63
      throw new Error(LOCAL_FILE_MISSING);
×
64
    }
65
  }
66

67
  return yaml.parse(content);
1✔
68
}
69

70
async function fetch_changed_files() {
71
  const context = get_context();
2✔
72
  const octokit = get_octokit();
2✔
73

74
  const changed_files = [];
2✔
75

76
  const per_page = 100;
2✔
77
  let page = 0;
2✔
78
  let number_of_files_in_current_page;
79

80
  do {
2✔
81
    page += 1;
4✔
82

83
    const { data: response_body } = await octokit.pulls.listFiles({
4✔
84
      owner: context.repo.owner,
85
      repo: context.repo.repo,
86
      pull_number: context.payload.pull_request.number,
87
      page,
88
      per_page,
89
    });
90

91
    number_of_files_in_current_page = response_body.length;
4✔
92
    changed_files.push(...response_body.map((file) => file.filename));
224✔
93

94
  } while (number_of_files_in_current_page === per_page);
95

96
  return changed_files;
2✔
97
}
98

99
async function assign_reviewers(reviewers) {
100
  const context = get_context();
1✔
101
  const octokit = get_octokit();
1✔
102

103
  const [ teams_with_prefix, individuals ] = partition(reviewers, (reviewer) => reviewer.startsWith('team:'));
3✔
104
  const teams = teams_with_prefix.map((team_with_prefix) => team_with_prefix.replace('team:', ''));
1✔
105

106
  return octokit.pulls.requestReviewers({
1✔
107
    owner: context.repo.owner,
108
    repo: context.repo.repo,
109
    pull_number: context.payload.pull_request.number,
110
    reviewers: individuals,
111
    team_reviewers: teams,
112
  });
113
}
114

115
// https://docs.github.com/en/rest/teams/members?apiVersion=2022-11-28#list-team-members
116
async function get_team_members(team) {
117
  const context = get_context();
1✔
118
  const octokit = get_octokit();
1✔
119

120
  return octokit.teams.listMembersInOrg({
1✔
121
    org: context.repo.org,
122
    team_slug: team,
123
  })?.data?.map((member) => member.login);
×
124
}
125

126
/* Private */
127

128
let context_cache;
129
let token_cache;
130
let config_path_cache;
131
let use_local_cache;
132
let octokit_cache;
133

134
function get_context() {
135
  return context_cache || (context_cache = github.context);
6✔
136
}
137

138
function get_token() {
139
  return token_cache || (token_cache = core.getInput('token'));
5✔
140
}
141

142
function get_config_path() {
143
  return config_path_cache || (config_path_cache = core.getInput('config'));
1✔
144
}
145

146
function get_use_local() {
147
  return use_local_cache ?? (use_local_cache = core.getInput('use_local') === 'true');
1✔
148
}
149

150
function get_octokit() {
151
  if (octokit_cache) {
5!
152
    return octokit_cache;
×
153
  }
154

155
  const token = get_token();
5✔
156
  return octokit_cache = github.getOctokit(token);
5✔
157
}
158

159
function clear_cache() {
160
  context_cache = undefined;
15✔
161
  token_cache = undefined;
15✔
162
  config_path_cache = undefined;
15✔
163
  octokit_cache = undefined;
15✔
164
}
165

166
module.exports = {
1✔
167
  get_pull_request,
168
  fetch_config,
169
  fetch_changed_files,
170
  assign_reviewers,
171
  clear_cache,
172
  get_team_members,
173
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc