This repository was archived by the owner on Apr 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new command to disable a project
- Loading branch information
1 parent
509c611
commit 990e386
Showing
2 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
const gh = require('../helpers/github'); | ||
const Repository = require('../models/Repository'); | ||
const User = require('../models/User'); | ||
|
||
/** | ||
* config = { | ||
* logger: { info: Function, error: Function }, | ||
* } | ||
*/ | ||
exports.configure = config => async (owner, repo) => { | ||
const repository = await Repository.findOne({ | ||
name: repo, | ||
owner, | ||
}) | ||
.catch(() => null); | ||
|
||
if (!repository) { | ||
config.logger.error(`No project found for owner = "${owner}" and repo = "${repo}".`); | ||
return; | ||
} | ||
|
||
if (!repository.github_hook_id) { | ||
config.logger.error('No webhook registered for this project.'); | ||
return; | ||
} | ||
|
||
if (!repository.enabled_by || !repository.enabled_by.user_id) { | ||
config.logger.error(`Cannot find a user ID for this repository.`); | ||
return; | ||
} | ||
|
||
try { | ||
const user = await User.findOneById(repository.enabled_by.user_id); | ||
|
||
if (!user) { | ||
config.logger.error(`User not found`); | ||
return; | ||
} | ||
|
||
await gh.auth(user).repos.deleteHook({ | ||
owner, | ||
repo, | ||
id: repository.github_hook_id, | ||
}); | ||
|
||
await repository.set({ | ||
enabled: false, | ||
github_hook_id: undefined, | ||
}).save(); | ||
} catch (err) { | ||
config.logger.error(err.message || err); | ||
return; | ||
} | ||
|
||
config.logger.info(`${owner}/${repo} successfully disabled.`); | ||
}; |