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

Feature Request: Per commit modified_files, JSONDiffForFile, fileMatch #1341

Open
glensc opened this issue Dec 15, 2022 · 1 comment
Open
Labels

Comments

@glensc
Copy link
Contributor

glensc commented Dec 15, 2022

Describe the feature

Converting #1335 to a feature request

Expected behavior

There are use-cases where you want to analyse files changed by a commit:

  1. you changed package.json but not yarn.lock
  2. you pinned a package and didn't leave commit message body why you pinned.
  3. modified source, but didn't add/update tests

and those per commit, not whole merge request.

therefore would want to have commit.modified_files, commit.JSONDiffForFile, commit.fileMatch methods:

  const packageDiff = await commit.JSONDiffForFile("package.json")

  if (packageDiff.dependencies) {
      const newDependencies = packageDiff.dependencies.added;
      if (newDependencies.includes(blacklist)) {
        fail(`${commit.sha}: Do not add ${blacklist} to our dependencies, see CVE #23`);
      }
  }
  
  const packageJson = commit.fileMatch("package.json");
  const packageLock = commit.fileMatch("yarn.lock");

  if (packageJson.modified && !packageLock.modified) {
    warn("${commit.sha}: This commit modified `package.json`, but not `yarn.lock`");
  }
  
  const testChanges = commit.modified_files.filter(filepath => filepath.match(/(test|cypress)/));
  if (testChanges.length < 1) {
    warn(`${commit.sha}: This commit does not have any changes in automated tests. Please consider adding some tests.`)
  }
@glensc glensc added the bug label Dec 15, 2022
@adamsosterics
Copy link

This is something that I'd also like to see.
I'd like to add a review comment on Github for each commit that is not linked to an issue, but for that I also need to provide a path to a file that has been changed in that commit (see: https://docs.github.com/en/rest/pulls/comments?apiVersion=2022-11-28#create-a-review-comment-for-a-pull-request). For this usecase it doesn't really matter which file I'm putting the comment on.
A review comment is a bit better in this case than danger's message because the PR can't be merged until all review comments are resolved.
I'd like to write a dangerfile like this:

import { danger } from "danger";

const ISSUE_REFERENCE = `https://github.com/${danger.github.thisPR.owner}/${danger.github.thisPR.repo}/issues/`;

danger.git.commits.forEach((commit) => {
  if (!commit.message.includes(ISSUE_REFERENCE)) {
    danger.github.api.pulls.createReviewComment({
      owner: danger.github.thisPR.owner,
      repo: danger.github.thisPR.repo,
      pull_number: danger.github.thisPR.number,
      commit_id: commit.sha,
      body: "question: Do these changes belong to an issue?\n\nIf so, please add a reference to the issue in the commit messages.",
      path: commit.modified_files[0], // <--- note this
      side: "RIGHT",
      subject_type: "file",
    });
  }
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants