Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guibranco/0608762b8e7d3ecec27edbdcd72f7f2b to your computer and use it in GitHub Desktop.
Save guibranco/0608762b8e7d3ecec27edbdcd72f7f2b to your computer and use it in GitHub Desktop.
Create an issue to replace part of a file based on its content (and existence).
const ghToken = pm.globals.get("GH_PAT");
const authorizationHeader = `Authorization: Bearer ${ghToken}`;
const fileName = "appveyor.yml";
const pattern = "ps: $COVERLET_FORMATS = @('cobertura', 'lcov', 'opencover')";
const issueTitle = "Run all coverage formats at once per project";
const issueBody = "**Is your feature request related to a problem? Please describe.**\r\nRun all coverage formats at once per project\r\n\r\n**Describe the solution you'd like**\r\nReplace the actual code:\r\n```ps\r\n- ps: $TEST_PROJECTS = (Get-ChildItem -Path .\Tests\**\ -Recurse -Include *.csproj).Fullname\r\n- ps: $COVERLET_FORMATS = @('cobertura', 'lcov', 'opencover')\r\n- ps: |\r\n foreach($testProject in $TEST_PROJECTS)\r\n {\r\n foreach($coverletFormat in $COVERLET_FORMATS)\r\n {\r\n dotnet test $testProject /p:CollectCoverage=true /p:CoverletOutputFormat=\"$coverletFormat\"\r\n }\r\n }\r\n```\r\n\r\nWith the following code:\r\n```ps\r\n- ps: $TEST_PROJECTS = (Get-ChildItem -Path .\Tests\**\ -Recurse -Include *.csproj).Fullname\r\n- ps: |\r\n foreach($testProject in $TEST_PROJECTS)\r\n {\r\n dotnet test $testProject /p:CollectCoverage=true \"/p:CoverletOutputFormat=\`\"cobertura,lcov,opencover\`\"\"\r\n }\r\n```\r\n\r\n\r\n**Describe alternatives you've considered**\r\nA clear and concise description of any alternative solutions or features you've considered.\r\n\r\n**Additional context**\r\nAdd any other context or screenshots about the feature request here.\r\n";
const repositories = pm.response.json();
for (let i = 0; i < repositories.length; i++) {
const repository = repositories[i];
if (!repository.has_issues || repository.archived) {
continue;
}
pm.sendRequest({
url: `https://api.github.com/repos/${repository.full_name}/contents/${fileName}`,
method: "GET",
header: { "Authorization": "Bearer " + ghToken, "Accept": "application/vnd.github+json" }
}, function (err, res) {
if(res.code == 200) {
var content = atob(res.json().content);
if(content.indexOf(pattern) > 0){
const createIssue = `https://api.github.com/repos/${repository.full_name}/issues`;
pm.sendRequest({
url: createIssue,
method: "POST",
header: authorizationHeader,
body: JSON.stringify({
title: issueTitle,
body: issueBody,
assignee: "guibranco",
labels: ["CI/CD", "github-actions", "enhancement"]
}),
},
function (err, res) {
console.log(res);
});
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment